Skip to content

Commit 0f832b9

Browse files
authored
Merge branch 'main' into bugfix5705
2 parents 59041d3 + 8dca9cc commit 0f832b9

File tree

4 files changed

+137
-13
lines changed

4 files changed

+137
-13
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm
88

99
## [Unreleased]
1010

11+
### Added
12+
13+
- Support `OTEL_EXPORTER_OTLP_LOGS_INSECURE` and `OTEL_EXPORTER_OTLP_INSECURE` environments in `go.opentelemetry.io/otel/exporters/otlp/otlplog/otlploggrpc`. (#5739)
14+
1115
### Fixed
1216

1317
- Fix memory leak in the global `MeterProvider` when identical instruments are repeatedly created. (#5754)

exporters/otlp/otlplog/otlploggrpc/config.go

Lines changed: 50 additions & 13 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
envHeaders = []string{
4245
"OTEL_EXPORTER_OTLP_LOGS_HEADERS",
@@ -110,6 +113,7 @@ func newConfig(options []Option) config {
110113
fallback[string](defaultEndpoint),
111114
)
112115
c.insecure = c.insecure.Resolve(
116+
loadInsecureFromEnvEndpoint(envEndpoint),
113117
getEnv[bool](envInsecure, convInsecure),
114118
)
115119
c.tlsCfg = c.tlsCfg.Resolve(
@@ -205,11 +209,7 @@ func WithEndpointURL(rawURL string) Option {
205209
}
206210
return fnOpt(func(c config) config {
207211
c.endpoint = newSetting(u.Host)
208-
if u.Scheme != "https" {
209-
c.insecure = newSetting(true)
210-
} else {
211-
c.insecure = newSetting(false)
212-
}
212+
c.insecure = insecureFromScheme(c.insecure, u.Scheme)
213213
return c
214214
})
215215
}
@@ -395,15 +395,39 @@ func convEndpoint(s string) (string, error) {
395395
return u.Host, nil
396396
}
397397

398-
// convInsecure parses s as a URL string and returns if the connection should
399-
// use client transport security or not. If s is an invalid URL, false and an
400-
// error are returned.
398+
// convInsecure converts s from string to bool without case sensitivity.
399+
// If s is not valid returns error.
401400
func convInsecure(s string) (bool, error) {
402-
u, err := url.Parse(s)
403-
if err != nil {
404-
return false, err
401+
s = strings.ToLower(s)
402+
if s != "true" && s != "false" {
403+
return false, fmt.Errorf("can't convert %q to bool", s)
404+
}
405+
406+
return s == "true", nil
407+
}
408+
409+
// loadInsecureFromEnvEndpoint returns a resolver that fetches
410+
// insecure setting from envEndpoint is it possible.
411+
func loadInsecureFromEnvEndpoint(envEndpoint []string) resolver[bool] {
412+
return func(s setting[bool]) setting[bool] {
413+
if s.Set {
414+
// Passed, valid, options have precedence.
415+
return s
416+
}
417+
418+
for _, key := range envEndpoint {
419+
if vStr := os.Getenv(key); vStr != "" {
420+
u, err := url.Parse(vStr)
421+
if err != nil {
422+
otel.Handle(fmt.Errorf("invalid %s value %s: %w", key, vStr, err))
423+
continue
424+
}
425+
426+
return insecureFromScheme(s, u.Scheme)
427+
}
428+
}
429+
return s
405430
}
406-
return u.Scheme != "https", nil
407431
}
408432

409433
// convHeaders converts the OTel environment variable header value s into a
@@ -550,6 +574,19 @@ func loadCertificates(certPath, keyPath string) ([]tls.Certificate, error) {
550574
return []tls.Certificate{crt}, nil
551575
}
552576

577+
// insecureFromScheme return setting if the connection should
578+
// use client transport security or not.
579+
// Empty scheme doesn't force insecure setting.
580+
func insecureFromScheme(prev setting[bool], scheme string) setting[bool] {
581+
if scheme == "https" {
582+
return newSetting(false)
583+
} else if len(scheme) > 0 {
584+
return newSetting(true)
585+
}
586+
587+
return prev
588+
}
589+
553590
func compressorToCompression(compressor string) Compression {
554591
c, err := convCompression(compressor)
555592
if err != nil {

exporters/otlp/otlplog/otlploggrpc/config_test.go

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -373,6 +373,83 @@ func TestNewConfig(t *testing.T) {
373373
},
374374
errs: []string{`invalid header key: inva lid`},
375375
},
376+
{
377+
name: "OptionEndpointURLWithoutScheme",
378+
options: []Option{
379+
WithEndpointURL("//env.endpoint:8080/prefix"),
380+
},
381+
want: config{
382+
endpoint: newSetting("env.endpoint:8080"),
383+
retryCfg: newSetting(defaultRetryCfg),
384+
timeout: newSetting(defaultTimeout),
385+
},
386+
},
387+
{
388+
name: "EnvEndpointWithoutScheme",
389+
envars: map[string]string{
390+
"OTEL_EXPORTER_OTLP_LOGS_ENDPOINT": "//env.endpoint:8080/prefix",
391+
},
392+
want: config{
393+
endpoint: newSetting("env.endpoint:8080"),
394+
retryCfg: newSetting(defaultRetryCfg),
395+
timeout: newSetting(defaultTimeout),
396+
},
397+
},
398+
{
399+
name: "DefaultEndpointWithEnvInsecure",
400+
envars: map[string]string{
401+
"OTEL_EXPORTER_OTLP_LOGS_INSECURE": "true",
402+
},
403+
want: config{
404+
endpoint: newSetting(defaultEndpoint),
405+
insecure: newSetting(true),
406+
retryCfg: newSetting(defaultRetryCfg),
407+
timeout: newSetting(defaultTimeout),
408+
},
409+
},
410+
{
411+
name: "EnvEndpointWithoutSchemeWithEnvInsecure",
412+
envars: map[string]string{
413+
"OTEL_EXPORTER_OTLP_LOGS_ENDPOINT": "//env.endpoint:8080/prefix",
414+
"OTEL_EXPORTER_OTLP_LOGS_INSECURE": "true",
415+
},
416+
want: config{
417+
endpoint: newSetting("env.endpoint:8080"),
418+
insecure: newSetting(true),
419+
retryCfg: newSetting(defaultRetryCfg),
420+
timeout: newSetting(defaultTimeout),
421+
},
422+
},
423+
{
424+
name: "OptionEndpointURLWithoutSchemeWithEnvInsecure",
425+
options: []Option{
426+
WithEndpointURL("//env.endpoint:8080/prefix"),
427+
},
428+
envars: map[string]string{
429+
"OTEL_EXPORTER_OTLP_LOGS_INSECURE": "true",
430+
},
431+
want: config{
432+
endpoint: newSetting("env.endpoint:8080"),
433+
insecure: newSetting(true),
434+
retryCfg: newSetting(defaultRetryCfg),
435+
timeout: newSetting(defaultTimeout),
436+
},
437+
},
438+
{
439+
name: "OptionEndpointWithEnvInsecure",
440+
options: []Option{
441+
WithEndpoint("env.endpoint:8080"),
442+
},
443+
envars: map[string]string{
444+
"OTEL_EXPORTER_OTLP_LOGS_INSECURE": "true",
445+
},
446+
want: config{
447+
endpoint: newSetting("env.endpoint:8080"),
448+
insecure: newSetting(true),
449+
retryCfg: newSetting(defaultRetryCfg),
450+
timeout: newSetting(defaultTimeout),
451+
},
452+
},
376453
}
377454

378455
for _, tc := range testcases {

exporters/otlp/otlplog/otlploggrpc/doc.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,12 @@ The value should not contain a query string or fragment.
1818
OTEL_EXPORTER_OTLP_LOGS_ENDPOINT takes precedence over OTEL_EXPORTER_OTLP_ENDPOINT.
1919
The configuration can be overridden by [WithEndpoint], [WithEndpointURL], [WithInsecure], and [WithGRPCConn] options.
2020
21+
OTEL_EXPORTER_OTLP_INSECURE, OTEL_EXPORTER_OTLP_LOGS_INSECURE (default: "false") -
22+
setting "true" disables client transport security for the exporter's gRPC connection.
23+
You can use this only when an endpoint is provided without scheme.
24+
OTEL_EXPORTER_OTLP_LOGS_INSECURE takes precedence over OTEL_EXPORTER_OTLP_INSECURE.
25+
The configuration can be overridden by [WithInsecure], [WithGRPCConn] options.
26+
2127
OTEL_EXPORTER_OTLP_HEADERS, OTEL_EXPORTER_OTLP_LOGS_HEADERS (default: none) -
2228
key-value pairs used as gRPC metadata associated with gRPC requests.
2329
The value is expected to be represented in a format matching the [W3C Baggage HTTP Header Content Format],

0 commit comments

Comments
 (0)