| 1 | // Copyright The OpenTelemetry Authors |
| 2 | // SPDX-License-Identifier: Apache-2.0 |
| 3 | |
| 4 | // Package internal provides internal functionality for the otelgrpc package. |
| 5 | package internal // import "go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc/internal" |
| 6 | |
| 7 | import ( |
| 8 | "strings" |
| 9 | |
| 10 | "go.opentelemetry.io/otel/attribute" |
| 11 | oldsemconv "go.opentelemetry.io/otel/semconv/v1.37.0" //nolint:depguard // Use of v1.37.0 is required for backward compatibility stability opt-in. |
| 12 | semconv "go.opentelemetry.io/otel/semconv/v1.40.0" |
| 13 | ) |
| 14 | |
| 15 | // ParseFullMethod returns a span name following the OpenTelemetry semantic |
| 16 | // conventions as well as all applicable span attribute.KeyValue attributes based |
| 17 | // on a gRPC's FullMethod. |
| 18 | // |
| 19 | // Parsing is consistent with grpc-go implementation: |
| 20 | // https://github.com/grpc/grpc-go/blob/v1.57.0/internal/grpcutil/method.go#L26-L39 |
| 21 | func ParseFullMethod(fullMethod string) (string, []attribute.KeyValue) { |
| 22 | if !strings.HasPrefix(fullMethod, "/") { |
| 23 | // Invalid format, does not follow `/package.service/method`. |
| 24 | return fullMethod, nil |
| 25 | } |
| 26 | name := fullMethod[1:] |
| 27 | return name, []attribute.KeyValue{semconv.RPCMethod(name)} |
| 28 | } |
| 29 | |
| 30 | // ParseFullMethodOld returns a span name following the old OpenTelemetry semantic |
| 31 | // conventions as well as all applicable span attribute.KeyValue attributes based |
| 32 | // on a gRPC's FullMethod. |
| 33 | // Based on the implementation in: |
| 34 | // https://github.com/open-telemetry/opentelemetry-go-contrib/blob/072dcf8ad7e5e48b506e05720b29d8b078759606/instrumentation/google.golang.org/grpc/otelgrpc/internal/parse.go#L20 |
| 35 | func ParseFullMethodOld(fullMethod string) (string, []attribute.KeyValue) { |
| 36 | if !strings.HasPrefix(fullMethod, "/") { |
| 37 | return fullMethod, nil |
| 38 | } |
| 39 | name := fullMethod[1:] |
| 40 | parts := strings.Split(name, "/") |
| 41 | if len(parts) < 2 { |
| 42 | return name, []attribute.KeyValue{ |
| 43 | attribute.String("rpc.system", "grpc"), |
| 44 | } |
| 45 | } |
| 46 | service := parts[0] |
| 47 | method := parts[1] |
| 48 | return name, []attribute.KeyValue{ |
| 49 | oldsemconv.RPCSystemKey.String("grpc"), |
| 50 | oldsemconv.RPCServiceKey.String(service), |
| 51 | oldsemconv.RPCMethodKey.String(method), |
| 52 | } |
| 53 | } |