Allow the span kind to be set via StartSpanOptions (#23590)

This commit is contained in:
Kevin Glowacz 2024-10-16 08:53:41 -05:00 коммит произвёл GitHub
Родитель 40dcd1b036
Коммит 7a626d2e84
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: B5690EEEBB952194
3 изменённых файлов: 36 добавлений и 3 удалений

Просмотреть файл

@ -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

Просмотреть файл

@ -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
}
@ -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
@ -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)

Просмотреть файл

@ -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)
}