| 
									
										
										
										
											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" | 
					
						
							| 
									
										
										
										
											2019-03-23 10:43:15 +01:00
										 |  |  | 	"github.com/coredns/coredns/plugin/metrics/vars" | 
					
						
							| 
									
										
										
										
											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
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-07-03 09:04:47 +08:00
										 |  |  | 	"github.com/caddyserver/caddy" | 
					
						
							| 
									
										
										
										
											2016-08-19 17:14:17 -07:00
										 |  |  | ) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-04-25 11:45:09 +01:00
										 |  |  | var ( | 
					
						
							|  |  |  | 	log      = clog.NewWithPlugin("prometheus") | 
					
						
							| 
									
										
										
										
											2019-05-18 18:34:46 +01:00
										 |  |  | 	u        = uniq.New() | 
					
						
							|  |  |  | 	registry = newReg() | 
					
						
							| 
									
										
										
										
											2018-04-25 11:45:09 +01:00
										 |  |  | ) | 
					
						
							| 
									
										
										
										
											2018-04-22 21:40:33 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-09-20 08:02:30 +01:00
										 |  |  | func init() { plugin.Register("prometheus", setup) } | 
					
						
							| 
									
										
										
										
											2016-08-19 17:14:17 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  | func setup(c *caddy.Controller) error { | 
					
						
							| 
									
										
										
										
											2019-05-13 12:26:05 +01:00
										 |  |  | 	m, err := parse(c) | 
					
						
							| 
									
										
										
										
											2016-08-19 17:14:17 -07:00
										 |  |  | 	if err != nil { | 
					
						
							| 
									
										
										
										
											2017-09-14 09:36:06 +01:00
										 |  |  | 		return plugin.Error("prometheus", err) | 
					
						
							| 
									
										
										
										
											2016-08-19 17:14:17 -07:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2019-05-18 18:34:46 +01:00
										 |  |  | 	m.Reg = registry.getOrSet(m.Addr, m.Reg) | 
					
						
							| 
									
										
										
										
											2016-08-19 17:14:17 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-18 18:34:46 +01:00
										 |  |  | 	c.OnStartup(func() error { m.Reg = registry.getOrSet(m.Addr, m.Reg); u.Set(m.Addr, m.OnStartup); return nil }) | 
					
						
							|  |  |  | 	c.OnRestartFailed(func() error { m.Reg = registry.getOrSet(m.Addr, m.Reg); u.Set(m.Addr, m.OnStartup); return nil }) | 
					
						
							| 
									
										
										
										
											2016-08-19 17:14:17 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-18 18:34:46 +01:00
										 |  |  | 	c.OnStartup(func() error { return u.ForEach() }) | 
					
						
							|  |  |  | 	c.OnRestartFailed(func() error { return u.ForEach() }) | 
					
						
							| 
									
										
										
										
											2018-03-02 17:16:25 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-13 12:26:05 +01:00
										 |  |  | 	c.OnStartup(func() error { | 
					
						
							|  |  |  | 		conf := dnsserver.GetConfig(c) | 
					
						
							|  |  |  | 		for _, h := range conf.ListenHosts { | 
					
						
							|  |  |  | 			addrstr := conf.Transport + "://" + net.JoinHostPort(h, conf.Port) | 
					
						
							|  |  |  | 			for _, p := range conf.Handlers() { | 
					
						
							|  |  |  | 				vars.PluginEnabled.WithLabelValues(addrstr, conf.Zone, p.Name()).Set(1) | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2019-03-23 10:43:15 +01:00
										 |  |  | 		return nil | 
					
						
							|  |  |  | 	}) | 
					
						
							| 
									
										
										
										
											2019-05-13 12:26:05 +01:00
										 |  |  | 	c.OnRestartFailed(func() error { | 
					
						
							| 
									
										
										
										
											2019-03-23 10:43:15 +01:00
										 |  |  | 		conf := dnsserver.GetConfig(c) | 
					
						
							|  |  |  | 		for _, h := range conf.ListenHosts { | 
					
						
							|  |  |  | 			addrstr := conf.Transport + "://" + net.JoinHostPort(h, conf.Port) | 
					
						
							| 
									
										
										
										
											2019-05-13 12:26:05 +01:00
										 |  |  | 			for _, p := range conf.Handlers() { | 
					
						
							| 
									
										
										
										
											2019-03-23 10:43:15 +01:00
										 |  |  | 				vars.PluginEnabled.WithLabelValues(addrstr, conf.Zone, p.Name()).Set(1) | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		return nil | 
					
						
							|  |  |  | 	}) | 
					
						
							| 
									
										
										
										
											2019-05-13 12:26:05 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-04-21 17:43:02 +01:00
										 |  |  | 	c.OnRestart(m.OnRestart) | 
					
						
							| 
									
										
										
										
											2019-05-13 12:26:05 +01:00
										 |  |  | 	c.OnRestart(func() error { vars.PluginEnabled.Reset(); return nil }) | 
					
						
							| 
									
										
										
										
											2018-04-21 17:43:02 +01:00
										 |  |  | 	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) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-13 12:26:05 +01:00
										 |  |  | 	dnsserver.GetConfig(c).AddPlugin(func(next plugin.Handler) plugin.Handler { | 
					
						
							|  |  |  | 		m.Next = next | 
					
						
							|  |  |  | 		return m | 
					
						
							|  |  |  | 	}) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-08-19 17:14:17 -07:00
										 |  |  | 	return nil | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-13 12:26:05 +01:00
										 |  |  | func parse(c *caddy.Controller) (*Metrics, error) { | 
					
						
							| 
									
										
										
										
											2019-05-18 18:34:46 +01:00
										 |  |  | 	met := New(defaultAddr) | 
					
						
							| 
									
										
										
										
											2016-08-19 17:14:17 -07: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" |