| 
									
										
										
										
											2017-01-23 15:40:47 -05:00
										 |  |  | package trace | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import ( | 
					
						
							|  |  |  | 	"fmt" | 
					
						
							| 
									
										
										
										
											2017-02-16 12:13:18 -05:00
										 |  |  | 	"strconv" | 
					
						
							| 
									
										
										
										
											2017-01-23 15:40:47 -05:00
										 |  |  | 	"strings" | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-21 22:51:47 -08:00
										 |  |  | 	"github.com/coredns/coredns/core/dnsserver" | 
					
						
							| 
									
										
										
										
											2017-09-14 09:36:06 +01:00
										 |  |  | 	"github.com/coredns/coredns/plugin" | 
					
						
							| 
									
										
										
										
											2017-01-23 15:40:47 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	"github.com/mholt/caddy" | 
					
						
							|  |  |  | ) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func init() { | 
					
						
							|  |  |  | 	caddy.RegisterPlugin("trace", caddy.Plugin{ | 
					
						
							|  |  |  | 		ServerType: "dns", | 
					
						
							|  |  |  | 		Action:     setup, | 
					
						
							|  |  |  | 	}) | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func setup(c *caddy.Controller) error { | 
					
						
							|  |  |  | 	t, err := traceParse(c) | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							| 
									
										
										
										
											2017-09-14 09:36:06 +01:00
										 |  |  | 		return plugin.Error("trace", err) | 
					
						
							| 
									
										
										
										
											2017-01-23 15:40:47 -05:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-09-14 09:36:06 +01:00
										 |  |  | 	dnsserver.GetConfig(c).AddPlugin(func(next plugin.Handler) plugin.Handler { | 
					
						
							| 
									
										
										
										
											2017-01-23 15:40:47 -05:00
										 |  |  | 		t.Next = next | 
					
						
							|  |  |  | 		return t | 
					
						
							|  |  |  | 	}) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-16 12:13:18 -05:00
										 |  |  | 	c.OnStartup(t.OnStartup) | 
					
						
							| 
									
										
										
										
											2017-01-23 15:40:47 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	return nil | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-01 10:41:54 -05:00
										 |  |  | func traceParse(c *caddy.Controller) (*trace, error) { | 
					
						
							| 
									
										
										
										
											2017-01-23 15:40:47 -05:00
										 |  |  | 	var ( | 
					
						
							| 
									
										
										
										
											2018-03-09 16:08:57 -04:00
										 |  |  | 		tr  = &trace{every: 1, serviceName: defServiceName} | 
					
						
							| 
									
										
										
										
											2017-01-23 15:40:47 -05:00
										 |  |  | 		err error | 
					
						
							|  |  |  | 	) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	cfg := dnsserver.GetConfig(c) | 
					
						
							| 
									
										
										
										
											2018-02-14 14:19:32 -05:00
										 |  |  | 	tr.ServiceEndpoint = cfg.HostAddresses() | 
					
						
							| 
									
										
										
										
											2017-02-16 12:13:18 -05:00
										 |  |  | 	for c.Next() { // trace | 
					
						
							|  |  |  | 		var err error | 
					
						
							|  |  |  | 		args := c.RemainingArgs() | 
					
						
							|  |  |  | 		switch len(args) { | 
					
						
							|  |  |  | 		case 0: | 
					
						
							| 
									
										
										
										
											2018-03-09 16:08:57 -04:00
										 |  |  | 			tr.EndpointType, tr.Endpoint, err = normalizeEndpoint(defEpType, "") | 
					
						
							| 
									
										
										
										
											2017-02-16 12:13:18 -05:00
										 |  |  | 		case 1: | 
					
						
							| 
									
										
										
										
											2018-03-09 16:08:57 -04:00
										 |  |  | 			tr.EndpointType, tr.Endpoint, err = normalizeEndpoint(defEpType, args[0]) | 
					
						
							| 
									
										
										
										
											2017-02-16 12:13:18 -05:00
										 |  |  | 		case 2: | 
					
						
							| 
									
										
										
										
											2018-03-09 16:08:57 -04:00
										 |  |  | 			epType := strings.ToLower(args[0]) | 
					
						
							|  |  |  | 			tr.EndpointType, tr.Endpoint, err = normalizeEndpoint(epType, args[1]) | 
					
						
							| 
									
										
										
										
											2017-02-16 12:13:18 -05:00
										 |  |  | 		default: | 
					
						
							|  |  |  | 			err = c.ArgErr() | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		if err != nil { | 
					
						
							|  |  |  | 			return tr, err | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2017-02-22 07:25:58 +00:00
										 |  |  | 		for c.NextBlock() { | 
					
						
							|  |  |  | 			switch c.Val() { | 
					
						
							|  |  |  | 			case "every": | 
					
						
							|  |  |  | 				args := c.RemainingArgs() | 
					
						
							|  |  |  | 				if len(args) != 1 { | 
					
						
							|  |  |  | 					return nil, c.ArgErr() | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 				tr.every, err = strconv.ParseUint(args[0], 10, 64) | 
					
						
							|  |  |  | 				if err != nil { | 
					
						
							|  |  |  | 					return nil, err | 
					
						
							|  |  |  | 				} | 
					
						
							| 
									
										
										
										
											2017-02-16 12:13:18 -05:00
										 |  |  | 			case "service": | 
					
						
							|  |  |  | 				args := c.RemainingArgs() | 
					
						
							| 
									
										
										
										
											2017-02-22 07:25:58 +00:00
										 |  |  | 				if len(args) != 1 { | 
					
						
							|  |  |  | 					return nil, c.ArgErr() | 
					
						
							|  |  |  | 				} | 
					
						
							| 
									
										
										
										
											2017-02-16 12:13:18 -05:00
										 |  |  | 				tr.serviceName = args[0] | 
					
						
							|  |  |  | 			case "client_server": | 
					
						
							|  |  |  | 				args := c.RemainingArgs() | 
					
						
							|  |  |  | 				if len(args) > 1 { | 
					
						
							|  |  |  | 					return nil, c.ArgErr() | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 				tr.clientServer = true | 
					
						
							|  |  |  | 				if len(args) == 1 { | 
					
						
							|  |  |  | 					tr.clientServer, err = strconv.ParseBool(args[0]) | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 				if err != nil { | 
					
						
							|  |  |  | 					return nil, err | 
					
						
							|  |  |  | 				} | 
					
						
							| 
									
										
										
										
											2017-01-23 15:40:47 -05:00
										 |  |  | 			} | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	return tr, err | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-09 16:08:57 -04:00
										 |  |  | func normalizeEndpoint(epType, ep string) (string, string, error) { | 
					
						
							|  |  |  | 	if _, ok := supportedProviders[epType]; !ok { | 
					
						
							|  |  |  | 		return "", "", fmt.Errorf("tracing endpoint type '%s' is not supported", epType) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if ep == "" { | 
					
						
							|  |  |  | 		ep = supportedProviders[epType] | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if epType == "zipkin" { | 
					
						
							| 
									
										
										
										
											2017-08-06 05:54:24 -07:00
										 |  |  | 		if !strings.Contains(ep, "http") { | 
					
						
							| 
									
										
										
										
											2017-01-23 15:40:47 -05:00
										 |  |  | 			ep = "http://" + ep + "/api/v1/spans" | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2018-03-09 16:08:57 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	return epType, ep, nil | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | var supportedProviders = map[string]string{ | 
					
						
							|  |  |  | 	"zipkin":  "localhost:9411", | 
					
						
							|  |  |  | 	"datadog": "localhost:8126", | 
					
						
							| 
									
										
										
										
											2017-01-23 15:40:47 -05:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | const ( | 
					
						
							| 
									
										
										
										
											2017-02-22 07:25:58 +00:00
										 |  |  | 	defEpType      = "zipkin" | 
					
						
							| 
									
										
										
										
											2017-02-16 12:13:18 -05:00
										 |  |  | 	defServiceName = "coredns" | 
					
						
							| 
									
										
										
										
											2017-01-23 15:40:47 -05:00
										 |  |  | ) |