Skip to content

Commit

Permalink
Allow specifying custom timestamps for events
Browse files Browse the repository at this point in the history
Adding event with timestamp is not yet a part of the OpenTelemetry
specification, but this function will come in handy when implementing
the OpenTracing bridge.
  • Loading branch information
krnowak committed Sep 21, 2019
1 parent c70cb29 commit c92fcdb
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 0 deletions.
3 changes: 3 additions & 0 deletions api/trace/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 5 additions & 0 deletions api/trace/current_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package trace_test
import (
"context"
"testing"
"time"

"google.golang.org/grpc/codes"

Expand Down Expand Up @@ -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) {
}
Expand Down
5 changes: 5 additions & 0 deletions api/trace/noop_span.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package trace

import (
"context"
"time"

"google.golang.org/grpc/codes"

Expand Down Expand Up @@ -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) {
}
Expand Down
4 changes: 4 additions & 0 deletions experimental/streaming/sdk/span.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
7 changes: 7 additions & 0 deletions sdk/trace/span.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down

0 comments on commit c92fcdb

Please sign in to comment.