otlpmetricgrpc

package module
v1.25.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 5, 2024 License: Apache-2.0 Imports: 20 Imported by: 316

README

OTLP Metric gRPC Exporter

PkgGoDev

Documentation

Overview

Package otlpmetricgrpc provides an OTLP metrics exporter using gRPC. By default the telemetry is sent to https://localhost:4317.

Exporter should be created using New and used with a metric.PeriodicReader.

The environment variables described below can be used for configuration.

OTEL_EXPORTER_OTLP_ENDPOINT, OTEL_EXPORTER_OTLP_METRICS_ENDPOINT (default: "https://localhost:4317") - target to which the exporter sends telemetry. The target syntax is defined in https://github.com/grpc/grpc/blob/master/doc/naming.md. The value must contain a host. The value may additionally a port, a scheme, and a path. The value accepts "http" and "https" scheme. The value should not contain a query string or fragment. OTEL_EXPORTER_OTLP_METRICS_ENDPOINT takes precedence over OTEL_EXPORTER_OTLP_ENDPOINT. The configuration can be overridden by WithEndpoint, WithEndpointURL, WithInsecure, and WithGRPCConn options.

OTEL_EXPORTER_OTLP_INSECURE, OTEL_EXPORTER_OTLP_METRICS_INSECURE (default: "false") - setting "true" disables client transport security for the exporter's gRPC connection. You can use this only when an endpoint is provided without the http or https scheme. OTEL_EXPORTER_OTLP_ENDPOINT, OTEL_EXPORTER_OTLP_METRICS_ENDPOINT setting overrides the scheme defined via OTEL_EXPORTER_OTLP_ENDPOINT, OTEL_EXPORTER_OTLP_METRICS_ENDPOINT. OTEL_EXPORTER_OTLP_METRICS_INSECURE takes precedence over OTEL_EXPORTER_OTLP_INSECURE. The configuration can be overridden by WithInsecure, WithGRPCConn options.

OTEL_EXPORTER_OTLP_HEADERS, OTEL_EXPORTER_OTLP_METRICS_HEADERS (default: none) - key-value pairs used as gRPC metadata associated with gRPC requests. The value is expected to be represented in a format matching the W3C Baggage HTTP Header Content Format, except that additional semi-colon delimited metadata is not supported. Example value: "key1=value1,key2=value2". OTEL_EXPORTER_OTLP_METRICS_HEADERS takes precedence over OTEL_EXPORTER_OTLP_HEADERS. The configuration can be overridden by WithHeaders option.

OTEL_EXPORTER_OTLP_TIMEOUT, OTEL_EXPORTER_OTLP_METRICS_TIMEOUT (default: "10000") - maximum time in milliseconds the OTLP exporter waits for each batch export. OTEL_EXPORTER_OTLP_METRICS_TIMEOUT takes precedence over OTEL_EXPORTER_OTLP_TIMEOUT. The configuration can be overridden by WithTimeout option.

OTEL_EXPORTER_OTLP_COMPRESSION, OTEL_EXPORTER_OTLP_METRICS_COMPRESSION (default: none) - the gRPC compressor the exporter uses. Supported value: "gzip". OTEL_EXPORTER_OTLP_METRICS_COMPRESSION takes precedence over OTEL_EXPORTER_OTLP_COMPRESSION. The configuration can be overridden by WithCompressor, WithGRPCConn options.

OTEL_EXPORTER_OTLP_CERTIFICATE, OTEL_EXPORTER_OTLP_METRICS_CERTIFICATE (default: none) - the filepath to the trusted certificate to use when verifying a server's TLS credentials. OTEL_EXPORTER_OTLP_METRICS_CERTIFICATE takes precedence over OTEL_EXPORTER_OTLP_CERTIFICATE. The configuration can be overridden by WithTLSCredentials, WithGRPCConn options.

OTEL_EXPORTER_OTLP_CLIENT_CERTIFICATE, OTEL_EXPORTER_OTLP_METRICS_CLIENT_CERTIFICATE (default: none) - the filepath to the client certificate/chain trust for client's private key to use in mTLS communication in PEM format. OTEL_EXPORTER_OTLP_METRICS_CLIENT_CERTIFICATE takes precedence over OTEL_EXPORTER_OTLP_CLIENT_CERTIFICATE. The configuration can be overridden by WithTLSCredentials, WithGRPCConn options.

