浏览代码

add kindJaegerUdp

xiandong 2 年之前
父节点
当前提交
b6bedcd522
共有 3 个文件被更改,包括 36 次插入47 次删除
  1. 9 9
      core/trace/agent.go
  2. 9 25
      core/trace/agent_test.go
  3. 18 13
      core/trace/config.go

+ 9 - 9
core/trace/agent.go

@@ -18,10 +18,11 @@ import (
 )
 )
 
 
 const (
 const (
-	kindJaeger   = "jaeger"
-	kindZipkin   = "zipkin"
-	kindOtlpGrpc = "otlpgrpc"
-	kindOtlpHttp = "otlphttp"
+	kindJaeger    = "jaeger"
+	kindJaegerUdp = "jaegerudp"
+	kindZipkin    = "zipkin"
+	kindOtlpGrpc  = "otlpgrpc"
+	kindOtlpHttp  = "otlphttp"
 )
 )
 
 
 var (
 var (
@@ -35,7 +36,7 @@ func StartAgent(c Config) {
 	lock.Lock()
 	lock.Lock()
 	defer lock.Unlock()
 	defer lock.Unlock()
 
 
-	_, ok := agents[c.getEndpoint()]
+	_, ok := agents[c.Endpoint]
 	if ok {
 	if ok {
 		return
 		return
 	}
 	}
@@ -45,7 +46,7 @@ func StartAgent(c Config) {
 		return
 		return
 	}
 	}
 
 
-	agents[c.getEndpoint()] = lang.Placeholder
+	agents[c.Endpoint] = lang.Placeholder
 }
 }
 
 
 // StopAgent shuts down the span processors in the order they were registered.
 // StopAgent shuts down the span processors in the order they were registered.
@@ -57,10 +58,9 @@ func createExporter(c Config) (sdktrace.SpanExporter, error) {
 	// Just support jaeger and zipkin now, more for later
 	// Just support jaeger and zipkin now, more for later
 	switch c.Batcher {
 	switch c.Batcher {
 	case kindJaeger:
 	case kindJaeger:
-		if c.isAgentEndPoint() {
-			return jaeger.New(jaeger.WithAgentEndpoint(jaeger.WithAgentHost(c.AgentHost), jaeger.WithAgentPort(c.AgentPort)))
-		}
 		return jaeger.New(jaeger.WithCollectorEndpoint(jaeger.WithEndpoint(c.Endpoint)))
 		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:
 	case kindZipkin:
 		return zipkin.New(c.Endpoint)
 		return zipkin.New(c.Endpoint)
 	case kindOtlpGrpc:
 	case kindOtlpGrpc:

+ 9 - 25
core/trace/agent_test.go

@@ -11,12 +11,11 @@ func TestStartAgent(t *testing.T) {
 	logx.Disable()
 	logx.Disable()
 
 
 	const (
 	const (
-		endpoint1  = "localhost:1234"
-		endpoint2  = "remotehost:1234"
-		endpoint3  = "localhost:1235"
-		endpoint4  = "localhost:1236"
-		agentHost1 = "localhost"
-		agentPort1 = "6831"
+		endpoint1 = "localhost:1234"
+		endpoint2 = "remotehost:1234"
+		endpoint3 = "localhost:1235"
+		endpoint4 = "localhost:1236"
+		endpoint5 = "localhost:6831"
 	)
 	)
 	c1 := Config{
 	c1 := Config{
 		Name: "foo",
 		Name: "foo",
@@ -47,17 +46,9 @@ func TestStartAgent(t *testing.T) {
 		Batcher:  kindOtlpHttp,
 		Batcher:  kindOtlpHttp,
 	}
 	}
 	c7 := Config{
 	c7 := Config{
-		Name:      "jaegerUDP",
-		AgentHost: agentHost1,
-		AgentPort: agentPort1,
-		Batcher:   kindJaeger,
-	}
-	c8 := Config{
-		Name:      "jaegerUDP",
-		AgentHost: agentHost1,
-		AgentPort: agentPort1,
-		Endpoint:  endpoint1,
-		Batcher:   kindJaeger,
+		Name:     "UDP",
+		Endpoint: endpoint5,
+		Batcher:  kindJaegerUdp,
 	}
 	}
 
 
 	StartAgent(c1)
 	StartAgent(c1)
@@ -68,7 +59,6 @@ func TestStartAgent(t *testing.T) {
 	StartAgent(c5)
 	StartAgent(c5)
 	StartAgent(c6)
 	StartAgent(c6)
 	StartAgent(c7)
 	StartAgent(c7)
-	StartAgent(c8)
 
 
 	lock.Lock()
 	lock.Lock()
 	defer lock.Unlock()
 	defer lock.Unlock()
@@ -81,12 +71,6 @@ func TestStartAgent(t *testing.T) {
 	assert.True(t, ok)
 	assert.True(t, ok)
 	_, ok = agents[endpoint2]
 	_, ok = agents[endpoint2]
 	assert.False(t, ok)
 	assert.False(t, ok)
-	_, ok = agents[c2.getEndpoint()]
-	assert.True(t, ok)
-	_, ok = agents[c3.getEndpoint()]
-	assert.False(t, ok)
-	_, ok = agents[c7.getEndpoint()]
-	assert.True(t, ok)
-	_, ok = agents[c8.getEndpoint()]
+	_, ok = agents[endpoint5]
 	assert.True(t, ok)
 	assert.True(t, ok)
 }
 }

+ 18 - 13
core/trace/config.go

@@ -1,27 +1,32 @@
 package trace
 package trace
 
 
-import "fmt"
+import (
+	"strings"
+)
 
 
 // TraceName represents the tracing name.
 // TraceName represents the tracing name.
 const TraceName = "go-zero"
 const TraceName = "go-zero"
 
 
 // A Config is an opentelemetry config.
 // A Config is an opentelemetry config.
 type Config struct {
 type Config struct {
-	Name      string  `json:",optional"`
-	AgentHost string  `json:",optional"`
-	AgentPort string  `json:",optional"`
-	Endpoint  string  `json:",optional"`
-	Sampler   float64 `json:",default=1.0"`
-	Batcher   string  `json:",default=jaeger,options=jaeger|jaegerudp|zipkin|otlpgrpc|otlphttp"`
+	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) isAgentEndPoint() bool {
-	return len(c.AgentHost) != 0 && len(c.AgentPort) != 0
+func (c *Config) getEndpointHost() string {
+	EndpointSlice := strings.Split(c.Endpoint, ":")
+	if len(EndpointSlice) > 0 {
+		return strings.TrimSpace(EndpointSlice[0])
+	}
+	return ""
 }
 }
 
 
-func (c *Config) getEndpoint() string {
-	if c.isAgentEndPoint() {
-		return fmt.Sprintf("%s:%s", c.AgentHost, c.AgentPort)
+func (c *Config) getEndpointPort() string {
+	EndpointSlice := strings.Split(c.Endpoint, ":")
+	if len(EndpointSlice) > 1 {
+		return strings.TrimSpace(EndpointSlice[1])
 	}
 	}
-	return c.Endpoint
+	return ""
 }
 }