Skip to content

Commit 0f0b04d

Browse files
stellarhumanRaghav Rathi
and
Raghav Rathi
authored
feat: Update span interface to add a new function which will return span id (#245)
* get spanId * added tests and impl * Better Tests and impl for mock span * Comments --------- Co-authored-by: Raghav Rathi <[email protected]>
1 parent 9789c3a commit 0f0b04d

File tree

4 files changed

+18
-0
lines changed

4 files changed

+18
-0
lines changed

instrumentation/opentelemetry/span.go

+4
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,10 @@ func (s *Span) AddEvent(name string, ts time.Time, attributes map[string]interfa
112112
s.Span.AddEvent(name, trace.WithTimestamp(ts), trace.WithAttributes(otAttributes...))
113113
}
114114

115+
func (s *Span) GetSpanId() string {
116+
return s.Span.SpanContext().SpanID().String()
117+
}
118+
115119
func SpanFromContext(ctx context.Context) sdk.Span {
116120
return &Span{trace.SpanFromContext(ctx)}
117121
}

instrumentation/opentelemetry/span_test.go

+6
Original file line numberDiff line numberDiff line change
@@ -148,3 +148,9 @@ func TestLen(t *testing.T) {
148148
// service.instance.id is added implicitly in StartSpan so 3 attributes will be present.
149149
assert.Equal(t, 3, s.GetAttributes().Len())
150150
}
151+
152+
func TestGetSpanID(t *testing.T) {
153+
_, s, _ := StartSpan(context.Background(), "test_span", &sdk.SpanOptions{})
154+
spanId := s.GetSpanId()
155+
assert.NotEqual(t, 0, len(spanId))
156+
}

sdk/internal/mock/span.go

+5
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,11 @@ func (s *Span) AddEvent(name string, ts time.Time, attributes map[string]interfa
113113
s.spanEvents = append(s.spanEvents, spanEvent{name, ts, attributes})
114114
}
115115

116+
// This function has no use, it has been added just so that the interface in sdk/span.go remains implemented
117+
func (s *Span) GetSpanId() string {
118+
return ""
119+
}
120+
116121
type spanKey string
117122

118123
func SpanFromContext(ctx context.Context) sdk.Span {

sdk/span.go

+3
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,9 @@ type Span interface {
3636

3737
// AddEvent adds an event to the Span with the provided name, timestamp and attributes.
3838
AddEvent(name string, ts time.Time, attributes map[string]interface{})
39+
40+
//GetSpanId fetches the ID of the span that it is called upon
41+
GetSpanId() string
3942
}
4043

4144
// SpanFromContext retrieves the existing span from a context

0 commit comments

Comments
 (0)