otelgrpc – go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc Index | Examples | Files | Directories

package otelgrpc

import "go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc"

Package otelgrpc is the instrumentation library for google.golang.org/grpc.

Use NewClientHandler with grpc.WithStatsHandler to instrument a gRPC client.

Use NewServerHandler with grpc.StatsHandler to instrument a gRPC server.

Index

Examples

Constants

const ScopeName = "go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc"

ScopeName is the instrumentation scope name.

const Version = "0.68.0"

Version is the current release version of the gRPC instrumentation.

Functions

func NewClientHandler

func NewClientHandler(opts ...Option) stats.Handler

NewClientHandler creates a stats.Handler for a gRPC client.

Example

Code:play 

package main

import (
	"google.golang.org/grpc"

	"go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc"
)

func main() {
	_, _ = grpc.NewClient("localhost", grpc.WithStatsHandler(otelgrpc.NewClientHandler()))
}
Example (WithMetricAttributesFn_baggage)

ExampleNewClientHandler_withMetricAttributesFn_baggage demonstrates how to add a dynamic client-side attribute using W3C baggage to the auto-instrumented metrics.

Code:play 

package main

import (
	"context"

	"go.opentelemetry.io/otel/attribute"
	"go.opentelemetry.io/otel/baggage"
	"google.golang.org/grpc"

	"go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc"
)

func main() {
	// Baggage must be set in the context before making the call, e.g.,
	// ...
	// member, err := baggage.NewMember("traffic.type", "internal")
	// ...
	// bag, err := baggage.New(member)
	// ...
	// ctx := baggage.ContextWithBaggage(ctx, bag)
	// ...

	_, _ = grpc.NewClient("localhost", grpc.WithStatsHandler(otelgrpc.NewClientHandler(
		otelgrpc.WithMetricAttributesFn(func(ctx context.Context) []attribute.KeyValue {
			bag := baggage.FromContext(ctx)
			if trafficType := bag.Member("traffic.type"); trafficType.Value() != "" {
				return []attribute.KeyValue{
					attribute.String("traffic.type", trafficType.Value()),
				}
			}

			return nil
		}),
	)))
}
Example (WithMetricAttributesFn_interceptor)

ExampleNewClientHandler_withMetricAttributesFn_interceptor demonstrates how to add a dynamic client-side attribute using gRPC interceptors to the auto-instrumented metrics.

Code:play 

package main

import (
	"context"

	"go.opentelemetry.io/otel/attribute"
	"google.golang.org/grpc"

	"go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc"
)

func main() {
	// should be centralized, example only
	type retryAttemptKey struct{}

	// a gRPC client interceptor must populate that key with the actual retry attempt, e.g.,
	// ...
	// interceptor := func(ctx context.Context, method string, req, reply any,
	//     cc *grpc.ClientConn, invoker grpc.UnaryInvoker, opts ...grpc.CallOption) error {
	//     ...
	//     ctx = context.WithValue(ctx, retryAttemptKey{}, attempt)
	//     return invoker(ctx, method, req, reply, cc, opts...)
	// }
	// ...

	_, _ = grpc.NewClient("localhost", grpc.WithStatsHandler(otelgrpc.NewClientHandler(
		otelgrpc.WithMetricAttributesFn(func(ctx context.Context) []attribute.KeyValue {
			if attempt, ok := ctx.Value(retryAttemptKey{}).(int); ok {
				return []attribute.KeyValue{
					// Caution: example only.
					// This must be a controlled, bounded and very limited set of numbers
					// so that you don't end up with very high cardinality.
					attribute.Int("retry.attempt", attempt),
				}
			}

			return nil
		}),
	)))
}

func NewServerHandler

func NewServerHandler(opts ...Option) stats.Handler

NewServerHandler creates a stats.Handler for a gRPC server.

Example

Code:play 

package main

import (
	"google.golang.org/grpc"

	"go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc"
)

func main() {
	_ = grpc.NewServer(grpc.StatsHandler(otelgrpc.NewServerHandler()))
}
Example (WithMetricAttributesFn_baggage)

ExampleNewServerHandler_withMetricAttributesFn_baggage demonstrates how to add a dynamic server-side attribute using W3C baggage to the auto-instrumented metrics.

