| 
									
										
										
										
											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"
 | 
					
						
							| 
									
										
										
										
											2017-02-22 16:06:20 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	"github.com/mholt/caddy"
 | 
					
						
							| 
									
										
										
										
											2016-08-19 17:14:17 -07:00
										 |  |  | )
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											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 {
 | 
					
						
							|  |  |  | 	found := false
 | 
					
						
							| 
									
										
										
										
											2017-04-24 10:27:26 -04:00
										 |  |  | 	h := &handler{addr: defaultAddr}
 | 
					
						
							| 
									
										
										
										
											2016-08-19 17:14:17 -07:00
										 |  |  | 	for c.Next() {
 | 
					
						
							|  |  |  | 		if found {
 | 
					
						
							| 
									
										
										
										
											2017-09-14 09:36:06 +01:00
										 |  |  | 			return plugin.Error("pprof", c.Err("pprof can only be specified once"))
 | 
					
						
							| 
									
										
										
										
											2016-08-19 17:14:17 -07:00
										 |  |  | 		}
 | 
					
						
							| 
									
										
										
										
											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
										 |  |  | 		}
 | 
					
						
							|  |  |  | 		found = true
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	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
 |