Documentation
¶
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func InitGrafana ¶
InitGrafana initializes the global tracer and metric providers for OpenTelemetry, configured to send telemetry data to Grafana Cloud or any compatible OTLP endpoint.
It sets up: - Distributed tracing using the OTLP HTTP exporter - Metrics collection via OTLP HTTP exporter - Runtime metrics for Go applications (memory, GC, goroutines, etc.) - Custom application metrics defined in the metrics package
The function registers all necessary shutdown handlers with the provided shutdowns instance. These handlers will be called during application termination to ensure proper cleanup.
Example:
shutdowns := shutdown.New()
err := otel.InitGrafana(ctx, otel.Config{
GrafanaEndpoint: "https://otlp-gateway-prod-us-east-0.grafana.net/otlp",
Application: "unkey-api",
Version: version.Version,
}, shutdowns)
if err != nil {
log.Fatalf("Failed to initialize telemetry: %v", err)
}
// Later during shutdown:
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
defer cancel()
errs := shutdowns.Shutdown(ctx)
for _, err := range errs {
log.Printf("Shutdown error: %v", err)
}
func NewSpanName ¶
NewSpanName creates a standardized span name by joining package and method names. This ensures consistent span naming across the application, making it easier to filter and analyze traces.
The format is "{package}.{method}", e.g., "db.query" or "api.verify_key".
Parameters:
- pkg: The package or module name
- method: The method or function name
Example:
// In a database query function
ctx, span := tracing.Start(ctx, otel.NewSpanName("db", "findUserByID"))
defer span.End()
// In an API handler
ctx, span := tracing.Start(ctx, otel.NewSpanName("api", "verifyKey"))
defer span.End()
func RecordError ¶
RecordError marks a span as having encountered an error. If the error is nil, this function does nothing. This should be called whenever an error occurs within a traced operation to ensure that errors are properly recorded in the tracing system.
Example:
ctx, span := tracing.Start(ctx, "database.Query")
defer span.End()
result, err := db.Query(ctx, "SELECT * FROM users WHERE id = ?", userID)
if err != nil {
otel.RecordError(span, err)
return nil, err
}
Types ¶
type Config ¶
type Config struct {
// InstanceID is a unique identifier for the current service instance,
// used to distinguish between multiple instances of the same service.
InstanceID string
// CloudRegion indicates the geographic region where this service instance is running,
// which helps with identifying regional performance patterns or issues.
CloudRegion string
// Application is the name of your application, used to identify the source of telemetry data.
// This appears in Grafana dashboards and alerts.
Application string
// Version is the current version of your application, allowing you to correlate
// behavior changes with specific releases.
Version string
// TraceSampleRate controls what percentage of traces are sampled.
// Values range from 0.0 to 1.0, where:
// - 1.0 means all traces are sampled (100%)
// - 0.25 means 25% of traces are sampled (the default if not specified)
// - 0.0 means no traces are sampled (0%)
//
// As long as the sampling rate is greater than 0.0, all errors will be sampled.
TraceSampleRate float64
}
Config defines the configuration settings for OpenTelemetry integration with Grafana. It specifies connection details and application metadata needed for proper telemetry.
Directories
¶
| Path | Synopsis |
|---|---|
|
Package logging provides a flexible structured logging interface that follows the patterns of Go's standard slog package while abstracting over concrete logging implementations.
|
Package logging provides a flexible structured logging interface that follows the patterns of Go's standard slog package while abstracting over concrete logging implementations. |