-
Notifications
You must be signed in to change notification settings - Fork 877
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
[azservicebus] Enable distributed tracing #23860
Draft
karenychen
wants to merge
22
commits into
Azure:main
Choose a base branch
from
karenychen:karenchen/azservicebus-tracing
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from 19 commits
Commits
Show all changes
22 commits
Select commit
Hold shift + click to select a range
feffdf8
add internal tracing wrapper and fake tracer for UT
karenychen 2f4a2b2
set up tracer in SB client and traces in sender methods
karenychen 7c2e4ce
add more unit tests
karenychen 23d7e94
linting
karenychen 1be63f7
move matcher to sdk/internal folder and add callback function for sta…
karenychen b026074
address comments and moved startspan snippet to retrier layer
karenychen 3d07a0a
reverting some files
karenychen 1bb68ed
added receiver traces and some UT
karenychen e653088
add session traces
karenychen 992d9b9
linting
karenychen af668bf
reverting some files
karenychen eaf0389
Merge remote-tracking branch 'origin/main' into karenchen/azservicebu…
karenychen bd50b2a
linting
karenychen ab81102
move tracer to sender/receiver level, refractor SetAttrFn patter, add…
karenychen 09f971e
include span kind in our spans and tests + more unit test
karenychen 14c6576
add internal tracing for NegotiateClaim
karenychen aa0f561
Merge remote-tracking branch 'origin/main' into karenchen/azservicebu…
karenychen 338275d
pushing staged changes that were verified in live test
karenychen b9631d9
clean up
karenychen d2953cd
clean up
karenychen 0f6cf28
Merge remote-tracking branch 'origin/main' into karenchen/azservicebu…
karenychen 3eed895
merge with main changes and fix ut
karenychen 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 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 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 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,127 @@ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT License. | ||
|
||
package tracing | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
|
||
"github.com/Azure/azure-sdk-for-go/sdk/azcore/tracing" | ||
"github.com/Azure/azure-sdk-for-go/sdk/tracing/azotel" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
type Span = tracing.Span | ||
type SpanStatus = tracing.SpanStatus | ||
|
||
// NewSpanValidator creates a Provider that verifies a span was created that matches the specified SpanMatcher. | ||
// The returned Provider can be used to create a client with a tracing provider that will validate spans in unit tests. | ||
func NewSpanValidator(t *testing.T, matcher SpanMatcher) tracing.Provider { | ||
return tracing.NewProvider(func(name, version string) tracing.Tracer { | ||
tt := matchingTracer{ | ||
matcher: matcher, | ||
} | ||
|
||
t.Cleanup(func() { | ||
require.NotNil(t, tt.match, "didn't find a span with name %s", tt.matcher.Name) | ||
require.True(t, tt.match.ended, "span wasn't ended") | ||
if tt.matcher.Kind != 0 { | ||
require.EqualValues(t, tt.matcher.Kind, tt.match.kind, "span kind values don't match") | ||
} | ||
require.EqualValues(t, matcher.Status, tt.match.status, "span status values don't match") | ||
require.ElementsMatch(t, matcher.Attributes, tt.match.attrs, "span attributes don't match") | ||
require.ElementsMatch(t, matcher.Links, tt.match.links, "span links don't match") | ||
}) | ||
|
||
return tracing.NewTracer(func(ctx context.Context, spanName string, options *tracing.SpanOptions) (context.Context, Span) { | ||
kind := tracing.SpanKindInternal | ||
attrs := []tracing.Attribute{} | ||
links := []tracing.Link{} | ||
if options != nil { | ||
kind = options.Kind | ||
attrs = append(attrs, options.Attributes...) | ||
links = append(links, options.Links...) | ||
} | ||
return tt.Start(ctx, spanName, kind, attrs, links) | ||
}, &tracing.TracerOptions{ | ||
SpanFromContext: func(ctx context.Context) Span { | ||
return convertSpan(tt.match) | ||
}, | ||
LinkFromContext: func(ctx context.Context, attrs ...tracing.Attribute) tracing.Link { | ||
return tracing.Link{Attributes: attrs} | ||
}, | ||
}) | ||
}, &tracing.ProviderOptions{ | ||
// use the wrapped propagation.TraceContext propagator | ||
NewPropagatorFn: func() tracing.Propagator { | ||
return azotel.NewTracingProvider(nil, nil).NewPropagator() | ||
}, | ||
}) | ||
} | ||
|
||
// SpanMatcher contains the values to match when a span is created. | ||
type SpanMatcher struct { | ||
Name string | ||
Kind tracing.SpanKind | ||
Status SpanStatus | ||
Attributes []tracing.Attribute | ||
Links []tracing.Link | ||
} | ||
|
||
type matchingTracer struct { | ||
matcher SpanMatcher | ||
match *span | ||
} | ||
|
||
func (mt *matchingTracer) Start(ctx context.Context, spanName string, kind tracing.SpanKind, attrs []tracing.Attribute, links []tracing.Link) (context.Context, Span) { | ||
if spanName != mt.matcher.Name { | ||
return ctx, Span{} | ||
} | ||
// span name matches our matcher, track it | ||
mt.match = &span{ | ||
name: spanName, | ||
kind: kind, | ||
attrs: attrs, | ||
links: links, | ||
} | ||
|
||
return ctx, convertSpan(mt.match) | ||
} | ||
|
||
func convertSpan(sp *span) Span { | ||
return tracing.NewSpan(tracing.SpanImpl{ | ||
End: sp.End, | ||
SetStatus: sp.SetStatus, | ||
SetAttributes: sp.SetAttributes, | ||
AddLink: sp.AddLink, | ||
}) | ||
} | ||
|
||
type span struct { | ||
name string | ||
kind tracing.SpanKind | ||
status SpanStatus | ||
attrs []tracing.Attribute | ||
links []tracing.Link | ||
desc string | ||
ended bool | ||
} | ||
|
||
func (s *span) End() { | ||
s.ended = true | ||
} | ||
|
||
func (s *span) SetAttributes(attrs ...tracing.Attribute) { | ||
s.attrs = append(s.attrs, attrs...) | ||
} | ||
|
||
func (s *span) AddLink(link tracing.Link) { | ||
s.links = append(s.links, link) | ||
} | ||
|
||
func (s *span) SetStatus(code SpanStatus, desc string) { | ||
s.status = code | ||
s.desc = desc | ||
s.ended = true | ||
} |
This file contains 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,69 @@ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT License. | ||
|
||
package tracing | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
|
||
"github.com/Azure/azure-sdk-for-go/sdk/azcore/runtime" | ||
"github.com/Azure/azure-sdk-for-go/sdk/azcore/tracing" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestNewSpanValidator(t *testing.T) { | ||
provider := NewSpanValidator(t, SpanMatcher{ | ||
Name: "TestSpan", | ||
Kind: tracing.SpanKindClient, | ||
Status: tracing.SpanStatusUnset, | ||
Attributes: []tracing.Attribute{ | ||
{Key: "testKey", Value: "testValue"}, | ||
}, | ||
}) | ||
tracer := provider.NewTracer("module", "version") | ||
require.NotNil(t, tracer) | ||
require.True(t, tracer.Enabled()) | ||
|
||
ctx, endSpan := runtime.StartSpan(context.Background(), "TestSpan", tracer, &runtime.StartSpanOptions{ | ||
Kind: tracing.SpanKindClient, | ||
Attributes: []tracing.Attribute{ | ||
{Key: "testKey", Value: "testValue"}, | ||
}, | ||
}) | ||
defer func() { endSpan(nil) }() | ||
|
||
require.NotNil(t, tracer.SpanFromContext(ctx)) | ||
require.NotNil(t, tracer.LinkFromContext(ctx)) | ||
} | ||
|
||
func TestMatchingTracerStart(t *testing.T) { | ||
matcher := SpanMatcher{ | ||
Name: "TestSpan", | ||
Kind: tracing.SpanKindProducer, | ||
Status: tracing.SpanStatusUnset, | ||
Attributes: []tracing.Attribute{ | ||
{Key: "testKey1", Value: "testValue1"}, | ||
{Key: "testKey2", Value: "testValue2"}, | ||
}, | ||
} | ||
tracer := matchingTracer{ | ||
matcher: matcher, | ||
} | ||
ctx := context.Background() | ||
// no-op when SpanName doesn't match | ||
_, spn := tracer.Start(ctx, "BadSpanName", tracing.SpanKindProducer, nil, nil) | ||
require.EqualValues(t, spn, tracing.Span{}) | ||
// tracks span when SpanName matches | ||
_, spn = tracer.Start(ctx, "TestSpan", tracing.SpanKindProducer, []tracing.Attribute{ | ||
{Key: "testKey1", Value: "testValue1"}, | ||
{Key: "testKey2", Value: "testValue2"}, | ||
}, nil) | ||
require.NotNil(t, spn) | ||
spn.SetAttributes(tracing.Attribute{ | ||
Key: "TestAttributeKey", | ||
Value: "TestAttributeValue", | ||
}) | ||
spn.AddLink(tracing.Link{}) | ||
spn.SetStatus(tracing.SpanStatusOK, "ok") | ||
} |
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.
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.
This is a span validator used to validate spans in unit tests. I will make a separate PR on it but putting it here for reference