diff --git a/CHANGELOG.md b/CHANGELOG.md index 9ad961384fa3..ed060a9b5761 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -24,6 +24,7 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm - The `Status` type was added to the `go.opentelemetry.io/otel/sdk/trace` package to represent the status of a span. (#1874) - The `SpanStub` type and its associated functions were added to the `go.opentelemetry.io/otel/sdk/trace/tracetest` package. This type can be used as a testing replacement for the `SpanSnapshot` that was removed from the `go.opentelemetry.io/otel/sdk/trace` package. (#1873) +- Adds support for scheme in `OTEL_EXPORTER_OTLP_ENDPOINT` according to the spec (#xxxx) ### Changed diff --git a/exporters/otlp/internal/otlpconfig/envconfig.go b/exporters/otlp/internal/otlpconfig/envconfig.go index b52f3dc537a8..97f3e7c0f9e1 100644 --- a/exporters/otlp/internal/otlpconfig/envconfig.go +++ b/exporters/otlp/internal/otlpconfig/envconfig.go @@ -71,13 +71,13 @@ func (e *EnvOptionsReader) GetOptionsFromEnv() []GenericOption { // Endpoint if v, ok := e.getEnvValue("ENDPOINT"); ok { - opts = append(opts, WithEndpoint(v)) + opts = append(opts, endpointOptions(v, WithEndpoint, withInsecure)...) } if v, ok := e.getEnvValue("TRACES_ENDPOINT"); ok { - opts = append(opts, WithTracesEndpoint(v)) + opts = append(opts, endpointOptions(v, WithTracesEndpoint, withInsecureTraces)...) } if v, ok := e.getEnvValue("METRICS_ENDPOINT"); ok { - opts = append(opts, WithMetricsEndpoint(v)) + opts = append(opts, endpointOptions(v, WithMetricsEndpoint, withInsecureMetrics)...) } // Certificate File @@ -145,6 +145,18 @@ func (e *EnvOptionsReader) GetOptionsFromEnv() []GenericOption { return opts } +func endpointOptions(endpoint string, endpointOption func(endpoint string) GenericOption, insecureOption func(insecure bool) GenericOption) []GenericOption { + var endpointOptions []GenericOption + + endpointOptions = append(endpointOptions, insecureOption(strings.HasPrefix(endpoint, "http://"))) + + endpoint = strings.Replace(endpoint, "https://", "", 1) + endpoint = strings.Replace(endpoint, "http://", "", 1) + endpointOptions = append(endpointOptions, endpointOption(endpoint)) + + return endpointOptions +} + // getEnvValue gets an OTLP environment variable value of the specified key using the GetEnv function. // This function already prepends the OTLP prefix to all key lookup. func (e *EnvOptionsReader) getEnvValue(key string) (string, bool) { diff --git a/exporters/otlp/internal/otlpconfig/options.go b/exporters/otlp/internal/otlpconfig/options.go index 53fe06e06185..b12bc54286c1 100644 --- a/exporters/otlp/internal/otlpconfig/options.go +++ b/exporters/otlp/internal/otlpconfig/options.go @@ -297,21 +297,33 @@ func WithMetricsTLSClientConfig(tlsCfg *tls.Config) GenericOption { } func WithInsecure() GenericOption { + return withInsecure(true) +} + +func withInsecure(insecure bool) GenericOption { return newGenericOption(func(cfg *Config) { - cfg.Traces.Insecure = true - cfg.Metrics.Insecure = true + cfg.Traces.Insecure = insecure + cfg.Metrics.Insecure = insecure }) } func WithInsecureTraces() GenericOption { + return withInsecureTraces(true) +} + +func withInsecureTraces(insecure bool) GenericOption { return newGenericOption(func(cfg *Config) { - cfg.Traces.Insecure = true + cfg.Traces.Insecure = insecure }) } func WithInsecureMetrics() GenericOption { + return withInsecureMetrics(true) +} + +func withInsecureMetrics(insecure bool) GenericOption { return newGenericOption(func(cfg *Config) { - cfg.Metrics.Insecure = true + cfg.Metrics.Insecure = insecure }) } diff --git a/exporters/otlp/internal/otlpconfig/options_test.go b/exporters/otlp/internal/otlpconfig/options_test.go index 7a70ba53619c..36b032557fab 100644 --- a/exporters/otlp/internal/otlpconfig/options_test.go +++ b/exporters/otlp/internal/otlpconfig/options_test.go @@ -124,6 +124,58 @@ func TestConfigs(t *testing.T) { assert.Equal(t, "env_endpoint", c.Metrics.Endpoint) }, }, + { + name: "Test Environment Endpoint with HTTP scheme", + env: map[string]string{ + "OTEL_EXPORTER_OTLP_ENDPOINT": "http://env_endpoint", + }, + asserts: func(t *testing.T, c *Config, grpcOption bool) { + assert.Equal(t, "env_endpoint", c.Traces.Endpoint) + assert.Equal(t, "env_endpoint", c.Metrics.Endpoint) + assert.Equal(t, true, c.Traces.Insecure) + assert.Equal(t, true, c.Metrics.Insecure) + }, + }, + { + name: "Test Environment Endpoint with HTTPS scheme", + env: map[string]string{ + "OTEL_EXPORTER_OTLP_ENDPOINT": "https://env_endpoint", + }, + asserts: func(t *testing.T, c *Config, grpcOption bool) { + assert.Equal(t, "env_endpoint", c.Traces.Endpoint) + assert.Equal(t, "env_endpoint", c.Metrics.Endpoint) + assert.Equal(t, false, c.Traces.Insecure) + assert.Equal(t, false, c.Metrics.Insecure) + }, + }, + { + name: "Test Environment Signal Specific Endpoint", + env: map[string]string{ + "OTEL_EXPORTER_OTLP_ENDPOINT": "http://overrode_by_signal_specific", + "OTEL_EXPORTER_OTLP_TRACES_ENDPOINT": "http://env_traces_endpoint", + "OTEL_EXPORTER_OTLP_METRICS_ENDPOINT": "https://env_metrics_endpoint", + }, + asserts: func(t *testing.T, c *Config, grpcOption bool) { + assert.Equal(t, "env_traces_endpoint", c.Traces.Endpoint) + assert.Equal(t, "env_metrics_endpoint", c.Metrics.Endpoint) + assert.Equal(t, true, c.Traces.Insecure) + assert.Equal(t, false, c.Metrics.Insecure) + }, + }, + { + name: "Test Environment Signal Specific Endpoint #2", + env: map[string]string{ + "OTEL_EXPORTER_OTLP_ENDPOINT": "http://overrode_by_signal_specific", + "OTEL_EXPORTER_OTLP_TRACES_ENDPOINT": "http://env_traces_endpoint", + "OTEL_EXPORTER_OTLP_METRICS_ENDPOINT": "env_metrics_endpoint", + }, + asserts: func(t *testing.T, c *Config, grpcOption bool) { + assert.Equal(t, "env_traces_endpoint", c.Traces.Endpoint) + assert.Equal(t, "env_metrics_endpoint", c.Metrics.Endpoint) + assert.Equal(t, true, c.Traces.Insecure) + assert.Equal(t, false, c.Metrics.Insecure) + }, + }, // Certificate tests {