| 
									
										
										
										
											2016-08-19 17:14:17 -07:00
										 |  |  | package metrics | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import ( | 
					
						
							| 
									
										
										
										
											2016-10-04 11:05:04 +01:00
										 |  |  | 	"net" | 
					
						
							| 
									
										
										
										
											2018-04-21 18:59:35 +01:00
										 |  |  | 	"runtime" | 
					
						
							| 
									
										
										
										
											2016-08-19 17:14:17 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-21 22:51:47 -08:00
										 |  |  | 	"github.com/coredns/coredns/core/dnsserver" | 
					
						
							| 
									
										
										
										
											2018-04-21 18:59:35 +01:00
										 |  |  | 	"github.com/coredns/coredns/coremain" | 
					
						
							| 
									
										
										
										
											2017-09-14 09:36:06 +01:00
										 |  |  | 	"github.com/coredns/coredns/plugin" | 
					
						
							| 
									
										
										
										
											2018-04-22 21:40:33 +01:00
										 |  |  | 	clog "github.com/coredns/coredns/plugin/pkg/log" | 
					
						
							| 
									
										
										
										
											2018-04-25 11:45:09 +01:00
										 |  |  | 	"github.com/coredns/coredns/plugin/pkg/uniq" | 
					
						
							| 
									
										
										
										
											2016-08-19 17:14:17 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	"github.com/mholt/caddy" | 
					
						
							|  |  |  | ) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-04-25 11:45:09 +01:00
										 |  |  | var ( | 
					
						
							|  |  |  | 	log      = clog.NewWithPlugin("prometheus") | 
					
						
							|  |  |  | 	uniqAddr = uniq.New() | 
					
						
							|  |  |  | ) | 
					
						
							| 
									
										
										
										
											2018-04-22 21:40:33 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-08-19 17:14:17 -07:00
										 |  |  | func init() { | 
					
						
							|  |  |  | 	caddy.RegisterPlugin("prometheus", caddy.Plugin{ | 
					
						
							|  |  |  | 		ServerType: "dns", | 
					
						
							|  |  |  | 		Action:     setup, | 
					
						
							|  |  |  | 	}) | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func setup(c *caddy.Controller) error { | 
					
						
							|  |  |  | 	m, err := prometheusParse(c) | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							| 
									
										
										
										
											2017-09-14 09:36:06 +01:00
										 |  |  | 		return plugin.Error("prometheus", err) | 
					
						
							| 
									
										
										
										
											2016-08-19 17:14:17 -07:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-09-14 09:36:06 +01:00
										 |  |  | 	dnsserver.GetConfig(c).AddPlugin(func(next plugin.Handler) plugin.Handler { | 
					
						
							| 
									
										
										
										
											2016-08-19 17:14:17 -07:00
										 |  |  | 		m.Next = next | 
					
						
							|  |  |  | 		return m | 
					
						
							|  |  |  | 	}) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-04-21 17:43:02 +01:00
										 |  |  | 	c.OncePerServerBlock(func() error { | 
					
						
							|  |  |  | 		c.OnStartup(func() error { | 
					
						
							| 
									
										
										
										
											2018-04-25 11:45:09 +01:00
										 |  |  | 			return uniqAddr.ForEach() | 
					
						
							| 
									
										
										
										
											2018-04-21 17:43:02 +01:00
										 |  |  | 		}) | 
					
						
							|  |  |  | 		return nil | 
					
						
							|  |  |  | 	}) | 
					
						
							| 
									
										
										
										
											2018-03-02 17:16:25 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-04-21 17:43:02 +01:00
										 |  |  | 	c.OnRestart(m.OnRestart) | 
					
						
							|  |  |  | 	c.OnFinalShutdown(m.OnFinalShutdown) | 
					
						
							| 
									
										
										
										
											2016-08-19 17:14:17 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-04-21 18:59:35 +01:00
										 |  |  | 	// Initialize metrics. | 
					
						
							|  |  |  | 	buildInfo.WithLabelValues(coremain.CoreVersion, coremain.GitCommit, runtime.Version()).Set(1) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-08-19 17:14:17 -07:00
										 |  |  | 	return nil | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-04 11:05:04 +01:00
										 |  |  | func prometheusParse(c *caddy.Controller) (*Metrics, error) { | 
					
						
							| 
									
										
										
										
											2017-12-13 16:59:10 -05:00
										 |  |  | 	var met = New(defaultAddr) | 
					
						
							| 
									
										
										
										
											2016-08-19 17:14:17 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-04-12 10:10:57 +00:00
										 |  |  | 	defer func() { | 
					
						
							| 
									
										
										
										
											2018-04-25 11:45:09 +01:00
										 |  |  | 		uniqAddr.Set(met.Addr, met.OnStartup) | 
					
						
							| 
									
										
										
										
											2017-04-12 10:10:57 +00:00
										 |  |  | 	}() | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-02 17:16:25 -08:00
										 |  |  | 	i := 0 | 
					
						
							| 
									
										
										
										
											2016-08-19 17:14:17 -07:00
										 |  |  | 	for c.Next() { | 
					
						
							| 
									
										
										
										
											2018-03-02 17:16:25 -08:00
										 |  |  | 		if i > 0 { | 
					
						
							|  |  |  | 			return nil, plugin.ErrOnce | 
					
						
							| 
									
										
										
										
											2016-08-19 17:14:17 -07:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2018-03-02 17:16:25 -08:00
										 |  |  | 		i++ | 
					
						
							| 
									
										
										
										
											2016-10-26 10:01:52 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		for _, z := range c.ServerBlockKeys { | 
					
						
							| 
									
										
										
										
											2017-09-14 09:36:06 +01:00
										 |  |  | 			met.AddZone(plugin.Host(z).Normalize()) | 
					
						
							| 
									
										
										
										
											2016-08-19 17:14:17 -07:00
										 |  |  | 		} | 
					
						
							|  |  |  | 		args := c.RemainingArgs() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		switch len(args) { | 
					
						
							|  |  |  | 		case 0: | 
					
						
							|  |  |  | 		case 1: | 
					
						
							|  |  |  | 			met.Addr = args[0] | 
					
						
							| 
									
										
										
										
											2016-10-04 11:05:04 +01:00
										 |  |  | 			_, _, e := net.SplitHostPort(met.Addr) | 
					
						
							|  |  |  | 			if e != nil { | 
					
						
							|  |  |  | 				return met, e | 
					
						
							|  |  |  | 			} | 
					
						
							| 
									
										
										
										
											2016-08-19 17:14:17 -07:00
										 |  |  | 		default: | 
					
						
							| 
									
										
										
										
											2016-10-04 11:05:04 +01:00
										 |  |  | 			return met, c.ArgErr() | 
					
						
							| 
									
										
										
										
											2016-08-19 17:14:17 -07:00
										 |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2017-12-13 16:59:10 -05:00
										 |  |  | 	return met, nil | 
					
						
							| 
									
										
										
										
											2016-08-19 17:14:17 -07:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-12-13 16:59:10 -05:00
										 |  |  | // defaultAddr is the address the where the metrics are exported by default. | 
					
						
							|  |  |  | const defaultAddr = "localhost:9153" |