doc.go (1964B)
1 // Copyright The 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 /* 16 Package trace provides an implementation of the tracing part of the 17 OpenTelemetry API. 18 19 To participate in distributed traces a Span needs to be created for the 20 operation being performed as part of a traced workflow. In its simplest form: 21 22 var tracer trace.Tracer 23 24 func init() { 25 tracer = otel.Tracer("instrumentation/package/name") 26 } 27 28 func operation(ctx context.Context) { 29 var span trace.Span 30 ctx, span = tracer.Start(ctx, "operation") 31 defer span.End() 32 // ... 33 } 34 35 A Tracer is unique to the instrumentation and is used to create Spans. 36 Instrumentation should be designed to accept a TracerProvider from which it 37 can create its own unique Tracer. Alternatively, the registered global 38 TracerProvider from the go.opentelemetry.io/otel package can be used as 39 a default. 40 41 const ( 42 name = "instrumentation/package/name" 43 version = "0.1.0" 44 ) 45 46 type Instrumentation struct { 47 tracer trace.Tracer 48 } 49 50 func NewInstrumentation(tp trace.TracerProvider) *Instrumentation { 51 if tp == nil { 52 tp = otel.TracerProvider() 53 } 54 return &Instrumentation{ 55 tracer: tp.Tracer(name, trace.WithInstrumentationVersion(version)), 56 } 57 } 58 59 func operation(ctx context.Context, inst *Instrumentation) { 60 var span trace.Span 61 ctx, span = inst.tracer.Start(ctx, "operation") 62 defer span.End() 63 // ... 64 } 65 */ 66 package trace // import "go.opentelemetry.io/otel/trace"