Skip to content

Commit

Permalink
fix: pass context along to avoid disconnected spans (#1994)
Browse files Browse the repository at this point in the history
Ref: SRX-YNEAHU
  • Loading branch information
sven-urbanski-freiheit-com authored Sep 27, 2024
1 parent a2fd990 commit d82c5af
Show file tree
Hide file tree
Showing 8 changed files with 91 additions and 86 deletions.
2 changes: 1 addition & 1 deletion pkg/db/cutoff.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import (
)

func DBReadCutoff(h *DBHandler, ctx context.Context, tx *sql.Tx) (*EslVersion, error) {
span, _ := tracer.StartSpanFromContext(ctx, "DBReadCutoff")
span, ctx := tracer.StartSpanFromContext(ctx, "DBReadCutoff")
defer span.Finish()

selectQuery := h.AdaptQuery("SELECT eslVersion FROM cutoff ORDER BY eslVersion DESC LIMIT 1;")
Expand Down
120 changes: 60 additions & 60 deletions pkg/db/db.go

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions services/cd-service/pkg/repository/repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -2186,7 +2186,7 @@ func (s *State) WriteAllCurrentlyDeployed(ctx context.Context, transaction *sql.

// WriteCurrentEnvironmentLocks gets all locks on any environment in manifest and writes them to the DB
func (s *State) WriteCurrentEnvironmentLocks(ctx context.Context, transaction *sql.Tx, dbHandler *db.DBHandler) error {
ddSpan, _ := tracer.StartSpanFromContext(ctx, "WriteCurrentEnvironmentLocks")
ddSpan, ctx := tracer.StartSpanFromContext(ctx, "WriteCurrentEnvironmentLocks")
defer ddSpan.Finish()
_, envNames, err := s.GetEnvironmentConfigsSortedFromManifest() // this is intentional, when doing custom migrations (which is where this function is called), we want to read from the manifest repo explicitly
if err != nil {
Expand Down Expand Up @@ -2236,7 +2236,7 @@ func (s *State) WriteCurrentEnvironmentLocks(ctx context.Context, transaction *s
}

func (s *State) WriteCurrentApplicationLocks(ctx context.Context, transaction *sql.Tx, dbHandler *db.DBHandler) error {
ddSpan, _ := tracer.StartSpanFromContext(ctx, "WriteCurrentApplicationLocks")
ddSpan, ctx := tracer.StartSpanFromContext(ctx, "WriteCurrentApplicationLocks")
defer ddSpan.Finish()
_, envNames, err := s.GetEnvironmentConfigsSortedFromManifest() // this is intentional, when doing custom migrations (which is where this function is called), we want to read from the manifest repo explicitly

Expand Down Expand Up @@ -2295,7 +2295,7 @@ func (s *State) WriteCurrentApplicationLocks(ctx context.Context, transaction *s
}

func (s *State) WriteAllQueuedAppVersions(ctx context.Context, transaction *sql.Tx, dbHandler *db.DBHandler) error {
ddSpan, _ := tracer.StartSpanFromContext(ctx, "GetAllQueuedAppVersions")
ddSpan, ctx := tracer.StartSpanFromContext(ctx, "GetAllQueuedAppVersions")
defer ddSpan.Finish()
_, envNames, err := s.GetEnvironmentConfigsSortedFromManifest()

Expand Down
40 changes: 23 additions & 17 deletions services/cd-service/pkg/repository/transformer.go
Original file line number Diff line number Diff line change
Expand Up @@ -289,7 +289,7 @@ type Transformer interface {
}

type TransformerContext interface {
Execute(t Transformer, transaction *sql.Tx) error
Execute(ctx context.Context, t Transformer, transaction *sql.Tx) error
AddAppEnv(app string, env string, team string)
DeleteEnvFromApp(app string, env string)
}
Expand All @@ -299,11 +299,10 @@ func RunTransformer(ctx context.Context, t Transformer, s *State, transaction *s
ChangedApps: nil,
DeletedRootApps: nil,
Commits: nil,
Context: ctx,
State: s,
Stack: [][]string{nil},
}
if err := runner.Execute(t, transaction); err != nil {
if err := runner.Execute(ctx, t, transaction); err != nil {
return "", nil, err
}
commitMsg := ""
Expand All @@ -318,8 +317,8 @@ func RunTransformer(ctx context.Context, t Transformer, s *State, transaction *s
}

type transformerRunner struct {
Context context.Context
State *State
//Context context.Context
State *State
// Stores the current stack of commit messages. Each entry of
// the outer slice corresponds to a step being executed. Each
// entry of the inner slices correspond to a message generated
Expand All @@ -330,9 +329,9 @@ type transformerRunner struct {
Commits *CommitIds
}

func (r *transformerRunner) Execute(t Transformer, transaction *sql.Tx) error {
func (r *transformerRunner) Execute(ctx context.Context, t Transformer, transaction *sql.Tx) error {
r.Stack = append(r.Stack, nil)
msg, err := t.Transform(r.Context, r.State, r, transaction)
msg, err := t.Transform(ctx, r.State, r, transaction)
if err != nil {
return err
}
Expand Down Expand Up @@ -765,7 +764,7 @@ func (c *CreateApplicationVersion) Transform(
TransformerEslVersion: c.TransformerEslVersion,
SkipOverview: false,
}
err := t.Execute(d, transaction)
err := t.Execute(ctx, d, transaction)
if err != nil {
_, ok := err.(*LockedError)
if ok {
Expand Down Expand Up @@ -1369,7 +1368,7 @@ func (c *CreateUndeployApplicationVersion) Transform(
CiLink: "",
SkipOverview: false,
}
err := t.Execute(d, transaction)
err := t.Execute(ctx, d, transaction)
if err != nil {
_, ok := err.(*LockedError)
if ok {
Expand Down Expand Up @@ -2193,7 +2192,7 @@ func (c *CreateEnvironmentGroupLock) Transform(
CiLink: c.CiLink,
AllowedDomains: c.AllowedDomains,
}
if err := t.Execute(&x, transaction); err != nil {
if err := t.Execute(ctx, &x, transaction); err != nil {
return "", err
}
}
Expand Down Expand Up @@ -2237,7 +2236,7 @@ func (c *DeleteEnvironmentGroupLock) Transform(
LockId: c.LockId,
TransformerEslVersion: c.TransformerEslVersion,
}
if err := t.Execute(&x, transaction); err != nil {
if err := t.Execute(ctx, &x, transaction); err != nil {
return "", err
}
}
Expand Down Expand Up @@ -3050,7 +3049,7 @@ func (c *DeployApplicationVersion) Transform(
TransformerEslVersion: c.TransformerEslVersion,
}

if err := t.Execute(d, transaction); err != nil {
if err := t.Execute(ctx, d, transaction); err != nil {
return "", err
}
}
Expand Down Expand Up @@ -3375,6 +3374,9 @@ func (c *ReleaseTrain) Prognosis(
) ReleaseTrainPrognosis {
span, ctx := tracer.StartSpanFromContext(ctx, "ReleaseTrain Prognosis")
defer span.Finish()
span.SetTag("targetEnv", c.Target)
span.SetTag("targetType", c.TargetType)
span.SetTag("team", c.Team)

var targetGroupName = c.Target
var envGroupConfigs, isEnvGroup = getEnvironmentGroupsEnvironmentsOrEnvironment(configs, targetGroupName, c.TargetType)
Expand Down Expand Up @@ -3456,6 +3458,8 @@ func (c *ReleaseTrain) Transform(
}

prognosis := c.Prognosis(ctx, state, transaction, configs)
span2, ctx := tracer.StartSpanFromContext(ctx, "ReleaseTrain Apply")
defer span2.Finish()

if prognosis.Error != nil {
return "", prognosis.Error
Expand All @@ -3477,7 +3481,7 @@ func (c *ReleaseTrain) Transform(
trainGroup = conversion.FromString(targetGroupName)
}

if err := t.Execute(&envReleaseTrain{
if err := t.Execute(ctx, &envReleaseTrain{
Parent: c,
Env: envName,
EnvConfigs: configs,
Expand Down Expand Up @@ -3522,6 +3526,8 @@ func (c *envReleaseTrain) prognosis(
) ReleaseTrainEnvironmentPrognosis {
span, ctx := tracer.StartSpanFromContext(ctx, "EnvReleaseTrain Prognosis")
defer span.Finish()
span.SetTag("env", c.Env)

envConfig := c.EnvGroupConfigs[c.Env]
if envConfig.Upstream == nil {
return ReleaseTrainEnvironmentPrognosis{
Expand Down Expand Up @@ -4027,7 +4033,7 @@ func (c *envReleaseTrain) Transform(
CiLink: c.CiLink,
SkipOverview: true,
}
if err := t.Execute(d, transaction); err != nil {
if err := t.Execute(ctx, d, transaction); err != nil {
return "", grpc.InternalError(ctx, fmt.Errorf("unexpected error while deploying app %q to env %q: %w", appName, c.Env, err))
}

Expand All @@ -4047,7 +4053,7 @@ func (c *envReleaseTrain) Transform(
if c.Parent.Team != "" {
teamInfo = " for team '" + c.Parent.Team + "'"
}
if err := t.Execute(&skippedServices{
if err := t.Execute(ctx, &skippedServices{
Messages: skipped,
TransformerEslVersion: c.TransformerEslVersion,
}, transaction); err != nil {
Expand Down Expand Up @@ -4104,7 +4110,7 @@ func (c *skippedServices) SetEslVersion(id db.TransformerID) {
}

func (c *skippedServices) Transform(
_ context.Context,
ctx context.Context,
_ *State,
t TransformerContext,
transaction *sql.Tx,
Expand All @@ -4113,7 +4119,7 @@ func (c *skippedServices) Transform(
return "", nil
}
for _, msg := range c.Messages {
if err := t.Execute(&skippedService{Message: msg, TransformerEslVersion: c.TransformerEslVersion}, transaction); err != nil {
if err := t.Execute(ctx, &skippedService{Message: msg, TransformerEslVersion: c.TransformerEslVersion}, transaction); err != nil {
return "", err
}
}
Expand Down
3 changes: 1 addition & 2 deletions services/cd-service/pkg/service/commit_deployment.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ ON
latest.latest=deployments.eslVersion
AND latest.appname=deployments.appname;
`
span, _ := tracer.StartSpanFromContext(ctx, "GetCommitDeploymentInfo")
span, ctx := tracer.StartSpanFromContext(ctx, "GetCommitDeploymentInfo")
defer span.Finish()
span.SetTag("commit_id", in.CommitId)

Expand Down Expand Up @@ -130,7 +130,6 @@ ON
}

func getCommitDeploymentInfoForApp(ctx context.Context, h *db.DBHandler, commitReleaseNumber uint64, app string, environments []string, appDeployments []byte) (*api.AppCommitDeploymentStatus, error) {

span, _ := tracer.StartSpanFromContext(ctx, "getCommitDeploymentInfoForApp")
defer span.Finish()
span.SetTag("app", app)
Expand Down
2 changes: 1 addition & 1 deletion services/cd-service/pkg/service/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ type VersionServiceServer struct {
func (o *VersionServiceServer) GetVersion(
ctx context.Context,
in *api.GetVersionRequest) (*api.GetVersionResponse, error) {
span, _ := tracer.StartSpanFromContext(ctx, "GetVersion")
span, ctx := tracer.StartSpanFromContext(ctx, "GetVersion")
defer span.Finish()

state := o.Repository.State()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -726,7 +726,7 @@ func (r *repository) updateArgoCdApps(ctx context.Context, transaction *sql.Tx,
if apps, err := state.GetEnvironmentApplications(ctx, transaction, env); err != nil {
return err
} else {
spanCollectData, _ := tracer.StartSpanFromContext(ctx, "collectData")
spanCollectData, ctx := tracer.StartSpanFromContext(ctx, "collectData")
defer spanCollectData.Finish()
appData := []argocd.AppData{}
sort.Strings(apps)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -477,7 +477,7 @@ func (c *DeployApplicationVersion) Transform(
}

func writeEvent(ctx context.Context, eventId string, sourceCommitId string, filesystem billy.Filesystem, ev event.Event) error {
span, _ := tracer.StartSpanFromContext(ctx, "writeEvent")
span, ctx := tracer.StartSpanFromContext(ctx, "writeEvent")
defer span.Finish()
if !valid.SHA1CommitID(sourceCommitId) {
logger.FromContext(ctx).Sugar().Warnf(
Expand Down

0 comments on commit d82c5af

Please sign in to comment.