Do not create observability model by default

Co-authored-by: Kevin Pollet <pollet.kevin@gmail.com>
This commit is contained in:
Romain 2025-01-29 13:56:04 +01:00 committed by GitHub
parent fb527dac1c
commit 857fbb933e
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
9 changed files with 133 additions and 62 deletions

View file

@ -60,8 +60,6 @@ func (ep *EntryPoint) SetDefaults() {
ep.HTTP.SetDefaults() ep.HTTP.SetDefaults()
ep.HTTP2 = &HTTP2Config{} ep.HTTP2 = &HTTP2Config{}
ep.HTTP2.SetDefaults() ep.HTTP2.SetDefaults()
ep.Observability = &ObservabilityConfig{}
ep.Observability.SetDefaults()
} }
// HTTPConfig is the HTTP configuration of an entry point. // HTTPConfig is the HTTP configuration of an entry point.
@ -164,14 +162,15 @@ func (u *UDPConfig) SetDefaults() {
// ObservabilityConfig holds the observability configuration for an entry point. // ObservabilityConfig holds the observability configuration for an entry point.
type ObservabilityConfig struct { type ObservabilityConfig struct {
AccessLogs bool `json:"accessLogs,omitempty" toml:"accessLogs,omitempty" yaml:"accessLogs,omitempty" export:"true"` AccessLogs *bool `json:"accessLogs,omitempty" toml:"accessLogs,omitempty" yaml:"accessLogs,omitempty" export:"true"`
Tracing bool `json:"tracing,omitempty" toml:"tracing,omitempty" yaml:"tracing,omitempty" export:"true"` Tracing *bool `json:"tracing,omitempty" toml:"tracing,omitempty" yaml:"tracing,omitempty" export:"true"`
Metrics bool `json:"metrics,omitempty" toml:"metrics,omitempty" yaml:"metrics,omitempty" export:"true"` Metrics *bool `json:"metrics,omitempty" toml:"metrics,omitempty" yaml:"metrics,omitempty" export:"true"`
} }
// SetDefaults sets the default values. // SetDefaults sets the default values.
func (o *ObservabilityConfig) SetDefaults() { func (o *ObservabilityConfig) SetDefaults() {
o.AccessLogs = true defaultValue := true
o.Tracing = true o.AccessLogs = &defaultValue
o.Metrics = true o.Tracing = &defaultValue
o.Metrics = &defaultValue
} }

View file

@ -77,11 +77,6 @@ func TestConfiguration_SetEffectiveConfiguration(t *testing.T) {
UDP: &UDPConfig{ UDP: &UDPConfig{
Timeout: 3000000000, Timeout: 3000000000,
}, },
Observability: &ObservabilityConfig{
AccessLogs: true,
Tracing: true,
Metrics: true,
},
}}, }},
Providers: &Providers{}, Providers: &Providers{},
}, },
@ -127,11 +122,6 @@ func TestConfiguration_SetEffectiveConfiguration(t *testing.T) {
UDP: &UDPConfig{ UDP: &UDPConfig{
Timeout: 3000000000, Timeout: 3000000000,
}, },
Observability: &ObservabilityConfig{
AccessLogs: true,
Tracing: true,
Metrics: true,
},
}}, }},
Providers: &Providers{}, Providers: &Providers{},
CertificatesResolvers: map[string]CertificateResolver{ CertificatesResolvers: map[string]CertificateResolver{
@ -188,11 +178,6 @@ func TestConfiguration_SetEffectiveConfiguration(t *testing.T) {
UDP: &UDPConfig{ UDP: &UDPConfig{
Timeout: 3000000000, Timeout: 3000000000,
}, },
Observability: &ObservabilityConfig{
AccessLogs: true,
Tracing: true,
Metrics: true,
},
}}, }},
Providers: &Providers{}, Providers: &Providers{},
CertificatesResolvers: map[string]CertificateResolver{ CertificatesResolvers: map[string]CertificateResolver{
@ -253,11 +238,6 @@ func TestConfiguration_SetEffectiveConfiguration(t *testing.T) {
UDP: &UDPConfig{ UDP: &UDPConfig{
Timeout: 3000000000, Timeout: 3000000000,
}, },
Observability: &ObservabilityConfig{
AccessLogs: true,
Tracing: true,
Metrics: true,
},
}}, }},
Providers: &Providers{}, Providers: &Providers{},
CertificatesResolvers: map[string]CertificateResolver{ CertificatesResolvers: map[string]CertificateResolver{

View file

@ -240,9 +240,9 @@ func (i *Provider) entryPointModels(cfg *dynamic.Configuration) {
if ep.Observability != nil { if ep.Observability != nil {
httpModel.Observability = dynamic.RouterObservabilityConfig{ httpModel.Observability = dynamic.RouterObservabilityConfig{
AccessLogs: &ep.Observability.AccessLogs, AccessLogs: ep.Observability.AccessLogs,
Tracing: &ep.Observability.Tracing, Tracing: ep.Observability.Tracing,
Metrics: &ep.Observability.Metrics, Metrics: ep.Observability.Metrics,
} }
} }

View file

@ -18,6 +18,8 @@ import (
var updateExpected = flag.Bool("update_expected", false, "Update expected files in fixtures") var updateExpected = flag.Bool("update_expected", false, "Update expected files in fixtures")
func pointer[T any](v T) *T { return &v }
func Test_createConfiguration(t *testing.T) { func Test_createConfiguration(t *testing.T) {
testCases := []struct { testCases := []struct {
desc string desc string
@ -185,9 +187,9 @@ func Test_createConfiguration(t *testing.T) {
}, },
}, },
Observability: &static.ObservabilityConfig{ Observability: &static.ObservabilityConfig{
AccessLogs: false, AccessLogs: pointer(false),
Tracing: false, Tracing: pointer(false),
Metrics: false, Metrics: pointer(false),
}, },
}, },
}, },

View file

@ -191,14 +191,14 @@ func applyModel(cfg dynamic.Configuration) dynamic.Configuration {
cp.Observability.AccessLogs = m.Observability.AccessLogs cp.Observability.AccessLogs = m.Observability.AccessLogs
} }
if cp.Observability.Tracing == nil {
cp.Observability.Tracing = m.Observability.Tracing
}
if cp.Observability.Metrics == nil { if cp.Observability.Metrics == nil {
cp.Observability.Metrics = m.Observability.Metrics cp.Observability.Metrics = m.Observability.Metrics
} }
if cp.Observability.Tracing == nil {
cp.Observability.Tracing = m.Observability.Tracing
}
rtName := name rtName := name
if len(eps) > 1 { if len(eps) > 1 {
rtName = epName + "-" + name rtName = epName + "-" + name
@ -215,6 +215,9 @@ func applyModel(cfg dynamic.Configuration) dynamic.Configuration {
cfg.HTTP.Routers = rts cfg.HTTP.Routers = rts
} }
// Apply default observability model to HTTP routers.
applyDefaultObservabilityModel(cfg)
if cfg.TCP == nil || len(cfg.TCP.Models) == 0 { if cfg.TCP == nil || len(cfg.TCP.Models) == 0 {
return cfg return cfg
} }
@ -238,3 +241,38 @@ func applyModel(cfg dynamic.Configuration) dynamic.Configuration {
return cfg return cfg
} }
// applyDefaultObservabilityModel applies the default observability model to the configuration.
// This function is used to ensure that the observability configuration is set for all routers,
// and make sure it is serialized and available in the API.
// We could have introduced a "default" model, but it would have been more complex to manage for now.
// This could be generalized in the future.
func applyDefaultObservabilityModel(cfg dynamic.Configuration) {
if cfg.HTTP != nil {
for _, router := range cfg.HTTP.Routers {
if router.Observability == nil {
router.Observability = &dynamic.RouterObservabilityConfig{
AccessLogs: pointer(true),
Metrics: pointer(true),
Tracing: pointer(true),
}
continue
}
if router.Observability.AccessLogs == nil {
router.Observability.AccessLogs = pointer(true)
}
if router.Observability.Tracing == nil {
router.Observability.Tracing = pointer(true)
}
if router.Observability.Metrics == nil {
router.Observability.Metrics = pointer(true)
}
}
}
}
func pointer[T any](v T) *T { return &v }

