otelconnect

package module
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: May 2, 2023 License: Apache-2.0 Imports: 21 Imported by: 67

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 connectrpc.com.

An example

package main

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

	"connectrpc.com/connect"
	otelconnect "connectrpc.com/otelconnect"
	// Generated from your protobuf schema by protoc-gen-go and
	// protoc-gen-connect-go.
	pingv1 "connectrpc.com/otelconnect/internal/gen/observability/ping/v1"
	"connectrpc.com/otelconnect/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

connect-opentelemetry-go is available as a beta release.

Unary Streaming Client Streaming Handler
Metrics
Tracing

Users of this package should expect breaking changes as the underlying OpenTelemetry APIs change. Once the Go OpenTelemetry metrics SDK stabilizes, we'll release a stable v1 of this package.

Ecosystem

Support and Versioning

connect-opentelemetry-go supports:

  • The two most recent major releases of Go.
  • v1 of the go.opentelemetry.io/otel tracing SDK.
  • The current alpha release of the go.opentelemetry.io/otel metrics SDK.

It's not yet stable. We take every effort to maintain backward compatibility, but can't commit to a stable v1 until the OpenTelemetry APIs are stable.

Offered under the Apache 2 license.

Documentation

Overview

Package otelconnect provides OpenTelemetry tracing and metrics for connectrpc.com/connect 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. This is recommended to use as net.peer server attributes have high cardinality.

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