From 4e6d800877fbad12ef5200dd06d5d227177fd5c5 Mon Sep 17 00:00:00 2001 From: xiandong Date: Fri, 17 Feb 2023 13:46:45 +0800 Subject: [PATCH] add parseEndpoint --- core/trace/agent.go | 3 ++- core/trace/config.go | 15 +++++---------- core/trace/config_test.go | 12 +++++++----- 3 files changed, 14 insertions(+), 16 deletions(-) diff --git a/core/trace/agent.go b/core/trace/agent.go index d8749912..4536cf5e 100644 --- a/core/trace/agent.go +++ b/core/trace/agent.go @@ -60,7 +60,8 @@ func createExporter(c Config) (sdktrace.SpanExporter, error) { 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()))) + 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/config.go b/core/trace/config.go index 5b181202..3c6cf8c8 100644 --- a/core/trace/config.go +++ b/core/trace/config.go @@ -15,18 +15,13 @@ type Config struct { Batcher string `json:",default=jaeger,options=jaeger|jaegerudp|zipkin|otlpgrpc|otlphttp"` } -func (c *Config) getEndpointHost() string { +func (c *Config) parseEndpoint() (host string, port string) { EndpointSlice := strings.Split(c.Endpoint, ":") if len(EndpointSlice) > 0 { - return strings.TrimSpace(EndpointSlice[0]) + host = strings.TrimSpace(EndpointSlice[0]) } - return "" -} - -func (c *Config) getEndpointPort() string { - EndpointSlice := strings.Split(c.Endpoint, ":") - if len(EndpointSlice) > 1 { - return strings.TrimSpace(EndpointSlice[1]) + if len(EndpointSlice) > 0 { + port = strings.TrimSpace(EndpointSlice[1]) } - return "" + return host, port } diff --git a/core/trace/config_test.go b/core/trace/config_test.go index b325948f..2eaefc7a 100644 --- a/core/trace/config_test.go +++ b/core/trace/config_test.go @@ -6,7 +6,7 @@ import ( "testing" ) -func TestConfig_getEndpointHost(t *testing.T) { +func TestConfig_parseEndpoint(t *testing.T) { logx.Disable() c1 := Config{ @@ -19,8 +19,10 @@ func TestConfig_getEndpointHost(t *testing.T) { Endpoint: "localhost:6831", Batcher: kindJaegerUdp, } - assert.NotEqual(t, "localhost", c1.getEndpointHost()) - assert.NotEqual(t, "14268", c1.getEndpointPort()) - assert.Equal(t, "localhost", c2.getEndpointHost()) - assert.Equal(t, "6831", c2.getEndpointPort()) + 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) }