View file

@ -9,8 +9,6 @@ import (
"github.com/traefik/traefik/v3/pkg/tls" "github.com/traefik/traefik/v3/pkg/tls"
) )
func pointer[T any](v T) *T { return &v }
func Test_mergeConfiguration(t *testing.T) { func Test_mergeConfiguration(t *testing.T) {
testCases := []struct { testCases := []struct {
desc string desc string
@ -508,6 +506,33 @@ func Test_applyModel(t *testing.T) {
}, },
}, },
}, },
{
desc: "without model, one router",
input: dynamic.Configuration{
HTTP: &dynamic.HTTPConfiguration{
Routers: map[string]*dynamic.Router{"test": {}},
Middlewares: make(map[string]*dynamic.Middleware),
Services: make(map[string]*dynamic.Service),
Models: make(map[string]*dynamic.Model),
},
},
expected: dynamic.Configuration{
HTTP: &dynamic.HTTPConfiguration{
Routers: map[string]*dynamic.Router{
"test": {
Observability: &dynamic.RouterObservabilityConfig{
AccessLogs: pointer(true),
Metrics: pointer(true),
Tracing: pointer(true),
},
},
},
Middlewares: make(map[string]*dynamic.Middleware),
Services: make(map[string]*dynamic.Service),
Models: make(map[string]*dynamic.Model),
},
},
},
{ {
desc: "with model, not used", desc: "with model, not used",
input: dynamic.Configuration{ input: dynamic.Configuration{
@ -563,7 +588,11 @@ func Test_applyModel(t *testing.T) {
EntryPoints: []string{"websecure"}, EntryPoints: []string{"websecure"},
Middlewares: []string{"test"}, Middlewares: []string{"test"},
TLS: &dynamic.RouterTLSConfig{}, TLS: &dynamic.RouterTLSConfig{},
Observability: &dynamic.RouterObservabilityConfig{}, Observability: &dynamic.RouterObservabilityConfig{
AccessLogs: pointer(true),
Metrics: pointer(true),
Tracing: pointer(true),
},
}, },
}, },
Middlewares: make(map[string]*dynamic.Middleware), Middlewares: make(map[string]*dynamic.Middleware),
@ -659,9 +688,9 @@ func Test_applyModel(t *testing.T) {
Middlewares: []string{"test"}, Middlewares: []string{"test"},
TLS: &dynamic.RouterTLSConfig{CertResolver: "router"}, TLS: &dynamic.RouterTLSConfig{CertResolver: "router"},
Observability: &dynamic.RouterObservabilityConfig{ Observability: &dynamic.RouterObservabilityConfig{
AccessLogs: nil, AccessLogs: pointer(true),
Tracing: nil, Metrics: pointer(true),
Metrics: nil, Tracing: pointer(true),
}, },
}, },
}, },
@ -700,12 +729,21 @@ func Test_applyModel(t *testing.T) {
Routers: map[string]*dynamic.Router{ Routers: map[string]*dynamic.Router{
"test": { "test": {
EntryPoints: []string{"web"}, EntryPoints: []string{"web"},
Observability: &dynamic.RouterObservabilityConfig{
AccessLogs: pointer(true),
Metrics: pointer(true),
Tracing: pointer(true),
},
}, },
"websecure-test": { "websecure-test": {
EntryPoints: []string{"websecure"}, EntryPoints: []string{"websecure"},
Middlewares: []string{"test"}, Middlewares: []string{"test"},
TLS: &dynamic.RouterTLSConfig{}, TLS: &dynamic.RouterTLSConfig{},
Observability: &dynamic.RouterObservabilityConfig{}, Observability: &dynamic.RouterObservabilityConfig{
AccessLogs: pointer(true),
Metrics: pointer(true),
Tracing: pointer(true),
},
}, },
}, },
Middlewares: make(map[string]*dynamic.Middleware), Middlewares: make(map[string]*dynamic.Middleware),

View file

@ -84,7 +84,8 @@ func TestNewConfigurationWatcher(t *testing.T) {
th.WithRouters( th.WithRouters(
th.WithRouter("test@mock", th.WithRouter("test@mock",
th.WithEntryPoints("e"), th.WithEntryPoints("e"),
th.WithServiceName("scv"))), th.WithServiceName("scv"),
th.WithObservability())),
th.WithMiddlewares(), th.WithMiddlewares(),
th.WithLoadBalancerServices(), th.WithLoadBalancerServices(),
), ),
@ -175,7 +176,7 @@ func TestIgnoreTransientConfiguration(t *testing.T) {
expectedConfig := dynamic.Configuration{ expectedConfig := dynamic.Configuration{
HTTP: th.BuildConfiguration( HTTP: th.BuildConfiguration(
th.WithRouters(th.WithRouter("foo@mock", th.WithEntryPoints("ep"))), th.WithRouters(th.WithRouter("foo@mock", th.WithEntryPoints("ep"), th.WithObservability())),
th.WithLoadBalancerServices(th.WithService("bar@mock")), th.WithLoadBalancerServices(th.WithService("bar@mock")),
th.WithMiddlewares(), th.WithMiddlewares(),
), ),
@ -200,7 +201,7 @@ func TestIgnoreTransientConfiguration(t *testing.T) {
expectedConfig3 := dynamic.Configuration{ expectedConfig3 := dynamic.Configuration{
HTTP: th.BuildConfiguration( HTTP: th.BuildConfiguration(
th.WithRouters(th.WithRouter("foo@mock", th.WithEntryPoints("ep"))), th.WithRouters(th.WithRouter("foo@mock", th.WithEntryPoints("ep"), th.WithObservability())),
th.WithLoadBalancerServices(th.WithService("bar-config3@mock")), th.WithLoadBalancerServices(th.WithService("bar-config3@mock")),
th.WithMiddlewares(), th.WithMiddlewares(),
), ),
@ -447,7 +448,7 @@ func TestListenProvidersDoesNotSkipFlappingConfiguration(t *testing.T) {
expected := dynamic.Configuration{ expected := dynamic.Configuration{
HTTP: th.BuildConfiguration( HTTP: th.BuildConfiguration(
th.WithRouters(th.WithRouter("foo@mock", th.WithEntryPoints("ep"))), th.WithRouters(th.WithRouter("foo@mock", th.WithEntryPoints("ep"), th.WithObservability())),
th.WithLoadBalancerServices(th.WithService("bar@mock")), th.WithLoadBalancerServices(th.WithService("bar@mock")),
th.WithMiddlewares(), th.WithMiddlewares(),
), ),
@ -538,7 +539,7 @@ func TestListenProvidersIgnoreSameConfig(t *testing.T) {
expected := dynamic.Configuration{ expected := dynamic.Configuration{
HTTP: th.BuildConfiguration( HTTP: th.BuildConfiguration(
th.WithRouters(th.WithRouter("foo@mock", th.WithEntryPoints("ep"))), th.WithRouters(th.WithRouter("foo@mock", th.WithEntryPoints("ep"), th.WithObservability())),
th.WithLoadBalancerServices(th.WithService("bar@mock")), th.WithLoadBalancerServices(th.WithService("bar@mock")),
th.WithMiddlewares(), th.WithMiddlewares(),
), ),
@ -674,7 +675,7 @@ func TestListenProvidersIgnoreIntermediateConfigs(t *testing.T) {
expected := dynamic.Configuration{ expected := dynamic.Configuration{
HTTP: th.BuildConfiguration( HTTP: th.BuildConfiguration(
th.WithRouters(th.WithRouter("final@mock", th.WithEntryPoints("ep"))), th.WithRouters(th.WithRouter("final@mock", th.WithEntryPoints("ep"), th.WithObservability())),
th.WithLoadBalancerServices(th.WithService("final@mock")), th.WithLoadBalancerServices(th.WithService("final@mock")),
th.WithMiddlewares(), th.WithMiddlewares(),
), ),
@ -738,8 +739,8 @@ func TestListenProvidersPublishesConfigForEachProvider(t *testing.T) {
expected := dynamic.Configuration{ expected := dynamic.Configuration{
HTTP: th.BuildConfiguration( HTTP: th.BuildConfiguration(
th.WithRouters( th.WithRouters(
th.WithRouter("foo@mock", th.WithEntryPoints("ep")), th.WithRouter("foo@mock", th.WithEntryPoints("ep"), th.WithObservability()),
th.WithRouter("foo@mock2", th.WithEntryPoints("ep")), th.WithRouter("foo@mock2", th.WithEntryPoints("ep"), th.WithObservability()),
), ),
th.WithLoadBalancerServices( th.WithLoadBalancerServices(
th.WithService("bar@mock"), th.WithService("bar@mock"),

View file

@ -110,7 +110,7 @@ func (o *ObservabilityMgr) ShouldAddAccessLogs(serviceName string, observability
return false return false
} }
return observabilityConfig == nil || observabilityConfig.AccessLogs != nil && *observabilityConfig.AccessLogs return observabilityConfig == nil || observabilityConfig.AccessLogs == nil || *observabilityConfig.AccessLogs
} }
// ShouldAddMetrics returns whether the metrics should be enabled for the given resource and the observability config. // ShouldAddMetrics returns whether the metrics should be enabled for the given resource and the observability config.
@ -127,7 +127,7 @@ func (o *ObservabilityMgr) ShouldAddMetrics(serviceName string, observabilityCon
return false return false
} }
return observabilityConfig == nil || observabilityConfig.Metrics != nil && *observabilityConfig.Metrics return observabilityConfig == nil || observabilityConfig.Metrics == nil || *observabilityConfig.Metrics
} }
// ShouldAddTracing returns whether the tracing should be enabled for the given serviceName and the observability config. // ShouldAddTracing returns whether the tracing should be enabled for the given serviceName and the observability config.
@ -144,7 +144,7 @@ func (o *ObservabilityMgr) ShouldAddTracing(serviceName string, observabilityCon
return false return false
} }
return observabilityConfig == nil || observabilityConfig.Tracing != nil && *observabilityConfig.Tracing return observabilityConfig == nil || observabilityConfig.Tracing == nil || *observabilityConfig.Tracing
} }
// MetricsRegistry is an accessor to the metrics registry. // MetricsRegistry is an accessor to the metrics registry.

View file

@ -53,6 +53,17 @@ func WithServiceName(serviceName string) func(*dynamic.Router) {
} }
} }
// WithObservability is a helper to create a configuration.
func WithObservability() func(*dynamic.Router) {
return func(r *dynamic.Router) {
r.Observability = &dynamic.RouterObservabilityConfig{
AccessLogs: pointer(true),
Metrics: pointer(true),
Tracing: pointer(true),
}
}
}
// WithLoadBalancerServices is a helper to create a configuration. // WithLoadBalancerServices is a helper to create a configuration.
func WithLoadBalancerServices(opts ...func(service *dynamic.ServersLoadBalancer) string) func(*dynamic.HTTPConfiguration) { func WithLoadBalancerServices(opts ...func(service *dynamic.ServersLoadBalancer) string) func(*dynamic.HTTPConfiguration) {
return func(c *dynamic.HTTPConfiguration) { return func(c *dynamic.HTTPConfiguration) {
@ -149,3 +160,5 @@ func WithSticky(cookieName string) func(*dynamic.ServersLoadBalancer) {
} }
} }
} }
func pointer[T any](v T) *T { return &v }