diff --git a/api/trace/api.go b/api/trace/api.go index afb49fa42a54..0167fd25581e 100644 --- a/api/trace/api.go +++ b/api/trace/api.go @@ -71,6 +71,9 @@ type Span interface { // AddEvent adds an event to the span. AddEvent(ctx context.Context, msg string, attrs ...core.KeyValue) + // AddEventWithTimestamp adds an event with a custom timestamp + // to the span. + AddEventWithTimestamp(ctx context.Context, timestamp time.Time, msg string, attrs ...core.KeyValue) // IsRecordingEvents returns true if the span is active and recording events is enabled. IsRecordingEvents() bool diff --git a/api/trace/current_test.go b/api/trace/current_test.go index 599a1b5142f4..7da2b3e71fc0 100644 --- a/api/trace/current_test.go +++ b/api/trace/current_test.go @@ -3,6 +3,7 @@ package trace_test import ( "context" "testing" + "time" "google.golang.org/grpc/codes" @@ -108,6 +109,10 @@ func (mockSpan) Tracer() trace.Tracer { func (mockSpan) AddEvent(ctx context.Context, msg string, attrs ...core.KeyValue) { } +// AddEventWithTimestamp does nothing. +func (mockSpan) AddEventWithTimestamp(ctx context.Context, timestamp time.Time, msg string, attrs ...core.KeyValue) { +} + // AddLink does nothing. func (mockSpan) AddLink(link trace.Link) { } diff --git a/api/trace/noop_span.go b/api/trace/noop_span.go index c4be98ab5e88..cc1b260e1b05 100644 --- a/api/trace/noop_span.go +++ b/api/trace/noop_span.go @@ -16,6 +16,7 @@ package trace import ( "context" + "time" "google.golang.org/grpc/codes" @@ -75,6 +76,10 @@ func (NoopSpan) Tracer() Tracer { func (NoopSpan) AddEvent(ctx context.Context, msg string, attrs ...core.KeyValue) { } +// AddEventWithTimestamp does nothing. +func (NoopSpan) AddEventWithTimestamp(ctx context.Context, timestamp time.Time, msg string, attrs ...core.KeyValue) { +} + // SetName does nothing. func (NoopSpan) SetName(name string) { } diff --git a/experimental/streaming/sdk/span.go b/experimental/streaming/sdk/span.go index 477e764bb9bb..46ad9dc47ed3 100644 --- a/experimental/streaming/sdk/span.go +++ b/experimental/streaming/sdk/span.go @@ -112,6 +112,10 @@ func (sp *span) AddEvent(ctx context.Context, msg string, attrs ...core.KeyValue sp.addEventWithTime(ctx, time.Time{}, msg, attrs...) } +func (sp *span) AddEventWithTimestamp(ctx context.Context, timestamp time.Time, msg string, attrs ...core.KeyValue) { + sp.addEventWithTime(ctx, timestamp, msg, attrs...) +} + func (sp *span) addEventWithTime(ctx context.Context, timestamp time.Time, msg string, attrs ...core.KeyValue) { observer.Record(observer.Event{ Time: timestamp, diff --git a/sdk/trace/span.go b/sdk/trace/span.go index 38e6fd949314..f0347f9481e9 100644 --- a/sdk/trace/span.go +++ b/sdk/trace/span.go @@ -158,6 +158,13 @@ func (s *span) AddEvent(ctx context.Context, msg string, attrs ...core.KeyValue) s.addEventWithTimestamp(time.Now(), msg, attrs...) } +func (s *span) AddEventWithTimestamp(ctx context.Context, timestamp time.Time, msg string, attrs ...core.KeyValue) { + if !s.IsRecordingEvents() { + return + } + s.addEventWithTimestamp(timestamp, msg, attrs...) +} + func (s *span) addEventWithTimestamp(timestamp time.Time, msg string, attrs ...core.KeyValue) { s.mu.Lock() defer s.mu.Unlock()