-
Notifications
You must be signed in to change notification settings - Fork 14
feat(jobs): facet Consumer spans by environment.id in Datadog #795
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,44 @@ | ||
| package jobs | ||
|
|
||
| import ( | ||
| "context" | ||
|
|
||
| "go.opentelemetry.io/otel/attribute" | ||
| "go.opentelemetry.io/otel/trace" | ||
| ) | ||
|
|
||
| // annotateConsumerEnvironment copies the environment id onto the active | ||
| // go-queue Consumer span (EnvironmentScrape process / SitespeedScrape process). | ||
| // Child Internal spans already carry richer environment attributes; Datadog | ||
| // facets on the entry span need this low-cardinality id without opening | ||
| // children. Do not add high-cardinality fields (URL, emails, extension lists). | ||
| func annotateConsumerEnvironment(ctx context.Context, environmentID int32) { | ||
| trace.SpanFromContext(ctx).SetAttributes( | ||
| attribute.Int("environment.id", int(environmentID)), | ||
| ) | ||
| } | ||
|
|
||
| // annotateConsumerOutcome stamps a tiny ok/error enum on the active Consumer | ||
| // span after the handler returns. Mirrors go-queue otel status (error vs ok) | ||
| // as a facetable attribute; business-specific outcomes stay on metrics/child spans. | ||
| func annotateConsumerOutcome(ctx context.Context, err error) { | ||
| outcome := "ok" | ||
| if err != nil { | ||
| outcome = "error" | ||
| } | ||
| trace.SpanFromContext(ctx).SetAttributes( | ||
| attribute.String("outcome", outcome), | ||
| ) | ||
| } | ||
|
|
||
| // runEnvironmentJob annotates the Consumer span with environment.id up front | ||
| // and outcome when the job finishes, then returns the job error unchanged. | ||
| // Do not RecordError / SetStatus here: queueotel.Middleware already does that | ||
| // on the Consumer span after the handler returns; duplicating it would emit | ||
| // two exception events for the same failure. | ||
| func runEnvironmentJob(ctx context.Context, environmentID int32, run func(context.Context) error) error { | ||
| annotateConsumerEnvironment(ctx, environmentID) | ||
| err := run(ctx) | ||
| annotateConsumerOutcome(ctx, err) | ||
| return err | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,73 @@ | ||
| package jobs | ||
|
|
||
| import ( | ||
| "context" | ||
| "errors" | ||
| "testing" | ||
|
|
||
| "github.com/stretchr/testify/assert" | ||
| "github.com/stretchr/testify/require" | ||
| "go.opentelemetry.io/otel/attribute" | ||
| sdktrace "go.opentelemetry.io/otel/sdk/trace" | ||
| ) | ||
|
|
||
| func TestRunEnvironmentJobSetsConsumerSpanAttributes(t *testing.T) { | ||
| tp := sdktrace.NewTracerProvider() | ||
| tracer := tp.Tracer("test") | ||
| ctx, span := tracer.Start(context.Background(), "EnvironmentScrape process") | ||
|
|
||
| err := runEnvironmentJob(ctx, 42, func(context.Context) error { | ||
| return nil | ||
| }) | ||
| require.NoError(t, err) | ||
| span.End() | ||
|
|
||
| attrs := spanAttributes(span.(sdktrace.ReadOnlySpan)) | ||
| assert.Equal(t, int64(42), attrs["environment.id"].AsInt64()) | ||
| assert.Equal(t, "ok", attrs["outcome"].AsString()) | ||
| } | ||
|
|
||
| func TestRunEnvironmentJobSetsErrorOutcome(t *testing.T) { | ||
| tp := sdktrace.NewTracerProvider() | ||
| tracer := tp.Tracer("test") | ||
| ctx, span := tracer.Start(context.Background(), "SitespeedScrape process") | ||
|
|
||
| jobErr := errors.New("sitespeed failed") | ||
| err := runEnvironmentJob(ctx, 7, func(context.Context) error { | ||
| return jobErr | ||
| }) | ||
| assert.ErrorIs(t, err, jobErr) | ||
| span.End() | ||
|
|
||
| attrs := spanAttributes(span.(sdktrace.ReadOnlySpan)) | ||
| assert.Equal(t, int64(7), attrs["environment.id"].AsInt64()) | ||
| assert.Equal(t, "error", attrs["outcome"].AsString()) | ||
| } | ||
|
|
||
| func TestRunEnvironmentJobAnnotatesParentNotChild(t *testing.T) { | ||
| // Handlers receive the Consumer span in ctx; job body may start Internal | ||
| // children. Attributes must stay on the Consumer entry span. | ||
| tp := sdktrace.NewTracerProvider() | ||
| tracer := tp.Tracer("test") | ||
| ctx, consumer := tracer.Start(context.Background(), "EnvironmentScrape process") | ||
|
|
||
| err := runEnvironmentJob(ctx, 99, func(ctx context.Context) error { | ||
| _, child := tracer.Start(ctx, "environment.scrape") | ||
| child.End() | ||
| return nil | ||
| }) | ||
| require.NoError(t, err) | ||
| consumer.End() | ||
|
|
||
| attrs := spanAttributes(consumer.(sdktrace.ReadOnlySpan)) | ||
| assert.Equal(t, int64(99), attrs["environment.id"].AsInt64()) | ||
| assert.Equal(t, "ok", attrs["outcome"].AsString()) | ||
| } | ||
|
|
||
| func spanAttributes(span sdktrace.ReadOnlySpan) map[string]attribute.Value { | ||
| out := make(map[string]attribute.Value, len(span.Attributes())) | ||
| for _, attr := range span.Attributes() { | ||
| out[string(attr.Key)] = attr.Value | ||
| } | ||
| return out | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.