Documentation ¶
Overview ¶
Example (Insecure) ¶
package main import ( "context" "log" "time" "go.opentelemetry.io/otel/exporters/otlp/otlpmetric" "go.opentelemetry.io/otel/exporters/otlp/otlpmetric/otlpmetricgrpc" "go.opentelemetry.io/otel/sdk/metric/selector/simple" "go.opentelemetry.io/otel" "go.opentelemetry.io/otel/metric" "go.opentelemetry.io/otel/metric/global" controller "go.opentelemetry.io/otel/sdk/metric/controller/basic" processor "go.opentelemetry.io/otel/sdk/metric/processor/basic" ) func main() { ctx := context.Background() client := otlpmetricgrpc.NewClient(otlpmetricgrpc.WithInsecure()) exp, err := otlpmetric.New(ctx, client) if err != nil { log.Fatalf("Failed to create the collector exporter: %v", err) } defer func() { ctx, cancel := context.WithTimeout(ctx, time.Second) defer cancel() if err := exp.Shutdown(ctx); err != nil { otel.Handle(err) } }() pusher := controller.New( processor.NewFactory( simple.NewWithExactDistribution(), exp, ), controller.WithExporter(exp), controller.WithCollectPeriod(2*time.Second), ) global.SetMeterProvider(pusher) if err := pusher.Start(ctx); err != nil { log.Fatalf("could not start metric controoler: %v", err) } defer func() { ctx, cancel := context.WithTimeout(ctx, time.Second) defer cancel() // pushes any last exports to the receiver if err := pusher.Stop(ctx); err != nil { otel.Handle(err) } }() meter := global.Meter("test-meter") // Recorder metric example counter := metric.Must(meter). NewFloat64Counter( "an_important_metric", metric.WithDescription("Measures the cumulative epicness of the app"), ) for i := 0; i < 10; i++ { log.Printf("Doing really hard work (%d / 10)\n", i+1) counter.Add(ctx, 1.0) } }
Output:
Example (WithDifferentSignalCollectors) ¶
package main import ( "context" "log" "time" "go.opentelemetry.io/otel/exporters/otlp/otlpmetric" "go.opentelemetry.io/otel/exporters/otlp/otlpmetric/otlpmetricgrpc" "go.opentelemetry.io/otel/sdk/metric/selector/simple" "go.opentelemetry.io/otel" "go.opentelemetry.io/otel/metric" "go.opentelemetry.io/otel/metric/global" controller "go.opentelemetry.io/otel/sdk/metric/controller/basic" processor "go.opentelemetry.io/otel/sdk/metric/processor/basic" ) func main() { client := otlpmetricgrpc.NewClient( otlpmetricgrpc.WithInsecure(), otlpmetricgrpc.WithEndpoint("localhost:30080"), ) ctx := context.Background() exp, err := otlpmetric.New(ctx, client) if err != nil { log.Fatalf("failed to create the collector exporter: %v", err) } defer func() { ctx, cancel := context.WithTimeout(ctx, time.Second) defer cancel() if err := exp.Shutdown(ctx); err != nil { otel.Handle(err) } }() pusher := controller.New( processor.NewFactory( simple.NewWithExactDistribution(), exp, ), controller.WithExporter(exp), controller.WithCollectPeriod(2*time.Second), ) global.SetMeterProvider(pusher) if err := pusher.Start(ctx); err != nil { log.Fatalf("could not start metric controoler: %v", err) } defer func() { ctx, cancel := context.WithTimeout(ctx, time.Second) defer cancel() // pushes any last exports to the receiver if err := pusher.Stop(ctx); err != nil { otel.Handle(err) } }() meter := global.Meter("test-meter") // Recorder metric example counter := metric.Must(meter). NewFloat64Counter( "an_important_metric", metric.WithDescription("Measures the cumulative epicness of the app"), ) for i := 0; i < 10; i++ { log.Printf("Doing really hard work (%d / 10)\n", i+1) counter.Add(ctx, 1.0) } log.Printf("Done!") }
Output:
Example (WithTLS) ¶
package main import ( "context" "log" "time" "go.opentelemetry.io/otel/exporters/otlp/otlpmetric" "go.opentelemetry.io/otel/exporters/otlp/otlpmetric/otlpmetricgrpc" "go.opentelemetry.io/otel/sdk/metric/selector/simple" "google.golang.org/grpc/credentials" "go.opentelemetry.io/otel" "go.opentelemetry.io/otel/metric" "go.opentelemetry.io/otel/metric/global" controller "go.opentelemetry.io/otel/sdk/metric/controller/basic" processor "go.opentelemetry.io/otel/sdk/metric/processor/basic" ) func main() { // Please take at look at https://pkg.go.dev/google.golang.org/grpc/credentials#TransportCredentials // for ways on how to initialize gRPC TransportCredentials. creds, err := credentials.NewClientTLSFromFile("my-cert.pem", "") if err != nil { log.Fatalf("failed to create gRPC client TLS credentials: %v", err) } ctx := context.Background() client := otlpmetricgrpc.NewClient(otlpmetricgrpc.WithTLSCredentials(creds)) exp, err := otlpmetric.New(ctx, client) if err != nil { log.Fatalf("failed to create the collector exporter: %v", err) } defer func() { ctx, cancel := context.WithTimeout(ctx, time.Second) defer cancel() if err := exp.Shutdown(ctx); err != nil { otel.Handle(err) } }() pusher := controller.New( processor.NewFactory( simple.NewWithExactDistribution(), exp, ), controller.WithExporter(exp), controller.WithCollectPeriod(2*time.Second), ) global.SetMeterProvider(pusher) if err := pusher.Start(ctx); err != nil { log.Fatalf("could not start metric controoler: %v", err) } defer func() { ctx, cancel := context.WithTimeout(ctx, time.Second) defer cancel() // pushes any last exports to the receiver if err := pusher.Stop(ctx); err != nil { otel.Handle(err) } }() meter := global.Meter("test-meter") // Recorder metric example counter := metric.Must(meter). NewFloat64Counter( "an_important_metric", metric.WithDescription("Measures the cumulative epicness of the app"), ) for i := 0; i < 10; i++ { log.Printf("Doing really hard work (%d / 10)\n", i+1) counter.Add(ctx, 1.0) } }
Output:
Index ¶
- func New(ctx context.Context, opts ...Option) (*otlpmetric.Exporter, error)
- func NewClient(opts ...Option) otlpmetric.Client
- func NewUnstarted(opts ...Option) *otlpmetric.Exporter
- type Option
- func WithCompressor(compressor string) Option
- func WithDialOption(opts ...grpc.DialOption) Option
- func WithEndpoint(endpoint string) Option
- func WithGRPCConn(conn *grpc.ClientConn) Option
- func WithHeaders(headers map[string]string) Option
- func WithInsecure() Option
- func WithReconnectionPeriod(rp time.Duration) Option
- func WithRetry(settings RetrySettings) Option
- func WithServiceConfig(serviceConfig string) Option
- func WithTLSCredentials(creds credentials.TransportCredentials) Option
- func WithTimeout(duration time.Duration) Option
- type RetrySettings
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func NewClient ¶
func NewClient(opts ...Option) otlpmetric.Client
NewClient creates a new gRPC metric client.
func NewUnstarted ¶
func NewUnstarted(opts ...Option) *otlpmetric.Exporter
NewUnstarted constructs a new Exporter and does not start it.
Types ¶
type Option ¶
type Option interface {
// contains filtered or unexported methods
}
Option applies an option to the gRPC client.
func WithCompressor ¶
WithCompressor will set the compressor for the gRPC client to use when sending requests. It is the responsibility of the caller to ensure that the compressor set has been registered with google.golang.org/grpc/encoding. This can be done by encoding.RegisterCompressor. Some compressors auto-register on import, such as gzip, which can be registered by calling `import _ "google.golang.org/grpc/encoding/gzip"`.
func WithDialOption ¶
func WithDialOption(opts ...grpc.DialOption) Option
WithDialOption opens support to any grpc.DialOption to be used. If it conflicts with some other configuration the GRPC specified via the collector the ones here will take preference since they are set last.
func WithEndpoint ¶
WithEndpoint allows one to set the endpoint that the exporter will connect to the collector on. If unset, it will instead try to use connect to DefaultCollectorHost:DefaultCollectorPort.
func WithGRPCConn ¶ added in v0.25.0
func WithGRPCConn(conn *grpc.ClientConn) Option
WithGRPCConn allows reusing existing gRPC connection when it has already been established for other services. When set, other dial options will be ignored.
func WithHeaders ¶
WithHeaders will send the provided headers with gRPC requests.
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. Note, by default, client security is required unless WithInsecure is used.
func WithReconnectionPeriod ¶
WithReconnectionPeriod allows one to set the delay between next connection attempt after failing to connect with the collector.
func WithRetry ¶
func WithRetry(settings RetrySettings) Option
WithRetry configures the retry policy for transient errors that may occurs when exporting metrics. An exponential back-off algorithm is used to ensure endpoints are not overwhelmed with retries. If unset, the default retry policy will retry after 5 seconds and increase exponentially after each error for a total of 1 minute.
func WithServiceConfig ¶
WithServiceConfig defines the default gRPC service config used.
func WithTLSCredentials ¶
func WithTLSCredentials(creds credentials.TransportCredentials) Option
WithTLSCredentials allows the connection to use TLS credentials when talking to the server. It takes in grpc.TransportCredentials instead of say a Certificate file or a tls.Certificate, because the retrieving of these credentials can be done in many ways e.g. plain file, in code tls.Config or by certificate rotation, so it is up to the caller to decide what to use.
func WithTimeout ¶
WithTimeout tells the client the max waiting time for the backend to process each metrics batch. If unset, the default will be 10 seconds.
type RetrySettings ¶
type RetrySettings otlpconfig.RetrySettings
RetrySettings defines configuration for retrying batches in case of export failure using an exponential backoff.