Skip to content

Commit 1ce1c8f

Browse files
committed
1.7: Add back support for OTLP config from toml
This was broken due to 9360e37 (backport from main). The backport should not have broken the toml config so this more or less restores that. This treats the env vars as preferred. It is not perfect since, as an example, `Endpoint` could be set on the config and the env to set the protocol could be set and these could conflict, however I think this is an unlikely scenario and people should use one or the other, not both. This change converts the config into the relevant environment variables before initializing tracers. Signed-off-by: Brian Goff <[email protected]>
1 parent 0a9ac76 commit 1ce1c8f

1 file changed

Lines changed: 39 additions & 2 deletions

File tree

tracing/plugin/otlp.go

Lines changed: 39 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,15 @@ const (
5050
otlpTracesEndpointEnv = "OTEL_EXPORTER_OTLP_TRACES_ENDPOINT"
5151
otlpProtocolEnv = "OTEL_EXPORTER_OTLP_PROTOCOL"
5252
otlpTracesProtocolEnv = "OTEL_EXPORTER_OTLP_TRACES_PROTOCOL"
53+
otlpInsecureEnv = "OTEL_EXPORTER_OTLP_INSECURE"
54+
otlpTracesInsecureEnv = "OTEL_EXPORTER_OTLP_TRACES_INSECURE"
5355

54-
otelTracesExporterEnv = "OTEL_TRACES_EXPORTER"
55-
otelServiceNameEnv = "OTEL_SERVICE_NAME"
56+
otelTracesExporterEnv = "OTEL_TRACES_EXPORTER"
57+
otelServiceNameEnv = "OTEL_SERVICE_NAME"
58+
otelTracesSamplerEnv = "OTEL_TRACES_SAMPLER"
59+
otelTracesSamplerArgEnv = "OTEL_TRACES_SAMPLER_ARG"
60+
61+
traceIDRatioSampler = "traceidratio"
5662
)
5763

5864
func init() {
@@ -64,6 +70,7 @@ func init() {
6470
if err := warnOTLPConfig(ic); err != nil {
6571
return nil, err
6672
}
73+
convertOTLPToEnv(ic)
6774
if err := checkDisabled(); err != nil {
6875
return nil, err
6976
}
@@ -91,6 +98,7 @@ func init() {
9198
if err := warnTraceConfig(ic); err != nil {
9299
return nil, err
93100
}
101+
convertToTracesEnv(ic)
94102
if err := checkDisabled(); err != nil {
95103
return nil, err
96104
}
@@ -135,6 +143,35 @@ type TraceConfig struct {
135143
TraceSamplingRatio float64 `toml:"sampling_ratio,omitempty"`
136144
}
137145

146+
func convertOTLPToEnv(ic *plugin.InitContext) {
147+
cfg := ic.Config.(*OTLPConfig)
148+
149+
if cfg.Endpoint != "" && os.Getenv(otlpEndpointEnv) == "" && os.Getenv(otlpTracesEndpointEnv) == "" {
150+
os.Setenv(otlpEndpointEnv, cfg.Endpoint)
151+
}
152+
153+
if cfg.Protocol != "" && os.Getenv(otlpProtocolEnv) == "" && os.Getenv(otlpTracesProtocolEnv) == "" {
154+
os.Setenv(otlpProtocolEnv, cfg.Protocol)
155+
}
156+
157+
if cfg.Insecure && os.Getenv(otlpInsecureEnv) == "" && os.Getenv(otlpTracesInsecureEnv) == "" {
158+
os.Setenv(otlpInsecureEnv, "true")
159+
}
160+
}
161+
162+
func convertToTracesEnv(ic *plugin.InitContext) {
163+
cfg := ic.Config.(*TraceConfig)
164+
165+
if cfg.ServiceName != "" && os.Getenv(otelServiceNameEnv) == "" {
166+
os.Setenv(otelServiceNameEnv, cfg.ServiceName)
167+
}
168+
169+
if cfg.TraceSamplingRatio > 0 && os.Getenv(otelTracesSamplerArgEnv) == "" && os.Getenv(otelTracesSamplerEnv) == "" {
170+
os.Setenv(otelTracesSamplerEnv, traceIDRatioSampler)
171+
os.Setenv(otelTracesSamplerArgEnv, strconv.FormatFloat(cfg.TraceSamplingRatio, 'f', -1, 64))
172+
}
173+
}
174+
138175
func checkDisabled() error {
139176
v := os.Getenv(sdkDisabledEnv)
140177
if v != "" {

0 commit comments

Comments
 (0)