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 13 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
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` into the `Span` interface in the trace API. (#5032)
q-cheng marked this conversation as resolved.
Show resolved Hide resolved
- 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 @@ type MockEvent struct {
Attributes []attribute.KeyValue
}

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

type MockSpan struct {
embedded.Span

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

var (
Expand Down Expand Up @@ -286,6 +292,13 @@ func (s *MockSpan) AddEvent(name string, o ...trace.EventOption) {
})
}

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

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 @@ func (nonRecordingSpan) RecordError(error, ...trace.EventOption) {}
// AddEvent does nothing.
func (nonRecordingSpan) AddEvent(string, ...trace.EventOption) {}

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

// 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 @@ func (s *recordingSpan) Resource() *resource.Resource {
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 @@ func (nonRecordingSpan) RecordError(error, ...trace.EventOption) {}
// AddEvent does nothing.
func (nonRecordingSpan) AddEvent(string, ...trace.EventOption) {}

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

// 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 TestAddSpanWithInvalidSpanContext(t *testing.T) {
q-cheng marked this conversation as resolved.
Show resolved Hide resolved
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("Link: -got +want %s", diff)
q-cheng marked this conversation as resolved.
Show resolved Hide resolved
}
}

func TestAddSpan(t *testing.T) {
q-cheng 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("Link: -got +want %s", diff)
q-cheng marked this conversation as resolved.
Show resolved Hide resolved
}
}
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 @@ func (noopSpan) RecordError(error, ...EventOption) {}
// AddEvent does nothing.
func (noopSpan) AddEvent(string, ...EventOption) {}

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

// 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
3 changes: 3 additions & 0 deletions trace/trace.go
Original file line number Diff line number Diff line change
Expand Up @@ -350,6 +350,9 @@ type Span interface {
// AddEvent adds an event with the provided name and options.
AddEvent(name string, options ...EventOption)

// AddLink adds link after span starts.
q-cheng marked this conversation as resolved.
Show resolved Hide resolved
q-cheng marked this conversation as resolved.
Show resolved Hide resolved
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
Loading