| 
									
										
										
										
											2016-08-19 17:14:17 -07:00
										 |  |  | package pprof | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import ( | 
					
						
							| 
									
										
										
										
											2017-04-24 10:27:26 -04:00
										 |  |  | 	"net" | 
					
						
							| 
									
										
										
										
											2016-08-19 17:14:17 -07:00
										 |  |  | 	"sync" | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											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" | 
					
						
							| 
									
										
										
										
											2017-02-22 16:06:20 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	"github.com/mholt/caddy" | 
					
						
							| 
									
										
										
										
											2016-08-19 17:14:17 -07:00
										 |  |  | ) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-04-22 21:40:33 +01:00
										 |  |  | var log = clog.NewWithPlugin("pprof") | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-04-24 10:27:26 -04:00
										 |  |  | const defaultAddr = "localhost:6053" | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-08-19 17:14:17 -07:00
										 |  |  | func init() { | 
					
						
							|  |  |  | 	caddy.RegisterPlugin("pprof", caddy.Plugin{ | 
					
						
							|  |  |  | 		ServerType: "dns", | 
					
						
							|  |  |  | 		Action:     setup, | 
					
						
							|  |  |  | 	}) | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func setup(c *caddy.Controller) error { | 
					
						
							| 
									
										
										
										
											2017-04-24 10:27:26 -04:00
										 |  |  | 	h := &handler{addr: defaultAddr} | 
					
						
							| 
									
										
										
										
											2018-02-28 18:16:05 -08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	i := 0 | 
					
						
							| 
									
										
										
										
											2016-08-19 17:14:17 -07:00
										 |  |  | 	for c.Next() { | 
					
						
							| 
									
										
										
										
											2018-02-28 18:16:05 -08:00
										 |  |  | 		if i > 0 { | 
					
						
							|  |  |  | 			return plugin.Error("pprof", plugin.ErrOnce) | 
					
						
							| 
									
										
										
										
											2016-08-19 17:14:17 -07:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2018-02-28 18:16:05 -08:00
										 |  |  | 		i++ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-04-24 10:27:26 -04:00
										 |  |  | 		args := c.RemainingArgs() | 
					
						
							|  |  |  | 		if len(args) == 1 { | 
					
						
							|  |  |  | 			h.addr = args[0] | 
					
						
							|  |  |  | 			_, _, e := net.SplitHostPort(h.addr) | 
					
						
							|  |  |  | 			if e != nil { | 
					
						
							|  |  |  | 				return e | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		if len(args) > 1 { | 
					
						
							| 
									
										
										
										
											2017-09-14 09:36:06 +01:00
										 |  |  | 			return plugin.Error("pprof", c.ArgErr()) | 
					
						
							| 
									
										
										
										
											2016-08-19 17:14:17 -07:00
										 |  |  | 		} | 
					
						
							|  |  |  | 		if c.NextBlock() { | 
					
						
							| 
									
										
										
										
											2017-09-14 09:36:06 +01:00
										 |  |  | 			return plugin.Error("pprof", c.ArgErr()) | 
					
						
							| 
									
										
										
										
											2016-08-19 17:14:17 -07:00
										 |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	pprofOnce.Do(func() { | 
					
						
							| 
									
										
										
										
											2016-09-23 09:14:12 +01:00
										 |  |  | 		c.OnStartup(h.Startup) | 
					
						
							|  |  |  | 		c.OnShutdown(h.Shutdown) | 
					
						
							| 
									
										
										
										
											2016-08-19 17:14:17 -07:00
										 |  |  | 	}) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return nil | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | var pprofOnce sync.Once |