opentelemetry
packageThis package is not in the latest version of its module.
Details
Validgo.mod file
The Go module system was introduced in Go 1.11 and is the official dependency management solution for Go.
Redistributable license
Redistributable licenses place minimal restrictions on how software can be used, modified, and redistributed.
Tagged version
Modules with tagged versions give importers more predictable builds.
Stable version
When a project reaches major version v1 it is considered stable.
- Learn more about best practices
Repository
Links
Documentation¶
Overview¶
Package opentelemetry implements opentelemetry instrumentation code forgRPC-Go clients and servers.
For details on configuring opentelemetry and various instruments that thispackage creates, see[gRPC OpenTelemetry Metrics](https://grpc.io/docs/guides/opentelemetry-metrics/).
Index¶
Examples¶
Constants¶
const (// ClientAttemptStartedMetricName is the number of client call attempts// started.ClientAttemptStartedMetricNamestring = "grpc.client.attempt.started"// ClientAttemptDurationMetricName is the end-to-end time taken to complete// a client call attempt.ClientAttemptDurationMetricNamestring = "grpc.client.attempt.duration"// ClientAttemptSentCompressedTotalMessageSizeMetricName is the compressed// message bytes sent per client call attempt.ClientAttemptSentCompressedTotalMessageSizeMetricNamestring = "grpc.client.attempt.sent_total_compressed_message_size"// ClientAttemptRcvdCompressedTotalMessageSizeMetricName is the compressed// message bytes received per call attempt.ClientAttemptRcvdCompressedTotalMessageSizeMetricNamestring = "grpc.client.attempt.rcvd_total_compressed_message_size"// ClientCallDurationMetricName is the time taken by gRPC to complete an RPC// from application's perspective.ClientCallDurationMetricNamestring = "grpc.client.call.duration")
const (// ServerCallStartedMetricName is the number of server calls started.ServerCallStartedMetricNamestring = "grpc.server.call.started"// ServerCallSentCompressedTotalMessageSizeMetricName is the compressed// message bytes sent per server call.ServerCallSentCompressedTotalMessageSizeMetricNamestring = "grpc.server.call.sent_total_compressed_message_size"// ServerCallRcvdCompressedTotalMessageSizeMetricName is the compressed// message bytes received per server call.ServerCallRcvdCompressedTotalMessageSizeMetricNamestring = "grpc.server.call.rcvd_total_compressed_message_size"// ServerCallDurationMetricName is the end-to-end time taken to complete a// call from server transport's perspective.ServerCallDurationMetricNamestring = "grpc.server.call.duration")
Variables¶
var (// DefaultLatencyBounds are the default bounds for latency metrics.DefaultLatencyBounds = []float64{0, 0.00001, 0.00005, 0.0001, 0.0003, 0.0006, 0.0008, 0.001, 0.002, 0.003, 0.004, 0.005, 0.006, 0.008, 0.01, 0.013, 0.016, 0.02, 0.025, 0.03, 0.04, 0.05, 0.065, 0.08, 0.1, 0.13, 0.16, 0.2, 0.25, 0.3, 0.4, 0.5, 0.65, 0.8, 1, 2, 5, 10, 20, 50, 100}// provide "advice" through API, SDK should set this too// DefaultSizeBounds are the default bounds for metrics which record size.DefaultSizeBounds = []float64{0, 1024, 2048, 4096, 16384, 65536, 262144, 1048576, 4194304, 16777216, 67108864, 268435456, 1073741824, 4294967296})
Users of this component should use these bucket boundaries as part of theirSDK MeterProvider passed in. This component sends this as "advice" to theAPI, which works, however this stability is not guaranteed, so for safety theSDK Meter Provider provided should set these bounds for correspondingmetrics.
Functions¶
funcDefaultMetrics¶
DefaultMetrics returns a set of default OpenTelemetry metrics.
This should only be invoked after init time.
funcDialOption¶
func DialOption(oOptions)grpc.DialOption
DialOption returns a dial option which enables OpenTelemetry instrumentationcode for a grpc.ClientConn.
Client applications interested in instrumenting their grpc.ClientConn shouldpass the dial option returned from this function as a dial option togrpc.NewClient().
For the metrics supported by this instrumentation code, specify the clientmetrics to record in metrics options. Also provide an implementation of aMeterProvider. If the passed in Meter Provider does not have the viewconfigured for an individual metric turned on, the API call in this componentwill create a default view for that metric.
For the traces supported by this instrumentation code, provide animplementation of a TextMapPropagator and OpenTelemetry TracerProvider.
Example (Basic)¶
package mainimport ("google.golang.org/grpc""google.golang.org/grpc/credentials/insecure""google.golang.org/grpc/stats/opentelemetry""go.opentelemetry.io/otel/sdk/metric")func main() {// This is setting default bounds for a view. Setting these bounds through// meter provider from SDK is recommended, as API calls in this module// provide default bounds, but these calls are not guaranteed to be stable// and API implementors are not required to implement bounds. Setting bounds// through SDK ensures the bounds get picked up. The specific fields in// Aggregation take precedence over defaults from API. For any fields unset// in aggregation, defaults get picked up, so can have a mix of fields from// SDK and fields created from API call. The overridden views themselves// also follow same logic, only the specific views being created in the SDK// use SDK information, the rest are created from API call.reader := metric.NewManualReader()provider := metric.NewMeterProvider(metric.WithReader(reader),metric.WithView(metric.NewView(metric.Instrument{Name: "grpc.client.call.duration",},metric.Stream{Aggregation: metric.AggregationExplicitBucketHistogram{Boundaries: opentelemetry.DefaultSizeBounds, // The specific fields set in SDK take precedence over API.},},)),)opts := opentelemetry.Options{MetricsOptions: opentelemetry.MetricsOptions{MeterProvider: provider,Metrics: opentelemetry.DefaultMetrics(), // equivalent to unset - distinct from empty},}do := opentelemetry.DialOption(opts)cc, err := grpc.NewClient("<target string>", do, grpc.WithTransportCredentials(insecure.NewCredentials()))if err != nil {// Handle err.}defer cc.Close()}funcServerOption¶
func ServerOption(oOptions)grpc.ServerOption
ServerOption returns a server option which enables OpenTelemetryinstrumentation code for a grpc.Server.
Server applications interested in instrumenting their grpc.Server should passthe server option returned from this function as an argument togrpc.NewServer().
For the metrics supported by this instrumentation code, specify the servermetrics to record in metrics options. Also provide an implementation of aMeterProvider. If the passed in Meter Provider does not have the viewconfigured for an individual metric turned on, the API call in this componentwill create a default view for that metric.
For the traces supported by this instrumentation code, provide animplementation of a TextMapPropagator and OpenTelemetry TracerProvider.
Example (MethodFilter)¶
package mainimport ("google.golang.org/grpc""google.golang.org/grpc/credentials/insecure""google.golang.org/grpc/stats/opentelemetry""go.opentelemetry.io/otel/sdk/metric")func main() {reader := metric.NewManualReader()provider := metric.NewMeterProvider(metric.WithReader(reader))opts := opentelemetry.Options{MetricsOptions: opentelemetry.MetricsOptions{MeterProvider: provider,// Because Metrics is unset, the user will get default metrics.MethodAttributeFilter: func(str string) bool {// Will allow duplex/any other type of RPC.return str != "/grpc.testing.TestService/UnaryCall"},},}cc, err := grpc.NewClient("some-target", opentelemetry.DialOption(opts), grpc.WithTransportCredentials(insecure.NewCredentials()))if err != nil {// Handle err.}defer cc.Close()}Types¶
typeGRPCTraceBinPropagator¶added inv1.69.0
type GRPCTraceBinPropagator struct{}GRPCTraceBinPropagator is an OpenTelemetry TextMapPropagator which is usedto extract and inject trace context data from and into headers exchanged bygRPC applications. It propagates trace data in binary format using the`grpc-trace-bin` header.
func (GRPCTraceBinPropagator)Extract¶added inv1.69.0
func (GRPCTraceBinPropagator) Extract(ctxcontext.Context, carrierotelpropagation.TextMapCarrier)context.Context
Extract reads OpenTelemetry span context from the `grpc-trace-bin` header ofcarrier into the provided context, if present.
If a valid span context is retrieved from `grpc-trace-bin`, it returns a newcontext containing the extracted OpenTelemetry span context marked asremote.
If `grpc-trace-bin` header is not present, it returns the context as is.
func (GRPCTraceBinPropagator)Fields¶added inv1.69.0
func (GRPCTraceBinPropagator) Fields() []string
Fields returns the keys whose values are set with Inject.
GRPCTraceBinPropagator always returns a slice containing only`grpc-trace-bin` key because it only sets the `grpc-trace-bin` header forpropagating trace context.
func (GRPCTraceBinPropagator)Inject¶added inv1.69.0
func (GRPCTraceBinPropagator) Inject(ctxcontext.Context, carrierotelpropagation.TextMapCarrier)
Inject sets OpenTelemetry span context from the Context into the carrier asa `grpc-trace-bin` header if span context is valid.
If span context is not valid, it returns without setting `grpc-trace-bin`header.
typeMetricsOptions¶
type MetricsOptions struct {// MeterProvider is the MeterProvider instance that will be used to create// instruments. To enable metrics collection, set a meter provider. If// unset, no metrics will be recorded.MeterProviderotelmetric.MeterProvider// Metrics are the metrics to instrument. Will create instrument and record telemetry// for corresponding metric supported by the client and server// instrumentation components if applicable. If not set, the default metrics// will be recorded.Metrics *stats.MetricSet// MethodAttributeFilter is a function that determines whether to record the// method name of RPCs as an attribute, or to bucket into "other". Take care// to limit the values allowed, as allowing too many will increase// cardinality and could cause severe memory or performance problems.//// This only applies for server-side metrics. For clients, to record the// method name in the attributes, pass grpc.StaticMethodCallOption to Invoke// or NewStream. Note that when using protobuf generated clients, this// CallOption is included automatically.MethodAttributeFilter func(string)bool// OptionalLabels specifies a list of optional labels to enable on any// metrics that support them.OptionalLabels []string// contains filtered or unexported fields}MetricsOptions are the metrics options for OpenTelemetry instrumentation.
typeOptions¶
type Options struct {// MetricsOptions are the metrics options for OpenTelemetry instrumentation.MetricsOptionsMetricsOptions// TraceOptions are the tracing options for OpenTelemetry instrumentation.TraceOptionsexperimental.TraceOptions}Options are the options for OpenTelemetry instrumentation.
Example (AddExperimentalMetrics)¶
package mainimport ("google.golang.org/grpc""google.golang.org/grpc/credentials/insecure""google.golang.org/grpc/stats/opentelemetry")func main() {opts := opentelemetry.Options{MetricsOptions: opentelemetry.MetricsOptions{// These are example experimental gRPC metrics, which are disabled// by default and must be explicitly enabled. For the full,// up-to-date list of metrics, see:// https://grpc.io/docs/guides/opentelemetry-metrics/#instrumentsMetrics: opentelemetry.DefaultMetrics().Add("grpc.lb.pick_first.connection_attempts_succeeded","grpc.lb.pick_first.connection_attempts_failed",),},}do := opentelemetry.DialOption(opts)cc, err := grpc.NewClient("<target string>", do, grpc.WithTransportCredentials(insecure.NewCredentials()))if err != nil {// Handle error.}defer cc.Close()}
Source Files¶
Directories¶
| Path | Synopsis |
|---|---|
Package csm contains utilities for Google Cloud Service Mesh observability. | Package csm contains utilities for Google Cloud Service Mesh observability. |
Package internal defines the PluginOption interface. | Package internal defines the PluginOption interface. |
testutils Package testutils contains helpers for OpenTelemetry tests. | Package testutils contains helpers for OpenTelemetry tests. |
tracing Package tracing implements the OpenTelemetry carrier for context propagation in gRPC tracing. | Package tracing implements the OpenTelemetry carrier for context propagation in gRPC tracing. |