| 1 |
// Copyright 2019-2025 The Go Cloud Development Kit 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 |
// https://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 otel |
| 16 |
|
| 17 |
import ( |
| 18 |
"context" |
| 19 |
"fmt" |
| 20 |
"go.opentelemetry.io/otel" |
| 21 |
"go.opentelemetry.io/otel/attribute" |
| 22 |
"go.opentelemetry.io/otel/codes" |
| 23 |
"go.opentelemetry.io/otel/metric" |
| 24 |
"go.opentelemetry.io/otel/trace" |
| 25 |
"gocloud.dev/gcerrors" |
| 26 |
"reflect" |
| 27 |
"time" |
| 28 |
) |
| 29 |
|
| 30 |
// Common attribute keys used across the Go CDK. |
| 31 |
var ( |
| 32 |
methodKey = attribute.Key("gocdk_method") |
| 33 |
packageKey = attribute.Key("gocdk_package") |
| 34 |
providerKey = attribute.Key("gocdk_provider") |
| 35 |
statusKey = attribute.Key("gocdk_status") |
| 36 |
errorKey = attribute.Key("gocdk_error") |
| 37 |
) |
| 38 |
|
| 39 |
const ( |
| 40 |
startTimeContextKey = "spanStartTimeCtxKey" |
| 41 |
methodNameContextKey = "methodNameCtxKey" |
| 42 |
) |
| 43 |
|
| 44 |
// Tracer provides OpenTelemetry tracing for Go CDK packages. |
| 45 |
type Tracer struct { |
| 46 |
pkg string |
| 47 |
provider string |
| 48 |
tracer trace.Tracer |
| 49 |
latencyMeasure metric.Float64Histogram |
| 50 |
} |
| 51 |
|
| 52 |
// ProviderName returns the name of the provider associated with the driver value. |
| 53 |
// It is intended to be used as the provider argument to NewTracer. |
| 54 |
// It actually returns the package path of the driver's type. |
| 55 |
func ProviderName(driver any) string { |
| 56 |
// Return the last component of the package path. |
| 57 |
if driver == nil { |
| 58 |
return "" |
| 59 |
} |
| 60 |
t := reflect.TypeOf(driver) |
| 61 |
if t.Kind() == reflect.Pointer { |
| 62 |
t = t.Elem() |
| 63 |
} |
| 64 |
return t.PkgPath() |
| 65 |
} |
| 66 |
|
| 67 |
// NewTracer creates a new Tracer for a package and provider. |
| 68 |
func NewTracer(pkg string, provider string) *Tracer { |
| 69 |
|
| 70 |
attrs := []attribute.KeyValue{ |
| 71 |
packageKey.String(pkg), |
| 72 |
providerKey.String(provider), |
| 73 |
} |
| 74 |
|
| 75 |
tracer := otel.Tracer(pkg, trace.WithInstrumentationAttributes(attrs...)) |
| 76 |
|
| 77 |
return &Tracer{ |
| 78 |
pkg: pkg, |
| 79 |
provider: provider, |
| 80 |
tracer: tracer, |
| 81 |
latencyMeasure: LatencyMeasure(pkg, provider), |
| 82 |
} |
| 83 |
} |
| 84 |
|
| 85 |
// Start creates and starts a new span and returns the updated context and span. |
| 86 |
func (t *Tracer) Start(ctx context.Context, methodName string) (context.Context, trace.Span) { |
| 87 |
fullName := t.pkg + "." + methodName |
| 88 |
|
| 89 |
sCtx, span := t.tracer.Start(ctx, fullName, trace.WithAttributes(methodKey.String(methodName))) |
| 90 |
sCtx = context.WithValue(sCtx, startTimeContextKey, time.Now()) |
| 91 |
return context.WithValue(sCtx, methodNameContextKey, fullName), span |
| 92 |
} |
| 93 |
|
| 94 |
// End completes a span with error information if applicable. |
| 95 |
func (t *Tracer) End(ctx context.Context, span trace.Span, err error) { |
| 96 |
startTime := ctx.Value(startTimeContextKey).(time.Time) |
| 97 |
elapsed := time.Since(startTime) |
| 98 |
|
| 99 |
code := gcerrors.OK |
| 100 |
|
| 101 |
if err != nil { |
| 102 |
code = gcerrors.Code(err) |
| 103 |
span.SetAttributes( |
| 104 |
errorKey.String(err.Error()), |
| 105 |
statusKey.String(fmt.Sprint(code)), |
| 106 |
) |
| 107 |
span.SetStatus(codes.Error, err.Error()) |
| 108 |
span.RecordError(err) |
| 109 |
} else { |
| 110 |
span.SetStatus(codes.Ok, "") |
| 111 |
} |
| 112 |
|
| 113 |
span.End() |
| 114 |
|
| 115 |
methodName := ctx.Value(methodNameContextKey).(string) |
| 116 |
|
| 117 |
t.latencyMeasure.Record(ctx, |
| 118 |
float64(elapsed.Milliseconds()), |
| 119 |
|
| 120 |
metric.WithAttributes( |
| 121 |
statusKey.String(fmt.Sprint(code)), |
| 122 |
methodKey.String(methodName)), |
| 123 |
) |
| 124 |
} |