Debian Codegraph @

instrumentation/google.golang.org/grpc/otelgrpc/interceptor.go

raw symbols SCIP
1 // Copyright The OpenTelemetry Authors
2 // SPDX-License-Identifier: Apache-2.0
3
4 package otelgrpc // import "go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc"
5
6 // gRPC tracing middleware
7 // https://opentelemetry.io/docs/specs/semconv/rpc/
8 import (
9 "net"
10 "strconv"
11
12 "go.opentelemetry.io/otel/attribute"
13 "go.opentelemetry.io/otel/codes"
14 semconv "go.opentelemetry.io/otel/semconv/v1.40.0"
15 grpc_codes "google.golang.org/grpc/codes"
16 "google.golang.org/grpc/status"
17 )
18
19 // serverAddrAttrs returns the server address attributes for the hostport.
20 func serverAddrAttrs(hostport string) []attribute.KeyValue {
21 h, pStr, err := net.SplitHostPort(hostport)
22 if err != nil {
23 // The server.address attribute is required.
24 return []attribute.KeyValue{semconv.ServerAddress(hostport)}
25 }
26 p, err := strconv.Atoi(pStr)
27 if err != nil {
28 return []attribute.KeyValue{semconv.ServerAddress(h)}
29 }
30 return []attribute.KeyValue{
31 semconv.ServerAddress(h),
32 semconv.ServerPort(p),
33 }
34 }
35
36 // serverStatus returns a span status code and message for a given gRPC
37 // status code. It maps specific gRPC status codes to a corresponding span
38 // status code and message. This function is intended for use on the server
39 // side of a gRPC connection.
40 //
41 // If the gRPC status code is Unknown, DeadlineExceeded, Unimplemented,
42 // Internal, Unavailable, or DataLoss, it returns a span status code of Error
43 // and the message from the gRPC status. Otherwise, it returns a span status
44 // code of Unset and an empty message.
45 func serverStatus(grpcStatus *status.Status) (codes.Code, string) {
46 switch grpcStatus.Code() {
47 case grpc_codes.Unknown,
48 grpc_codes.DeadlineExceeded,
49 grpc_codes.Unimplemented,
50 grpc_codes.Internal,
51 grpc_codes.Unavailable,
52 grpc_codes.DataLoss:
53 return codes.Error, grpcStatus.Message()
54 default:
55 return codes.Unset, ""
56 }
57 }