Skip to content

Commit 9aa4dea

Browse files
committed
improve tracing
1 parent b955901 commit 9aa4dea

File tree

2 files changed

+30
-5
lines changed

2 files changed

+30
-5
lines changed

example/example.go

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,14 @@ func main() {
2323
zerologger.Configure(zerologger.Log{Level: zerologger.TraceLevel})
2424

2525
// configure tracing
26-
err := tracing.ConfigureOTLPoverHTTP(initialCtx, tracing.OTLPoverHTTPConfig{
27-
Endpoint: "http://localhost:4318/v1/traces",
28-
Ratio: 1,
29-
},
26+
tracingCfg := tracing.Config{
27+
Protocol: "otlp_http",
28+
OTLPEndpoint: "http://localhost:4318/v1/traces",
29+
Ratio: 1,
30+
}
31+
log.Info().Msg(tracingCfg.String())
32+
33+
err := tracing.StartWithContext(initialCtx, tracingCfg,
3034
semconv.ServiceName("pkg_example"),
3135
attribute.String("environment", "example"),
3236
)

tracing/all.go

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,11 +51,32 @@ type Config struct {
5151
Ratio float64 `yaml:"ratio" validate:"required,lte=1,gte=0"`
5252
}
5353

54+
func (c *Config) String() string {
55+
switch c.Protocol {
56+
case "otlp_http", "OTLP_HTTP":
57+
return fmt.Sprintf("Sending spans via OTLP HTTP into %s with ratio %.0f%%...",
58+
c.OTLPEndpoint, 100*c.Ratio)
59+
case "udp", "UDP":
60+
return fmt.Sprintf("Sending spans over compact thrift protocol over udp into %s:%s with ratio %.0f%%...",
61+
c.Host, c.Port, 100*c.Ratio)
62+
case "http", "HTTP":
63+
return fmt.Sprintf("Sending spans over compact thrift protocol over http into %s with ratio %.0f%%...",
64+
c.Endpoint, 100*c.Ratio)
65+
default:
66+
return fmt.Sprintf("Unknown protocol %s", c.Protocol)
67+
}
68+
}
69+
5470
// Start starts telemetry exporter
5571
func Start(cfg Config, extraAttributes ...attribute.KeyValue) (err error) {
72+
return StartWithContext(context.Background(), cfg, extraAttributes...)
73+
}
74+
75+
// StartWithContext starts telemetry exporter with context provided
76+
func StartWithContext(ctx context.Context, cfg Config, extraAttributes ...attribute.KeyValue) (err error) {
5677
switch cfg.Protocol {
5778
case "otlp_http", "OTLP_HTTP":
58-
return ConfigureOTLPoverHTTP(context.Background(), OTLPoverHTTPConfig{
79+
return ConfigureOTLPoverHTTP(ctx, OTLPoverHTTPConfig{
5980
Endpoint: cfg.Endpoint,
6081
Compression: true,
6182
Ratio: cfg.Ratio,

0 commit comments

Comments
 (0)