dockerExporterOTLPEndpoint retrieves the OTLP endpoint used for the docker reporter from the current context.
(cli Cli)
| 30 | // dockerExporterOTLPEndpoint retrieves the OTLP endpoint used for the docker reporter |
| 31 | // from the current context. |
| 32 | func dockerExporterOTLPEndpoint(cli Cli) (endpoint string, secure bool) { |
| 33 | meta, err := cli.ContextStore().GetMetadata(cli.CurrentContext()) |
| 34 | if err != nil { |
| 35 | otel.Handle(err) |
| 36 | return "", false |
| 37 | } |
| 38 | |
| 39 | var otelCfg any |
| 40 | switch m := meta.Metadata.(type) { |
| 41 | case DockerContext: |
| 42 | otelCfg = m.AdditionalFields[otelContextFieldName] |
| 43 | case map[string]any: |
| 44 | otelCfg = m[otelContextFieldName] |
| 45 | } |
| 46 | |
| 47 | if otelCfg != nil { |
| 48 | otelMap, ok := otelCfg.(map[string]any) |
| 49 | if !ok { |
| 50 | otel.Handle(fmt.Errorf( |
| 51 | "unexpected type for field %q: %T (expected: %T)", |
| 52 | otelContextFieldName, |
| 53 | otelCfg, |
| 54 | otelMap, |
| 55 | )) |
| 56 | } |
| 57 | // keys from https://opentelemetry.io/docs/concepts/sdk-configuration/otlp-exporter-configuration/ |
| 58 | endpoint, _ = otelMap[otelExporterOTLPEndpoint].(string) |
| 59 | } |
| 60 | |
| 61 | // Override with env var value if it exists AND IS SET |
| 62 | // (ignore otel defaults for this override when the key exists but is empty) |
| 63 | if override := os.Getenv(debugEnvVarPrefix + otelExporterOTLPEndpoint); override != "" { |
| 64 | endpoint = override |
| 65 | } |
| 66 | |
| 67 | if endpoint == "" { |
| 68 | return "", false |
| 69 | } |
| 70 | |
| 71 | // Parse the endpoint. The docker config expects the endpoint to be |
| 72 | // in the form of a URL to match the environment variable, but this |
| 73 | // option doesn't correspond directly to WithEndpoint. |
| 74 | // |
| 75 | // We pretend we're the same as the environment reader. |
| 76 | u, err := url.Parse(endpoint) |
| 77 | if err != nil { |
| 78 | otel.Handle(fmt.Errorf("docker otel endpoint is invalid: %s", err)) |
| 79 | return "", false |
| 80 | } |
| 81 | |
| 82 | switch u.Scheme { |
| 83 | case "unix": |
| 84 | endpoint = unixSocketEndpoint(u) |
| 85 | case "https": |
| 86 | secure = true |
| 87 | fallthrough |
| 88 | case "http": |
| 89 | endpoint = path.Join(u.Host, u.Path) |
no test coverage detected
searching dependent graphs…