otelconnect

package module
v0.4.0 Latest Latest
Warning

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

Go to latest
Published: Jul 7, 2023 License: Apache-2.0 Imports: 21 Imported by: 9

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

Unary Streaming Client Streaming Handler
Metrics
Tracing

Ecosystem

Support and Versioning

connect-opentelemetry-go supports:

Offered under the Apache 2 license.

Documentation

Overview

Package otelconnect provides OpenTelemetry tracing and metrics for github.com/bufbuild/connect-go servers and clients. The specification followed was the OpenTelemetry specification with both the rpc metrics specification and rpc trace specification implemented.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type AttributeFilter

type AttributeFilter func(*Request, attribute.KeyValue) bool

AttributeFilter is used to filter attributes out based on the Request and attribute.KeyValue. If the filter returns true the attribute will be kept else it will be removed. AttributeFilter must be safe to call concurrently.

type Interceptor

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

Interceptor implements connect.Interceptor that adds OpenTelemetry metrics and tracing to connect handlers and clients.

func NewInterceptor

func NewInterceptor(options ...Option) *Interceptor

NewInterceptor constructs and returns an Interceptor which implements connect.Interceptor that adds OpenTelemetry metrics and tracing to Connect handlers and clients.

func (*Interceptor) WrapStreamingClient

func (i *Interceptor) WrapStreamingClient(next connect.StreamingClientFunc) connect.StreamingClientFunc

WrapStreamingClient implements otel tracing and metrics for streaming connect clients.

func (*Interceptor) WrapStreamingHandler

WrapStreamingHandler implements otel tracing and metrics for streaming connect handlers.

func (*Interceptor) WrapUnary

func (i *Interceptor) WrapUnary(next connect.UnaryFunc) connect.UnaryFunc

WrapUnary implements otel tracing and metrics for unary handlers.

type Option

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

An Option configures the OpenTelemetry instrumentation.

func WithAttributeFilter

func WithAttributeFilter(filter AttributeFilter) Option

WithAttributeFilter sets the attribute filter for all metrics and trace attributes.

func WithFilter

func WithFilter(filter func(context.Context, *Request) bool) Option

WithFilter configures the instrumentation to emit traces and metrics only when the filter function returns true. Filter functions must be safe to call concurrently.

func WithMeterProvider

func WithMeterProvider(provider metric.MeterProvider) Option

WithMeterProvider configures the instrumentation to use the supplied metric.MeterProvider when extracting and injecting trace context. By default, the instrumentation uses global.MeterProvider().

func WithPropagator

func WithPropagator(propagator propagation.TextMapPropagator) Option

WithPropagator configures the instrumentation to use the supplied propagator when extracting and injecting trace context. By default, the instrumentation uses otel.GetTextMapPropagator().

func WithTraceRequestHeader

func WithTraceRequestHeader(keys ...string) Option

WithTraceRequestHeader enables header attributes for the request header keys provided. Attributes will be added as Trace attributes only.

func WithTraceResponseHeader

func WithTraceResponseHeader(keys ...string) Option

WithTraceResponseHeader enables header attributes for the response header keys provided. Attributes will be added as Trace attributes only.

func WithTracerProvider

func WithTracerProvider(provider trace.TracerProvider) Option

WithTracerProvider configures the instrumentation to use the supplied provider when creating a tracer. By default, the instrumentation uses otel.GetTracerProvider().

func WithTrustRemote

func WithTrustRemote() Option

WithTrustRemote sets the Interceptor to trust remote spans. By default, all incoming server spans are untrusted and will be linked with a trace.Link and will not be a child span. By default, all client spans are trusted and no change occurs when WithTrustRemote is used.

func WithoutMetrics

func WithoutMetrics() Option

WithoutMetrics disables metrics.

func WithoutServerPeerAttributes

func WithoutServerPeerAttributes() Option

WithoutServerPeerAttributes removes net.peer.port and net.peer.name attributes from server trace and span attributes. The default behavior follows the OpenTelemetry semantic conventions for RPC, but produces very high-cardinality data; this option significantly reduces cardinality in most environments.

func WithoutTraceEvents added in v0.4.0

func WithoutTraceEvents() Option

WithoutTraceEvents disables trace events for both unary and streaming interceptors. This reduces the quantity of data sent to your tracing system by omitting per-message information like message size.

func WithoutTracing

func WithoutTracing() Option

WithoutTracing disables tracing.

type Request

type Request struct {
	Spec   connect.Spec
	Peer   connect.Peer
	Header http.Header
}

Request is the information about each RPC available to filter functions. It contains the common subset of connect.AnyRequest, connect.StreamingClientConn, and connect.StreamingHandlerConn.

Directories

Path Synopsis
internal

Jump to

Keyboard shortcuts

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