| 
									
										
										
											
												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)
											
										 
											2018-04-01 13:57:03 +01:00
										 |  |  | package metrics | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import ( | 
					
						
							| 
									
										
										
										
											2018-04-20 11:01:06 +01:00
										 |  |  | 	"context" | 
					
						
							| 
									
										
										
										
											2018-04-22 08:34:35 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-03-25 17:46:44 +00:00
										 |  |  | 	"github.com/coredns/coredns/core/dnsserver" | 
					
						
							| 
									
										
										
											
												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)
											
										 
											2018-04-01 13:57:03 +01:00
										 |  |  | ) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // 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) | 
					
						
							| 
									
										
										
										
											2019-03-25 17:46:44 +00:00
										 |  |  | func WithServer(ctx context.Context) string { | 
					
						
							|  |  |  | 	srv := ctx.Value(dnsserver.Key{}) | 
					
						
							|  |  |  | 	if srv == nil { | 
					
						
							|  |  |  | 		return "" | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	return srv.(*dnsserver.Server).Addr | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2022-09-08 14:56:27 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | // WithView returns the name of the view currently handling the request, if a view is defined. | 
					
						
							|  |  |  | // | 
					
						
							|  |  |  | // Basic usage with a metric: | 
					
						
							|  |  |  | // | 
					
						
							|  |  |  | // <metric>.WithLabelValues(metrics.WithView(ctx), labels..).Add(1) | 
					
						
							|  |  |  | func WithView(ctx context.Context) string { | 
					
						
							|  |  |  | 	v := ctx.Value(dnsserver.ViewKey{}) | 
					
						
							|  |  |  | 	if v == nil { | 
					
						
							|  |  |  | 		return "" | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	return v.(string) | 
					
						
							|  |  |  | } |