mirror of
https://github.com/zeromicro/go-zero.git
synced 2025-01-23 17:20:24 +08:00
opentelemetry support AgentHost, AgentPort
This commit is contained in:
parent
e575bf8317
commit
12060c9c0c
@ -35,7 +35,7 @@ func StartAgent(c Config) {
|
|||||||
lock.Lock()
|
lock.Lock()
|
||||||
defer lock.Unlock()
|
defer lock.Unlock()
|
||||||
|
|
||||||
_, ok := agents[c.Endpoint]
|
_, ok := agents[c.getEndpoint()]
|
||||||
if ok {
|
if ok {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@ -45,7 +45,7 @@ func StartAgent(c Config) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
agents[c.Endpoint] = lang.Placeholder
|
agents[c.getEndpoint()] = 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,11 +57,10 @@ 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.AgentHost != "" && c.AgentPort != "" {
|
if c.isAgentEndPoint() {
|
||||||
return jaeger.New(jaeger.WithAgentEndpoint(jaeger.WithAgentHost(c.AgentHost), jaeger.WithAgentPort(c.AgentPort)))
|
return jaeger.New(jaeger.WithAgentEndpoint(jaeger.WithAgentHost(c.AgentHost), jaeger.WithAgentPort(c.AgentPort)))
|
||||||
} else {
|
|
||||||
return jaeger.New(jaeger.WithCollectorEndpoint(jaeger.WithEndpoint(c.Endpoint)))
|
|
||||||
}
|
}
|
||||||
|
return jaeger.New(jaeger.WithCollectorEndpoint(jaeger.WithEndpoint(c.Endpoint)))
|
||||||
case kindZipkin:
|
case kindZipkin:
|
||||||
return zipkin.New(c.Endpoint)
|
return zipkin.New(c.Endpoint)
|
||||||
case kindOtlpGrpc:
|
case kindOtlpGrpc:
|
||||||
|
@ -15,6 +15,8 @@ func TestStartAgent(t *testing.T) {
|
|||||||
endpoint2 = "remotehost:1234"
|
endpoint2 = "remotehost:1234"
|
||||||
endpoint3 = "localhost:1235"
|
endpoint3 = "localhost:1235"
|
||||||
endpoint4 = "localhost:1236"
|
endpoint4 = "localhost:1236"
|
||||||
|
agentHost1 = "localhost"
|
||||||
|
agentPort1 = "6831"
|
||||||
)
|
)
|
||||||
c1 := Config{
|
c1 := Config{
|
||||||
Name: "foo",
|
Name: "foo",
|
||||||
@ -44,6 +46,19 @@ func TestStartAgent(t *testing.T) {
|
|||||||
Endpoint: endpoint4,
|
Endpoint: endpoint4,
|
||||||
Batcher: kindOtlpHttp,
|
Batcher: kindOtlpHttp,
|
||||||
}
|
}
|
||||||
|
c7 := Config{
|
||||||
|
Name: "jaegerUDP",
|
||||||
|
AgentHost: agentHost1,
|
||||||
|
AgentPort: agentPort1,
|
||||||
|
Batcher: kindJaeger,
|
||||||
|
}
|
||||||
|
c8 := Config{
|
||||||
|
Name: "jaegerUDP",
|
||||||
|
AgentHost: agentHost1,
|
||||||
|
AgentPort: agentPort1,
|
||||||
|
Endpoint: endpoint1,
|
||||||
|
Batcher: kindJaeger,
|
||||||
|
}
|
||||||
|
|
||||||
StartAgent(c1)
|
StartAgent(c1)
|
||||||
StartAgent(c1)
|
StartAgent(c1)
|
||||||
@ -52,16 +67,26 @@ func TestStartAgent(t *testing.T) {
|
|||||||
StartAgent(c4)
|
StartAgent(c4)
|
||||||
StartAgent(c5)
|
StartAgent(c5)
|
||||||
StartAgent(c6)
|
StartAgent(c6)
|
||||||
|
StartAgent(c7)
|
||||||
|
StartAgent(c8)
|
||||||
|
|
||||||
lock.Lock()
|
lock.Lock()
|
||||||
defer lock.Unlock()
|
defer lock.Unlock()
|
||||||
|
|
||||||
// because remotehost cannot be resolved
|
// because remotehost cannot be resolved
|
||||||
assert.Equal(t, 4, len(agents))
|
assert.Equal(t, 5, len(agents))
|
||||||
_, ok := agents[""]
|
_, ok := agents[""]
|
||||||
assert.True(t, ok)
|
assert.True(t, ok)
|
||||||
_, ok = agents[endpoint1]
|
_, ok = agents[endpoint1]
|
||||||
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()]
|
||||||
|
assert.True(t, ok)
|
||||||
}
|
}
|
||||||
|
@ -1,5 +1,7 @@
|
|||||||
package trace
|
package trace
|
||||||
|
|
||||||
|
import "fmt"
|
||||||
|
|
||||||
// TraceName represents the tracing name.
|
// TraceName represents the tracing name.
|
||||||
const TraceName = "go-zero"
|
const TraceName = "go-zero"
|
||||||
|
|
||||||
@ -10,5 +12,16 @@ type Config struct {
|
|||||||
AgentPort string `json:",optional"`
|
AgentPort string `json:",optional"`
|
||||||
Endpoint string `json:",optional"`
|
Endpoint string `json:",optional"`
|
||||||
Sampler float64 `json:",default=1.0"`
|
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) isAgentEndPoint() bool {
|
||||||
|
return len(c.AgentHost) != 0 && len(c.AgentPort) != 0
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *Config) getEndpoint() string {
|
||||||
|
if c.isAgentEndPoint() {
|
||||||
|
return fmt.Sprintf("%s:%s", c.AgentHost, c.AgentPort)
|
||||||
|
}
|
||||||
|
return c.Endpoint
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user