sigs.k8s.io/cluster-api-provider-azure@v1.14.3/util/tele/tele.go (about) 1 /* 2 Copyright 2020 The Kubernetes Authors. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package tele 18 19 import ( 20 "context" 21 22 "go.opentelemetry.io/otel" 23 "go.opentelemetry.io/otel/attribute" 24 "go.opentelemetry.io/otel/trace" 25 ) 26 27 type tracer struct { 28 trace.Tracer 29 } 30 31 // Start creates a new context with a new Azure correlation ID, then 32 // creates a new trace.Span with that new context. This function then 33 // returns the new Context and Span. 34 func (t tracer) Start( 35 ctx context.Context, 36 op string, 37 opts ...trace.SpanStartOption, 38 ) (context.Context, trace.Span) { 39 ctx, corrID := ctxWithCorrID(ctx) 40 opts = append( 41 opts, 42 trace.WithSpanKind(trace.SpanKindClient), 43 trace.WithAttributes(attribute.String( 44 string(CorrIDKeyVal), 45 string(corrID), 46 )), 47 ) 48 return t.Tracer.Start(ctx, op, opts...) 49 } 50 51 // Tracer returns an OpenTelemetry Tracer implementation to be used 52 // to create spans. If you need access to the raw globally-registered 53 // tracer, use this function. 54 // 55 // Most people should not use this function directly, however. 56 // Instead, consider using StartSpanWithLogger, which uses 57 // this tracer to start a new span, configures logging, and 58 // more. 59 // 60 // Example usage: 61 // 62 // ctx, span := tele.Tracer().Start(ctx, "myFunction") 63 // defer span.End() 64 // // use the span and context here 65 func Tracer() trace.Tracer { 66 return tracer{ 67 Tracer: otel.Tracer("capz"), 68 } 69 }