Code:play 

package main

import (
	"context"

	"go.opentelemetry.io/otel/attribute"
	"go.opentelemetry.io/otel/baggage"
	"go.opentelemetry.io/otel/propagation"
	"google.golang.org/grpc"

	"go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc"
)

func main() {
	// The client must set baggage in context beforehand and have baggage propagators configured to
	// inject it into the headers (see https://pkg.go.dev/go.opentelemetry.io/otel/propagation#section-documentation), e.g.,
	//
	// conn, err := grpc.NewClient(
	// ...
	// grpc.WithStatsHandler(otelgrpc.NewClientHandler(
	//	    otelgrpc.WithPropagators(propagation.NewCompositeTextMapPropagator(
	//			  propagation.Baggage{},
	//		)),
	//	)),
	//)
	// ...
	// member, err := baggage.NewMember("tenant.tier", "premium")
	// ...
	// bag, err := baggage.New(member)
	// ...
	// ctx := baggage.ContextWithBaggage(ctx, bag)
	// ...

	_ = grpc.NewServer(grpc.StatsHandler(otelgrpc.NewServerHandler(
		// Propagators are required to extract baggage from incoming request headers.
		otelgrpc.WithPropagators(propagation.NewCompositeTextMapPropagator(
			propagation.TraceContext{},
			propagation.Baggage{},
		)),
		otelgrpc.WithMetricAttributesFn(func(ctx context.Context) []attribute.KeyValue {
			bag := baggage.FromContext(ctx)
			if tier := bag.Member("tenant.tier"); tier.Value() != "" {
				return []attribute.KeyValue{
					attribute.String("tenant.tier", tier.Value()),
				}
			}

			return nil
		}),
	)))
}
Example (WithMetricAttributesFn_metadata)

ExampleNewServerHandler_withMetricAttributesFn_metadata demonstrates how to add a dynamic server-side attribute using gRPC metadata to the auto-instrumented metrics.

Code:play 

package main

import (
	"context"

	"go.opentelemetry.io/otel/attribute"
	"google.golang.org/grpc"
	"google.golang.org/grpc/metadata"

	"go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc"
)

func main() {
	// The client must set metadata in the outgoing context beforehand, e.g.,
	// ...
	// ctx := metadata.NewOutgoingContext(context.Background(), metadata.Pairs("origin", "some-origin"))
	// ...
	_ = grpc.NewServer(grpc.StatsHandler(otelgrpc.NewServerHandler(
		otelgrpc.WithMetricAttributesFn(func(ctx context.Context) []attribute.KeyValue {
			md, ok := metadata.FromIncomingContext(ctx)
			if ok {
				if origins := md.Get("origin"); len(origins) > 0 && origins[0] != "" {
					return []attribute.KeyValue{attribute.String("origin", origins[0])}
				}
			}

			// Some use-cases might require to fallback to a default.
			return []attribute.KeyValue{attribute.String("origin", "unknown")}
		}),
	)))
}

Types

type Event

type Event int

Event type that can be recorded, see WithMessageEvents.

const (
	ReceivedEvents Event = iota
	SentEvents
)

Different types of events that can be recorded, see WithMessageEvents.

type Filter

type Filter func(*stats.RPCTagInfo) bool

Filter is a predicate used to determine whether a given request in should be instrumented by the attached RPC tag info. A Filter must return true if the request should be instrumented.

type InterceptorFilter

type InterceptorFilter func(*InterceptorInfo) bool

InterceptorFilter is a predicate used to determine whether a given request in interceptor info should be instrumented. A InterceptorFilter must return true if the request should be traced.

Deprecated: Use stats handlers instead.

type InterceptorInfo

type InterceptorInfo struct {
	// Method is method name registered to UnaryClient and StreamClient
	Method string
	// UnaryServerInfo is the metadata for UnaryServer
	UnaryServerInfo *grpc.UnaryServerInfo
	// StreamServerInfo if the metadata for StreamServer
	StreamServerInfo *grpc.StreamServerInfo
	// Type is the type for interceptor
	Type InterceptorType
}

InterceptorInfo is the union of some arguments to four types of gRPC interceptors.