OTEL_EXPORTER_OTLP_CLIENT_KEY, OTEL_EXPORTER_OTLP_METRICS_CLIENT_KEY (default: none) - the filepath to the client's private key to use in mTLS communication in PEM format. OTEL_EXPORTER_OTLP_METRICS_CLIENT_KEY takes precedence over OTEL_EXPORTER_OTLP_CLIENT_KEY. The configuration can be overridden by WithTLSCredentials, WithGRPCConn option.

OTEL_EXPORTER_OTLP_METRICS_TEMPORALITY_PREFERENCE (default: "cumulative") - aggregation temporality to use on the basis of instrument kind. Supported values:

  • "cumulative" - Cumulative aggregation temporality for all instrument kinds,
  • "delta" - Delta aggregation temporality for Counter, Asynchronous Counter and Histogram instrument kinds; Cumulative aggregation for UpDownCounter and Asynchronous UpDownCounter instrument kinds,
  • "lowmemory" - Delta aggregation temporality for Synchronous Counter and Histogram instrument kinds; Cumulative aggregation temporality for Synchronous UpDownCounter, Asynchronous Counter, and Asynchronous UpDownCounter instrument kinds.

The configuration can be overridden by WithTemporalitySelector option.

OTEL_EXPORTER_OTLP_METRICS_DEFAULT_HISTOGRAM_AGGREGATION (default: "explicit_bucket_histogram") - default aggregation to use for histogram instruments. Supported values:

The configuration can be overridden by WithAggregationSelector option.

Example
package main

import (
	"context"

	"go.opentelemetry.io/otel"
	"go.opentelemetry.io/otel/exporters/otlp/otlpmetric/otlpmetricgrpc"
	"go.opentelemetry.io/otel/sdk/metric"
)

func main() {
	ctx := context.Background()
	exp, err := otlpmetricgrpc.New(ctx)
	if err != nil {
		panic(err)
	}

	meterProvider := metric.NewMeterProvider(metric.WithReader(metric.NewPeriodicReader(exp)))
	defer func() {
		if err := meterProvider.Shutdown(ctx); err != nil {
			panic(err)
		}
	}()
	otel.SetMeterProvider(meterProvider)

	// From here, the meterProvider can be used by instrumentation to collect
	// telemetry.
}
Output:

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Version added in v0.43.0

func Version() string

Version is the current release version of the OpenTelemetry OTLP over gRPC metrics exporter in use.

Types

type Exporter added in v0.40.0

type Exporter struct {
	// contains filtered or unexported fields
}

Exporter is a OpenTelemetry metric Exporter using gRPC.

func New

func New(ctx context.Context, options ...Option) (*Exporter, error)

New returns an OpenTelemetry metric Exporter. The Exporter can be used with a PeriodicReader to export OpenTelemetry metric data to an OTLP receiving endpoint using gRPC.

If an already established gRPC ClientConn is not passed in options using WithGRPCConn, a connection to the OTLP endpoint will be established based on options. If a connection cannot be establishes in the lifetime of ctx, an error will be returned.

func (*Exporter) Aggregation added in v0.40.0

func (e *Exporter) Aggregation(k metric.InstrumentKind) metric.Aggregation

Aggregation returns the Aggregation to use for an instrument kind.

func (*Exporter) Export added in v0.40.0

Export transforms and transmits metric data to an OTLP receiver.

This method returns an error if called after Shutdown. This method returns an error if the method is canceled by the passed context.

func (*Exporter) ForceFlush added in v0.40.0

func (e *Exporter) ForceFlush(ctx context.Context) error

ForceFlush flushes any metric data held by an exporter.

This method returns an error if called after Shutdown. This method returns an error if the method is canceled by the passed context.

This method is safe to call concurrently.

func (*Exporter) MarshalLog added in v0.40.0

func (e *Exporter) MarshalLog() interface{}

MarshalLog returns logging data about the Exporter.

func (*Exporter) Shutdown added in v0.40.0

func (e *Exporter) Shutdown(ctx context.Context) error

