otlptracehttp

package module
v1.15.0 Latest Latest
Warning

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

Go to latest
Published: Apr 28, 2023 License: Apache-2.0 Imports: 21 Imported by: 500

Documentation

Overview

Package otlptracehttp a client that sends traces to the collector using HTTP with binary protobuf payloads.

Example
package main

import (
	"context"
	"fmt"
	"log"

	"go.opentelemetry.io/otel"
	"go.opentelemetry.io/otel/exporters/otlp/otlptrace"
	"go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracehttp"
	"go.opentelemetry.io/otel/sdk/resource"
	sdktrace "go.opentelemetry.io/otel/sdk/trace"
	semconv "go.opentelemetry.io/otel/semconv/v1.17.0"
	"go.opentelemetry.io/otel/trace"
)

const (
	instrumentationName    = "github.com/instrumentron"
	instrumentationVersion = "v0.1.0"
)

var (
	tracer = otel.GetTracerProvider().Tracer(
		instrumentationName,
		trace.WithInstrumentationVersion(instrumentationVersion),
		trace.WithSchemaURL(semconv.SchemaURL),
	)
)

func add(ctx context.Context, x, y int64) int64 {
	var span trace.Span
	_, span = tracer.Start(ctx, "Addition")
	defer span.End()

	return x + y
}

func multiply(ctx context.Context, x, y int64) int64 {
	var span trace.Span
	_, span = tracer.Start(ctx, "Multiplication")
	defer span.End()

	return x * y
}

func newResource() *resource.Resource {
	return resource.NewWithAttributes(
		semconv.SchemaURL,
		semconv.ServiceName("otlptrace-example"),
		semconv.ServiceVersion("0.0.1"),
	)
}

func installExportPipeline(ctx context.Context) (func(context.Context) error, error) {
	client := otlptracehttp.NewClient()
	exporter, err := otlptrace.New(ctx, client)
	if err != nil {
		return nil, fmt.Errorf("creating OTLP trace exporter: %w", err)
	}

	tracerProvider := sdktrace.NewTracerProvider(
		sdktrace.WithBatcher(exporter),
		sdktrace.WithResource(newResource()),
	)
	otel.SetTracerProvider(tracerProvider)

	return tracerProvider.Shutdown, nil
}

func main() {
	ctx := context.Background()
	// Registers a tracer Provider globally.
	shutdown, err := installExportPipeline(ctx)
	if err != nil {
		log.Fatal(err)
	}
	defer func() {
		if err := shutdown(ctx); err != nil {
			log.Fatal(err)
		}
	}()

	log.Println("the answer is", add(ctx, multiply(ctx, multiply(ctx, 2, 2), 10), 2))
}
Output:

Index

Examples

Constants

View Source
const (
	// NoCompression tells the driver to send payloads without
	// compression.
	NoCompression = Compression(otlpconfig.NoCompression)
	// GzipCompression tells the driver to send payloads after
	// compressing them with gzip.
	GzipCompression = Compression(otlpconfig.GzipCompression)
)

Variables

This section is empty.

Functions

func New

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

New constructs a new Exporter and starts it.

func NewClient

func NewClient(opts ...Option) otlptrace.Client

NewClient creates a new HTTP trace client.

func NewUnstarted

func NewUnstarted(opts ...Option) *otlptrace.Exporter

NewUnstarted constructs a new Exporter and does not start it.

Types

type Compression

type Compression otlpconfig.Compression

Compression describes the compression used for payloads sent to the collector.

type Option

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

Option applies an option to the HTTP client.

func WithCompression

func WithCompression(compression Compression) Option

WithCompression tells the driver to compress the sent data.

func WithEndpoint

func WithEndpoint(endpoint string) Option

WithEndpoint allows one to set the address of the collector endpoint that the driver will use to send spans. If unset, it will instead try to use the default endpoint (localhost:4318). Note that the endpoint must not contain any URL path.

func WithHeaders

func WithHeaders(headers map[string]string) Option

WithHeaders allows one to tell the driver to send additional HTTP headers with the payloads. Specifying headers like Content-Length, Content-Encoding and Content-Type may result in a broken driver.

func WithInsecure

func WithInsecure() Option

WithInsecure tells the driver to connect to the collector using the HTTP scheme, instead of HTTPS.

func WithRetry

func WithRetry(rc RetryConfig) Option

WithRetry configures the retry policy for transient errors that may occurs when exporting traces. 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 WithTLSClientConfig

func WithTLSClientConfig(tlsCfg *tls.Config) Option

WithTLSClientConfig can be used to set up a custom TLS configuration for the client used to send payloads to the collector. Use it if you want to use a custom certificate.

func WithTimeout

func WithTimeout(duration time.Duration) Option

WithTimeout tells the driver the max waiting time for the backend to process each spans batch. If unset, the default will be 10 seconds.

func WithURLPath

func WithURLPath(urlPath string) Option

WithURLPath allows one to override the default URL path used for sending traces. If unset, default ("/v1/traces") will be used.

type RetryConfig

type RetryConfig retry.Config

RetryConfig defines configuration for retrying batches in case of export failure using an exponential backoff.

Jump to

Keyboard shortcuts

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