Skip to content

Commit 521a6c4

Browse files
authored
move span, trace and event implementation to sdk. (#27)
* move event to sdk. * move trace and span implementation to sdk. - also added noop implementation of span and trace. * fix review comments.
1 parent 541621c commit 521a6c4

File tree

14 files changed

+1120
-105
lines changed

14 files changed

+1120
-105
lines changed

api/event/event.go

-28
Original file line numberDiff line numberDiff line change
@@ -15,17 +15,10 @@
1515
package event
1616

1717
import (
18-
"fmt"
19-
2018
"github.com/open-telemetry/opentelemetry-go/api/core"
2119
)
2220

2321
type (
24-
event struct {
25-
message string
26-
attributes []core.KeyValue
27-
}
28-
2922
// Event interface provides methods to retrieve Event properties.
3023
Event interface {
3124

@@ -36,24 +29,3 @@ type (
3629
Attributes() []core.KeyValue
3730
}
3831
)
39-
40-
var _ Event = (*event)(nil)
41-
42-
// WithAttr creates an Event with Attributes and a message.
43-
// Attributes are immutable.
44-
func WithAttr(msg string, attributes ...core.KeyValue) Event {
45-
return event{message: msg, attributes: attributes}
46-
}
47-
48-
// WithString creates an Event with formatted string.
49-
func WithString(f string, args ...interface{}) Event {
50-
return event{message: fmt.Sprint(f, args), attributes: nil}
51-
}
52-
53-
func (e event) Message() string {
54-
return e.message
55-
}
56-
57-
func (e event) Attributes() []core.KeyValue {
58-
return append(e.attributes[:0:0], e.attributes...)
59-
}

api/trace/api.go

+61-22
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ package trace
1616

1717
import (
1818
"context"
19+
"sync/atomic"
1920
"time"
2021

2122
"google.golang.org/grpc/codes"
@@ -31,14 +32,21 @@ type (
3132
Tracer interface {
3233
Start(context.Context, string, ...SpanOption) (context.Context, Span)
3334

35+
// WithSpan wraps the execution of the function body with a span.
36+
// It starts a new span and sets it as an active span in the context.
37+
// It then executes the body. It closes the span before returning the execution result.
38+
// TODO: Should it restore the previous span?
3439
WithSpan(
3540
ctx context.Context,
3641
operation string,
3742
body func(ctx context.Context) error,
3843
) error
3944

45+
// TODO: Do we need WithService and WithComponent?
4046
WithService(name string) Tracer
4147
WithComponent(name string) Tracer
48+
49+
// WithResources attaches resource attributes to the Tracer.
4250
WithResources(res ...core.KeyValue) Tracer
4351

4452
// Note: see https://github.com/opentracing/opentracing-go/issues/127
@@ -53,39 +61,50 @@ type (
5361

5462
stats.Interface
5563

56-
SetError(bool)
57-
64+
// Tracer returns tracer used to create this span. Tracer cannot be nil.
5865
Tracer() Tracer
5966

67+
// Finish completes the span. No updates are allowed to span after it
68+
// finishes. The only exception is setting status of the span.
6069
Finish()
6170

6271
// AddEvent adds an event to the span.
6372
AddEvent(ctx context.Context, event event.Event)
6473

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

6877
// SpancContext returns span context of the span. Return SpanContext is usable
6978
// even after the span is finished.
7079
SpanContext() core.SpanContext
7180

81+
// SetStatus sets the status of the span. The status of the span can be updated
82+
// even after span is finished.
7283
SetStatus(codes.Code)
7384
}
7485

7586
Injector interface {
87+
// Inject serializes span context and tag.Map and inserts them in to
88+
// carrier associated with the injector. For example in case of http request,
89+
// span context could added to the request (carrier) as W3C Trace context header.
7690
Inject(core.SpanContext, tag.Map)
7791
}
7892

7993
// SpanOption apply changes to SpanOptions.
8094
SpanOption func(*SpanOptions)
8195

96+
// SpanOptions provides options to set properties of span at the time of starting
97+
// a new span.
8298
SpanOptions struct {
83-
attributes []core.KeyValue
84-
startTime time.Time
85-
reference Reference
86-
recordEvent bool
99+
Attributes []core.KeyValue
100+
StartTime time.Time
101+
Reference Reference
102+
RecordEvent bool
87103
}
88104

105+
// Reference is used to establish relationship between newly created span and the
106+
// other span. The other span could be related as a parent or linked or any other
107+
// future relationship type.
89108
Reference struct {
90109
core.SpanContext
91110
RelationshipType
@@ -94,39 +113,49 @@ type (
94113
RelationshipType int
95114
)
96115

116+
var (
117+
// The process global tracer could have process-wide resource
118+
// tags applied directly, or we can have a SetGlobal tracer to
119+
// install a default tracer w/ resources.
120+
global atomic.Value
121+
122+
// TODO: create NOOP Tracer and register it instead of creating empty tracer here.
123+
nt = &noopTracer{}
124+
)
125+
97126
const (
98127
ChildOfRelationship RelationshipType = iota
99128
FollowsFromRelationship
100129
)
101130

131+
// GlobalTracer return tracer registered with global registry.
132+
// If no tracer is registered then an instance of noop Tracer is returned.
102133
func GlobalTracer() Tracer {
103134
if t := global.Load(); t != nil {
104135
return t.(Tracer)
105136
}
106-
return empty
137+
return nt
107138
}
108139

140+
// SetGlobalTracer sets provided tracer as a global tracer.
109141
func SetGlobalTracer(t Tracer) {
110142
global.Store(t)
111143
}
112144

145+
// Start starts a new span using registered global tracer.
113146
func Start(ctx context.Context, name string, opts ...SpanOption) (context.Context, Span) {
114147
return GlobalTracer().Start(ctx, name, opts...)
115148
}
116149

150+
// Active returns current span from the context.
117151
func Active(ctx context.Context) Span {
118-
span, _ := scope.Active(ctx).(*span)
152+
span, _ := scope.Active(ctx).(Span)
119153
return span
120154
}
121155

122-
func WithSpan(ctx context.Context, name string, body func(context.Context) error) error {
123-
return GlobalTracer().WithSpan(ctx, name, body)
124-
}
125-
126-
func SetError(ctx context.Context, v bool) {
127-
Active(ctx).SetError(v)
128-
}
129-
156+
// Inject is convenient function to inject current span context using injector.
157+
// Injector is expected to serialize span context and inject it in to a carrier.
158+
// An example of a carrier is http request.
130159
func Inject(ctx context.Context, injector Injector) {
131160
span := Active(ctx)
132161
if span == nil {
@@ -136,36 +165,46 @@ func Inject(ctx context.Context, injector Injector) {
136165
span.Tracer().Inject(ctx, span, injector)
137166
}
138167

168+
// WithStartTime sets the start time of the span to provided time t, when it is started.
169+
// In absensce of this option, wall clock time is used as start time.
170+
// This option is typically used when starting of the span is delayed.
139171
func WithStartTime(t time.Time) SpanOption {
140172
return func(o *SpanOptions) {
141-
o.startTime = t
173+
o.StartTime = t
142174
}
143175
}
144176

177+
// WithAttributes sets attributes to span. These attributes provides additional
178+
// data about the span.
145179
func WithAttributes(attrs ...core.KeyValue) SpanOption {
146180
return func(o *SpanOptions) {
147-
o.attributes = attrs
181+
o.Attributes = attrs
148182
}
149183
}
150184

185+
// WithRecordEvents enables recording of the events while the span is active.
186+
// In the absence of this option, RecordEvent is set to false, disabling any recording of
187+
// the events.
151188
func WithRecordEvents() SpanOption {
152189
return func(o *SpanOptions) {
153-
o.recordEvent = true
190+
o.RecordEvent = true
154191
}
155192
}
156193

194+
// ChildOf. TODO: do we need this?.
157195
func ChildOf(sc core.SpanContext) SpanOption {
158196
return func(o *SpanOptions) {
159-
o.reference = Reference{
197+
o.Reference = Reference{
160198
SpanContext: sc,
161199
RelationshipType: ChildOfRelationship,
162200
}
163201
}
164202
}
165203

204+
// FollowsFrom. TODO: do we need this?.
166205
func FollowsFrom(sc core.SpanContext) SpanOption {
167206
return func(o *SpanOptions) {
168-
o.reference = Reference{
207+
o.Reference = Reference{
169208
SpanContext: sc,
170209
RelationshipType: FollowsFromRelationship,
171210
}

api/trace/noop_span.go

+93
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
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 trace
16+
17+
import (
18+
"context"
19+
20+
"google.golang.org/grpc/codes"
21+
22+
"github.com/open-telemetry/opentelemetry-go/api/core"
23+
"github.com/open-telemetry/opentelemetry-go/api/event"
24+
)
25+
26+
var _ Span = (*noopSpan)(nil)
27+
28+
// SpancContext returns an invalid span context.
29+
func (sp *noopSpan) SpanContext() core.SpanContext {
30+
return core.INVALID_SPAN_CONTEXT
31+
}
32+
33+
// IsRecordingEvents always returns false for noopSpan.
34+
func (sp *noopSpan) IsRecordingEvents() bool {
35+
return false
36+
}
37+
38+
// SetStatus does nothing.
39+
func (sp *noopSpan) SetStatus(status codes.Code) {
40+
return
41+
}
42+
43+
// ScopeID returns and empty ScopeID.
44+
func (sp *noopSpan) ScopeID() core.ScopeID {
45+
return core.ScopeID{}
46+
}
47+
48+
// SetError does nothing.
49+
func (sp *noopSpan) SetError(v bool) {
50+
return
51+
}
52+
53+
// SetAttribute does nothing.
54+
func (sp *noopSpan) SetAttribute(attribute core.KeyValue) {
55+
return
56+
}
57+
58+
// SetAttributes does nothing.
59+
func (sp *noopSpan) SetAttributes(attributes ...core.KeyValue) {
60+
return
61+
}
62+
63+
// ModifyAttribute does nothing.
64+
func (sp *noopSpan) ModifyAttribute(mutator core.Mutator) {
65+
return
66+
}
67+
68+
// ModifyAttributes does nothing.
69+
func (sp *noopSpan) ModifyAttributes(mutators ...core.Mutator) {
70+
return
71+
}
72+
73+
// Finish does nothing.
74+
func (sp *noopSpan) Finish() {
75+
return
76+
}
77+
78+
// Tracer returns noop implementation of Tracer.
79+
func (sp *noopSpan) Tracer() Tracer {
80+
return t
81+
}
82+
83+
// AddEvent does nothing.
84+
func (sp *noopSpan) AddEvent(ctx context.Context, event event.Event) {
85+
}
86+
87+
// Record does nothing.
88+
func (sp *noopSpan) Record(ctx context.Context, m ...core.Measurement) {
89+
}
90+
91+
// RecordSingle does nothing.
92+
func (sp *noopSpan) RecordSingle(ctx context.Context, m core.Measurement) {
93+
}

0 commit comments

Comments
 (0)