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 ¶
- Constants
- func NewClientHandler(opts ...Option) stats.Handler
- func NewServerHandler(opts ...Option) stats.Handler
- type Event
- type Filter
- type InterceptorFilter
- type InterceptorInfo
- type InterceptorType
- type Option
- func WithFilter(f Filter) Option
- func WithInterceptorFilter(f InterceptorFilter) Option
- func WithMessageEvents(events ...Event) Option
- func WithMeterProvider(mp metric.MeterProvider) Option
- func WithMetricAttributes(a ...attribute.KeyValue) Option
- func WithMetricAttributesFn(fn func(ctx context.Context) []attribute.KeyValue) Option
- func WithPropagators(p propagation.TextMapPropagator) Option
- func WithPublicEndpoint() Option
- func WithPublicEndpointFn(fn func(context.Context, *stats.RPCTagInfo) bool) Option
- func WithSpanAttributes(a ...attribute.KeyValue) Option
- func WithSpanKind(sk trace.SpanKind) Option
- func WithSpanOptions(opts ...trace.SpanStartOption) Option
- func WithTracerProvider(tp trace.TracerProvider) Option
Examples ¶
- NewClientHandler
- NewClientHandler (WithMetricAttributesFn_baggage)
- NewClientHandler (WithMetricAttributesFn_interceptor)
- NewServerHandler
- NewServerHandler (WithMetricAttributesFn_baggage)
- NewServerHandler (WithMetricAttributesFn_metadata)
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 ¶
NewClientHandler creates a stats.Handler for a gRPC client.
Code:play
ExampleNewClientHandler_withMetricAttributesFn_baggage demonstrates how to add a dynamic
client-side attribute using W3C baggage to the auto-instrumented metrics.
Code:play
ExampleNewClientHandler_withMetricAttributesFn_interceptor demonstrates how to add a dynamic
client-side attribute using gRPC interceptors to the auto-instrumented metrics.
Code:play
Example¶
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)¶
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)¶
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 ¶
NewServerHandler creates a stats.Handler for a gRPC server.
Code:play
ExampleNewServerHandler_withMetricAttributesFn_baggage demonstrates how to add a dynamic
server-side attribute using W3C baggage to the auto-instrumented metrics.
Code:play
ExampleNewServerHandler_withMetricAttributesFn_metadata demonstrates how to add a dynamic
server-side attribute using gRPC metadata to the auto-instrumented metrics.
Code:play
Example¶
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)¶
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)¶
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.
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 ¶
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 ¶
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:
- ReceivedEvents: Record the number of bytes read after every gRPC read operation.
- SentEvents: Record the number of bytes written after every gRPC write operation.
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 ¶
WithMetricAttributes returns an Option to add custom attributes to the metrics.
func WithMetricAttributesFn ¶
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 ¶
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 ¶
WithSpanAttributes returns an Option to add custom attributes to the spans.
func WithSpanKind ¶
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 ¶
| Path | Synopsis |
|---|---|
| filters | Package filters provides a set of filters useful with the otelgrpc.WithFilter option to control which inbound requests are instrumented. |
| internal | Package internal provides internal functionality for the otelgrpc package. |
- Version
- v0.68.0 (latest)
- Published
- Apr 7, 2026
- Platform
- linux/amd64
- Imports
- 21 packages
- Last checked
- now –
Tools for package owners.