mirror of
https://github.com/coredns/coredns.git
synced 2026-04-11 06:25:33 -04:00
Allow selectively exporting all Go runtime metrics (#7990)
Signed-off-by: Ryan Brewster <rpb@anthropic.com>
This commit is contained in:
@@ -55,7 +55,9 @@ This plugin can only be used once per Server Block.
|
|||||||
## Syntax
|
## Syntax
|
||||||
|
|
||||||
~~~
|
~~~
|
||||||
prometheus [ADDRESS]
|
prometheus [ADDRESS] {
|
||||||
|
runtime_metrics
|
||||||
|
}
|
||||||
~~~
|
~~~
|
||||||
|
|
||||||
For each zone that you want to see metrics for.
|
For each zone that you want to see metrics for.
|
||||||
@@ -63,6 +65,12 @@ For each zone that you want to see metrics for.
|
|||||||
It optionally takes a bind address to which the metrics are exported; the default
|
It optionally takes a bind address to which the metrics are exported; the default
|
||||||
listens on `localhost:9153`. The metrics path is fixed to `/metrics`.
|
listens on `localhost:9153`. The metrics path is fixed to `/metrics`.
|
||||||
|
|
||||||
|
* `runtime_metrics` exports the full Go [runtime/metrics](https://pkg.go.dev/runtime/metrics)
|
||||||
|
set — notably `go_cpu_classes_*` for CPU attribution (GC mark/assist/pause vs user code)
|
||||||
|
and `go_sched_latencies_seconds` for goroutine scheduling delay. Adds roughly 100 scalars
|
||||||
|
and 8 histograms. This is a process-wide latch: enabling it in any server block enables it
|
||||||
|
for all, and it stays enabled across reloads until restart.
|
||||||
|
|
||||||
## Examples
|
## Examples
|
||||||
|
|
||||||
Use an alternative listening address:
|
Use an alternative listening address:
|
||||||
|
|||||||
@@ -3,6 +3,7 @@ package metrics
|
|||||||
import (
|
import (
|
||||||
"net"
|
"net"
|
||||||
"runtime"
|
"runtime"
|
||||||
|
"sync"
|
||||||
|
|
||||||
"github.com/coredns/caddy"
|
"github.com/coredns/caddy"
|
||||||
"github.com/coredns/coredns/core/dnsserver"
|
"github.com/coredns/coredns/core/dnsserver"
|
||||||
@@ -10,11 +11,19 @@ import (
|
|||||||
"github.com/coredns/coredns/plugin"
|
"github.com/coredns/coredns/plugin"
|
||||||
"github.com/coredns/coredns/plugin/metrics/vars"
|
"github.com/coredns/coredns/plugin/metrics/vars"
|
||||||
"github.com/coredns/coredns/plugin/pkg/uniq"
|
"github.com/coredns/coredns/plugin/pkg/uniq"
|
||||||
|
|
||||||
|
"github.com/prometheus/client_golang/prometheus"
|
||||||
|
"github.com/prometheus/client_golang/prometheus/collectors"
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
u = uniq.New()
|
u = uniq.New()
|
||||||
registry = newReg()
|
registry = newReg()
|
||||||
|
|
||||||
|
// There is one Go runtime per process, so this is a latch: the first server
|
||||||
|
// block to enable runtime_metrics swaps the collector for everyone, and the
|
||||||
|
// swap persists across reloads until process restart.
|
||||||
|
runtimeMetricsOnce sync.Once
|
||||||
)
|
)
|
||||||
|
|
||||||
func init() { plugin.Register("prometheus", setup) }
|
func init() { plugin.Register("prometheus", setup) }
|
||||||
@@ -96,9 +105,18 @@ func parse(c *caddy.Controller) (*Metrics, error) {
|
|||||||
return met, c.ArgErr()
|
return met, c.ArgErr()
|
||||||
}
|
}
|
||||||
|
|
||||||
// Parse TLS block if present
|
|
||||||
for c.NextBlock() {
|
for c.NextBlock() {
|
||||||
switch c.Val() {
|
switch c.Val() {
|
||||||
|
case "runtime_metrics":
|
||||||
|
if len(c.RemainingArgs()) != 0 {
|
||||||
|
return nil, c.ArgErr()
|
||||||
|
}
|
||||||
|
runtimeMetricsOnce.Do(func() {
|
||||||
|
prometheus.Unregister(collectors.NewGoCollector())
|
||||||
|
prometheus.MustRegister(collectors.NewGoCollector(
|
||||||
|
collectors.WithGoCollectorRuntimeMetrics(collectors.MetricsAll),
|
||||||
|
))
|
||||||
|
})
|
||||||
case "tls":
|
case "tls":
|
||||||
if met.tlsConfigPath != "" {
|
if met.tlsConfigPath != "" {
|
||||||
return nil, c.Err("tls block already specified")
|
return nil, c.Err("tls block already specified")
|
||||||
|
|||||||
@@ -15,8 +15,17 @@ func TestPrometheusParse(t *testing.T) {
|
|||||||
// oks
|
// oks
|
||||||
{`prometheus`, false, "localhost:9153"},
|
{`prometheus`, false, "localhost:9153"},
|
||||||
{`prometheus localhost:53`, false, "localhost:53"},
|
{`prometheus localhost:53`, false, "localhost:53"},
|
||||||
|
{`prometheus {
|
||||||
|
runtime_metrics
|
||||||
|
}`, false, "localhost:9153"},
|
||||||
|
{`prometheus localhost:53 {
|
||||||
|
runtime_metrics
|
||||||
|
}`, false, "localhost:53"},
|
||||||
// fails
|
// fails
|
||||||
{`prometheus {}`, true, ""},
|
{`prometheus {}`, true, ""},
|
||||||
|
{`prometheus {
|
||||||
|
runtime_metrics extra_arg
|
||||||
|
}`, true, ""},
|
||||||
{`prometheus /foo`, true, ""},
|
{`prometheus /foo`, true, ""},
|
||||||
{`prometheus a b c`, true, ""},
|
{`prometheus a b c`, true, ""},
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user