Categorygithub.com/bufbuild/connect-opentelemetry-go
modulepackage
0.4.0
Repository: https://github.com/bufbuild/connect-opentelemetry-go.git
Documentation: pkg.go.dev

# README

connect-opentelemetry-go

Build Report Card GoDoc

connect-opentelemetry-go adds support for OpenTelemetry tracing and metrics collection to connect-go servers and clients.

For more on Connect, OpenTelemetry, and otelconnect, see the Connect announcement blog post and the observability documentation on connect.build.

An example

package main

import (
	"context"
	"fmt"
	"log"
	"net/http"

	connect "github.com/bufbuild/connect-go"
	otelconnect "github.com/bufbuild/connect-opentelemetry-go"
	// Generated from your protobuf schema by protoc-gen-go and
	// protoc-gen-connect-go.
	pingv1 "github.com/bufbuild/connect-opentelemetry-go/internal/gen/observability/ping/v1"
	"github.com/bufbuild/connect-opentelemetry-go/internal/gen/observability/ping/v1/pingv1connect"
)

func main() {
	mux := http.NewServeMux()

	// otelconnect.NewInterceptor provides an interceptor that adds tracing and
	// metrics to both clients and handlers. By default, it uses OpenTelemetry's
	// global TracerProvider and MeterProvider, which you can configure by
	// following the OpenTelemetry documentation. If you'd prefer to avoid
	// globals, use otelconnect.WithTracerProvider and
	// otelconnect.WithMeterProvider.
	mux.Handle(pingv1connect.NewPingServiceHandler(
		&pingv1connect.UnimplementedPingServiceHandler{},
		connect.WithInterceptors(otelconnect.NewInterceptor()),
	))

	http.ListenAndServe("localhost:8080", mux)
}

func makeRequest() {
	client := pingv1connect.NewPingServiceClient(
		http.DefaultClient,
		"http://localhost:8080",
		connect.WithInterceptors(otelconnect.NewInterceptor()),
	)
	resp, err := client.Ping(
		context.Background(),
		connect.NewRequest(&pingv1.PingRequest{}),
	)
	if err != nil {
		log.Fatal(err)
	}
	fmt.Println(resp)
}

Configuration for internal services

By default, instrumented servers are conservative and behave as though they're internet-facing. They don't trust any tracing information sent by the client, and will create new trace spans for each request. The new spans are linked to the remote span for reference (using OpenTelemetry's trace.Link), but tracing UIs will display the request as a new top-level transaction.

If your server is deployed as an internal service, configure otelconnect to trust the client's tracing information using otelconnect.WithTrustRemote. With this option, servers will create child spans for each request.

Reducing metrics and tracing cardinality

By default, the OpenTelemetry RPC conventions produce high-cardinality server-side metric and tracing output. In particular, servers tag all metrics and trace data with the server's IP address and the remote port number. To drop these attributes, use otelconnect.WithoutServerPeerAttributes. For more customizable attribute filtering, use otelconnect.WithFilter.

Status

UnaryStreaming ClientStreaming Handler
Metrics
Tracing

Ecosystem

Support and Versioning

connect-opentelemetry-go supports:

Legal

Offered under the Apache 2 license.

# Functions

NewInterceptor constructs and returns an Interceptor which implements [connect.Interceptor] that adds OpenTelemetry metrics and tracing to Connect handlers and clients.
WithAttributeFilter sets the attribute filter for all metrics and trace attributes.
WithFilter configures the instrumentation to emit traces and metrics only when the filter function returns true.
WithMeterProvider configures the instrumentation to use the supplied [metric.MeterProvider] when extracting and injecting trace context.
WithoutMetrics disables metrics.
WithoutServerPeerAttributes removes net.peer.port and net.peer.name attributes from server trace and span attributes.
WithoutTraceEvents disables trace events for both unary and streaming interceptors.
WithoutTracing disables tracing.
WithPropagator configures the instrumentation to use the supplied propagator when extracting and injecting trace context.
WithTraceRequestHeader enables header attributes for the request header keys provided.
WithTraceResponseHeader enables header attributes for the response header keys provided.
WithTracerProvider configures the instrumentation to use the supplied provider when creating a tracer.
WithTrustRemote sets the Interceptor to trust remote spans.

# Structs

Interceptor implements [connect.Interceptor] that adds OpenTelemetry metrics and tracing to connect handlers and clients.
Request is the information about each RPC available to filter functions.

# Interfaces

An Option configures the OpenTelemetry instrumentation.

# Type aliases

AttributeFilter is used to filter attributes out based on the [Request] and [attribute.KeyValue].