diff --git a/core/trace/agent.go b/core/trace/agent.go index 4536cf5e..4dc4e935 100644 --- a/core/trace/agent.go +++ b/core/trace/agent.go @@ -3,6 +3,7 @@ package trace import ( "context" "fmt" + "net/url" "sync" "github.com/zeromicro/go-zero/core/lang" @@ -18,11 +19,10 @@ import ( ) const ( - kindJaeger = "jaeger" - kindJaegerUdp = "jaegerudp" - kindZipkin = "zipkin" - kindOtlpGrpc = "otlpgrpc" - kindOtlpHttp = "otlphttp" + kindJaeger = "jaeger" + kindZipkin = "zipkin" + kindOtlpGrpc = "otlpgrpc" + kindOtlpHttp = "otlphttp" ) var ( @@ -58,10 +58,14 @@ func createExporter(c Config) (sdktrace.SpanExporter, error) { // Just support jaeger and zipkin now, more for later switch c.Batcher { case kindJaeger: + u, parseErr := url.Parse(c.Endpoint) + if parseErr != nil { + return nil, fmt.Errorf("invalid jaeger endpoint: %s", c.Endpoint) + } + if u.Scheme == "udp" { + return jaeger.New(jaeger.WithAgentEndpoint(jaeger.WithAgentHost(u.Hostname()), jaeger.WithAgentPort(u.Port()))) + } return jaeger.New(jaeger.WithCollectorEndpoint(jaeger.WithEndpoint(c.Endpoint))) - case kindJaegerUdp: - host, port := c.parseEndpoint() - return jaeger.New(jaeger.WithAgentEndpoint(jaeger.WithAgentHost(host), jaeger.WithAgentPort(port))) case kindZipkin: return zipkin.New(c.Endpoint) case kindOtlpGrpc: diff --git a/core/trace/agent_test.go b/core/trace/agent_test.go index e77bd578..853fa6ba 100644 --- a/core/trace/agent_test.go +++ b/core/trace/agent_test.go @@ -15,7 +15,7 @@ func TestStartAgent(t *testing.T) { endpoint2 = "remotehost:1234" endpoint3 = "localhost:1235" endpoint4 = "localhost:1236" - endpoint5 = "localhost:6831" + endpoint5 = "udp://localhost:6831" ) c1 := Config{ Name: "foo", @@ -48,7 +48,7 @@ func TestStartAgent(t *testing.T) { c7 := Config{ Name: "UDP", Endpoint: endpoint5, - Batcher: kindJaegerUdp, + Batcher: kindJaeger, } StartAgent(c1) diff --git a/core/trace/config.go b/core/trace/config.go index 11f8fd70..83c62c3c 100644 --- a/core/trace/config.go +++ b/core/trace/config.go @@ -1,9 +1,5 @@ package trace -import ( - "strings" -) - // TraceName represents the tracing name. const TraceName = "go-zero" @@ -12,16 +8,5 @@ type Config struct { Name string `json:",optional"` Endpoint string `json:",optional"` Sampler float64 `json:",default=1.0"` - Batcher string `json:",default=jaeger,options=jaeger|jaegerudp|zipkin|otlpgrpc|otlphttp"` -} - -func (c *Config) parseEndpoint() (host string, port string) { - EndpointSlice := strings.Split(c.Endpoint, ":") - if len(EndpointSlice) > 0 { - host = strings.TrimSpace(EndpointSlice[0]) - } - if len(EndpointSlice) > 1 { - port = strings.TrimSpace(EndpointSlice[1]) - } - return host, port + Batcher string `json:",default=jaeger,options=jaeger|zipkin|otlpgrpc|otlphttp"` } diff --git a/core/trace/config_test.go b/core/trace/config_test.go deleted file mode 100644 index 2eaefc7a..00000000 --- a/core/trace/config_test.go +++ /dev/null @@ -1,28 +0,0 @@ -package trace - -import ( - "github.com/stretchr/testify/assert" - "github.com/zeromicro/go-zero/core/logx" - "testing" -) - -func TestConfig_parseEndpoint(t *testing.T) { - logx.Disable() - - c1 := Config{ - Name: "not UDP", - Endpoint: "http://localhost:14268/api/traces", - Batcher: kindJaegerUdp, - } - c2 := Config{ - Name: "UDP", - Endpoint: "localhost:6831", - Batcher: kindJaegerUdp, - } - host1, port1 := c1.parseEndpoint() - assert.NotEqual(t, "localhost", host1) - assert.NotEqual(t, "14268", port1) - host2, port2 := c2.parseEndpoint() - assert.Equal(t, "localhost", host2) - assert.Equal(t, "6831", port2) -}