Skip to content

Commit da01323

Browse files
authored
otlploghttp: support OTEL_EXPORTER_OTLP_LOGS_INSECURE and OTEL_EXPORTER_OTLP_INSECURE env vars (#7608)
1 parent 4200d1e commit da01323

File tree

3 files changed

+134
-9
lines changed

3 files changed

+134
-9
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm
2323
- Add experimental observability metrics in `go.opentelemetry.io/otel/exporters/otlp/otlplog/otlploghttp`. (#7512)
2424
- Add experimental observability metrics for manual reader in `go.opentelemetry.io/otel/sdk/metric`. (#7524)
2525
- Add experimental observability metrics for periodic reader in `go.opentelemetry.io/otel/sdk/metric`. (#7571)
26+
- Support `OTEL_EXPORTER_OTLP_LOGS_INSECURE` and `OTEL_EXPORTER_OTLP_INSECURE` environmental variables in `go.opentelemetry.io/otel/exporters/otlp/otlplog/otlploghttp`. (#7608)
2627

2728
### Fixed
2829

exporters/otlp/otlplog/otlploghttp/config.go

Lines changed: 50 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,10 @@ var (
3636
"OTEL_EXPORTER_OTLP_LOGS_ENDPOINT",
3737
"OTEL_EXPORTER_OTLP_ENDPOINT",
3838
}
39-
envInsecure = envEndpoint
39+
envInsecure = []string{
40+
"OTEL_EXPORTER_OTLP_LOGS_INSECURE",
41+
"OTEL_EXPORTER_OTLP_INSECURE",
42+
}
4043

4144
// Split because these are parsed differently.
4245
envPathSignal = []string{"OTEL_EXPORTER_OTLP_LOGS_ENDPOINT"}
@@ -114,6 +117,7 @@ func newConfig(options []Option) config {
114117
fallback[string](defaultPath),
115118
)
116119
c.insecure = c.insecure.Resolve(
120+
loadInsecureFromEnvEndpoint(envEndpoint),
117121
getenv[bool](envInsecure, convInsecure),
118122
)
119123
c.tlsCfg = c.tlsCfg.Resolve(
@@ -185,7 +189,7 @@ func WithEndpointURL(rawURL string) Option {
185189
return fnOpt(func(c config) config {
186190
c.endpoint = newSetting(u.Host)
187191
c.path = newSetting(u.Path)
188-
c.insecure = newSetting(u.Scheme != "https")
192+
c.insecure = insecureFromScheme(c.insecure, u.Scheme)
189193
return c
190194
})
191195
}
@@ -541,15 +545,52 @@ func convPath(s string) (string, error) {
541545
return u.Path + "/v1/logs", nil
542546
}
543547

544-
// convInsecure parses s as a URL string and returns if the connection should
545-
// use client transport security or not. If s is an invalid URL, false and an
546-
// error are returned.
548+
// convInsecure converts s from string to bool without case sensitivity.
549+
// If s is not valid returns error.
547550
func convInsecure(s string) (bool, error) {
548-
u, err := url.Parse(s)
549-
if err != nil {
550-
return false, err
551+
s = strings.ToLower(s)
552+
if s != "true" && s != "false" {
553+
return false, fmt.Errorf("can't convert %q to bool", s)
554+
}
555+
556+
return s == "true", nil
557+
}
558+
559+
// loadInsecureFromEnvEndpoint returns a resolver that fetches
560+
// insecure setting from envEndpoint is it possible.
561+
func loadInsecureFromEnvEndpoint(envEndpoint []string) resolver[bool] {
562+
return func(s setting[bool]) setting[bool] {
563+
if s.Set {
564+
// Passed, valid, options have precedence.
565+
return s
566+
}
567+
568+
for _, key := range envEndpoint {
569+
if vStr := os.Getenv(key); vStr != "" {
570+
u, err := url.Parse(vStr)
571+
if err != nil {
572+
otel.Handle(fmt.Errorf("invalid %s value %s: %w", key, vStr, err))
573+
continue
574+
}
575+
576+
return insecureFromScheme(s, u.Scheme)
577+
}
578+
}
579+
return s
551580
}
552-
return u.Scheme != "https", nil
581+
}
582+
583+
// insecureFromScheme return setting if the connection should
584+
// use client transport security or not.
585+
// Empty scheme doesn't force insecure setting.
586+
func insecureFromScheme(prev setting[bool], scheme string) setting[bool] {
587+
if scheme == "https" {
588+
return newSetting(false)
589+
} else if scheme != "" {
590+
return newSetting(true)
591+
}
592+
593+
return prev
553594
}
554595

555596
// convHeaders converts the OTel environment variable header value s into a

exporters/otlp/otlplog/otlploghttp/config_test.go

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -410,6 +410,89 @@ func TestNewConfig(t *testing.T) {
410410
retryCfg: newSetting(defaultRetryCfg),
411411
},
412412
},
413+
{
414+
name: "OptionEndpointURLWithoutScheme",
415+
options: []Option{
416+
WithEndpointURL("//env.endpoint:8080/prefix"),
417+
},
418+
want: config{
419+
endpoint: newSetting("env.endpoint:8080"),
420+
path: newSetting("/prefix"),
421+
retryCfg: newSetting(defaultRetryCfg),
422+
timeout: newSetting(defaultTimeout),
423+
},
424+
},
425+
{
426+
name: "EnvEndpointWithoutScheme",
427+
envars: map[string]string{
428+
"OTEL_EXPORTER_OTLP_LOGS_ENDPOINT": "//env.endpoint:8080/prefix",
429+
},
430+
want: config{
431+
endpoint: newSetting("env.endpoint:8080"),
432+
path: newSetting("/prefix"),
433+
retryCfg: newSetting(defaultRetryCfg),
434+
timeout: newSetting(defaultTimeout),
435+
},
436+
},
437+
{
438+
name: "DefaultEndpointWithEnvInsecure",
439+
envars: map[string]string{
440+
"OTEL_EXPORTER_OTLP_LOGS_INSECURE": "true",
441+
},
442+
want: config{
443+
endpoint: newSetting(defaultEndpoint),
444+
path: newSetting(defaultPath),
445+
insecure: newSetting(true),
446+
retryCfg: newSetting(defaultRetryCfg),
447+
timeout: newSetting(defaultTimeout),
448+
},
449+
},
450+
{
451+
name: "EnvEndpointWithoutSchemeWithEnvInsecure",
452+
envars: map[string]string{
453+
"OTEL_EXPORTER_OTLP_LOGS_ENDPOINT": "//env.endpoint:8080/prefix",
454+
"OTEL_EXPORTER_OTLP_LOGS_INSECURE": "true",
455+
},
456+
want: config{
457+
endpoint: newSetting("env.endpoint:8080"),
458+
path: newSetting("/prefix"),
459+
insecure: newSetting(true),
460+
retryCfg: newSetting(defaultRetryCfg),
461+
timeout: newSetting(defaultTimeout),
462+
},
463+
},
464+
{
465+
name: "OptionEndpointURLWithoutSchemeWithEnvInsecure",
466+
options: []Option{
467+
WithEndpointURL("//env.endpoint:8080/prefix"),
468+
},
469+
envars: map[string]string{
470+
"OTEL_EXPORTER_OTLP_LOGS_INSECURE": "true",
471+
},
472+
want: config{
473+
endpoint: newSetting("env.endpoint:8080"),
474+
path: newSetting("/prefix"),
475+
insecure: newSetting(true),
476+
retryCfg: newSetting(defaultRetryCfg),
477+
timeout: newSetting(defaultTimeout),
478+
},
479+
},
480+
{
481+
name: "OptionEndpointWithEnvInsecure",
482+
options: []Option{
483+
WithEndpoint("env.endpoint:8080"),
484+
},
485+
envars: map[string]string{
486+
"OTEL_EXPORTER_OTLP_LOGS_INSECURE": "true",
487+
},
488+
want: config{
489+
endpoint: newSetting("env.endpoint:8080"),
490+
path: newSetting(defaultPath),
491+
insecure: newSetting(true),
492+
retryCfg: newSetting(defaultRetryCfg),
493+
timeout: newSetting(defaultTimeout),
494+
},
495+
},
413496
}
414497

415498
for _, tc := range testcases {

0 commit comments

Comments
 (0)