...

Source file src/go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc/internal/parse.go

Documentation: go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc/internal

     1  // Copyright The OpenTelemetry Authors
     2  // SPDX-License-Identifier: Apache-2.0
     3  
     4  package internal // import "go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc/internal"
     5  
     6  import (
     7  	"strings"
     8  
     9  	"go.opentelemetry.io/otel/attribute"
    10  	semconv "go.opentelemetry.io/otel/semconv/v1.17.0"
    11  )
    12  
    13  // ParseFullMethod returns a span name following the OpenTelemetry semantic
    14  // conventions as well as all applicable span attribute.KeyValue attributes based
    15  // on a gRPC's FullMethod.
    16  //
    17  // Parsing is consistent with grpc-go implementation:
    18  // https://github.com/grpc/grpc-go/blob/v1.57.0/internal/grpcutil/method.go#L26-L39
    19  func ParseFullMethod(fullMethod string) (string, []attribute.KeyValue) {
    20  	if !strings.HasPrefix(fullMethod, "/") {
    21  		// Invalid format, does not follow `/package.service/method`.
    22  		return fullMethod, nil
    23  	}
    24  	name := fullMethod[1:]
    25  	pos := strings.LastIndex(name, "/")
    26  	if pos < 0 {
    27  		// Invalid format, does not follow `/package.service/method`.
    28  		return name, nil
    29  	}
    30  	service, method := name[:pos], name[pos+1:]
    31  
    32  	var attrs []attribute.KeyValue
    33  	if service != "" {
    34  		attrs = append(attrs, semconv.RPCService(service))
    35  	}
    36  	if method != "" {
    37  		attrs = append(attrs, semconv.RPCMethod(method))
    38  	}
    39  	return name, attrs
    40  }
    41  

View as plain text