OpenTelemetry Protocol

Introduction

OpenTelemetry Protocolopen in new window (OTLP) is a general-purpose telemetry data delivery protocol. It defines the encoding of telemetry data and the protocol used to exchange data between the client and the server.

Getting started

  • Step 1. Createopen in new window an Uptrace project to get a DSN, for example, https://<token>@api.uptrace.dev/<project_id>.

DSN

package main

import (
	"context"
	"errors"
	"fmt"
	"os"

	"go.opentelemetry.io/otel"
	"go.opentelemetry.io/otel/attribute"
	"go.opentelemetry.io/otel/exporters/otlp"
	"go.opentelemetry.io/otel/exporters/otlp/otlpgrpc"
	sdktrace "go.opentelemetry.io/otel/sdk/trace"
	"google.golang.org/grpc/credentials"
	_ "google.golang.org/grpc/encoding/gzip"
)

func main() {
	ctx := context.Background()

	// Create credentials using system certificates.
	creds := credentials.NewClientTLSFromCert(nil, "")
	driver := otlpgrpc.NewDriver(
		otlpgrpc.WithEndpoint("otlp.uptrace.dev:4317"),
		otlpgrpc.WithTLSCredentials(creds),
		otlpgrpc.WithHeaders(map[string]string{
			"uptrace-dsn": "<dsn>",
		}),
		otlpgrpc.WithCompressor("gzip"),
	)

	exporter, err := otlp.NewExporter(ctx, driver)
	if err != nil {
		panic(err)
	}

	bsp := sdktrace.NewBatchSpanProcessor(exporter,
		sdktrace.WithMaxQueueSize(1000),
		sdktrace.WithMaxExportBatchSize(1000))
	// Call shutdown to flush the buffers when program exits.
	defer bsp.Shutdown(ctx)

	tracerProvider := sdktrace.NewTracerProvider()
	tracerProvider.RegisterSpanProcessor(bsp)

	// Install our tracer provider and we are done.
	otel.SetTracerProvider(tracerProvider)

	tracer := otel.Tracer("app_or_package_name")
	ctx, span := tracer.Start(ctx, "main")
	defer span.End()

	_, child1 := tracer.Start(ctx, "child1")
	child1.SetAttributes(attribute.String("key1", "value1"))
	child1.RecordError(errors.New("error1"))
	child1.End()

	_, child2 := tracer.Start(ctx, "child2")
	child2.SetAttributes(attribute.Int("key2", 42), attribute.Float64("key3", 123.456))
	child2.End()

	fmt.Println("trace id:", span.SpanContext().TraceID())
}
go run main.go
trace id: <trace_id>

By now, you should have a fully configured OpenTelemetry distribution. Next, learn more about OpenTelemetry API and check available instrumentationsopen in new window.