Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

trace: Add Span.AddLink method #5032

Merged
merged 17 commits into from
Mar 28, 2024
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm

### Added

- Add `AddLink` method to the `Span` interface in `go.opentelemetry.io/otel/trace`. (#5032)
- Add `WithProxy` option in `go.opentelemetry.io/otel/exporters/otlp/otlpmetric/otlpmetrichttp`. (#4906)
- Add `WithProxy` option in `go.opentelemetry.io/otel/exporters/otlp/otlpmetric/otlptracehttp`. (#4906)
- The `Enabled` method is added to the `Logger` interface in `go.opentelemetry.io/otel/log`.
Expand Down
13 changes: 13 additions & 0 deletions bridge/opentracing/internal/mock.go
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,11 @@
Attributes []attribute.KeyValue
}

type MockLink struct {
SpanContext trace.SpanContext
Attributes []attribute.KeyValue
}

type MockSpan struct {
embedded.Span

Expand All @@ -190,6 +195,7 @@
EndTime time.Time
ParentSpanID trace.SpanID
Events []MockEvent
Links []MockLink
}

var (
Expand Down Expand Up @@ -286,6 +292,13 @@
})
}

func (s *MockSpan) AddLink(link trace.Link) {
s.Links = append(s.Links, MockLink{
SpanContext: link.SpanContext,
Attributes: link.Attributes,
})

Check warning on line 299 in bridge/opentracing/internal/mock.go

View check run for this annotation

Codecov / codecov/patch

bridge/opentracing/internal/mock.go#L295-L299

Added lines #L295 - L299 were not covered by tests
}

func (s *MockSpan) OverrideTracer(tracer trace.Tracer) {
s.officialTracer = tracer
}
Expand Down
3 changes: 3 additions & 0 deletions internal/global/trace.go
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,9 @@
// AddEvent does nothing.
func (nonRecordingSpan) AddEvent(string, ...trace.EventOption) {}

// AddLink does nothing.
func (nonRecordingSpan) AddLink(trace.Link) {}

Check warning on line 186 in internal/global/trace.go

View check run for this annotation

Codecov / codecov/patch

internal/global/trace.go#L186

Added line #L186 was not covered by tests

// SetName does nothing.
func (nonRecordingSpan) SetName(string) {}

Expand Down
5 changes: 4 additions & 1 deletion sdk/trace/span.go
Original file line number Diff line number Diff line change
Expand Up @@ -629,7 +629,7 @@
return s.tracer.provider.resource
}

