2024-01-30 15:28:05 +00:00
|
|
|
package middleware
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"io"
|
|
|
|
"net/http"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/containous/alice"
|
|
|
|
"github.com/rs/zerolog/log"
|
|
|
|
"github.com/traefik/traefik/v3/pkg/config/static"
|
|
|
|
"github.com/traefik/traefik/v3/pkg/logs"
|
|
|
|
"github.com/traefik/traefik/v3/pkg/metrics"
|
|
|
|
"github.com/traefik/traefik/v3/pkg/middlewares/accesslog"
|
|
|
|
"github.com/traefik/traefik/v3/pkg/middlewares/capture"
|
|
|
|
metricsMiddle "github.com/traefik/traefik/v3/pkg/middlewares/metrics"
|
2024-03-12 08:48:04 +00:00
|
|
|
"github.com/traefik/traefik/v3/pkg/middlewares/observability"
|
2024-03-11 10:50:04 +00:00
|
|
|
"github.com/traefik/traefik/v3/pkg/tracing"
|
2024-01-30 15:28:05 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// ObservabilityMgr is a manager for observability (AccessLogs, Metrics and Tracing) enablement.
|
|
|
|
type ObservabilityMgr struct {
|
|
|
|
config static.Configuration
|
|
|
|
accessLoggerMiddleware *accesslog.Handler
|
|
|
|
metricsRegistry metrics.Registry
|
2024-03-12 08:48:04 +00:00
|
|
|
semConvMetricRegistry *metrics.SemConvMetricsRegistry
|
2024-03-11 10:50:04 +00:00
|
|
|
tracer *tracing.Tracer
|
2024-01-30 15:28:05 +00:00
|
|
|
tracerCloser io.Closer
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewObservabilityMgr creates a new ObservabilityMgr.
|
2024-03-12 08:48:04 +00:00
|
|
|
func NewObservabilityMgr(config static.Configuration, metricsRegistry metrics.Registry, semConvMetricRegistry *metrics.SemConvMetricsRegistry, accessLoggerMiddleware *accesslog.Handler, tracer *tracing.Tracer, tracerCloser io.Closer) *ObservabilityMgr {
|
2024-01-30 15:28:05 +00:00
|
|
|
return &ObservabilityMgr{
|
|
|
|
config: config,
|
|
|
|
metricsRegistry: metricsRegistry,
|
2024-03-12 08:48:04 +00:00
|
|
|
semConvMetricRegistry: semConvMetricRegistry,
|
2024-01-30 15:28:05 +00:00
|
|
|
accessLoggerMiddleware: accessLoggerMiddleware,
|
|
|
|
tracer: tracer,
|
|
|
|
tracerCloser: tracerCloser,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// BuildEPChain an observability middleware chain by entry point.
|
2024-03-12 08:48:04 +00:00
|
|
|
func (o *ObservabilityMgr) BuildEPChain(ctx context.Context, entryPointName string, resourceName string) alice.Chain {
|
2024-01-30 15:28:05 +00:00
|
|
|
chain := alice.New()
|
|
|
|
|
2024-03-12 08:48:04 +00:00
|
|
|
if o == nil {
|
2024-01-30 15:28:05 +00:00
|
|
|
return chain
|
|
|
|
}
|
|
|
|
|
2024-03-12 08:48:04 +00:00
|
|
|
if o.accessLoggerMiddleware != nil || o.metricsRegistry != nil && (o.metricsRegistry.IsEpEnabled() || o.metricsRegistry.IsRouterEnabled() || o.metricsRegistry.IsSvcEnabled()) {
|
|
|
|
if o.ShouldAddAccessLogs(resourceName) || o.ShouldAddMetrics(resourceName) {
|
2024-01-30 15:28:05 +00:00
|
|
|
chain = chain.Append(capture.Wrap)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-12 08:48:04 +00:00
|
|
|
if o.accessLoggerMiddleware != nil && o.ShouldAddAccessLogs(resourceName) {
|
|
|
|
chain = chain.Append(accesslog.WrapHandler(o.accessLoggerMiddleware))
|
2024-01-30 15:28:05 +00:00
|
|
|
chain = chain.Append(func(next http.Handler) (http.Handler, error) {
|
|
|
|
return accesslog.NewFieldHandler(next, logs.EntryPointName, entryPointName, accesslog.InitServiceFields), nil
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2024-03-12 08:48:04 +00:00
|
|
|
if (o.tracer != nil && o.ShouldAddTracing(resourceName)) || (o.metricsRegistry != nil && o.metricsRegistry.IsEpEnabled() && o.ShouldAddMetrics(resourceName)) {
|
|
|
|
chain = chain.Append(observability.WrapEntryPointHandler(ctx, o.tracer, o.semConvMetricRegistry, entryPointName))
|
2024-01-30 15:28:05 +00:00
|
|
|
}
|
|
|
|
|
2024-03-12 08:48:04 +00:00
|
|
|
if o.metricsRegistry != nil && o.metricsRegistry.IsEpEnabled() && o.ShouldAddMetrics(resourceName) {
|
|
|
|
metricsHandler := metricsMiddle.WrapEntryPointHandler(ctx, o.metricsRegistry, entryPointName)
|
2024-01-30 15:28:05 +00:00
|
|
|
|
2024-03-12 08:48:04 +00:00
|
|
|
if o.tracer != nil && o.ShouldAddTracing(resourceName) {
|
|
|
|
chain = chain.Append(observability.WrapMiddleware(ctx, metricsHandler))
|
2024-01-30 15:28:05 +00:00
|
|
|
} else {
|
|
|
|
chain = chain.Append(metricsHandler)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return chain
|
|
|
|
}
|
|
|
|
|
|
|
|
// ShouldAddAccessLogs returns whether the access logs should be enabled for the given resource.
|
2024-03-12 08:48:04 +00:00
|
|
|
func (o *ObservabilityMgr) ShouldAddAccessLogs(resourceName string) bool {
|
|
|
|
if o == nil {
|
2024-01-30 15:28:05 +00:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2024-03-12 08:48:04 +00:00
|
|
|
return o.config.AccessLog != nil && (o.config.AccessLog.AddInternals || !strings.HasSuffix(resourceName, "@internal"))
|
2024-01-30 15:28:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ShouldAddMetrics returns whether the metrics should be enabled for the given resource.
|
2024-03-12 08:48:04 +00:00
|
|
|
func (o *ObservabilityMgr) ShouldAddMetrics(resourceName string) bool {
|
|
|
|
if o == nil {
|
2024-01-30 15:28:05 +00:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2024-03-12 08:48:04 +00:00
|
|
|
return o.config.Metrics != nil && (o.config.Metrics.AddInternals || !strings.HasSuffix(resourceName, "@internal"))
|
2024-01-30 15:28:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ShouldAddTracing returns whether the tracing should be enabled for the given resource.
|
2024-03-12 08:48:04 +00:00
|
|
|
func (o *ObservabilityMgr) ShouldAddTracing(resourceName string) bool {
|
|
|
|
if o == nil {
|
2024-01-30 15:28:05 +00:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2024-03-12 08:48:04 +00:00
|
|
|
return o.config.Tracing != nil && (o.config.Tracing.AddInternals || !strings.HasSuffix(resourceName, "@internal"))
|
2024-01-30 15:28:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// MetricsRegistry is an accessor to the metrics registry.
|
2024-03-12 08:48:04 +00:00
|
|
|
func (o *ObservabilityMgr) MetricsRegistry() metrics.Registry {
|
|
|
|
if o == nil {
|
2024-01-30 15:28:05 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-03-12 08:48:04 +00:00
|
|
|
return o.metricsRegistry
|
|
|
|
}
|
|
|
|
|
|
|
|
// SemConvMetricsRegistry is an accessor to the semantic conventions metrics registry.
|
|
|
|
func (o *ObservabilityMgr) SemConvMetricsRegistry() *metrics.SemConvMetricsRegistry {
|
|
|
|
if o == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return o.semConvMetricRegistry
|
2024-01-30 15:28:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Close closes the accessLogger and tracer.
|
2024-03-12 08:48:04 +00:00
|
|
|
func (o *ObservabilityMgr) Close() {
|
|
|
|
if o == nil {
|
2024-01-30 15:28:05 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-03-12 08:48:04 +00:00
|
|
|
if o.accessLoggerMiddleware != nil {
|
|
|
|
if err := o.accessLoggerMiddleware.Close(); err != nil {
|
2024-01-30 15:28:05 +00:00
|
|
|
log.Error().Err(err).Msg("Could not close the access log file")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-12 08:48:04 +00:00
|
|
|
if o.tracerCloser != nil {
|
|
|
|
if err := o.tracerCloser.Close(); err != nil {
|
2024-01-30 15:28:05 +00:00
|
|
|
log.Error().Err(err).Msg("Could not close the tracer")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-12 08:48:04 +00:00
|
|
|
func (o *ObservabilityMgr) RotateAccessLogs() error {
|
|
|
|
if o.accessLoggerMiddleware == nil {
|
2024-01-30 15:28:05 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-03-12 08:48:04 +00:00
|
|
|
return o.accessLoggerMiddleware.Rotate()
|
2024-01-30 15:28:05 +00:00
|
|
|
}
|