Shutdown flushes all metric data held by an exporter and releases any held computational resources.

This method returns an error if called after Shutdown. This method returns an error if the method is canceled by the passed context.

This method is safe to call concurrently.

func (*Exporter) Temporality added in v0.40.0

Temporality returns the Temporality to use for an instrument kind.

type Option

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

Option applies a configuration option to the Exporter.

func WithAggregationSelector added in v0.34.0

func WithAggregationSelector(selector metric.AggregationSelector) Option

WithAggregationSelector sets the AggregationSelector the client will use to determine the aggregation to use for an instrument based on its kind. If this option is not used, the reader will use the DefaultAggregationSelector from the go.opentelemetry.io/otel/sdk/metric package, or the aggregation explicitly passed for a view matching an instrument.

func WithCompressor

func WithCompressor(compressor string) Option

WithCompressor sets the compressor the gRPC client uses. Supported compressor values: "gzip".

If the OTEL_EXPORTER_OTLP_COMPRESSION or OTEL_EXPORTER_OTLP_METRICS_COMPRESSION environment variable is set, and this option is not passed, that variable value will be used. That value can be either "none" or "gzip". If both are set, OTEL_EXPORTER_OTLP_METRICS_COMPRESSION will take precedence.

By default, if an environment variable is not set, and this option is not passed, no compressor will be used.

This option has no effect if WithGRPCConn is used.

func WithDialOption

func WithDialOption(opts ...grpc.DialOption) Option

WithDialOption sets explicit grpc.DialOptions to use when establishing a gRPC connection. The options here are appended to the internal grpc.DialOptions used so they will take precedence over any other internal grpc.DialOptions they might conflict with. The grpc.WithBlock, grpc.WithTimeout, and grpc.WithReturnConnectionError grpc.DialOptions are ignored.

This option has no effect if WithGRPCConn is used.

func WithEndpoint

func WithEndpoint(endpoint string) Option

WithEndpoint sets the target endpoint the Exporter will connect to.

If the OTEL_EXPORTER_OTLP_ENDPOINT or OTEL_EXPORTER_OTLP_METRICS_ENDPOINT environment variable is set, and this option is not passed, that variable value will be used. If both are set, OTEL_EXPORTER_OTLP_METRICS_ENDPOINT will take precedence.

If both this option and WithEndpointURL are used, the last used option will take precedence.

By default, if an environment variable is not set, and this option is not passed, "localhost:4317" will be used.

This option has no effect if WithGRPCConn is used.

func WithEndpointURL added in v1.23.0

func WithEndpointURL(u string) Option

WithEndpointURL sets the target endpoint URL the Exporter will connect to.

If the OTEL_EXPORTER_OTLP_ENDPOINT or OTEL_EXPORTER_OTLP_METRICS_ENDPOINT environment variable is set, and this option is not passed, that variable value will be used. If both are set, OTEL_EXPORTER_OTLP_METRICS_ENDPOINT will take precedence.

If both this option and WithEndpoint are used, the last used option will take precedence.

If an invalid URL is provided, the default value will be kept.

By default, if an environment variable is not set, and this option is not passed, "localhost:4317" will be used.

This option has no effect if WithGRPCConn is used.

func WithGRPCConn added in v0.25.0

func WithGRPCConn(conn *grpc.ClientConn) Option

WithGRPCConn sets conn as the gRPC ClientConn used for all communication.

This option takes precedence over any other option that relates to establishing or persisting a gRPC connection to a target endpoint. Any other option of those types passed will be ignored.

It is the callers responsibility to close the passed conn. The Exporter Shutdown method will not close this connection.

func WithHeaders

func WithHeaders(headers map[string]string) Option

WithHeaders will send the provided headers with each gRPC requests.

If the OTEL_EXPORTER_OTLP_HEADERS or OTEL_EXPORTER_OTLP_METRICS_HEADERS environment variable is set, and this option is not passed, that variable value will be used. The value will be parsed as a list of key value pairs. These pairs are expected to be in the W3C Correlation-Context format without additional semi-colon delimited metadata (i.e. "k1=v1,k2=v2"). If both are set, OTEL_EXPORTER_OTLP_METRICS_HEADERS will take precedence.

