Repository: zeromicro/go-zero
Pull request #3292 (2023-05-28 09:47:10)

feat: support optional otel global initialization for #3284
Support optional otel global initialization for #3284

codecov[bot]: ## [Codecov](https://app.codecov.io/gh/zeromicro/go-zero/pull/3292?src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=None) Report
> Merging [#3292](https://app.codecov.io/gh/zeromicro/go-zero/pull/3292?src=pr&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=None) (e3af1e4) into [master](https://app.codecov.io/gh/zeromicro/go-zero/commit/3726851c7fbbf3f23c465f9a5c0dc1cfd3e7e36b?el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=None) (3726851) will **increase** coverage by `0.02%`.
> The diff coverage is `100.00%`.


| [Impacted Files](https://app.codecov.io/gh/zeromicro/go-zero/pull/3292?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=None) | Coverage Δ | |
|---|---|---|
| [core/stores/sqlc/cachedsql.go](https://app.codecov.io/gh/zeromicro/go-zero/pull/3292?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=None#diff-Y29yZS9zdG9yZXMvc3FsYy9jYWNoZWRzcWwuZ28=) | `97.11% <100.00%> (+3.84%)` | :arrow_up: |
| [core/trace/agent.go](https://app.codecov.io/gh/zeromicro/go-zero/pull/3292?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=None#diff-Y29yZS90cmFjZS9hZ2VudC5nbw==) | `94.11% <100.00%> (+0.29%)` | :arrow_up: |


Patch:
diff --git a/core/trace/agent.go b/core/trace/agent.go
index 5bae9f6d..5823a0a6 100644
--- a/core/trace/agent.go
+++ b/core/trace/agent.go
@@ -33,6 +33,11 @@ var (
 
 // StartAgent starts an opentelemetry agent.
 func StartAgent(c Config) {
+
+	if c.Disabled {
+		return
+	}
+
 	lock.Lock()
 	defer lock.Unlock()
 
diff --git a/core/trace/config.go b/core/trace/config.go
index b59af555..d1b32749 100644
--- a/core/trace/config.go
+++ b/core/trace/config.go
@@ -16,5 +16,8 @@ type Config struct {
 	// OtlpHttpPath represents the path for OTLP HTTP transport.
 	// For example
 	// /v1/traces
-	OtlpHttpPath string            `json:",optional"`
+	OtlpHttpPath string `json:",optional"`
+
+	// Disabled indicates whether StartAgent should be called when starting the server.
+	Disabled bool
 }


Test patch:
diff --git a/core/trace/agent_test.go b/core/trace/agent_test.go
index cbced387..36edba21 100644
--- a/core/trace/agent_test.go
+++ b/core/trace/agent_test.go
@@ -16,6 +16,7 @@ func TestStartAgent(t *testing.T) {
 		endpoint3 = "localhost:1235"
 		endpoint4 = "localhost:1236"
 		endpoint5 = "udp://localhost:6831"
+		endpoint6 = "localhost:1237"
 	)
 	c1 := Config{
 		Name: "foo",
@@ -57,6 +58,11 @@ func TestStartAgent(t *testing.T) {
 		Endpoint: endpoint5,
 		Batcher:  kindJaeger,
 	}
+	c8 := Config{
+		Disabled: true,
+		Endpoint: endpoint6,
+		Batcher:  kindJaeger,
+	}
 
 	StartAgent(c1)
 	StartAgent(c1)
@@ -66,6 +72,7 @@ func TestStartAgent(t *testing.T) {
 	StartAgent(c5)
 	StartAgent(c6)
 	StartAgent(c7)
+	StartAgent(c8)
 
 	lock.Lock()
 	defer lock.Unlock()
@@ -80,4 +87,6 @@ func TestStartAgent(t *testing.T) {
 	assert.False(t, ok)
 	_, ok = agents[endpoint5]
 	assert.True(t, ok)
+	_, ok = agents[endpoint6]
+	assert.False(t, ok)
 }