aboutsummaryrefslogtreecommitdiff
path: root/plugin/metrics/context.go
diff options
context:
space:
mode:
authorGravatar Miek Gieben <miek@miek.nl> 2018-04-01 13:57:03 +0100
committerGravatar GitHub <noreply@github.com> 2018-04-01 13:57:03 +0100
commit4df416ca1da2fff396ff84805518aa8f8b55b80b (patch)
tree2ed84dce69a0a6565505aa1ad9dab5d8f4c58a75 /plugin/metrics/context.go
parent5c5a98ee292e68cf59faf66f601fdcb169ea7eed (diff)
downloadcoredns-4df416ca1da2fff396ff84805518aa8f8b55b80b.tar.gz
coredns-4df416ca1da2fff396ff84805518aa8f8b55b80b.tar.zst
coredns-4df416ca1da2fff396ff84805518aa8f8b55b80b.zip
Metrics (#1579)
* plugin/metrics: set server address in context Allow cross server block metrics to co-exist; for this we should label each metric with the server label. Put this information in the context and provide a helper function to get it out. Abstracting with entirely away with difficult as the release client_go (0.8.0) doesn't have the CurryWith functions yet. So current use is like so: define metric, with server label: RcodeCount = prometheus.NewCounterVec(prometheus.CounterOpts{ Namespace: plugin.Namespace, Subsystem: "forward", Name: "response_rcode_count_total", Help: "Counter of requests made per upstream.", }, []string{"server", "rcode", "to"}) And report ith with the helper function metrics.WithServer: RcodeCount.WithLabelValues(metrics.WithServer(ctx), rc, p.addr).Add(1)
Diffstat (limited to 'plugin/metrics/context.go')
-rw-r--r--plugin/metrics/context.go24
1 files changed, 24 insertions, 0 deletions
diff --git a/plugin/metrics/context.go b/plugin/metrics/context.go
new file mode 100644
index 000000000..4cce871f5
--- /dev/null
+++ b/plugin/metrics/context.go
@@ -0,0 +1,24 @@
+package metrics
+
+import (
+ "github.com/coredns/coredns/plugin"
+
+ "golang.org/x/net/context"
+)
+
+// WithServer returns the current server handling the request. It returns the
+// server listening address: <scheme>://[<bind>]:<port> Normally this is
+// something like "dns://:53", but if the bind plugin is used, i.e. "bind
+// 127.0.0.53", it will be "dns://127.0.0.53:53", etc. If not address is found
+// the empty string is returned.
+//
+// Basic usage with a metric:
+//
+// <metric>.WithLabelValues(metrics.WithServer(ctx), labels..).Add(1)
+func WithServer(ctx context.Context) string {
+ srv := ctx.Value(plugin.ServerCtx{})
+ if srv == nil {
+ return ""
+ }
+ return srv.(string)
+}