-
Notifications
You must be signed in to change notification settings - Fork 2.1k
[exporterhelper] persist spancontext through persistent queue #12934
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
Closed
jackgopack4
wants to merge
22
commits into
open-telemetry:main
from
DataDog:jackgopack4/save-span-links-persistentqueue
Closed
Changes from all commits
Commits
Show all changes
22 commits
Select commit
Hold shift + click to select a range
de02e51
OTEL-2540 Add SpanContext to persistent queue
jackgopack4 27cad76
OTEL-2540 add changelog
jackgopack4 bce7256
OTEL-2540 add marshalRequestWithSpanContext and unmarshalRequestWithS…
jackgopack4 e2ee866
add test coverage
jackgopack4 9ac33b5
add persistentqueue benchmark test OTEL-2540
jackgopack4 c8be153
switch marshal from JSON to byte-based
jackgopack4 e04b5c6
switch approach to make multiple storage operations OTEL-2540
jackgopack4 8685a6e
remove inadvertent debug code
jackgopack4 fdeba3a
add test coverage OTEL-2540
jackgopack4 dba9427
add exporter.PersistSpanContext featuregate OTEL-2540
jackgopack4 fe26a0c
add comments OTEL-2540
jackgopack4 94df316
add test coverage OTEL-2540
jackgopack4 9592b65
rename spanContextWrapper and spanContext objects
jackgopack4 e934aa0
change approach to local spancontext
jackgopack4 bbd9198
OTEL-2540 unit tests
jackgopack4 b1b9771
add unit test coverage OTEL-2540
jackgopack4 2940da2
apply suggestions from code review
jackgopack4 dae829e
remove unnecessary helper function
jackgopack4 cf73142
fix linter and create persistent_queue_context
jackgopack4 34b6200
Merge branch 'main' into jackgopack4/save-span-links-persistentqueue
mx-psi b1292da
Merge branch 'main' into jackgopack4/save-span-links-persistentqueue
jackgopack4 b8b78a9
Merge branch 'main' into jackgopack4/save-span-links-persistentqueue
jackgopack4 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
27 changes: 27 additions & 0 deletions
27
.chloggen/jackgopack4-add-spancontext-persistentqueue.yaml
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,27 @@ | ||
| # Use this changelog template to create an entry for release notes. | ||
|
|
||
| # One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix' | ||
| change_type: 'enhancement' | ||
|
|
||
| # The name of the component, or a single word describing the area of concern, (e.g. otlpreceiver) | ||
| component: 'exporterhelper' | ||
|
|
||
| # A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`). | ||
| note: "Add `exporter.PropagateSpanContext` to enable propagating SpanContext along with telemetry requests in the persistent queue" | ||
|
|
||
| # One or more tracking issues or pull requests related to the change | ||
| issues: [11740, 12212, 12934] | ||
|
|
||
| # (Optional) One or more lines of additional information to render under the primary note. | ||
| # These lines will be padded with 2 spaces and then inserted directly into the document. | ||
| # Use pipe (|) for multiline entries. | ||
| subtext: | | ||
| This change will allow internal telemetry spans to be processed when using persistent queue/storage. | ||
| When enabled, requests will use approximately 128 bytes more in persistent storage. | ||
|
|
||
| # Optional: The change log or logs in which this entry should be included. | ||
| # e.g. '[user]' or '[user, api]' | ||
| # Include 'user' if the change is relevant to end users. | ||
| # Include 'api' if there is a change to a library API. | ||
| # Default: '[user]' | ||
| change_logs: [user] |
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
107 changes: 107 additions & 0 deletions
107
exporter/exporterhelper/internal/queuebatch/persistent_queue_context.go
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,107 @@ | ||
| // Copyright The OpenTelemetry Authors | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| package queuebatch // import "go.opentelemetry.io/collector/exporter/exporterhelper/internal/queuebatch" | ||
|
|
||
| import ( | ||
| "context" | ||
| "encoding/hex" | ||
| "encoding/json" | ||
| "errors" | ||
| "strconv" | ||
|
|
||
| "go.opentelemetry.io/otel/trace" | ||
|
|
||
| "go.opentelemetry.io/collector/featuregate" | ||
| ) | ||
|
|
||
| const ( | ||
| errInvalidTraceFlagsLength = "trace flags must only be 1 byte" | ||
| ) | ||
|
|
||
| // persistRequestContextFeatureGate controls whether request context should be persisted in the queue. | ||
| var persistRequestContextFeatureGate = featuregate.GlobalRegistry().MustRegister( | ||
| "exporter.PersistRequestContext", | ||
| featuregate.StageAlpha, | ||
| featuregate.WithRegisterFromVersion("v0.127.0"), | ||
| featuregate.WithRegisterDescription("controls whether context should be stored alongside requests in the persistent queue"), | ||
| featuregate.WithRegisterReferenceURL("https://github.com/open-telemetry/opentelemetry-collector/pull/12934"), | ||
| ) | ||
|
|
||
| // necessary due to SpanContext and SpanContextConfig not supporting Unmarshal interface, | ||
| // see https://github.com/open-telemetry/opentelemetry-go/issues/1819. | ||
| type spanContext struct { | ||
| TraceID string | ||
| SpanID string | ||
| TraceFlags string | ||
| TraceState string | ||
| Remote bool | ||
| } | ||
|
|
||
| func localSpanContextFromTraceSpanContext(sc trace.SpanContext) spanContext { | ||
| return spanContext{ | ||
| TraceID: sc.TraceID().String(), | ||
| SpanID: sc.SpanID().String(), | ||
| TraceFlags: sc.TraceFlags().String(), | ||
| TraceState: sc.TraceState().String(), | ||
| Remote: sc.IsRemote(), | ||
| } | ||
| } | ||
|
|
||
| func contextWithLocalSpanContext(ctx context.Context, sc spanContext) context.Context { | ||
| traceID, err := trace.TraceIDFromHex(sc.TraceID) | ||
| if err != nil { | ||
| return ctx | ||
| } | ||
| spanID, err := trace.SpanIDFromHex(sc.SpanID) | ||
| if err != nil { | ||
| return ctx | ||
| } | ||
| traceFlags, err := traceFlagsFromHex(sc.TraceFlags) | ||
| if err != nil { | ||
| return ctx | ||
| } | ||
| traceState, err := trace.ParseTraceState(sc.TraceState) | ||
| if err != nil { | ||
| return ctx | ||
| } | ||
|
|
||
| return trace.ContextWithSpanContext(ctx, trace.NewSpanContext(trace.SpanContextConfig{ | ||
| TraceID: traceID, | ||
| SpanID: spanID, | ||
| TraceFlags: *traceFlags, | ||
| TraceState: traceState, | ||
| Remote: sc.Remote, | ||
| })) | ||
| } | ||
|
|
||
| // requestContext wraps trace.SpanContext to allow for unmarshaling as well as | ||
| // future metadata key/value pairs to be added. | ||
| type requestContext struct { | ||
| SpanContext spanContext | ||
| } | ||
|
|
||
| // reverse of code in trace library https://github.com/open-telemetry/opentelemetry-go/blob/v1.35.0/trace/trace.go#L143-L168 | ||
| func traceFlagsFromHex(hexStr string) (*trace.TraceFlags, error) { | ||
| decoded, err := hex.DecodeString(hexStr) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
| if len(decoded) != 1 { | ||
| return nil, errors.New(errInvalidTraceFlagsLength) | ||
| } | ||
| traceFlags := trace.TraceFlags(decoded[0]) | ||
| return &traceFlags, nil | ||
| } | ||
|
|
||
| func getAndMarshalSpanContext(ctx context.Context) ([]byte, error) { | ||
| if !persistRequestContextFeatureGate.IsEnabled() { | ||
| return nil, nil | ||
| } | ||
| rc := localSpanContextFromTraceSpanContext(trace.SpanContextFromContext(ctx)) | ||
| return json.Marshal(requestContext{SpanContext: rc}) | ||
| } | ||
|
|
||
| func getContextKey(index uint64) string { | ||
| return strconv.FormatUint(index, 10) + "_context" | ||
| } |
Oops, something went wrong.
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.