Skip to content

Commit

Permalink
Override insecure when endpoint URL is set (#5944)
Browse files Browse the repository at this point in the history
When an endpoint is set in both Environment variable with "http" and
passed in WithEndpointURL with "https", Insecure is set to true while
the endpoint is used from WithEndpointURL.

Example
- OTEL_EXPORTER_OTLP_ENDPOINT is set to "http://env.endpoint/prefix" 
- WithEndpointURL is passed "https://someendpoint/somepath"

The real endpoint used is "http://someendpoint/somepath", which is
actually neither of both.

---------

Co-authored-by: Robert Pająk <pellared@hotmail.com>
  • Loading branch information
sevaorlov and pellared authored Nov 7, 2024
1 parent 937813d commit b62a3fd
Show file tree
Hide file tree
Showing 16 changed files with 199 additions and 23 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm

- Global MeterProvider registration unwraps global instrument Observers, the undocumented Unwrap() methods are now private. (#5881)
- Fix `go.opentelemetry.io/otel/exporters/prometheus` trying to add exemplars to Gauge metrics, which is unsupported. (#5912)
- Fix `WithEndpointURL` to always use a secure connection when an https URL is passed in `go.opentelemetry.io/otel/exporters/otlp/otlpmetric/otlpmetricgrpc`. (#5944)
- Fix `WithEndpointURL` to always use a secure connection when an https URL is passed in `go.opentelemetry.io/otel/exporters/otlp/otlpmetric/otlpmetrichttp`. (#5944)
- Fix `WithEndpointURL` to always use a secure connection when an https URL is passed in `go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracegrpc`. (#5944)
- Fix `WithEndpointURL` to always use a secure connection when an https URL is passed in `go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracehttp`. (#5944)

### Changed

Expand Down
30 changes: 30 additions & 0 deletions exporters/otlp/otlplog/otlploggrpc/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,21 @@ func TestNewConfig(t *testing.T) {
retryCfg: newSetting(defaultRetryCfg),
},
},
{
name: "WithEndpointURL secure when Environment Endpoint is set insecure",
envars: map[string]string{
"OTEL_EXPORTER_OTLP_LOGS_ENDPOINT": "http://env.endpoint:8080/prefix",
},
options: []Option{
WithEndpointURL("https://test:8080/path"),
},
want: config{
endpoint: newSetting("test:8080"),
insecure: newSetting(false),
timeout: newSetting(defaultTimeout),
retryCfg: newSetting(defaultRetryCfg),
},
},
{
name: "LogEnvironmentVariables",
envars: map[string]string{
Expand Down Expand Up @@ -235,6 +250,21 @@ func TestNewConfig(t *testing.T) {
retryCfg: newSetting(defaultRetryCfg),
},
},
{
name: "WithEndpointURL secure when Environment insecure is set false",
envars: map[string]string{
"OTEL_EXPORTER_OTLP_LOGS_INSECURE": "true",
},
options: []Option{
WithEndpointURL("https://test:8080/path"),
},
want: config{
endpoint: newSetting("test:8080"),
insecure: newSetting(false),
timeout: newSetting(defaultTimeout),
retryCfg: newSetting(defaultRetryCfg),
},
},
{
name: "EnvironmentVariablesPrecedence",
envars: map[string]string{
Expand Down
6 changes: 1 addition & 5 deletions exporters/otlp/otlplog/otlploghttp/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -183,11 +183,7 @@ func WithEndpointURL(rawURL string) Option {
return fnOpt(func(c config) config {
c.endpoint = newSetting(u.Host)
c.path = newSetting(u.Path)
if u.Scheme != "https" {
c.insecure = newSetting(true)
} else {
c.insecure = newSetting(false)
}
c.insecure = newSetting(u.Scheme != "https")
return c
})
}
Expand Down
32 changes: 32 additions & 0 deletions exporters/otlp/otlplog/otlploghttp/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,38 @@ func TestNewConfig(t *testing.T) {
retryCfg: newSetting(defaultRetryCfg),
},
},
{
name: "WithEndpointURL secure when Environment Endpoint is set insecure",
envars: map[string]string{
"OTEL_EXPORTER_OTLP_LOGS_ENDPOINT": "http://env.endpoint:8080/prefix",
},
options: []Option{
WithEndpointURL("https://test:8080/path"),
},
want: config{
endpoint: newSetting("test:8080"),
path: newSetting("/path"),
insecure: newSetting(false),
timeout: newSetting(defaultTimeout),
retryCfg: newSetting(defaultRetryCfg),
},
},
{
name: "WithEndpointURL secure when Environment insecure is set false",
envars: map[string]string{
"OTEL_EXPORTER_OTLP_LOGS_INSECURE": "true",
},
options: []Option{
WithEndpointURL("https://test:8080/path"),
},
want: config{
endpoint: newSetting("test:8080"),
path: newSetting("/path"),
insecure: newSetting(false),
timeout: newSetting(defaultTimeout),
retryCfg: newSetting(defaultRetryCfg),
},
},
{
name: "LogEnvironmentVariables",
envars: map[string]string{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -287,9 +287,7 @@ func WithEndpointURL(v string) GenericOption {

cfg.Metrics.Endpoint = u.Host
cfg.Metrics.URLPath = u.Path
if u.Scheme != "https" {
cfg.Metrics.Insecure = true
}
cfg.Metrics.Insecure = u.Scheme != "https"

return cfg
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,34 @@ func TestConfigs(t *testing.T) {
assert.Equal(t, "someendpoint", c.Metrics.Endpoint)
},
},
{
name: "Test With WithEndpointURL secure when Environment Endpoint is set insecure",
env: map[string]string{
"OTEL_EXPORTER_OTLP_ENDPOINT": "http://env.endpoint/prefix",
},
opts: []GenericOption{
WithEndpointURL("https://someendpoint/somepath"),
},
asserts: func(t *testing.T, c *Config, grpcOption bool) {
assert.Equal(t, "someendpoint", c.Metrics.Endpoint)
assert.Equal(t, "/somepath", c.Metrics.URLPath)
assert.False(t, c.Metrics.Insecure)
},
},
{
name: "Test With WithEndpointURL secure when Environment insecure is set true",
env: map[string]string{
"OTEL_EXPORTER_OTLP_INSECURE": "true",
},
opts: []GenericOption{
WithEndpointURL("https://someendpoint/somepath"),
},
asserts: func(t *testing.T, c *Config, grpcOption bool) {
assert.Equal(t, "someendpoint", c.Metrics.Endpoint)
assert.Equal(t, "/somepath", c.Metrics.URLPath)
assert.False(t, c.Metrics.Insecure)
},
},
{
name: "Test Environment Endpoint",
env: map[string]string{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -287,9 +287,7 @@ func WithEndpointURL(v string) GenericOption {

cfg.Metrics.Endpoint = u.Host
cfg.Metrics.URLPath = u.Path
if u.Scheme != "https" {
cfg.Metrics.Insecure = true
}
cfg.Metrics.Insecure = u.Scheme != "https"

return cfg
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,34 @@ func TestConfigs(t *testing.T) {
assert.Equal(t, "someendpoint", c.Metrics.Endpoint)
},
},
{
name: "Test With WithEndpointURL secure when Environment Endpoint is set insecure",
env: map[string]string{
"OTEL_EXPORTER_OTLP_ENDPOINT": "http://env.endpoint/prefix",
},
opts: []GenericOption{
WithEndpointURL("https://someendpoint/somepath"),
},
asserts: func(t *testing.T, c *Config, grpcOption bool) {
assert.Equal(t, "someendpoint", c.Metrics.Endpoint)
assert.Equal(t, "/somepath", c.Metrics.URLPath)
assert.False(t, c.Metrics.Insecure)
},
},
{
name: "Test With WithEndpointURL secure when Environment insecure is set true",
env: map[string]string{
"OTEL_EXPORTER_OTLP_INSECURE": "true",
},
opts: []GenericOption{
WithEndpointURL("https://someendpoint/somepath"),
},
asserts: func(t *testing.T, c *Config, grpcOption bool) {
assert.Equal(t, "someendpoint", c.Metrics.Endpoint)
assert.Equal(t, "/somepath", c.Metrics.URLPath)
assert.False(t, c.Metrics.Insecure)
},
},
{
name: "Test Environment Endpoint",
env: map[string]string{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -278,9 +278,7 @@ func WithEndpointURL(v string) GenericOption {

cfg.Traces.Endpoint = u.Host
cfg.Traces.URLPath = u.Path
if u.Scheme != "https" {
cfg.Traces.Insecure = true
}
cfg.Traces.Insecure = u.Scheme != "https"

return cfg
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,20 @@ func TestConfigs(t *testing.T) {
assert.Equal(t, "someendpoint", c.Traces.Endpoint)
},
},
{
name: "Test With WithEndpointURL secure when Environment Endpoint is set insecure",
env: map[string]string{
"OTEL_EXPORTER_OTLP_ENDPOINT": "http://env.endpoint/prefix",
},
opts: []GenericOption{
WithEndpointURL("https://someendpoint/somepath"),
},
asserts: func(t *testing.T, c *Config, grpcOption bool) {
assert.Equal(t, "someendpoint", c.Traces.Endpoint)
assert.Equal(t, "/somepath", c.Traces.URLPath)
assert.False(t, c.Traces.Insecure)
},
},
{
name: "Test Environment Endpoint",
env: map[string]string{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -278,9 +278,7 @@ func WithEndpointURL(v string) GenericOption {

cfg.Traces.Endpoint = u.Host
cfg.Traces.URLPath = u.Path
if u.Scheme != "https" {
cfg.Traces.Insecure = true
}
cfg.Traces.Insecure = u.Scheme != "https"

return cfg
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,20 @@ func TestConfigs(t *testing.T) {
assert.Equal(t, "someendpoint", c.Traces.Endpoint)
},
},
{
name: "Test With WithEndpointURL secure when Environment Endpoint is set insecure",
env: map[string]string{
"OTEL_EXPORTER_OTLP_ENDPOINT": "http://env.endpoint/prefix",
},
opts: []GenericOption{
WithEndpointURL("https://someendpoint/somepath"),
},
asserts: func(t *testing.T, c *Config, grpcOption bool) {
assert.Equal(t, "someendpoint", c.Traces.Endpoint)
assert.Equal(t, "/somepath", c.Traces.URLPath)
assert.False(t, c.Traces.Insecure)
},
},
{
name: "Test Environment Endpoint",
env: map[string]string{
Expand Down
4 changes: 1 addition & 3 deletions internal/shared/otlp/otlpmetric/oconf/options.go.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -287,9 +287,7 @@ func WithEndpointURL(v string) GenericOption {

cfg.Metrics.Endpoint = u.Host
cfg.Metrics.URLPath = u.Path
if u.Scheme != "https" {
cfg.Metrics.Insecure = true
}
cfg.Metrics.Insecure = u.Scheme != "https"

return cfg
})
Expand Down
28 changes: 28 additions & 0 deletions internal/shared/otlp/otlpmetric/oconf/options_test.go.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,34 @@ func TestConfigs(t *testing.T) {
assert.Equal(t, "someendpoint", c.Metrics.Endpoint)
},
},
{
name: "Test With WithEndpointURL secure when Environment Endpoint is set insecure",
env: map[string]string{
"OTEL_EXPORTER_OTLP_ENDPOINT": "http://env.endpoint/prefix",
},
opts: []GenericOption{
WithEndpointURL("https://someendpoint/somepath"),
},
asserts: func(t *testing.T, c *Config, grpcOption bool) {
assert.Equal(t, "someendpoint", c.Metrics.Endpoint)
assert.Equal(t, "/somepath", c.Metrics.URLPath)
assert.False(t, c.Metrics.Insecure)
},
},
{
name: "Test With WithEndpointURL secure when Environment insecure is set true",
env: map[string]string{
"OTEL_EXPORTER_OTLP_INSECURE": "true",
},
opts: []GenericOption{
WithEndpointURL("https://someendpoint/somepath"),
},
asserts: func(t *testing.T, c *Config, grpcOption bool) {
assert.Equal(t, "someendpoint", c.Metrics.Endpoint)
assert.Equal(t, "/somepath", c.Metrics.URLPath)
assert.False(t, c.Metrics.Insecure)
},
},
{
name: "Test Environment Endpoint",
env: map[string]string{
Expand Down
4 changes: 1 addition & 3 deletions internal/shared/otlp/otlptrace/otlpconfig/options.go.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -278,9 +278,7 @@ func WithEndpointURL(v string) GenericOption {

cfg.Traces.Endpoint = u.Host
cfg.Traces.URLPath = u.Path
if u.Scheme != "https" {
cfg.Traces.Insecure = true
}
cfg.Traces.Insecure = u.Scheme != "https"

return cfg
})
Expand Down
14 changes: 14 additions & 0 deletions internal/shared/otlp/otlptrace/otlpconfig/options_test.go.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,20 @@ func TestConfigs(t *testing.T) {
assert.Equal(t, "someendpoint", c.Traces.Endpoint)
},
},
{
name: "Test With WithEndpointURL secure when Environment Endpoint is set insecure",
env: map[string]string{
"OTEL_EXPORTER_OTLP_ENDPOINT": "http://env.endpoint/prefix",
},
opts: []GenericOption{
WithEndpointURL("https://someendpoint/somepath"),
},
asserts: func(t *testing.T, c *Config, grpcOption bool) {
assert.Equal(t, "someendpoint", c.Traces.Endpoint)
assert.Equal(t, "/somepath", c.Traces.URLPath)
assert.False(t, c.Traces.Insecure)
},
},
{
name: "Test Environment Endpoint",
env: map[string]string{
Expand Down

0 comments on commit b62a3fd

Please sign in to comment.