From 77a72b16e910f5b52820308c4469b0a41c63a9ed Mon Sep 17 00:00:00 2001 From: xiandong Date: Fri, 17 Feb 2023 12:58:31 +0800 Subject: [PATCH] add kindJaegerUdp --- core/trace/agent.go | 11 +++++++---- core/trace/agent_test.go | 9 +++++++++ core/trace/config.go | 23 ++++++++++++++++++++++- 3 files changed, 38 insertions(+), 5 deletions(-) diff --git a/core/trace/agent.go b/core/trace/agent.go index 6fbe2e78..d8749912 100644 --- a/core/trace/agent.go +++ b/core/trace/agent.go @@ -18,10 +18,11 @@ import ( ) const ( - kindJaeger = "jaeger" - kindZipkin = "zipkin" - kindOtlpGrpc = "otlpgrpc" - kindOtlpHttp = "otlphttp" + kindJaeger = "jaeger" + kindJaegerUdp = "jaegerudp" + kindZipkin = "zipkin" + kindOtlpGrpc = "otlpgrpc" + kindOtlpHttp = "otlphttp" ) var ( @@ -58,6 +59,8 @@ func createExporter(c Config) (sdktrace.SpanExporter, error) { switch c.Batcher { case kindJaeger: return jaeger.New(jaeger.WithCollectorEndpoint(jaeger.WithEndpoint(c.Endpoint))) + case kindJaegerUdp: + return jaeger.New(jaeger.WithAgentEndpoint(jaeger.WithAgentHost(c.getEndpointHost()), jaeger.WithAgentPort(c.getEndpointPort()))) case kindZipkin: return zipkin.New(c.Endpoint) case kindOtlpGrpc: diff --git a/core/trace/agent_test.go b/core/trace/agent_test.go index 1d20694e..72727bd2 100644 --- a/core/trace/agent_test.go +++ b/core/trace/agent_test.go @@ -15,6 +15,7 @@ func TestStartAgent(t *testing.T) { endpoint2 = "remotehost:1234" endpoint3 = "localhost:1235" endpoint4 = "localhost:1236" + endpoint5 = "localhost:6831" ) c1 := Config{ Name: "foo", @@ -44,6 +45,11 @@ func TestStartAgent(t *testing.T) { Endpoint: endpoint4, Batcher: kindOtlpHttp, } + c7 := Config{ + Name: "UDP", + Endpoint: endpoint5, + Batcher: kindJaegerUdp, + } StartAgent(c1) StartAgent(c1) @@ -52,6 +58,7 @@ func TestStartAgent(t *testing.T) { StartAgent(c4) StartAgent(c5) StartAgent(c6) + StartAgent(c7) lock.Lock() defer lock.Unlock() @@ -64,4 +71,6 @@ func TestStartAgent(t *testing.T) { assert.True(t, ok) _, ok = agents[endpoint2] assert.False(t, ok) + _, ok = agents[endpoint5] + assert.True(t, ok) } diff --git a/core/trace/config.go b/core/trace/config.go index 83c62c3c..a6652393 100644 --- a/core/trace/config.go +++ b/core/trace/config.go @@ -1,5 +1,9 @@ package trace +import ( + "strings" +) + // TraceName represents the tracing name. const TraceName = "go-zero" @@ -8,5 +12,22 @@ type Config struct { Name string `json:",optional"` Endpoint string `json:",optional"` Sampler float64 `json:",default=1.0"` - Batcher string `json:",default=jaeger,options=jaeger|zipkin|otlpgrpc|otlphttp"` + Batcher string `json:",default=jaeger,options=jaeger|jaegerudp|zipkin|otlpgrpc|otlphttp"` +} + +func (c *Config) getEndpointHost() string { + EndpointSlice := strings.Split(c.Endpoint, ":") + if len(EndpointSlice) > 0 { + return strings.TrimSpace(EndpointSlice[0]) + } + return "" +} + +func (c *Config) getEndpointPort() string { + EndpointSlice := strings.Split(c.Endpoint, ":") + if len(EndpointSlice) > 1 { + return strings.TrimSpace(EndpointSlice[1]) + } + + return "" }