1// Copyright The OpenTelemetry Authors
2// SPDX-License-Identifier: Apache-2.0
3
4/*
5Package trace provides an implementation of the tracing part of the
6OpenTelemetry API.
7
8To participate in distributed traces a Span needs to be created for the
9operation being performed as part of a traced workflow. In its simplest form:
10
11 var tracer trace.Tracer
12
13 func init() {
14 tracer = otel.Tracer("instrumentation/package/name")
15 }
16
17 func operation(ctx context.Context) {
18 var span trace.Span
19 ctx, span = tracer.Start(ctx, "operation")
20 defer span.End()
21 // ...
22 }
23
24A Tracer is unique to the instrumentation and is used to create Spans.
25Instrumentation should be designed to accept a TracerProvider from which it
26can create its own unique Tracer. Alternatively, the registered global
27TracerProvider from the go.opentelemetry.io/otel package can be used as
28a default.
29
30 const (
31 name = "instrumentation/package/name"
32 version = "0.1.0"
33 )
34
35 type Instrumentation struct {
36 tracer trace.Tracer
37 }
38
39 func NewInstrumentation(tp trace.TracerProvider) *Instrumentation {
40 if tp == nil {
41 tp = otel.TracerProvider()
42 }
43 return &Instrumentation{
44 tracer: tp.Tracer(name, trace.WithInstrumentationVersion(version)),
45 }
46 }
47
48 func operation(ctx context.Context, inst *Instrumentation) {
49 var span trace.Span
50 ctx, span = inst.tracer.Start(ctx, "operation")
51 defer span.End()
52 // ...
53 }
54
55# API Implementations
56
57This package does not conform to the standard Go versioning policy; all of its
58interfaces may have methods added to them without a package major version bump.
59This non-standard API evolution could surprise an uninformed implementation
60author. They could unknowingly build their implementation in a way that would
61result in a runtime panic for their users that update to the new API.
62
63The API is designed to help inform an instrumentation author about this
64non-standard API evolution. It requires them to choose a default behavior for
65unimplemented interface methods. There are three behavior choices they can
66make:
67
68 - Compilation failure
69 - Panic
70 - Default to another implementation
71
72All interfaces in this API embed a corresponding interface from
73[go.opentelemetry.io/otel/trace/embedded]. If an author wants the default
74behavior of their implementations to be a compilation failure, signaling to
75their users they need to update to the latest version of that implementation,
76they need to embed the corresponding interface from
77[go.opentelemetry.io/otel/trace/embedded] in their implementation. For
78example,
79
80 import "go.opentelemetry.io/otel/trace/embedded"
81
82 type TracerProvider struct {
83 embedded.TracerProvider
84 // ...
85 }
86
87If an author wants the default behavior of their implementations to panic, they
88can embed the API interface directly.
89
90 import "go.opentelemetry.io/otel/trace"
91
92 type TracerProvider struct {
93 trace.TracerProvider
94 // ...
95 }
96
97This option is not recommended. It will lead to publishing packages that
98contain runtime panics when users update to newer versions of
99[go.opentelemetry.io/otel/trace], which may be done with a transitive
100dependency.
101
102Finally, an author can embed another implementation in theirs. The embedded
103implementation will be used for methods not defined by the author. For example,
104an author who wants to default to silently dropping the call can use
105[go.opentelemetry.io/otel/trace/noop]:
106
107 import "go.opentelemetry.io/otel/trace/noop"
108
109 type TracerProvider struct {
110 noop.TracerProvider
111 // ...
112 }
113
114It is strongly recommended that authors only embed
115[go.opentelemetry.io/otel/trace/noop] if they choose this default behavior.
116That implementation is the only one OpenTelemetry authors can guarantee will
117fully implement all the API interfaces when a user updates their API.
118*/
119package trace // import "go.opentelemetry.io/otel/trace"