By default, if an environment variable is not set, and this option is not passed, no user headers will be set.

func WithInsecure

func WithInsecure() Option

WithInsecure disables client transport security for the Exporter's gRPC connection, just like grpc.WithInsecure() (https://pkg.go.dev/google.golang.org/grpc#WithInsecure) does.

If the OTEL_EXPORTER_OTLP_ENDPOINT or OTEL_EXPORTER_OTLP_METRICS_ENDPOINT environment variable is set, and this option is not passed, that variable value will be used to determine client security. If the endpoint has a scheme of "http" or "unix" client security will be disabled. If both are set, OTEL_EXPORTER_OTLP_METRICS_ENDPOINT will take precedence.

By default, if an environment variable is not set, and this option is not passed, client security will be used.

This option has no effect if WithGRPCConn is used.

func WithReconnectionPeriod

func WithReconnectionPeriod(rp time.Duration) Option

WithReconnectionPeriod set the minimum amount of time between connection attempts to the target endpoint.

This option has no effect if WithGRPCConn is used.

func WithRetry

func WithRetry(settings RetryConfig) Option

WithRetry sets the retry policy for transient retryable errors that are returned by the target endpoint.

If the target endpoint responds with not only a retryable error, but explicitly returns a backoff time in the response, that time will take precedence over these settings.

These settings do not define any network retry strategy. That is entirely handled by the gRPC ClientConn.

If unset, the default retry policy will be used. It will retry the export 5 seconds after receiving a retryable error and increase exponentially after each error for no more than a total time of 1 minute.

func WithServiceConfig

func WithServiceConfig(serviceConfig string) Option

WithServiceConfig defines the default gRPC service config used.

This option has no effect if WithGRPCConn is used.

func WithTLSCredentials

func WithTLSCredentials(creds credentials.TransportCredentials) Option

WithTLSCredentials sets the gRPC connection to use creds.

If the OTEL_EXPORTER_OTLP_CERTIFICATE or OTEL_EXPORTER_OTLP_METRICS_CERTIFICATE environment variable is set, and this option is not passed, that variable value will be used. The value will be parsed the filepath of the TLS certificate chain to use. If both are set, OTEL_EXPORTER_OTLP_METRICS_CERTIFICATE will take precedence.

By default, if an environment variable is not set, and this option is not passed, no TLS credentials will be used.

This option has no effect if WithGRPCConn is used.

func WithTemporalitySelector added in v0.34.0

func WithTemporalitySelector(selector metric.TemporalitySelector) Option

WithTemporalitySelector sets the TemporalitySelector the client will use to determine the Temporality of an instrument based on its kind. If this option is not used, the client will use the DefaultTemporalitySelector from the go.opentelemetry.io/otel/sdk/metric package.

func WithTimeout

func WithTimeout(duration time.Duration) Option

WithTimeout sets the max amount of time an Exporter will attempt an export.

This takes precedence over any retry settings defined by WithRetry. Once this time limit has been reached the export is abandoned and the metric data is dropped.

If the OTEL_EXPORTER_OTLP_TIMEOUT or OTEL_EXPORTER_OTLP_METRICS_TIMEOUT environment variable is set, and this option is not passed, that variable value will be used. The value will be parsed as an integer representing the timeout in milliseconds. If both are set, OTEL_EXPORTER_OTLP_METRICS_TIMEOUT will take precedence.

By default, if an environment variable is not set, and this option is not passed, a timeout of 10 seconds will be used.

type RetryConfig added in v0.26.0

type RetryConfig retry.Config

RetryConfig defines configuration for retrying the export of metric data that failed.

This configuration does not define any network retry strategy. That is entirely handled by the gRPC ClientConn.

Directories

Path Synopsis
retry
Package retry provides request retry functionality that can perform configurable exponential backoff for transient errors and honor any explicit throttle responses received.
Package retry provides request retry functionality that can perform configurable exponential backoff for transient errors and honor any explicit throttle responses received.
transform
Package transform provides transformation functionality from the sdk/metric/metricdata data-types into OTLP data-types.
Package transform provides transformation functionality from the sdk/metric/metricdata data-types into OTLP data-types.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL