Repository: zeromicro/go-zero
Pull request #2889 (2023-02-16 06:51:25)

add jaeger with UDP
export trace log to jaeger with UDP

codecov[bot]: # [Codecov](https://codecov.io/gh/zeromicro/go-zero/pull/2889?src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=None) Report
> Merging [#2889](https://codecov.io/gh/zeromicro/go-zero/pull/2889?src=pr&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=None) (274f774) into [master](https://codecov.io/gh/zeromicro/go-zero/commit/32e3116ee318b3c940bff7fbad87f527381c16fa?el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=None) (32e3116) will **decrease** coverage by `0.04%`.
> The diff coverage is `40.00%`.

> :exclamation: Current head 274f774 differs from pull request most recent head 68d3528. Consider uploading reports for the commit 68d3528 to get more accurate results


| [Impacted Files](https://codecov.io/gh/zeromicro/go-zero/pull/2889?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=None) | Coverage Δ | |
|---|---|---|
| [core/trace/agent.go](https://codecov.io/gh/zeromicro/go-zero/pull/2889?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=None#diff-Y29yZS90cmFjZS9hZ2VudC5nbw==) | `87.87% <40.00%> (-4.06%)` | :arrow_down: |
| [core/logx/rotatelogger.go](https://codecov.io/gh/zeromicro/go-zero/pull/2889?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=None#diff-Y29yZS9sb2d4L3JvdGF0ZWxvZ2dlci5nbw==) | `71.58% <0.00%> (-1.11%)` | :arrow_down: |


Patch:
diff --git a/core/trace/agent.go b/core/trace/agent.go
index 6fbe2e78..939c1d9e 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"
@@ -57,6 +58,10 @@ func createExporter(c Config) (sdktrace.SpanExporter, error) {
 	// Just support jaeger and zipkin now, more for later
 	switch c.Batcher {
 	case kindJaeger:
+		u, _ := url.Parse(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 kindZipkin:
 		return zipkin.New(c.Endpoint)


Test patch:
diff --git a/core/trace/agent_test.go b/core/trace/agent_test.go
index 1d20694e..853fa6ba 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 = "udp://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:  kindJaeger,
+	}
 
 	StartAgent(c1)
 	StartAgent(c1)
@@ -52,16 +58,19 @@ func TestStartAgent(t *testing.T) {
 	StartAgent(c4)
 	StartAgent(c5)
 	StartAgent(c6)
+	StartAgent(c7)
 
 	lock.Lock()
 	defer lock.Unlock()
 
 	// because remotehost cannot be resolved
-	assert.Equal(t, 4, len(agents))
+	assert.Equal(t, 5, len(agents))
 	_, ok := agents[""]
 	assert.True(t, ok)
 	_, ok = agents[endpoint1]
 	assert.True(t, ok)
 	_, ok = agents[endpoint2]
 	assert.False(t, ok)
+	_, ok = agents[endpoint5]
+	assert.True(t, ok)
 }