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 is the instrumentation scope name.
	ScopeName = "go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc"
	// GRPCStatusCodeKey is convention for numeric status code of a gRPC request.
	GRPCStatusCodeKey = attribute.Key("rpc.grpc.status_code")
)
const (
	// Name of message transmitted or received.
	RPCNameKey = attribute.Key("name")

	// Type of message transmitted or received.
	RPCMessageTypeKey = attribute.Key("message.type")

	// Identifier of message transmitted or received.
	RPCMessageIDKey = attribute.Key("message.id")

	// The compressed size of the message transmitted or received in bytes.
	RPCMessageCompressedSizeKey = attribute.Key("message.compressed_size")

	// The uncompressed size of the message transmitted or received in
	// bytes.
	RPCMessageUncompressedSizeKey = attribute.Key("message.uncompressed_size")
)

Semantic conventions for attribute keys for gRPC.

Variables

var (
	// Semantic convention for gRPC as the remoting system.
	RPCSystemGRPC = semconv.RPCSystemGRPC

	// Semantic convention for a message named message.
	RPCNameMessage = RPCNameKey.String("message")

	// Semantic conventions for RPC message types.
	RPCMessageTypeSent     = RPCMessageTypeKey.String("SENT")
	RPCMessageTypeReceived = RPCMessageTypeKey.String("RECEIVED")
)

Semantic conventions for common RPC attributes.

Functions

func Extract

func Extract(ctx context.Context, md *metadata.MD, opts ...Option) (baggage.Baggage, trace.SpanContext)

Extract returns the correlation context and span context that another service encoded in the gRPC metadata object with Inject. This function is meant to be used on incoming requests. Deprecated: Unnecessary public func.

func Inject

func Inject(ctx context.Context, md *metadata.MD, opts ...Option)

Inject injects correlation context and span context into the gRPC metadata object. This function is meant to be used on outgoing requests. Deprecated: Unnecessary public func.

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()))
}

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()))
}

func SemVersion

func SemVersion() string

SemVersion is the semantic version to be supplied to tracer/meter creation.

Deprecated: Use Version instead.

func StreamClientInterceptor

func StreamClientInterceptor(opts ...Option) grpc.StreamClientInterceptor

StreamClientInterceptor returns a grpc.StreamClientInterceptor suitable for use in a grpc.NewClient call.

Deprecated: Use NewClientHandler instead.

func StreamServerInterceptor

func StreamServerInterceptor(opts ...Option) grpc.StreamServerInterceptor

StreamServerInterceptor returns a grpc.StreamServerInterceptor suitable for use in a grpc.NewServer call.

Deprecated: Use NewServerHandler instead.

func UnaryClientInterceptor

func UnaryClientInterceptor(opts ...Option) grpc.UnaryClientInterceptor

UnaryClientInterceptor returns a grpc.UnaryClientInterceptor suitable for use in a grpc.NewClient call.

Deprecated: Use NewClientHandler instead.

func UnaryServerInterceptor

func UnaryServerInterceptor(opts ...Option) grpc.UnaryServerInterceptor

UnaryServerInterceptor returns a grpc.UnaryServerInterceptor suitable for use in a grpc.NewServer call.

Deprecated: Use NewServerHandler instead.

func Version

func Version() string

Version is the current release version of the gRPC instrumentation.

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 WithPropagators

func WithPropagators(p propagation.TextMapPropagator) Option

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

func WithSpanAttributes

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

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

func WithSpanOptions

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

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

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 semconv.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.
filters/interceptorPackage interceptor provides a set of filters useful with the otelgrpc.WithInterceptorFilter option to control which inbound requests are instrumented.
internal
Version
v0.60.0 (latest)
Published
Mar 6, 2025
Platform
linux/amd64
Imports
24 packages
Last checked
5 days ago

Tools for package owners.