@marcel91200 / caddy Mirror · read-only / commits / 4e620952
Fork 0

Fast and extensible multi-platform HTTP/1-2-3 web server with automatic HTTPS

Mirrored from https://github.com/Slazee/caddy.git

Synced Nov 11, 08:30 UTC

core: preserve metrics registry in Context.WithValue (#7861)

* core: preserve metrics registry in Context.WithValue Context.WithValue rebuilt the Context without copying the unexported metricsRegistry field, so any module provisioned under a context derived via WithValue saw a nil registry from GetMetricsRegistry(). * Potential fix for pull request finding Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com> Signed-off-by: Kévin Dunglas <kevin@dunglas.fr> --------- Signed-off-by: Kévin Dunglas <kevin@dunglas.fr> Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>

Kévin Dunglas committed Jul 7, 2026 at 20:52 UTC 4e62095245005b17f290dd1e4d9afe983df180ee
2 files changed +19
context.go
+1
@@ -676,6 +676,7 @@ func (ctx *Context) WithValue(key, value any) Context {
676 ancestry: ctx.ancestry,
677 cleanupFuncs: ctx.cleanupFuncs,
678 exitFuncs: ctx.exitFuncs,
679 + metricsRegistry: ctx.metricsRegistry,
680 }
681 }
682
context_test.go
+18
@@ -15,10 +15,28 @@
15 package caddy
16
17 import (
18 + "context"
19 "encoding/json"
20 "io"
21 + "testing"
22 )
23
24 +func TestContextWithValuePreservesMetricsRegistry(t *testing.T) {
25 + ctx, cancel := NewContext(Context{Context: context.Background()})
26 + t.Cleanup(cancel)
27 +
28 + reg := ctx.GetMetricsRegistry()
29 + if reg == nil {
30 + t.Fatal("expected a metrics registry on the base context")
31 + }
32 +
33 + type testKey struct{}
34 + derived := ctx.WithValue(testKey{}, "value")
35 + if got := derived.GetMetricsRegistry(); got != reg {
36 + t.Fatalf("WithValue must preserve the metrics registry: got %p, want %p", got, reg)
37 + }
38 +}
39 +
40 func ExampleContext_LoadModule() {
41 // this whole first part is just setting up for the example;
42 // note the struct tags - very important; we specify inline_key