Tracing: Add Start helper (#93052)

This commit is contained in:
Todd Treece 2024-09-06 13:02:17 -04:00 committed by GitHub
parent 539d363d2c
commit 85759da360
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 43 additions and 0 deletions

View File

@ -399,3 +399,10 @@ func Errorf(span trace.Span, format string, args ...any) error {
err := fmt.Errorf(format, args...)
return Error(span, err)
}
var instrumentationScope = "github.com/grafana/grafana/pkg/infra/tracing"
// Start only creates an OpenTelemetry span if the incoming context already includes a span.
func Start(ctx context.Context, name string, attributes ...attribute.KeyValue) (context.Context, trace.Span) {
return trace.SpanFromContext(ctx).TracerProvider().Tracer(instrumentationScope).Start(ctx, name, trace.WithAttributes(attributes...))
}

View File

@ -1,10 +1,13 @@
package tracing
import (
"context"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/trace"
)
func TestInitSampler(t *testing.T) {
@ -41,3 +44,36 @@ func TestInitSampler(t *testing.T) {
require.NoError(t, err)
assert.Equal(t, "RateLimitingSampler{100.25}", sampler.Description())
}
func TestStart(t *testing.T) {
name := "test-span"
attributes := []attribute.KeyValue{
attribute.String("test1", "1"),
attribute.Int("test2", 2),
}
t.Run("should return noop span if there is not currently a span in context", func(t *testing.T) {
ctx := context.Background()
_, span := Start(ctx, name, attributes...)
defer span.End()
require.NotNil(t, span)
require.False(t, span.SpanContext().IsValid())
})
t.Run("should return a span with a valid span context if there is currently a span in context", func(t *testing.T) {
spanCtx := trace.NewSpanContext(trace.SpanContextConfig{
TraceID: trace.TraceID{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16},
SpanID: trace.SpanID{1, 2, 3, 4, 5, 6, 7, 8},
TraceFlags: trace.FlagsSampled,
})
ctx := trace.ContextWithSpanContext(context.Background(), spanCtx)
_, childSpan := Start(ctx, name, attributes...)
defer childSpan.End()
require.NotNil(t, childSpan)
require.Equal(t, spanCtx.TraceID(), childSpan.SpanContext().TraceID())
require.True(t, childSpan.SpanContext().IsValid())
})
}