Categorygithub.com/grafana/otel-profiling-go
modulepackage
0.5.1
Repository: https://github.com/grafana/otel-profiling-go.git
Documentation: pkg.go.dev

# README

Profiling Instrumentation for OpenTelemetry Go SDK

NOTE: This is an experimental package -- and will be officially supported in future versions of Pyroscope

The package provides means to integrate tracing with profiling. More specifically, a TracerProvider implementation, that annotates profiling data with span IDs: when a new trace span emerges, the tracer adds a span_id pprof tag that points to the span. This makes it possible to filter out a profile of a particular trace span in Pyroscope.

Note that the module does not control pprof profiler itself – it still needs to be started for profiles to be collected. This can be done either via runtime/pprof package, or using the Pyroscope client.

By default, only the root span gets labeled (the first span created locally): such spans are marked with the pyroscope.profile.id attribute set to the span ID. Please note that presence of the attribute does not necessarily indicate that the span has a profile: stack trace samples might not be collected, if the utilized CPU time is less than the sample interval (10ms).

Limitations:

  • Only CPU profiling is fully supported at the moment.

Trace spans profiles

To start profiling trace spans, you need to include our go module in your app:

go get github.com/grafana/otel-profiling-go

Then add the pyroscope tracer provider:

package main

import (
	otelpyroscope "github.com/grafana/otel-profiling-go"
	"github.com/grafana/pyroscope-go"
)

func main() {
	// Initialize your tracer provider as usual.
	tp := initTracer()

	// Wrap it with otelpyroscope tracer provider.
	otel.SetTracerProvider(otelpyroscope.NewTracerProvider(tp))

	// If you're using Pyroscope Go SDK, initialize pyroscope profiler.
	_, _ = pyroscope.Start(pyroscope.Config{
		ApplicationName: "my-service",
		ServerAddress:   "http://localhost:4040",
	})

	// Your code goes here.
}

Tracing integration is supported in pull mode as well: if you scrape profiles using Grafana Agent, you should make sure that the pyroscope service_name label matches service.name attribute specified in the OTel SDK configuration. Please refer to the Grafana Agent documentation to learn more.

Example

You can find a complete example setup with Grafana Tempo in the Pyroscope repository.

image

# Functions

NewTracerProvider creates a new tracer provider that annotates pprof samples with span_id label.
WithAddSpanName specifies whether the current span name should be added to the profile labels.
WithAppName specifies the profiled application name.
WithDefaultProfileURLBuilder specifies the default profile URL builder.
WithProfileBaselineLabels provides a map of extra labels to be added to the baseline query alongside with pprof labels set in runtime.
WithProfileBaselineURL specifies whether to add the pyroscope.profile.baseline.url attribute with the URL to the baseline profile.
WithProfileURL specifies whether to add the pyroscope.profile.url attribute with the URL to the span profile.
WithProfileURLBuilder specifies how profile URL is to be built.
WithPyroscopeURL provides a base URL for the profile and baseline URLs.
WithRootSpanOnly indicates that only the root span is to be profiled.

# Structs

Config describes tracer configuration.

# Type aliases

No description provided by the author