1
0
mirror of https://github.com/open-telemetry/opentelemetry-go.git synced 2024-12-12 10:04:29 +02:00
opentelemetry-go/exporters/otlp
Krzesimir Nowak 8d80981465
Move gRPC driver to a subpackage and add an HTTP driver (#1420)
* Move grpc stuff to separate package

* Drop duplicated retryable status code

* Set default port to 4317

This is what the specification says for both gRPC and HTTP.

* Document gRPC option type

* Add an HTTP protocol driver for OTLP exporter

Currently it supports only binary protobuf payloads.

* Move end to end test to a separate package

It also adds some common code mock collectors can use. This will be
useful for testing the HTTP driver.

* Move export data creators to otlptest

It also extends the one record checkpointer a bit. This will be useful
for testing the HTTP driver.

* Add an HTTP mock collector and tests for HTTP driver

* Update changelog

* Do not depend on DefaultTransport

We create our own instance of the transport, which is based on
golang's DefaultTransport. That way we sidestep the issue of the
DefaultTransport being modified/overwritten. We won't have any panics
at init. The cost of it is to keep the transport fields in sync with
DefaultTransport.

* Read the whole response body before closing it

This may help with connection reuse.

* Change options to conform to our style guide

* Add jitter to backoff time

* Test TLS option

* Test extra headers

* Fix a comment

* Increase coverage

* Add a source of the backoff strategy
2021-01-11 22:55:24 -05:00
..
internal Move gRPC driver to a subpackage and add an HTTP driver (#1420) 2021-01-11 22:55:24 -05:00
otlpgrpc Move gRPC driver to a subpackage and add an HTTP driver (#1420) 2021-01-11 22:55:24 -05:00
otlphttp Move gRPC driver to a subpackage and add an HTTP driver (#1420) 2021-01-11 22:55:24 -05:00
doc.go Update README and documentation to better communicate pre-GA state (#1281) 2020-10-29 09:23:13 -07:00
go.mod Bump google.golang.org/grpc from 1.32.0 to 1.34.0 in /exporters/otlp (#1396) 2020-12-14 08:11:22 -08:00
go.sum Bump google.golang.org/grpc from 1.32.0 to 1.34.0 in /exporters/otlp (#1396) 2020-12-14 08:11:22 -08:00
options.go Move gRPC driver to a subpackage and add an HTTP driver (#1420) 2021-01-11 22:55:24 -05:00
otlp_metric_test.go Add a split protocol driver for otlp exporter (#1418) 2020-12-22 09:21:45 -08:00
otlp_span_test.go Add a split protocol driver for otlp exporter (#1418) 2020-12-22 09:21:45 -08:00
otlp_test.go Move gRPC driver to a subpackage and add an HTTP driver (#1420) 2021-01-11 22:55:24 -05:00
otlp.go Move gRPC driver to a subpackage and add an HTTP driver (#1420) 2021-01-11 22:55:24 -05:00
protocoldriver.go Add a split protocol driver for otlp exporter (#1418) 2020-12-22 09:21:45 -08:00
README.md Update exporters/otlp Readme.md (#1441) 2021-01-07 11:45:35 -08:00

OpenTelemetry Collector Go Exporter

PkgGoDev

This exporter exports OpenTelemetry spans and metrics to the OpenTelemetry Collector.

Installation and Setup

The exporter can be installed using standard go functionality.

$ go get -u go.opentelemetry.io/otel/exporters/otlp

A new exporter can be created using the NewExporter function.

package main

import (
	"context"
	"log"

	"go.opentelemetry.io/otel/sdk/metric/controller/push"
	"go.opentelemetry.io/otel/exporters/otlp"
	processor "go.opentelemetry.io/otel/sdk/metric/processor/basic"
	"go.opentelemetry.io/otel/sdk/metric/selector/simple"
	metricsdk "go.opentelemetry.io/otel/sdk/export/metric"
	sdktrace "go.opentelemetry.io/otel/sdk/trace"
)

func main() {
	ctx := context.Background()
	exporter, err := otlp.NewExporter(ctx) // Configure as needed.
	if err != nil {
		log.Fatalf("failed to create exporter: %v", err)
	}
	defer func() {
		err := exporter.Shutdown(ctx)
		if err != nil {
			log.Fatalf("failed to stop exporter: %v", err)
		}
	}()

	// Note: The exporter can also be used as a Batcher. E.g.
	//   tracerProvider := sdktrace.NewTracerProvider(
	//   	sdktrace.WithBatcher(exporter,
	//   		sdktrace.WithBatchTimeout(time.Second*15),
	//   		sdktrace.WithMaxExportBatchSize(100),
	//   	),
	//   )
	tracerProvider := sdktrace.NewTracerProvider(sdktrace.WithBatcher(exporter))
	processor := processor.New(simple.NewWithInexpensiveDistribution(), metricsdk.StatelessExportKindSelector())
	pusher := push.New(processor, exporter)
	pusher.Start()
	metricProvider := pusher.MeterProvider()

	// Your code here ...
}

Configuration

Configurations options can be specified when creating a new exporter (NewExporter).

WorkerCount(n uint)

Sets the number of Goroutines to use when processing telemetry.

WithInsecure()

Disables client transport security for the exporter's gRPC connection just like grpc.WithInsecure() does. By default, client security is required unless WithInsecure is used.

WithAddress(addr string)

Sets the address that the exporter will connect to the collector on. The default address the exporter connects to is localhost:55680.

WithReconnectionPeriod(rp time.Duration)

Set the delay between connection attempts after failing to connect with the collector.

WithCompressor(compressor string)

Set the compressor for the gRPC client to use when sending requests. The compressor used needs to have been registered with google.golang.org/grpc/encoding prior to using here. 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".

WithHeaders(headers map[string]string)

Headers to send with gRPC requests.

WithTLSCredentials(creds "google.golang.org/grpc/credentials".TransportCredentials)

TLS credentials to use when talking to the server.

WithGRPCServiceConfig(serviceConfig string)

The default gRPC service config used when .

By default, the exporter is configured to support retries.

{
	"methodConfig":[{
		"name":[
			{ "service":"opentelemetry.proto.collector.metrics.v1.MetricsService" },
			{ "service":"opentelemetry.proto.collector.trace.v1.TraceService" }
		],
		"waitForReady": true,
		"retryPolicy":{
			"MaxAttempts":5,
			"InitialBackoff":"0.3s",
			"MaxBackoff":"5s",
			"BackoffMultiplier":2,
			"RetryableStatusCodes":[
				"UNAVAILABLE",
				"CANCELLED",
				"DEADLINE_EXCEEDED",
				"RESOURCE_EXHAUSTED",
				"ABORTED",
				"OUT_OF_RANGE",
				"UNAVAILABLE",
				"DATA_LOSS"
			]
		}
	}]
}

WithGRPCDialOption(opts ..."google.golang.org/grpc".DialOption)

Additional grpc.DialOption to be used.

These options take precedence over any other set by other parts of the configuration.

Retries

The exporter will not, by default, retry failed requests to the collector. However, it is configured in a way that it can easily be enable.

To enable retries, the GRPC_GO_RETRY environment variable needs to be set to on. For example,

GRPC_GO_RETRY=on go run .

The default service config used by default is defined to retry failed requests with exponential backoff (0.3seconds * (2)^retry) with a max of 5 retries).

These retries are only attempted for reponses that are deemed "retry-able" by the collector.