Skip to content

Commit a776e95

Browse files
krnowakrghetia
authored andcommitted
Consolidate AddEvent and Event methods, add FinishOptions (open-telemetry#99)
* Merge two event methods in span API There was an agreement to get rid of the Event interface and consolidate the two methods for adding events into one. See open-telemetry#57. * Eliminate the use of the Event interface There is no need for the SDK to provide the implementation of the Event interface - it is used nowhere. * Drop the Event interface It's dead code now. * Make it possible to override a finish timestamp through options Opentracing to opentelemetry bridge will certainly use this feature. * Obey the start time option * Add tests for events and custom start/end times
1 parent 3fc6025 commit a776e95

File tree

15 files changed

+297
-98
lines changed

15 files changed

+297
-98
lines changed

api/event/event.go

-29
This file was deleted.

api/trace/api.go

+14-5
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ import (
2121
"google.golang.org/grpc/codes"
2222

2323
"go.opentelemetry.io/api/core"
24-
"go.opentelemetry.io/api/event"
2524
"go.opentelemetry.io/api/tag"
2625
)
2726

@@ -50,18 +49,28 @@ type Tracer interface {
5049
Inject(context.Context, Span, Injector)
5150
}
5251

52+
type FinishOptions struct {
53+
FinishTime time.Time
54+
}
55+
56+
type FinishOption func(*FinishOptions)
57+
58+
func WithFinishTime(finishTime time.Time) FinishOption {
59+
return func(opts *FinishOptions) {
60+
opts.FinishTime = finishTime
61+
}
62+
}
63+
5364
type Span interface {
5465
// Tracer returns tracer used to create this span. Tracer cannot be nil.
5566
Tracer() Tracer
5667

5768
// Finish completes the span. No updates are allowed to span after it
5869
// finishes. The only exception is setting status of the span.
59-
Finish()
70+
Finish(options ...FinishOption)
6071

6172
// AddEvent adds an event to the span.
62-
AddEvent(ctx context.Context, event event.Event)
63-
// AddEvent records an event to the span.
64-
Event(ctx context.Context, msg string, attrs ...core.KeyValue)
73+
AddEvent(ctx context.Context, msg string, attrs ...core.KeyValue)
6574

6675
// IsRecordingEvents returns true if the span is active and recording events is enabled.
6776
IsRecordingEvents() bool

api/trace/current_test.go

+2-7
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import (
77
"google.golang.org/grpc/codes"
88

99
"go.opentelemetry.io/api/core"
10-
"go.opentelemetry.io/api/event"
1110
"go.opentelemetry.io/api/tag"
1211
"go.opentelemetry.io/api/trace"
1312
)
@@ -97,18 +96,14 @@ func (mockSpan) ModifyAttributes(mutators ...tag.Mutator) {
9796
}
9897

9998
// Finish does nothing.
100-
func (mockSpan) Finish() {
99+
func (mockSpan) Finish(options ...trace.FinishOption) {
101100
}
102101

103102
// Tracer returns noop implementation of Tracer.
104103
func (mockSpan) Tracer() trace.Tracer {
105104
return trace.NoopTracer{}
106105
}
107106

108-
// AddEvent does nothing.
109-
func (mockSpan) AddEvent(ctx context.Context, event event.Event) {
110-
}
111-
112107
// Event does nothing.
113-
func (mockSpan) Event(ctx context.Context, msg string, attrs ...core.KeyValue) {
108+
func (mockSpan) AddEvent(ctx context.Context, msg string, attrs ...core.KeyValue) {
114109
}

api/trace/noop_span.go

+2-7
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ import (
2020
"google.golang.org/grpc/codes"
2121

2222
"go.opentelemetry.io/api/core"
23-
"go.opentelemetry.io/api/event"
2423
"go.opentelemetry.io/api/tag"
2524
)
2625

@@ -64,7 +63,7 @@ func (NoopSpan) ModifyAttributes(mutators ...tag.Mutator) {
6463
}
6564

6665
// Finish does nothing.
67-
func (NoopSpan) Finish() {
66+
func (NoopSpan) Finish(options ...FinishOption) {
6867
}
6968

7069
// Tracer returns noop implementation of Tracer.
@@ -73,11 +72,7 @@ func (NoopSpan) Tracer() Tracer {
7372
}
7473

7574
// AddEvent does nothing.
76-
func (NoopSpan) AddEvent(ctx context.Context, event event.Event) {
77-
}
78-
79-
// Event does nothing.
80-
func (NoopSpan) Event(ctx context.Context, msg string, attrs ...core.KeyValue) {
75+
func (NoopSpan) AddEvent(ctx context.Context, msg string, attrs ...core.KeyValue) {
8176
}
8277

8378
// SetName does nothing.

example/basic/main.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ func main() {
6363

6464
err := tracer.WithSpan(ctx, "operation", func(ctx context.Context) error {
6565

66-
trace.CurrentSpan(ctx).Event(ctx, "Nice operation!", key.New("bogons").Int(100))
66+
trace.CurrentSpan(ctx).AddEvent(ctx, "Nice operation!", key.New("bogons").Int(100))
6767

6868
trace.CurrentSpan(ctx).SetAttributes(anotherKey.String("yes"))
6969

@@ -75,7 +75,7 @@ func main() {
7575
func(ctx context.Context) error {
7676
trace.CurrentSpan(ctx).SetAttribute(lemonsKey.String("five"))
7777

78-
trace.CurrentSpan(ctx).Event(ctx, "Sub span event")
78+
trace.CurrentSpan(ctx).AddEvent(ctx, "Sub span event")
7979

8080
stats.Record(ctx, measureTwo.M(1.3))
8181

example/http/server/server.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ func main() {
4949
)
5050
defer span.Finish()
5151

52-
span.Event(ctx, "handling this...")
52+
span.AddEvent(ctx, "handling this...")
5353

5454
_, _ = io.WriteString(w, "Hello, world!\n")
5555
}

experimental/streaming/exporter/observer/observer.go

-1
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@ type ScopeID struct {
3636
core.SpanContext
3737
}
3838

39-
// TODO: this Event is confusing with event.Event.
4039
type Event struct {
4140
// Automatic fields
4241
Sequence EventID // Auto-filled
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
// Copyright 2019, OpenTelemetry Authors
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package internal // import "go.opentelemetry.io/experimental/streaming/sdk/internal"
16+
17+
import (
18+
"go.opentelemetry.io/experimental/streaming/exporter/observer"
19+
)
20+
21+
type eventsMap map[observer.EventType][]observer.Event
22+
23+
type TestObserver struct {
24+
events eventsMap
25+
}
26+
27+
var _ observer.Observer = &TestObserver{}
28+
29+
func NewRegisteredObserver() *TestObserver {
30+
o := &TestObserver{}
31+
observer.RegisterObserver(o)
32+
return o
33+
}
34+
35+
func (o *TestObserver) Unregister() {
36+
observer.UnregisterObserver(o)
37+
}
38+
39+
func (o *TestObserver) Observe(e observer.Event) {
40+
if o.events == nil {
41+
o.events = make(eventsMap)
42+
}
43+
o.events[e.Type] = append(o.events[e.Type], e)
44+
}
45+
46+
func (o *TestObserver) Clear() {
47+
o.events = nil
48+
}
49+
50+
func (o *TestObserver) ClearAndUnregister() {
51+
o.Clear()
52+
o.Unregister()
53+
}
54+
55+
func (o *TestObserver) Events(eType observer.EventType) []observer.Event {
56+
if o.events == nil {
57+
return nil
58+
}
59+
return o.events[eType]
60+
}

experimental/streaming/sdk/span.go

+11-10
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,11 @@ package sdk
1616

1717
import (
1818
"context"
19+
"time"
1920

2021
"google.golang.org/grpc/codes"
2122

2223
"go.opentelemetry.io/api/core"
23-
"go.opentelemetry.io/api/event"
2424
"go.opentelemetry.io/api/tag"
2525
apitrace "go.opentelemetry.io/api/trace"
2626
"go.opentelemetry.io/experimental/streaming/exporter/observer"
@@ -87,9 +87,14 @@ func (sp *span) ModifyAttributes(mutators ...tag.Mutator) {
8787
})
8888
}
8989

90-
func (sp *span) Finish() {
90+
func (sp *span) Finish(options ...apitrace.FinishOption) {
9191
recovered := recover()
92+
opts := apitrace.FinishOptions{}
93+
for _, opt := range options {
94+
opt(&opts)
95+
}
9296
observer.Record(observer.Event{
97+
Time: opts.FinishTime,
9398
Type: observer.FINISH_SPAN,
9499
Scope: sp.ScopeID(),
95100
Recovered: recovered,
@@ -103,17 +108,13 @@ func (sp *span) Tracer() apitrace.Tracer {
103108
return sp.tracer
104109
}
105110

106-
func (sp *span) AddEvent(ctx context.Context, event event.Event) {
107-
observer.Record(observer.Event{
108-
Type: observer.ADD_EVENT,
109-
String: event.Message(),
110-
Attributes: event.Attributes(),
111-
Context: ctx,
112-
})
111+
func (sp *span) AddEvent(ctx context.Context, msg string, attrs ...core.KeyValue) {
112+
sp.addEventWithTime(ctx, time.Time{}, msg, attrs...)
113113
}
114114

115-
func (sp *span) Event(ctx context.Context, msg string, attrs ...core.KeyValue) {
115+
func (sp *span) addEventWithTime(ctx context.Context, timestamp time.Time, msg string, attrs ...core.KeyValue) {
116116
observer.Record(observer.Event{
117+
Time: timestamp,
117118
Type: observer.ADD_EVENT,
118119
String: msg,
119120
Attributes: attrs,

0 commit comments

Comments
 (0)