Skip to content

Commit

Permalink
handle OTEL_EXPORTER_OTLP_TRACES and fix unix endpoints (#100)
Browse files Browse the repository at this point in the history
* handle OTEL_EXPORTER_OTLP_TRACES env variables

Defined in https://github.com/open-telemetry/opentelemetry-specification/blob/v1.8.0/specification/protocol/exporter.md

These variables are defined more specifically
so take priority over the generic vars if set.

Signed-off-by: Tonis Tiigi <tonistiigi@gmail.com>

* fix handling of unix URLs

Unix URLs should not need any special handling but
current implementation handles values without the
schema that is not allowed by the spec.

If this can be removed in the future then all these
exceptions can be removed as well and WithInsecure
is automatically detected by the endpoint value
in otlptracegrpc package.

Signed-off-by: Tonis Tiigi <tonistiigi@gmail.com>
  • Loading branch information
tonistiigi authored Feb 23, 2022
1 parent 86c6b24 commit 8488238
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 2 deletions.
12 changes: 10 additions & 2 deletions otelcli/plumbing.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func grpcOptions() []otlpgrpc.Option {
// an obvious "localhost", "127.0.0.x", or "::1" address.
if config.Insecure || isLoopbackAddr(config.Endpoint) {
grpcOpts = append(grpcOpts, otlpgrpc.WithInsecure())
} else {
} else if !isInsecureSchema(config.Endpoint) {
var tlsConfig *tls.Config
if config.NoTlsVerify {
tlsConfig = &tls.Config{
Expand Down Expand Up @@ -95,7 +95,7 @@ func httpOptions() []otlphttp.Option {
// "127.0.0.x", or "::1" address.
if config.Insecure || isLoopbackAddr(config.Endpoint) {
httpOpts = append(httpOpts, otlphttp.WithInsecure())
} else {
} else if !isInsecureSchema(config.Endpoint) {
var tlsConfig *tls.Config
if config.NoTlsVerify {
tlsConfig = &tls.Config{
Expand Down Expand Up @@ -203,6 +203,8 @@ func isLoopbackAddr(endpoint string) bool {
softFail("error parsing provided URI '%s': %s", endpoint, err)
}
hostname = u.Hostname()
} else if strings.HasPrefix(endpoint, "unix://") {
return false
} else {
softFail("'%s' is not a valid endpoint, must be host:port or a URI", endpoint)
}
Expand All @@ -224,3 +226,9 @@ func isLoopbackAddr(endpoint string) bool {
diagnostics.DetectedLocalhost = allAreLoopback
return allAreLoopback
}

// isInsecureSchema returns true if the provided endpoint is an unencrypted HTTP URL or unix socket
func isInsecureSchema(endpoint string) bool {
return strings.HasPrefix(endpoint, "http://") ||
strings.HasPrefix(endpoint, "unix://")
}
6 changes: 6 additions & 0 deletions otelcli/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,12 @@ func addCommonParams(cmd *cobra.Command) {
"timeout": "OTEL_EXPORTER_OTLP_TIMEOUT",
"verbose": "OTEL_CLI_VERBOSE",
}
if _, ok := os.LookupEnv("OTEL_EXPORTER_OTLP_TRACES_ENDPOINT"); ok {
common_env_flags["endpoint"] = "OTEL_EXPORTER_OTLP_TRACES_ENDPOINT"
}
if _, ok := os.LookupEnv("OTEL_EXPORTER_OTLP_TRACES_TIMEOUT"); ok {
common_env_flags["timeout"] = "OTEL_EXPORTER_OTLP_TRACES_TIMEOUT"
}

for config_key, env_value := range common_env_flags {
viper.BindPFlag(config_key, cmd.Flags().Lookup(config_key))
Expand Down

0 comments on commit 8488238

Please sign in to comment.