Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions instrumentation/opentelemetry/span.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do we expect this value to be not null everytime or do need to something like

if (s.Span.SpanContext().SpanID().HasSpanID()) {
   s.Span.SpanContext().SpanID().String()
 }
 return ""

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

idts, if a span doest exist, you should always be able to call this method

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this should be fine. String's null value is empty string in golang. As long as it returns a string, we dont have to check here explicitly

}

func SpanFromContext(ctx context.Context) sdk.Span {
return &Span{trace.SpanFromContext(ctx)}
}
Expand Down
6 changes: 6 additions & 0 deletions instrumentation/opentelemetry/span_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}
5 changes: 5 additions & 0 deletions sdk/internal/mock/span.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,11 @@
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 ""

Check warning on line 118 in sdk/internal/mock/span.go

View check run for this annotation

Codecov / codecov/patch

sdk/internal/mock/span.go#L117-L118

Added lines #L117 - L118 were not covered by tests
}

type spanKey string

func SpanFromContext(ctx context.Context) sdk.Span {
Expand Down
3 changes: 3 additions & 0 deletions sdk/span.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading