summaryrefslogtreecommitdiffhomepage
path: root/context.go
diff options
context:
space:
mode:
authorMohammed Al Sahaf <[email protected]>2024-10-02 17:23:26 +0300
committerGitHub <[email protected]>2024-10-02 08:23:26 -0600
commit41f5dd56e1b93ec815daa98dd1f1caa7f2087312 (patch)
tree4802459d6c89d6cbaf064d02b445130fa361b12d /context.go
parent16724842d9b9096b800326d0b7667a4361552552 (diff)
downloadcaddy-41f5dd56e1b93ec815daa98dd1f1caa7f2087312.tar.gz
caddy-41f5dd56e1b93ec815daa98dd1f1caa7f2087312.zip
metrics: scope metrics to active config, add optional per-host metrics (#6531)v2.9.0-beta.1
* Add per host config * Pass host label when option is enabled * Test per host enabled * metrics: scope metrics per loaded config * doc and linter Signed-off-by: Mohammed Al Sahaf <[email protected]> * inject the custom registry into the admin handler Co-Authored-By: Dave Henderson <[email protected]> * remove `TODO` comment * fixes Signed-off-by: Mohammed Al Sahaf <[email protected]> * refactor to delay metrics admin handler provision Signed-off-by: Mohammed Al Sahaf <[email protected]> --------- Signed-off-by: Mohammed Al Sahaf <[email protected]> Co-authored-by: Hussam Almarzooq <[email protected]> Co-authored-by: Dave Henderson <[email protected]>
Diffstat (limited to 'context.go')
-rw-r--r--context.go22
1 files changed, 21 insertions, 1 deletions
diff --git a/context.go b/context.go
index 5b8c10703..17a8aa4f8 100644
--- a/context.go
+++ b/context.go
@@ -23,6 +23,8 @@ import (
"reflect"
"github.com/caddyserver/certmagic"
+ "github.com/prometheus/client_golang/prometheus"
+ "github.com/prometheus/client_golang/prometheus/collectors"
"go.uber.org/zap"
"go.uber.org/zap/exp/zapslog"
@@ -47,6 +49,7 @@ type Context struct {
ancestry []Module
cleanupFuncs []func() // invoked at every config unload
exitFuncs []func(context.Context) // invoked at config unload ONLY IF the process is exiting (EXPERIMENTAL)
+ metricsRegistry *prometheus.Registry
}
// NewContext provides a new context derived from the given
@@ -58,7 +61,7 @@ type Context struct {
// modules which are loaded will be properly unloaded.
// See standard library context package's documentation.
func NewContext(ctx Context) (Context, context.CancelFunc) {
- newCtx := Context{moduleInstances: make(map[string][]Module), cfg: ctx.cfg}
+ newCtx := Context{moduleInstances: make(map[string][]Module), cfg: ctx.cfg, metricsRegistry: prometheus.NewPedanticRegistry()}
c, cancel := context.WithCancel(ctx.Context)
wrappedCancel := func() {
cancel()
@@ -79,6 +82,7 @@ func NewContext(ctx Context) (Context, context.CancelFunc) {
}
}
newCtx.Context = c
+ newCtx.initMetrics()
return newCtx, wrappedCancel
}
@@ -97,6 +101,22 @@ func (ctx *Context) Filesystems() FileSystems {
return ctx.cfg.filesystems
}
+// Returns the active metrics registry for the context
+// EXPERIMENTAL: This API is subject to change.
+func (ctx *Context) GetMetricsRegistry() *prometheus.Registry {
+ return ctx.metricsRegistry
+}
+
+func (ctx *Context) initMetrics() {
+ ctx.metricsRegistry.MustRegister(
+ collectors.NewBuildInfoCollector(),
+ adminMetrics.requestCount,
+ adminMetrics.requestErrors,
+ globalMetrics.configSuccess,
+ globalMetrics.configSuccessTime,
+ )
+}
+
// OnExit executes f when the process exits gracefully.
// The function is only executed if the process is gracefully
// shut down while this context is active.