Skip to content

Commit 97a4646

Browse files
chore: add traces to taint processor
1 parent fe1c1cb commit 97a4646

File tree

3 files changed

+59
-16
lines changed

3 files changed

+59
-16
lines changed

apps/workspace-engine/pkg/workspace/releasemanager/targets/taint.go

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,17 @@
11
package targets
22

33
import (
4+
"context"
45
"workspace-engine/pkg/changeset"
56
"workspace-engine/pkg/oapi"
67
"workspace-engine/pkg/workspace/store"
8+
9+
"go.opentelemetry.io/otel"
10+
"go.opentelemetry.io/otel/attribute"
711
)
812

13+
var tainProcessorTracer = otel.Tracer("TaintProcessor")
14+
915
// targetIndex provides efficient lookups of targets by their related entity IDs
1016
type targetIndex struct {
1117
byEnvironment map[string][]*oapi.ReleaseTarget
@@ -32,18 +38,21 @@ func buildTargetIndex(targets map[string]*oapi.ReleaseTarget) *targetIndex {
3238

3339
// TaintProcessor identifies which targets need to be re-evaluated based on changes
3440
type TaintProcessor struct {
41+
ctx context.Context
3542
store *store.Store
3643
index *targetIndex
3744
taintedTargets map[string]*oapi.ReleaseTarget
3845
}
3946

4047
// NewTaintProcessor creates a new taint processor and processes all changes in a single pass
4148
func NewTaintProcessor(
49+
ctx context.Context,
4250
store *store.Store,
4351
changeSet *changeset.ChangeSet[any],
4452
targets map[string]*oapi.ReleaseTarget,
4553
) *TaintProcessor {
4654
tp := &TaintProcessor{
55+
ctx: ctx,
4756
store: store,
4857
index: buildTargetIndex(targets),
4958
taintedTargets: make(map[string]*oapi.ReleaseTarget),
@@ -56,7 +65,11 @@ func NewTaintProcessor(
5665
// processChanges iterates through the changeset once and taints relevant targets
5766
// This replaces the previous approach of calling Process() multiple times
5867
func (tp *TaintProcessor) processChanges(changeSet *changeset.ChangeSet[any]) {
68+
_, span := tainProcessorTracer.Start(tp.ctx, "processChanges")
69+
defer span.End()
70+
5971
items := changeSet.Process().CollectEntities()
72+
span.SetAttributes(attribute.Int("changeset.count", len(items)))
6073
for _, item := range items {
6174
switch entity := item.(type) {
6275
case *oapi.Policy, *oapi.System:
@@ -113,31 +126,50 @@ func (tp *TaintProcessor) taintAll() {
113126

114127
// taintByEnvironment taints all targets in the given environment
115128
func (tp *TaintProcessor) taintByEnvironmentId(envId string) {
129+
_, span := tainProcessorTracer.Start(tp.ctx, "taintByEnvironmentId")
130+
defer span.End()
131+
132+
span.SetAttributes(attribute.String("environment.id", envId))
116133
for _, target := range tp.index.byEnvironment[envId] {
117134
tp.taintedTargets[target.Key()] = target
118135
}
119136
}
120137

121138
// taintByDeployment taints all targets for the given deployment
122139
func (tp *TaintProcessor) taintByDeploymentId(depId string) {
140+
_, span := tainProcessorTracer.Start(tp.ctx, "taintByDeploymentId")
141+
defer span.End()
142+
143+
span.SetAttributes(attribute.String("deployment.id", depId))
123144
for _, target := range tp.index.byDeployment[depId] {
124145
tp.taintedTargets[target.Key()] = target
125146
}
126147
}
127148

128149
// taintByResource taints all targets for the given resource
129150
func (tp *TaintProcessor) taintByResourceId(resId string) {
151+
_, span := tainProcessorTracer.Start(tp.ctx, "taintByResourceId")
152+
defer span.End()
153+
130154
for _, target := range tp.index.byResource[resId] {
131155
tp.taintedTargets[target.Key()] = target
132156
}
133157
}
134158

135159
// taintByJob taints the specific target associated with the job's release
136160
func (tp *TaintProcessor) taintByReleaseTarget(releaseTarget *oapi.ReleaseTarget) {
161+
_, span := tainProcessorTracer.Start(tp.ctx, "taintByReleaseTarget")
162+
defer span.End()
163+
164+
span.SetAttributes(attribute.String("release_target.key", releaseTarget.Key()))
137165
tp.taintedTargets[releaseTarget.Key()] = releaseTarget
138166
}
139167

140168
// Tainted returns the map of all targets that have been marked for tainting
141169
func (tp *TaintProcessor) Tainted() map[string]*oapi.ReleaseTarget {
170+
_, span := tainProcessorTracer.Start(tp.ctx, "Tainted")
171+
defer span.End()
172+
173+
span.SetAttributes(attribute.Int("tainted_targets.count", len(tp.taintedTargets)))
142174
return tp.taintedTargets
143175
}

apps/workspace-engine/pkg/workspace/releasemanager/targets/taint_test.go

Lines changed: 26 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package targets
22

33
import (
4-
"context"
54
"testing"
65
"time"
76
"workspace-engine/pkg/changeset"
@@ -142,6 +141,7 @@ func TestBuildTargetIndex(t *testing.T) {
142141

143142
// Test taint on Policy change (should taint all targets)
144143
func TestTaintProcessor_PolicyChange_TaintsAll(t *testing.T) {
144+
ctx := t.Context()
145145
sc := statechange.NewChangeSet[any]()
146146
st := store.New(sc)
147147

@@ -165,7 +165,7 @@ func TestTaintProcessor_PolicyChange_TaintsAll(t *testing.T) {
165165
cs.Record(changeset.ChangeTypeCreate, policy)
166166

167167
// Process
168-
tp := NewTaintProcessor(st, cs, targets)
168+
tp := NewTaintProcessor(ctx, st, cs, targets)
169169
tainted := tp.Tainted()
170170

171171
// Verify all targets are tainted
@@ -176,6 +176,7 @@ func TestTaintProcessor_PolicyChange_TaintsAll(t *testing.T) {
176176

177177
// Test taint on System change (should taint all targets)
178178
func TestTaintProcessor_SystemChange_TaintsAll(t *testing.T) {
179+
ctx := t.Context()
179180
sc := statechange.NewChangeSet[any]()
180181
st := store.New(sc)
181182

@@ -199,7 +200,7 @@ func TestTaintProcessor_SystemChange_TaintsAll(t *testing.T) {
199200
cs.Record(changeset.ChangeTypeUpdate, system)
200201

201202
// Process
202-
tp := NewTaintProcessor(st, cs, targets)
203+
tp := NewTaintProcessor(ctx, st, cs, targets)
203204
tainted := tp.Tainted()
204205

205206
// Verify all targets are tainted
@@ -210,6 +211,7 @@ func TestTaintProcessor_SystemChange_TaintsAll(t *testing.T) {
210211

211212
// Test taint on Environment change
212213
func TestTaintProcessor_EnvironmentChange_TaintsEnvironmentTargets(t *testing.T) {
214+
ctx := t.Context()
213215
sc := statechange.NewChangeSet[any]()
214216
st := store.New(sc)
215217

@@ -234,7 +236,7 @@ func TestTaintProcessor_EnvironmentChange_TaintsEnvironmentTargets(t *testing.T)
234236
cs.Record(changeset.ChangeTypeUpdate, env)
235237

236238
// Process
237-
tp := NewTaintProcessor(st, cs, targets)
239+
tp := NewTaintProcessor(ctx, st, cs, targets)
238240
tainted := tp.Tainted()
239241

240242
// Verify only env1 targets are tainted
@@ -245,6 +247,7 @@ func TestTaintProcessor_EnvironmentChange_TaintsEnvironmentTargets(t *testing.T)
245247

246248
// Test taint on Deployment change
247249
func TestTaintProcessor_DeploymentChange_TaintsDeploymentTargets(t *testing.T) {
250+
ctx := t.Context()
248251
sc := statechange.NewChangeSet[any]()
249252
st := store.New(sc)
250253

@@ -268,7 +271,7 @@ func TestTaintProcessor_DeploymentChange_TaintsDeploymentTargets(t *testing.T) {
268271
cs.Record(changeset.ChangeTypeUpdate, dep)
269272

270273
// Process
271-
tp := NewTaintProcessor(st, cs, targets)
274+
tp := NewTaintProcessor(ctx, st, cs, targets)
272275
tainted := tp.Tainted()
273276

274277
// Verify only dep1 targets are tainted
@@ -279,6 +282,7 @@ func TestTaintProcessor_DeploymentChange_TaintsDeploymentTargets(t *testing.T) {
279282

280283
// Test taint on DeploymentVersion change
281284
func TestTaintProcessor_DeploymentVersionChange_TaintsDeploymentTargets(t *testing.T) {
285+
ctx := t.Context()
282286
sc := statechange.NewChangeSet[any]()
283287
st := store.New(sc)
284288

@@ -302,7 +306,7 @@ func TestTaintProcessor_DeploymentVersionChange_TaintsDeploymentTargets(t *testi
302306
cs.Record(changeset.ChangeTypeCreate, version)
303307

304308
// Process
305-
tp := NewTaintProcessor(st, cs, targets)
309+
tp := NewTaintProcessor(ctx, st, cs, targets)
306310
tainted := tp.Tainted()
307311

308312
// Verify only dep1 targets are tainted
@@ -313,6 +317,7 @@ func TestTaintProcessor_DeploymentVersionChange_TaintsDeploymentTargets(t *testi
313317

314318
// Test taint on Resource change
315319
func TestTaintProcessor_ResourceChange_TaintsResourceTargets(t *testing.T) {
320+
ctx := t.Context()
316321
sc := statechange.NewChangeSet[any]()
317322
st := store.New(sc)
318323

@@ -336,7 +341,7 @@ func TestTaintProcessor_ResourceChange_TaintsResourceTargets(t *testing.T) {
336341
cs.Record(changeset.ChangeTypeUpdate, resource)
337342

338343
// Process
339-
tp := NewTaintProcessor(st, cs, targets)
344+
tp := NewTaintProcessor(ctx, st, cs, targets)
340345
tainted := tp.Tainted()
341346

342347
// Verify only resID1 targets are tainted
@@ -347,7 +352,7 @@ func TestTaintProcessor_ResourceChange_TaintsResourceTargets(t *testing.T) {
347352

348353
// Test taint on Job change
349354
func TestTaintProcessor_JobChange_TaintsJobReleaseTarget(t *testing.T) {
350-
ctx := context.Background()
355+
ctx := t.Context()
351356
sc := statechange.NewChangeSet[any]()
352357
st := store.New(sc)
353358

@@ -379,7 +384,7 @@ func TestTaintProcessor_JobChange_TaintsJobReleaseTarget(t *testing.T) {
379384
cs.Record(changeset.ChangeTypeUpdate, job)
380385

381386
// Process
382-
tp := NewTaintProcessor(st, cs, targets)
387+
tp := NewTaintProcessor(ctx, st, cs, targets)
383388
tainted := tp.Tainted()
384389

385390
// Verify only the target associated with the job's release is tainted
@@ -390,6 +395,7 @@ func TestTaintProcessor_JobChange_TaintsJobReleaseTarget(t *testing.T) {
390395

391396
// Test taint with job that has non-existent release
392397
func TestTaintProcessor_JobChange_NonExistentRelease(t *testing.T) {
398+
ctx := t.Context()
393399
sc := statechange.NewChangeSet[any]()
394400
st := store.New(sc)
395401

@@ -409,7 +415,7 @@ func TestTaintProcessor_JobChange_NonExistentRelease(t *testing.T) {
409415
cs.Record(changeset.ChangeTypeUpdate, job)
410416

411417
// Process
412-
tp := NewTaintProcessor(st, cs, targets)
418+
tp := NewTaintProcessor(ctx, st, cs, targets)
413419
tainted := tp.Tainted()
414420

415421
// Verify no targets are tainted
@@ -418,6 +424,7 @@ func TestTaintProcessor_JobChange_NonExistentRelease(t *testing.T) {
418424

419425
// Test multiple changes in single pass
420426
func TestTaintProcessor_MultipleChanges_SinglePass(t *testing.T) {
427+
ctx := t.Context()
421428
sc := statechange.NewChangeSet[any]()
422429
st := store.New(sc)
423430

@@ -450,7 +457,7 @@ func TestTaintProcessor_MultipleChanges_SinglePass(t *testing.T) {
450457
cs.Record(changeset.ChangeTypeUpdate, resource)
451458

452459
// Process
453-
tp := NewTaintProcessor(st, cs, targets)
460+
tp := NewTaintProcessor(ctx, st, cs, targets)
454461
tainted := tp.Tainted()
455462

456463
// Verify all targets are tainted (target1 by env+dep, target2 by dep+res, target3 by env+res)
@@ -462,6 +469,7 @@ func TestTaintProcessor_MultipleChanges_SinglePass(t *testing.T) {
462469

463470
// Test empty changeset
464471
func TestTaintProcessor_EmptyChangeset(t *testing.T) {
472+
ctx := t.Context()
465473
sc := statechange.NewChangeSet[any]()
466474
st := store.New(sc)
467475

@@ -479,7 +487,7 @@ func TestTaintProcessor_EmptyChangeset(t *testing.T) {
479487
cs := changeset.NewChangeSet[any]()
480488

481489
// Process
482-
tp := NewTaintProcessor(st, cs, targets)
490+
tp := NewTaintProcessor(ctx, st, cs, targets)
483491
tainted := tp.Tainted()
484492

485493
// Verify no targets are tainted
@@ -488,6 +496,7 @@ func TestTaintProcessor_EmptyChangeset(t *testing.T) {
488496

489497
// Test that Policy change short-circuits (taints all and returns early)
490498
func TestTaintProcessor_PolicyChange_ShortCircuits(t *testing.T) {
499+
ctx := t.Context()
491500
sc := statechange.NewChangeSet[any]()
492501
st := store.New(sc)
493502

@@ -514,7 +523,7 @@ func TestTaintProcessor_PolicyChange_ShortCircuits(t *testing.T) {
514523
cs.Record(changeset.ChangeTypeUpdate, env)
515524

516525
// Process
517-
tp := NewTaintProcessor(st, cs, targets)
526+
tp := NewTaintProcessor(ctx, st, cs, targets)
518527
tainted := tp.Tainted()
519528

520529
// Verify all targets are tainted (policy should cause all to be tainted and return early)
@@ -525,6 +534,7 @@ func TestTaintProcessor_PolicyChange_ShortCircuits(t *testing.T) {
525534

526535
// Test with no targets
527536
func TestTaintProcessor_NoTargets(t *testing.T) {
537+
ctx := t.Context()
528538
sc := statechange.NewChangeSet[any]()
529539
st := store.New(sc)
530540
targets := map[string]*oapi.ReleaseTarget{}
@@ -535,7 +545,7 @@ func TestTaintProcessor_NoTargets(t *testing.T) {
535545
cs.Record(changeset.ChangeTypeUpdate, env)
536546

537547
// Process
538-
tp := NewTaintProcessor(st, cs, targets)
548+
tp := NewTaintProcessor(ctx, st, cs, targets)
539549
tainted := tp.Tainted()
540550

541551
// Verify no targets are tainted (because there are none)
@@ -544,6 +554,7 @@ func TestTaintProcessor_NoTargets(t *testing.T) {
544554

545555
// Test taint deduplication (same target tainted by multiple changes)
546556
func TestTaintProcessor_Deduplication(t *testing.T) {
557+
ctx := t.Context()
547558
sc := statechange.NewChangeSet[any]()
548559
st := store.New(sc)
549560

@@ -568,7 +579,7 @@ func TestTaintProcessor_Deduplication(t *testing.T) {
568579
cs.Record(changeset.ChangeTypeUpdate, resource)
569580

570581
// Process
571-
tp := NewTaintProcessor(st, cs, targets)
582+
tp := NewTaintProcessor(ctx, st, cs, targets)
572583
tainted := tp.Tainted()
573584

574585
// Verify target is tainted only once (deduplication works)

apps/workspace-engine/pkg/workspace/releasemanager/targets/targets_manager.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ func (m *Manager) DetectChanges(ctx context.Context, changeSet *changeset.Change
5151
})
5252
defer changes.Finalize()
5353

54-
taintedTargets := NewTaintProcessor(m.store, changeSet, targets).Tainted()
54+
taintedTargets := NewTaintProcessor(ctx, m.store, changeSet, targets).Tainted()
5555

5656
var numTainted int
5757
var numCreated int

0 commit comments

Comments
 (0)