mirror of
https://github.com/andwati/zitadel.git
synced 2026-11-04 13:22:16 +00:00
# Which Problems Are Solved Zitadel did not provide easy correlation between errors, logs, traces and metrics. The configuration for those instrumentations was also not consistent, with some supporting different exporters then others. Implementation and parsing of config was also spaghettified over multiple packages, with awkward parsing and inconsistent naming of options. # How the Problems Are Solved All telemetry is now merged under the name "instrumentation". Why? 1. We thought it was a good idea in the past to call the milestone exporter `Telemtry` in the runtime config. Calling this `TelemetryV2` looks weird. 2. Not everything is a meter and not everything is sent (tele...). 3. It's also [defined](https://opentelemetry.io/docs/concepts/instrumentation/) as such by the OTEL documentation. ## New features - Adds structured, context based logging with trace-ID awareness - Static log fields are added to the context, such as service and request path - Static log fields are injected in each logline emitted by the application - Structured logs can also be send to an otel exporter - Structured logs can be printed to StdErr in text and JSON format - Error sinks make sure every error is logged at the correct level: - Warnings for client side errors (HTTP 400 range, Invalid request etc) - Error for server side errors (Internal server errors) - Metrics can now also be send to a OTEL collector. (previously they could only be scraped from `/debug/metrics` with prometheus) ## Exporters This change adds all the exporters supported by OTEL upstream and some google specific exporters for our cloud deployment. - StdOut / StdErr: all instrumentations - OTEL gRPC / HTTP: all instrumentations - Google: all instrumentations except logging - Prometheus (pull-based): only metrics The exception is profiling, which only supports the google exporting due to lack of support by OTEL upstream. ## Configuration and structure - All instrumentation is moved into the new `backend/v3/instrumentation` package. It reuses configuration types, so both code and runtime configuration are easier to understand. - The `internal/telemetry` packages are removed. - Instrumentation is started with a single function and a proper shutdown function is now provided. - Legacy configuration is still parsed from the runtime config, as long as the new configuration is disabled. This allows backporting this feature to v4 without breaking existing configurations. # Additional Changes - Devcontainer: set `$PATH` variable so installed go binaries can be run individually, without NX. - NX: install GCI tool to fix imports # Additional Context - Closes https://github.com/zitadel/zitadel/issues/8408 - Closes https://github.com/zitadel/zitadel/issues/6664 - Backport to v4
87 lines
2.3 KiB
Go
87 lines
2.3 KiB
Go
// Package instrumentation initializes and configures telemetry and logging instrumentation.
|
|
// After [Start] is called, the instrumentation providers can be accessed using global getters.
|
|
package instrumentation
|
|
|
|
import (
|
|
"context"
|
|
"log/slog"
|
|
"net/http"
|
|
"slices"
|
|
"strings"
|
|
"sync"
|
|
|
|
"go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp"
|
|
"go.opentelemetry.io/otel"
|
|
"go.opentelemetry.io/otel/metric"
|
|
"go.opentelemetry.io/otel/sdk/log"
|
|
"go.opentelemetry.io/otel/trace"
|
|
)
|
|
|
|
const Name = "zitadel/backend/v3/instrumentation/tracing"
|
|
|
|
type Config struct {
|
|
ServiceName string
|
|
Trace TraceConfig
|
|
Metric MetricConfig
|
|
Log LogConfig
|
|
Profile ProfileConfig
|
|
}
|
|
|
|
type ShutdownFunc func(context.Context) error
|
|
|
|
var (
|
|
startOnce sync.Once
|
|
shutdown ShutdownFunc
|
|
errStart error
|
|
)
|
|
|
|
// Start initializes global instrumentation once, based on the provided configuration.
|
|
// It is safe to call multiple times; subsequent calls will return the same shutdown function and error as the first call.
|
|
func Start(ctx context.Context, cfg Config) (ShutdownFunc, error) {
|
|
startOnce.Do(func() {
|
|
errStart = startProfiler(cfg.Profile, cfg.ServiceName)
|
|
if errStart != nil {
|
|
return
|
|
}
|
|
var logProvider *log.LoggerProvider
|
|
logProvider, shutdown, errStart = setupOTelSDK(ctx, cfg)
|
|
if errStart != nil {
|
|
return
|
|
}
|
|
setLogger(logProvider, cfg.Log)
|
|
})
|
|
return shutdown, errStart
|
|
}
|
|
|
|
// NewTracer creates a new tracer from the global provider.
|
|
// See [trace.TracerProvider.Tracer] for details.
|
|
func NewTracer(name string, options ...trace.TracerOption) *Tracer {
|
|
provider := otel.GetTracerProvider()
|
|
return &Tracer{provider.Tracer(name, options...)}
|
|
}
|
|
|
|
// NewMeter creates a new meter from the global provider.
|
|
// See [metric.MeterProvider.Meter] for details.
|
|
func NewMeter(name string, options ...metric.MeterOption) *Meter {
|
|
provider := otel.GetMeterProvider()
|
|
return &Meter{
|
|
Meter: provider.Meter(name, options...),
|
|
}
|
|
}
|
|
|
|
// Logger returns the globally configured logger.
|
|
func Logger() *slog.Logger {
|
|
return slog.Default()
|
|
}
|
|
|
|
func RequestFilter(ignoredPrefix ...string) otelhttp.Filter {
|
|
return func(r *http.Request) bool {
|
|
return !slices.ContainsFunc(
|
|
ignoredPrefix,
|
|
func(endpoint string) bool {
|
|
return strings.HasPrefix(r.URL.RequestURI(), endpoint)
|
|
},
|
|
)
|
|
}
|
|
}
|