feat(observability-proxy): add the --metrics-platform-namespace-prefix flag

The prefix decides which namespaces are trusted to attribute series
to another namespace, so it defaults to cozy-, the prefix of every
platform namespace, and can be emptied to turn that off. It is
limited to namespace name characters: a regex metacharacter or an
upper case letter would match namespaces other than the ones named.

Assisted-by: LLM
Signed-off-by: Maxim Belyy <maximbel2003@gmail.com>
This commit is contained in:
Maxim Belyy committed 2026-11-10 19:43:21 +07:00
1 parent 19e91d3957
commit 053026c8df
2 files changed
+23 -1

No files matched your search

+6
View File
@@ -92,6 +92,7 @@ func parseFlags(args []string) (options, error) {
fs.DurationVar(&o.metrics.Limits.MaxRange, "metrics-max-range", 30*24*time.Hour, "longest [start, end] and step a metrics request may use")
fs.IntVar(&o.metrics.Limits.MaxPoints, "metrics-max-points", 11000, "most points per series a range query may return")
fs.DurationVar(&o.metrics.Limits.MaxTimeout, "metrics-max-timeout", 30*time.Second, "timeout forwarded to vmselect; a longer client timeout is clamped; must be below --request-timeout when that is set")
fs.StringVar(&o.metrics.PlatformNamespacePrefix, "metrics-platform-namespace-prefix", "cozy-", "namespaces with this prefix run platform exporters, whose exported_namespace label adds series to that namespace's scope; empty disables")
fs.IntVar(&o.limiter.MaxConcurrentPerUser, "max-concurrent-per-user", 10, "concurrent non-tail requests per user; 0 disables")
fs.Float64Var(&o.limiter.RatePerUser, "rate-limit-per-user", 10, "sustained requests per second per user; 0 disables")
fs.IntVar(&o.limiter.RateBurst, "rate-limit-burst", 20, "request burst per user")
@@ -112,6 +113,11 @@ func parseFlags(args []string) (options, error) {
return o, fmt.Errorf("invalid metrics store name %q", store)
}
}
// Anything but namespace name characters would make the prefix match
// differently from the namespaces it is meant to name.
if p := o.metrics.PlatformNamespacePrefix; p != "" && len(validation.IsDNS1123Label(p+"x")) > 0 {
return o, fmt.Errorf("invalid metrics platform namespace prefix %q", p)
}
switch {
case o.rootTenant == "":
return o, errors.New("--root-tenant must not be empty")
+17 -1
View File
@@ -43,6 +43,10 @@ func TestParseFlagsDefaults(t *testing.T) {
if m.RootTenant != o.rootTenant {
t.Errorf("metrics root tenant = %q, want --root-tenant", m.RootTenant)
}
// The prefix every platform namespace carries.
if m.PlatformNamespacePrefix != "cozy-" {
t.Errorf("platform namespace prefix = %q, want cozy-", m.PlatformNamespacePrefix)
}
}
func TestParseFlagsOverrides(t *testing.T) {
@@ -63,15 +67,22 @@ func TestParseFlagsMetricsOverrides(t *testing.T) {
o, err := parseFlags([]string{
"--root-tenant=tenant-x", "--metrics-shortterm-store=fast", "--metrics-longterm-store=slow",
"--metrics-shortterm-retention=24h", "--metrics-max-range=168h", "--metrics-max-points=500", "--metrics-max-timeout=10s",
"--metrics-platform-namespace-prefix=sys-",
})
if err != nil {
t.Fatal(err)
}
m := o.metrics
if m.RootTenant != "tenant-x" || m.ShortTermStore != "fast" || m.LongTermStore != "slow" || m.ShortTermRetention != 24*time.Hour ||
m.Limits.MaxRange != 168*time.Hour || m.Limits.MaxPoints != 500 || m.Limits.MaxTimeout != 10*time.Second {
m.Limits.MaxRange != 168*time.Hour || m.Limits.MaxPoints != 500 || m.Limits.MaxTimeout != 10*time.Second ||
m.PlatformNamespacePrefix != "sys-" {
t.Errorf("overrides not applied: %+v", m)
}
// An empty prefix switches the exporter branch of the scope off.
o, err = parseFlags([]string{"--metrics-platform-namespace-prefix="})
if err != nil || o.metrics.PlatformNamespacePrefix != "" {
t.Errorf("empty platform namespace prefix: %q, %v", o.metrics.PlatformNamespacePrefix, err)
}
}
func TestParseFlagsInvalid(t *testing.T) {
@@ -101,6 +112,11 @@ func TestParseFlagsInvalid(t *testing.T) {
{"--metrics-max-range=0s"},
{"--metrics-max-points=1"},
{"--metrics-max-timeout=0s"},
// Only namespace name characters: a regex metacharacter or an upper
// case letter would widen or empty the scope.
{"--metrics-platform-namespace-prefix=Cozy-"},
{"--metrics-platform-namespace-prefix=cozy.*"},
{"--metrics-platform-namespace-prefix=-"},
{"--unknown"},
// The proxy would cut the request while vmselect is still within
// the timeout it was given.