type InterceptorType

type InterceptorType uint8

InterceptorType is the flag to define which gRPC interceptor the InterceptorInfo object is.

const (
	// UndefinedInterceptor is the type for the interceptor information that is not
	// well initialized or categorized to other types.
	UndefinedInterceptor InterceptorType = iota
	// UnaryClient is the type for grpc.UnaryClient interceptor.
	UnaryClient
	// StreamClient is the type for grpc.StreamClient interceptor.
	StreamClient
	// UnaryServer is the type for grpc.UnaryServer interceptor.
	UnaryServer
	// StreamServer is the type for grpc.StreamServer interceptor.
	StreamServer
)

type Option

type Option interface {
	// contains filtered or unexported methods
}

Option applies an option value for a config.

func WithFilter

func WithFilter(f Filter) Option

WithFilter returns an Option to use the request filter.

func WithInterceptorFilter

func WithInterceptorFilter(f InterceptorFilter) Option

WithInterceptorFilter returns an Option to use the request filter.

Deprecated: Use stats handlers instead.

func WithMessageEvents

func WithMessageEvents(events ...Event) Option

WithMessageEvents configures the Handler to record the specified events (span.AddEvent) on spans. By default only summary attributes are added at the end of the request.

Valid events are:

func WithMeterProvider

func WithMeterProvider(mp metric.MeterProvider) Option

WithMeterProvider returns an Option to use the MeterProvider when creating a Meter. If this option is not provide the global MeterProvider will be used.

func WithMetricAttributes

func WithMetricAttributes(a ...attribute.KeyValue) Option

WithMetricAttributes returns an Option to add custom attributes to the metrics.

func WithMetricAttributesFn

func WithMetricAttributesFn(fn func(ctx context.Context) []attribute.KeyValue) Option

WithMetricAttributesFn returns an Option to add dynamic custom attributes to the handler's metrics. The function is called once per RPC and the returned attributes are applied to all metrics recorded by this handler.

The context parameter is the standard gRPC request context and provides access to request-scoped data.

func WithPropagators

func WithPropagators(p propagation.TextMapPropagator) Option

WithPropagators returns an Option to use the Propagators when extracting and injecting trace context from requests.

func WithPublicEndpoint

func WithPublicEndpoint() Option

WithPublicEndpoint configures the Handler to link the span with an incoming span context. If this option is not provided, then the association is a child association instead of a link.

func WithPublicEndpointFn

func WithPublicEndpointFn(fn func(context.Context, *stats.RPCTagInfo) bool) Option

WithPublicEndpointFn runs with every request, and allows conditionally configuring the Handler to link the span with an incoming span context. If this option is not provided or returns false, then the association is a child association instead of a link. Note: WithPublicEndpoint takes precedence over WithPublicEndpointFn.

func WithSpanAttributes

func WithSpanAttributes(a ...attribute.KeyValue) Option

WithSpanAttributes returns an Option to add custom attributes to the spans.

func WithSpanKind

func WithSpanKind(sk trace.SpanKind) Option

WithSpanKind returns an Option to set the span kind for spans created by the handler.

By default, NewServerHandler creates spans with trace.SpanKindServer and NewClientHandler creates spans with trace.SpanKindClient.

func WithSpanOptions

func WithSpanOptions(opts ...trace.SpanStartOption) Option

WithSpanOptions configures an additional set of trace.SpanOptions, which are applied to each new span.

Deprecated: It is only used by the deprecated interceptor, and is unused by NewClientHandler and NewServerHandler.

func WithTracerProvider

func WithTracerProvider(tp trace.TracerProvider) Option

WithTracerProvider returns an Option to use the TracerProvider when creating a Tracer.

Source Files

config.go doc.go interceptor.go interceptorinfo.go metadata_supplier.go stats_handler.go version.go

Directories

PathSynopsis
filtersPackage filters provides a set of filters useful with the otelgrpc.WithFilter option to control which inbound requests are instrumented.
internalPackage internal provides internal functionality for the otelgrpc package.
Version
v0.68.0 (latest)
Published
Apr 7, 2026
Platform
darwin/amd64
Imports
21 packages
Last checked
now

Tools for package owners.