fork of https://github.com/sourcegraph/zoekt
1// Package otlpenv exports getters to read OpenTelemetry protocol configuration options
2// based on the official spec: https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/protocol/exporter.md#configuration-options
3package otlpenv
4
5import (
6 "os"
7 "strings"
8)
9
10// getWithDefault returns the default value if no env in keys is set, or the first env from keys that is
11// set.
12func getWithDefault(def string, keys ...string) string {
13 for _, k := range keys {
14 if v, set := os.LookupEnv(k); set {
15 return v
16 }
17 }
18 return def
19}
20
21// This is a custom default that's also not quite compliant but hopefully close enough (we
22// use 127.0.0.1 instead of localhost, since there's a linter rule banning localhost).
23const defaultGRPCCollectorEndpoint = "http://127.0.0.1:4317"
24
25// GetEndpoint returns the root collector endpoint, NOT per-signal endpoints. We do not
26// yet support per-signal endpoints.
27//
28// See: https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/protocol/exporter.md#configuration-options
29func GetEndpoint() string {
30 return getWithDefault(defaultGRPCCollectorEndpoint,
31 "OTEL_EXPORTER_OTLP_ENDPOINT")
32}
33
34type Protocol string
35
36const (
37 // ProtocolGRPC is protobuf-encoded data using gRPC wire format over HTTP/2 connection
38 ProtocolGRPC Protocol = "grpc"
39 // ProtocolHTTPProto is protobuf-encoded data over HTTP connection
40 ProtocolHTTPProto Protocol = "http/proto"
41 // ProtocolHTTPJSON is JSON-encoded data over HTTP connection
42 ProtocolHTTPJSON Protocol = "http/json"
43)
44
45// GetProtocol returns the configured protocol for the root collector endpoint, NOT
46// per-signal endpoints. We do not yet support per-signal endpoints.
47//
48// See: https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/protocol/exporter.md#specify-protocol
49func GetProtocol() Protocol {
50 return Protocol(getWithDefault(string(ProtocolGRPC),
51 "OTEL_EXPORTER_OTLP_PROTOCOL"))
52}
53
54func IsInsecure(endpoint string) bool {
55 return strings.HasPrefix(strings.ToLower(endpoint), "http://")
56}