Skip to content

Commit

Permalink
Allow the span kind to be set via StartSpanOptions (#23590)
Browse files Browse the repository at this point in the history
  • Loading branch information
kjg authored Oct 16, 2024
1 parent 40dcd1b commit 7a626d2
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 3 deletions.
2 changes: 2 additions & 0 deletions sdk/azcore/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

### Features Added

* Added field `Kind` to `runtime.StartSpanOptions` to allow a kind to be set when starting a span.

### Breaking Changes

### Bugs Fixed
Expand Down
10 changes: 7 additions & 3 deletions sdk/azcore/runtime/policy_http_trace.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,8 @@ func (h *httpTracePolicy) Do(req *policy.Request) (resp *http.Response, err erro

// StartSpanOptions contains the optional values for StartSpan.
type StartSpanOptions struct {
// Kind indicates the kind of Span.
Kind tracing.SpanKind
// Attributes contains key-value pairs of attributes for the span.
Attributes []tracing.Attribute
}
Expand All @@ -115,7 +117,6 @@ func StartSpan(ctx context.Context, name string, tracer tracing.Tracer, options
// we MUST propagate the active tracer before returning so that the trace policy can access it
ctx = context.WithValue(ctx, shared.CtxWithTracingTracer{}, tracer)

const newSpanKind = tracing.SpanKindInternal
if activeSpan := ctx.Value(ctxActiveSpan{}); activeSpan != nil {
// per the design guidelines, if a SDK method Foo() calls SDK method Bar(),
// then the span for Bar() must be suppressed. however, if Bar() makes a REST
Expand All @@ -131,12 +132,15 @@ func StartSpan(ctx context.Context, name string, tracer tracing.Tracer, options
if options == nil {
options = &StartSpanOptions{}
}
if options.Kind == 0 {
options.Kind = tracing.SpanKindInternal
}

ctx, span := tracer.Start(ctx, name, &tracing.SpanOptions{
Kind: newSpanKind,
Kind: options.Kind,
Attributes: options.Attributes,
})
ctx = context.WithValue(ctx, ctxActiveSpan{}, newSpanKind)
ctx = context.WithValue(ctx, ctxActiveSpan{}, options.Kind)
return ctx, func(err error) {
if err != nil {
errType := strings.Replace(fmt.Sprintf("%T", err), "*exported.", "*azcore.", 1)
Expand Down
27 changes: 27 additions & 0 deletions sdk/azcore/runtime/policy_http_trace_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -267,3 +267,30 @@ func TestStartSpanWithAttributes(t *testing.T) {
require.True(t, startCalled)
require.True(t, endCalled)
}

func TestStartSpanWithKind(t *testing.T) {
// span no error
var startCalled bool
var endCalled bool
tr := tracing.NewTracer(func(ctx context.Context, spanName string, options *tracing.SpanOptions) (context.Context, tracing.Span) {
startCalled = true
require.EqualValues(t, "TestStartSpan", spanName)
require.NotNil(t, options)
// The span kind should be passed through
require.EqualValues(t, tracing.SpanKindClient, options.Kind)
spanImpl := tracing.SpanImpl{
End: func() { endCalled = true },
}
return ctx, tracing.NewSpan(spanImpl)
}, nil)
ctx, end := StartSpan(context.Background(), "TestStartSpan", tr, &StartSpanOptions{
Kind: tracing.SpanKindClient,
})
end(nil)
ctxTr := ctx.Value(shared.CtxWithTracingTracer{})
require.NotNil(t, ctxTr)
_, ok := ctxTr.(tracing.Tracer)
require.True(t, ok)
require.True(t, startCalled)
require.True(t, endCalled)
}

0 comments on commit 7a626d2

Please sign in to comment.