otlp

package module
v0.5.0 Latest Latest
Warning

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

Go to latest
Published: May 14, 2020 License: Apache-2.0 Imports: 17 Imported by: 67

README

OpenTelemetry Collector Go Exporter

GoDoc

This exporter converts OpenTelemetry SpanData to OpenTelemetry Protocol Span and exports them to OpenTelemetry Collector.

Installation

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

Documentation

Overview

Package otlp contains an OpenTelemetry tracing exporter for OpenTelemetry Collector.

code in this package is mostly copied from contrib.go.opencensus.io/exporter/ocagent/connection.go

Example (Insecure)
package main

import (
	"context"
	"fmt"
	"log"
	"time"

	"go.opentelemetry.io/otel/api/global"
	"go.opentelemetry.io/otel/exporters/otlp"

	sdktrace "go.opentelemetry.io/otel/sdk/trace"
)

func main() {
	exp, err := otlp.NewExporter(otlp.WithInsecure())
	if err != nil {
		log.Fatalf("Failed to create the collector exporter: %v", err)
	}
	defer func() {
		_ = exp.Stop()
	}()

	tp, _ := sdktrace.NewProvider(
		sdktrace.WithConfig(sdktrace.Config{DefaultSampler: sdktrace.AlwaysSample()}),
		sdktrace.WithBatcher(exp, // add following two options to ensure flush
			sdktrace.WithScheduleDelayMillis(5),
			sdktrace.WithMaxExportBatchSize(10),
		))
	if err != nil {
		log.Fatalf("error creating trace provider: %v\n", err)
	}

	global.SetTraceProvider(tp)

	tracer := global.Tracer("test-tracer")

	// Then use the OpenTelemetry tracing library, like we normally would.
	ctx, span := tracer.Start(context.Background(), "CollectorExporter-Example")
	defer span.End()

	for i := 0; i < 10; i++ {
		_, iSpan := tracer.Start(ctx, fmt.Sprintf("Sample-%d", i))
		<-time.After(6 * time.Millisecond)
		iSpan.End()
	}
}
Output:

Example (WithTLS)
package main

import (
	"context"
	"fmt"
	"log"
	"time"

	"google.golang.org/grpc/credentials"

	"go.opentelemetry.io/otel/api/global"
	"go.opentelemetry.io/otel/exporters/otlp"

	sdktrace "go.opentelemetry.io/otel/sdk/trace"
)

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

	exp, err := otlp.NewExporter(otlp.WithTLSCredentials(creds))
	if err != nil {
		log.Fatalf("failed to create the collector exporter: %v", err)
	}
	defer func() {
		_ = exp.Stop()
	}()

	tp, err := sdktrace.NewProvider(
		sdktrace.WithConfig(sdktrace.Config{DefaultSampler: sdktrace.AlwaysSample()}),
		sdktrace.WithBatcher(exp, // add following two options to ensure flush
			sdktrace.WithScheduleDelayMillis(5),
			sdktrace.WithMaxExportBatchSize(10),
		))
	if err != nil {
		log.Fatalf("error creating trace provider: %v\n", err)
	}

	global.SetTraceProvider(tp)

	tracer := global.Tracer("test-tracer")

	// Then use the OpenTelemetry tracing library, like we normally would.
	ctx, span := tracer.Start(context.Background(), "Securely-Talking-To-Collector-Span")
	defer span.End()

	for i := 0; i < 10; i++ {
		_, iSpan := tracer.Start(ctx, fmt.Sprintf("Sample-%d", i))
		<-time.After(6 * time.Millisecond)
		iSpan.End()
	}
}
Output:

Index

Examples

Constants

View Source
const (
	DefaultCollectorPort uint16 = 55680
	DefaultCollectorHost string = "localhost"
	DefaultNumWorkers    uint   = 1
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Config

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

type Exporter

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

func NewExporter

func NewExporter(opts ...ExporterOption) (*Exporter, error)

func NewUnstartedExporter

func NewUnstartedExporter(opts ...ExporterOption) *Exporter

func (*Exporter) Export

func (e *Exporter) Export(parent context.Context, resource *resource.Resource, cps metricsdk.CheckpointSet) error

Export implements the "go.opentelemetry.io/otel/sdk/export/metric".Exporter interface. It transforms and batches metric Records into OTLP Metrics and transmits them to the configured collector.

func (*Exporter) ExportSpan

func (e *Exporter) ExportSpan(ctx context.Context, sd *tracesdk.SpanData)

func (*Exporter) ExportSpans

func (e *Exporter) ExportSpans(ctx context.Context, sds []*tracesdk.SpanData)

func (*Exporter) Start

func (e *Exporter) Start() error

Start dials to the collector, establishing a connection to it. It also initiates the Config and Trace services by sending over the initial messages that consist of the node identifier. Start invokes a background connector that will reattempt connections to the collector periodically if the connection dies.

func (*Exporter) Stop

func (e *Exporter) Stop() error

Stop shuts down all the connections and resources related to the exporter. If the exporter is not started then this func does nothing.

type ExporterOption

type ExporterOption func(*Config)

func WithAddress

func WithAddress(addr string) ExporterOption

WithAddress allows one to set the address that the exporter will connect to the collector on. If unset, it will instead try to use connect to DefaultCollectorHost:DefaultCollectorPort.

func WithCompressor

func WithCompressor(compressor string) ExporterOption

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 WithGRPCDialOption

func WithGRPCDialOption(opts ...grpc.DialOption) ExporterOption

WithGRPCDialOption 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 WithHeaders

func WithHeaders(headers map[string]string) ExporterOption

WithHeaders will send the provided headers when the gRPC stream connection is instantiated.

func WithInsecure

func WithInsecure() ExporterOption

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

func WithReconnectionPeriod(rp time.Duration) ExporterOption

WithReconnectionPeriod allows one to set the delay between next connection attempt after failing to connect with the collector.

func WithTLSCredentials

func WithTLSCredentials(creds credentials.TransportCredentials) ExporterOption

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 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 WorkerCount

func WorkerCount(n uint) ExporterOption

WorkerCount sets the number of Goroutines to use when processing telemetry.

Directories

Path Synopsis
internal
transform
Package transform provides translations for opentelemetry-go concepts and structures to otlp structures.
Package transform provides translations for opentelemetry-go concepts and structures to otlp structures.
retry Module
otlplog
otlploggrpc Module
otlploghttp Module
otlpmetric module
otlptrace module
otlptracegrpc Module
otlptracehttp Module

Jump to

Keyboard shortcuts

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