|
| 1 | +// Copyright 2025 Google LLC |
| 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 storage |
| 16 | + |
| 17 | +import ( |
| 18 | + "context" |
| 19 | + "fmt" |
| 20 | + "os" |
| 21 | + |
| 22 | + internalTrace "cloud.google.com/go/internal/trace" |
| 23 | + "cloud.google.com/go/storage/internal" |
| 24 | + "go.opentelemetry.io/otel" |
| 25 | + "go.opentelemetry.io/otel/attribute" |
| 26 | + otelcodes "go.opentelemetry.io/otel/codes" |
| 27 | + "go.opentelemetry.io/otel/trace" |
| 28 | +) |
| 29 | + |
| 30 | +const ( |
| 31 | + storageOtelTracingDevVar = "GO_STORAGE_DEV_OTEL_TRACING" |
| 32 | + defaultTracerName = "cloud.google.com/go/storage" |
| 33 | + gcpClientRepo = "googleapis/google-cloud-go" |
| 34 | + gcpClientArtifact = "cloud.google.com/go/storage" |
| 35 | +) |
| 36 | + |
| 37 | +// isOTelTracingDevEnabled checks the development flag until experimental feature is launched. |
| 38 | +// TODO: Remove development flag upon experimental launch. |
| 39 | +func isOTelTracingDevEnabled() bool { |
| 40 | + return os.Getenv(storageOtelTracingDevVar) == "true" |
| 41 | +} |
| 42 | + |
| 43 | +func tracer() trace.Tracer { |
| 44 | + return otel.Tracer(defaultTracerName, trace.WithInstrumentationVersion(internal.Version)) |
| 45 | +} |
| 46 | + |
| 47 | +// startSpan creates a span and a context.Context containing the newly-created span. |
| 48 | +// If the context.Context provided in `ctx` contains a span then the newly-created |
| 49 | +// span will be a child of that span, otherwise it will be a root span. |
| 50 | +func startSpan(ctx context.Context, name string, opts ...trace.SpanStartOption) (context.Context, trace.Span) { |
| 51 | + name = appendPackageName(name) |
| 52 | + // TODO: Remove internalTrace upon experimental launch. |
| 53 | + if !isOTelTracingDevEnabled() { |
| 54 | + ctx = internalTrace.StartSpan(ctx, name) |
| 55 | + return ctx, nil |
| 56 | + } |
| 57 | + opts = append(opts, getCommonTraceOptions()...) |
| 58 | + ctx, span := tracer().Start(ctx, name, opts...) |
| 59 | + return ctx, span |
| 60 | +} |
| 61 | + |
| 62 | +// endSpan retrieves the current span from ctx and completes the span. |
| 63 | +// If an error occurs, the error is recorded as an exception span event for this span, |
| 64 | +// and the span status is set in the form of a code and a description. |
| 65 | +func endSpan(ctx context.Context, err error) { |
| 66 | + // TODO: Remove internalTrace upon experimental launch. |
| 67 | + if !isOTelTracingDevEnabled() { |
| 68 | + internalTrace.EndSpan(ctx, err) |
| 69 | + } else { |
| 70 | + span := trace.SpanFromContext(ctx) |
| 71 | + if err != nil { |
| 72 | + span.SetStatus(otelcodes.Error, err.Error()) |
| 73 | + span.RecordError(err) |
| 74 | + } |
| 75 | + span.End() |
| 76 | + } |
| 77 | +} |
| 78 | + |
| 79 | +// getCommonTraceOptions includes the common attributes used for Cloud Trace adoption tracking. |
| 80 | +func getCommonTraceOptions() []trace.SpanStartOption { |
| 81 | + opts := []trace.SpanStartOption{ |
| 82 | + trace.WithAttributes( |
| 83 | + attribute.String("gcp.client.version", internal.Version), |
| 84 | + attribute.String("gcp.client.repo", gcpClientRepo), |
| 85 | + attribute.String("gcp.client.artifact", gcpClientArtifact), |
| 86 | + ), |
| 87 | + } |
| 88 | + return opts |
| 89 | +} |
| 90 | + |
| 91 | +func appendPackageName(spanName string) string { |
| 92 | + return fmt.Sprintf("%s.%s", gcpClientArtifact, spanName) |
| 93 | +} |
0 commit comments