From e6f5b4ae5b965a28e8b345a7fb202edeca891b0c Mon Sep 17 00:00:00 2001 From: Raghav Rathi Date: Fri, 14 Feb 2025 10:43:56 +0530 Subject: [PATCH 1/4] get spanId --- sdk/span.go | 2 ++ 1 file changed, 2 insertions(+) diff --git a/sdk/span.go b/sdk/span.go index 8dc1f75e..54e6f760 100644 --- a/sdk/span.go +++ b/sdk/span.go @@ -36,6 +36,8 @@ 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() string } // SpanFromContext retrieves the existing span from a context From 359569390190e8096c2326e3e186d98d0670d3fd Mon Sep 17 00:00:00 2001 From: Raghav Rathi Date: Fri, 14 Feb 2025 11:47:28 +0530 Subject: [PATCH 2/4] added tests and impl --- instrumentation/opentelemetry/span.go | 4 ++++ instrumentation/opentelemetry/span_test.go | 8 ++++++++ 2 files changed, 12 insertions(+) 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..18be5866 100644 --- a/instrumentation/opentelemetry/span_test.go +++ b/instrumentation/opentelemetry/span_test.go @@ -148,3 +148,11 @@ 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() + if len(spanId) == 0 { + t.Error("span id should not be empty") + } +} From 014b3feac6d9d8f3ebe3f8e6b0ca1b01781d648e Mon Sep 17 00:00:00 2001 From: Raghav Rathi Date: Mon, 17 Feb 2025 11:23:05 +0530 Subject: [PATCH 3/4] Better Tests and impl for mock span --- instrumentation/opentelemetry/span_test.go | 4 +--- sdk/internal/mock/span.go | 4 ++++ 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/instrumentation/opentelemetry/span_test.go b/instrumentation/opentelemetry/span_test.go index 18be5866..b4a3219b 100644 --- a/instrumentation/opentelemetry/span_test.go +++ b/instrumentation/opentelemetry/span_test.go @@ -152,7 +152,5 @@ func TestLen(t *testing.T) { func TestGetSpanID(t *testing.T) { _, s, _ := StartSpan(context.Background(), "test_span", &sdk.SpanOptions{}) spanId := s.GetSpanId() - if len(spanId) == 0 { - t.Error("span id should not be empty") - } + assert.NotEqual(t, 0, len(spanId)) } diff --git a/sdk/internal/mock/span.go b/sdk/internal/mock/span.go index c04d7794..af22bc50 100644 --- a/sdk/internal/mock/span.go +++ b/sdk/internal/mock/span.go @@ -113,6 +113,10 @@ func (s *Span) AddEvent(name string, ts time.Time, attributes map[string]interfa s.spanEvents = append(s.spanEvents, spanEvent{name, ts, attributes}) } +func (s *Span) GetSpanId() string { + return "" +} + type spanKey string func SpanFromContext(ctx context.Context) sdk.Span { From 63e66f46e7b88010ba2f4565094e411ead2e9ade Mon Sep 17 00:00:00 2001 From: Raghav Rathi Date: Mon, 17 Feb 2025 15:21:04 +0530 Subject: [PATCH 4/4] Comments --- sdk/internal/mock/span.go | 1 + sdk/span.go | 1 + 2 files changed, 2 insertions(+) diff --git a/sdk/internal/mock/span.go b/sdk/internal/mock/span.go index af22bc50..60e59f60 100644 --- a/sdk/internal/mock/span.go +++ b/sdk/internal/mock/span.go @@ -113,6 +113,7 @@ 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 "" } diff --git a/sdk/span.go b/sdk/span.go index 54e6f760..85fb249c 100644 --- a/sdk/span.go +++ b/sdk/span.go @@ -37,6 +37,7 @@ 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 }