func (s *recordingSpan) addLink(link trace.Link) {
func (s *recordingSpan) AddLink(link trace.Link) {
if !s.IsRecording() || !link.SpanContext.IsValid() {
return
}
Expand Down Expand Up @@ -803,6 +803,9 @@
// AddEvent does nothing.
func (nonRecordingSpan) AddEvent(string, ...trace.EventOption) {}

// AddLink does nothing.
func (nonRecordingSpan) AddLink(trace.Link) {}

Check warning on line 807 in sdk/trace/span.go

View check run for this annotation

Codecov / codecov/patch

sdk/trace/span.go#L807

Added line #L807 was not covered by tests

// SetName does nothing.
func (nonRecordingSpan) SetName(string) {}

Expand Down
78 changes: 78 additions & 0 deletions sdk/trace/trace_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1976,3 +1976,81 @@ func TestEmptyRecordingSpanAttributes(t *testing.T) {
func TestEmptyRecordingSpanDroppedAttributes(t *testing.T) {
assert.Equal(t, 0, (&recordingSpan{}).DroppedAttributes())
}

func TestAddLinkWithInvalidSpanContext(t *testing.T) {
te := NewTestExporter()
sl := NewSpanLimits()
tp := NewTracerProvider(
WithSpanLimits(sl),
WithSyncer(te),
WithResource(resource.Empty()),
)
span := startSpan(tp, "AddSpanWithInvalidSpanContext")
inValidContext := trace.NewSpanContext(trace.SpanContextConfig{
TraceID: trace.TraceID([16]byte{}),
SpanID: [8]byte{},
})
attrs := []attribute.KeyValue{{Key: "k", Value: attribute.StringValue("v")}}
span.AddLink(trace.Link{
SpanContext: inValidContext,
Attributes: attrs,
})

want := &snapshot{
name: "span0",
spanContext: trace.NewSpanContext(trace.SpanContextConfig{
TraceID: tid,
TraceFlags: 0x1,
}),
parent: sc.WithRemote(true),
links: nil,
spanKind: trace.SpanKindInternal,
instrumentationScope: instrumentation.Scope{Name: "AddSpanWithInvalidSpanContext"},
}
got, err := endSpan(te, span)
if err != nil {
t.Fatal(err)
}
if diff := cmpDiff(got, want); diff != "" {
t.Errorf("AddLinkWithInvalidSpanContext: -got +want %s", diff)
}
}

func TestAddLink(t *testing.T) {
pellared marked this conversation as resolved.
Show resolved Hide resolved
te := NewTestExporter()
sl := NewSpanLimits()
tp := NewTracerProvider(
WithSpanLimits(sl),
WithSyncer(te),
WithResource(resource.Empty()),
)
attrs := []attribute.KeyValue{{Key: "k", Value: attribute.StringValue("v")}}
span := startSpan(tp, "AddSpan")

link := trace.Link{SpanContext: sc, Attributes: attrs}
span.AddLink(link)

want := &snapshot{
name: "span0",
spanContext: trace.NewSpanContext(trace.SpanContextConfig{
TraceID: tid,
TraceFlags: 0x1,
}),
parent: sc.WithRemote(true),
links: []Link{
{
SpanContext: sc,
Attributes: attrs,
},
},
spanKind: trace.SpanKindInternal,
instrumentationScope: instrumentation.Scope{Name: "AddSpan"},
}
got, err := endSpan(te, span)
if err != nil {
t.Fatal(err)
}
if diff := cmpDiff(got, want); diff != "" {
t.Errorf("AddLink: -got +want %s", diff)
}
}
2 changes: 1 addition & 1 deletion sdk/trace/tracer.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ func (tr *tracer) newRecordingSpan(psc, sc trace.SpanContext, name string, sr Sa
}

for _, l := range config.Links() {
s.addLink(l)
s.AddLink(l)
}

s.SetAttributes(sr.Attributes...)
Expand Down
3 changes: 3 additions & 0 deletions trace/noop.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,9 @@
// AddEvent does nothing.
func (noopSpan) AddEvent(string, ...EventOption) {}

// AddLink does nothing.
func (noopSpan) AddLink(Link) {}

Check warning on line 79 in trace/noop.go

View check run for this annotation

Codecov / codecov/patch

trace/noop.go#L79

Added line #L79 was not covered by tests

// SetName does nothing.
func (noopSpan) SetName(string) {}

Expand Down
3 changes: 3 additions & 0 deletions trace/noop/noop.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,9 @@ func (Span) RecordError(error, ...trace.EventOption) {}
// AddEvent does nothing.
func (Span) AddEvent(string, ...trace.EventOption) {}

// AddLink does nothing.
func (Span) AddLink(trace.Link) {}

// SetName does nothing.
func (Span) SetName(string) {}

Expand Down
6 changes: 6 additions & 0 deletions trace/trace.go
Original file line number Diff line number Diff line change
Expand Up @@ -350,6 +350,12 @@ type Span interface {
// AddEvent adds an event with the provided name and options.
AddEvent(name string, options ...EventOption)

// AddLink adds a link.
// Adding links at span creation using WithLinks is preferred to calling AddLink
// later, for contexts that are available during span creation, because head
// sampling decisions can only consider information present during span creation.
AddLink(link Link)

// IsRecording returns the recording state of the Span. It will return
// true if the Span is active and events can be recorded.
IsRecording() bool
Expand Down