diff --git a/instrumentation/opentelemetry/span.go b/instrumentation/opentelemetry/span.go index 0872fbdb..58eca3c1 100644 --- a/instrumentation/opentelemetry/span.go +++ b/instrumentation/opentelemetry/span.go @@ -112,6 +112,10 @@ func (s *Span) AddEvent(name string, ts time.Time, attributes map[string]interfa s.Span.AddEvent(name, trace.WithTimestamp(ts), trace.WithAttributes(otAttributes...)) } +func (s *Span) GetSpanId() string { + return s.Span.SpanContext().SpanID().String() +} + func SpanFromContext(ctx context.Context) sdk.Span { return &Span{trace.SpanFromContext(ctx)} } diff --git a/instrumentation/opentelemetry/span_test.go b/instrumentation/opentelemetry/span_test.go index 1855e730..b4a3219b 100644 --- a/instrumentation/opentelemetry/span_test.go +++ b/instrumentation/opentelemetry/span_test.go @@ -148,3 +148,9 @@ func TestLen(t *testing.T) { // service.instance.id is added implicitly in StartSpan so 3 attributes will be present. assert.Equal(t, 3, s.GetAttributes().Len()) } + +func TestGetSpanID(t *testing.T) { + _, s, _ := StartSpan(context.Background(), "test_span", &sdk.SpanOptions{}) + spanId := s.GetSpanId() + assert.NotEqual(t, 0, len(spanId)) +} diff --git a/sdk/internal/mock/span.go b/sdk/internal/mock/span.go index c04d7794..60e59f60 100644 --- a/sdk/internal/mock/span.go +++ b/sdk/internal/mock/span.go @@ -113,6 +113,11 @@ func (s *Span) AddEvent(name string, ts time.Time, attributes map[string]interfa s.spanEvents = append(s.spanEvents, spanEvent{name, ts, attributes}) } +// This function has no use, it has been added just so that the interface in sdk/span.go remains implemented +func (s *Span) GetSpanId() string { + return "" +} + type spanKey string func SpanFromContext(ctx context.Context) sdk.Span { diff --git a/sdk/span.go b/sdk/span.go index 8dc1f75e..85fb249c 100644 --- a/sdk/span.go +++ b/sdk/span.go @@ -36,6 +36,9 @@ type Span interface { // AddEvent adds an event to the Span with the provided name, timestamp and attributes. AddEvent(name string, ts time.Time, attributes map[string]interface{}) + + //GetSpanId fetches the ID of the span that it is called upon + GetSpanId() string } // SpanFromContext retrieves the existing span from a context