Enable overriding of the otel exporter otlp endpoint via env var for testing purposes

Signed-off-by: Christopher Petito <chrisjpetito@gmail.com>
This commit is contained in:
Christopher Petito 2024-03-28 16:22:53 +00:00
parent 160f65d9db
commit b6e2eca4b8
1 changed files with 15 additions and 3 deletions

View File

@ -4,6 +4,7 @@ import (
"context" "context"
"fmt" "fmt"
"net/url" "net/url"
"os"
"path" "path"
"github.com/pkg/errors" "github.com/pkg/errors"
@ -14,7 +15,11 @@ import (
sdktrace "go.opentelemetry.io/otel/sdk/trace" sdktrace "go.opentelemetry.io/otel/sdk/trace"
) )
const otelContextFieldName = "otel" const (
otelContextFieldName string = "otel"
otelExporterOTLPEndpoint string = "OTEL_EXPORTER_OTLP_ENDPOINT"
debugEnvVarPrefix string = "DOCKER_CLI_"
)
// dockerExporterOTLPEndpoint retrieves the OTLP endpoint used for the docker reporter // dockerExporterOTLPEndpoint retrieves the OTLP endpoint used for the docker reporter
// from the current context. // from the current context.
@ -49,8 +54,15 @@ func dockerExporterOTLPEndpoint(cli Cli) (endpoint string, secure bool) {
} }
// keys from https://opentelemetry.io/docs/concepts/sdk-configuration/otlp-exporter-configuration/ // keys from https://opentelemetry.io/docs/concepts/sdk-configuration/otlp-exporter-configuration/
endpoint, ok = otelMap["OTEL_EXPORTER_OTLP_ENDPOINT"].(string) endpoint, _ = otelMap[otelExporterOTLPEndpoint].(string)
if !ok {
// Override with env var value if it exists AND IS SET
// (ignore otel defaults for this override when the key exists but is empty)
if override := os.Getenv(debugEnvVarPrefix + otelExporterOTLPEndpoint); override != "" {
endpoint = override
}
if endpoint == "" {
return "", false return "", false
} }