-
Notifications
You must be signed in to change notification settings - Fork 73
tracing: add ExtractTraceSpanID #550
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 4 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
8ee361e
tracing: add ExtractTraceSpanID function
krajorama de1a00f
update changelog and have a test
krajorama ab0f401
Remove unit test
krajorama 3fa763a
Revert "Remove unit test"
krajorama 7495931
Do not use pointer
krajorama bb5d549
Reduce copies by only returning relevant fields
krajorama 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
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
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,47 @@ | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package tracing | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
|
||
"github.com/opentracing/opentracing-go" | ||
"github.com/stretchr/testify/require" | ||
jaeger "github.com/uber/jaeger-client-go" | ||
) | ||
|
||
func TestExtractTraceSpanID(t *testing.T) { | ||
spanCtx := jaeger.NewSpanContext(jaeger.TraceID{High: 1, Low: 2}, jaeger.SpanID(3), 0, true, nil) | ||
tracer, closer := jaeger.NewTracer("test", jaeger.NewConstSampler(true), jaeger.NewNullReporter()) | ||
defer closer.Close() | ||
span := tracer.StartSpan("test", opentracing.ChildOf(spanCtx)) | ||
|
||
testCases := map[string]struct { | ||
ctx context.Context | ||
expectedOk bool | ||
expectedTraceID string | ||
expectedSpanID string | ||
}{ | ||
"no trace": { | ||
ctx: context.Background(), | ||
expectedOk: false, | ||
expectedTraceID: "", | ||
expectedSpanID: "", | ||
}, | ||
"trace": { | ||
ctx: opentracing.ContextWithSpan(context.Background(), span), | ||
expectedOk: true, | ||
expectedTraceID: "00000000000000010000000000000002", | ||
expectedSpanID: span.Context().(jaeger.SpanContext).SpanID().String(), | ||
}, | ||
} | ||
for name, tc := range testCases { | ||
t.Run(name, func(t *testing.T) { | ||
traceID, spanID, ok := ExtractTraceSpanID(tc.ctx) | ||
require.Equal(t, tc.expectedOk, ok) | ||
require.Equal(t, tc.expectedTraceID, traceID) | ||
require.Equal(t, tc.expectedSpanID, spanID) | ||
}) | ||
} | ||
} |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
By returning
&sctx
,jaeger.SpanContext
needs to be allocated on a heap first, and then pointer to it returned, right?Perhaps we could return just traceID and spanID directly? Or
jaeger.SpanContext
without the pointer.Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I thought about that , but that (direct return) would be duplicated code. I can definitely do a copy of the jeager.SpanContext thought, if that's preferred?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
By the way , I don't know if the compiler is smart enough to avoid allocation on the heap
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we keep ExtractTraceID, and call ExtractTraceSpanID inside, just discard spanID when return result. In that case no need to refectory this extractJaegerContext but leave all logics in ExtractTraceSpanID
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
facepalm, of course
although then your doing an extra string formatting call for the spanid
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it is a fair comment, current code LGTM, approved.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
it did inspire me to go ahead and look at the assembly :) Got the final result by deferring the formatting to the public function , but only returning the two relevant fields from the helper function. It's the least extra instructions.