-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Expand file tree
/
Copy pathtrace.go
More file actions
77 lines (68 loc) · 2.58 KB
/
trace.go
File metadata and controls
77 lines (68 loc) · 2.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
// Copyright 2025 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package spanner
import (
"context"
"errors"
"fmt"
"cloud.google.com/go/spanner/internal"
"go.opentelemetry.io/otel"
otelcodes "go.opentelemetry.io/otel/codes"
"go.opentelemetry.io/otel/trace"
"google.golang.org/api/googleapi"
"google.golang.org/grpc/status"
)
const (
defaultTracerName = "cloud.google.com/go/spanner"
gcpClientRepo = "googleapis/google-cloud-go"
gcpClientArtifact = "cloud.google.com/go/spanner"
// GcpResourceNamePrefix is the prefix for Spanner GCP resource names span attribute.
GcpResourceNamePrefix = "//spanner.googleapis.com/"
)
func tracer() trace.Tracer {
return otel.Tracer(defaultTracerName, trace.WithInstrumentationVersion(internal.Version))
}
// startSpan creates a span and a context.Context containing the newly-created span.
// If the context.Context provided in `ctx` contains a span then the newly-created
// span will be a child of that span, otherwise it will be a root span.
func startSpan(ctx context.Context, name string, opts ...trace.SpanStartOption) (context.Context, trace.Span) {
name = prependPackageName(name)
ctx, span := tracer().Start(ctx, name, opts...)
return ctx, span
}
// endSpan retrieves the current span from ctx and completes the span.
// If an error occurs, the error is recorded as an exception span event for this span,
// and the span status is set in the form of a code and a description.
func endSpan(ctx context.Context, err error) {
span := trace.SpanFromContext(ctx)
if err != nil {
span.SetStatus(otelcodes.Error, toOpenTelemetryStatusDescription(err))
span.RecordError(err)
}
span.End()
}
// toOpenTelemetryStatus converts an error to an equivalent OpenTelemetry status description.
func toOpenTelemetryStatusDescription(err error) string {
var err2 *googleapi.Error
if ok := errors.As(err, &err2); ok {
return err2.Message
} else if s, ok := status.FromError(err); ok {
return s.Message()
} else {
return err.Error()
}
}
func prependPackageName(spanName string) string {
return fmt.Sprintf("%s.%s", gcpClientArtifact, spanName)
}