package otlpconfig
import (
"crypto/tls"
"crypto/x509"
"net/url"
"os"
"path"
"strings"
"time"
"go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracegrpc/internal/envconfig"
)
var DefaultEnvOptionsReader = envconfig .EnvOptionsReader {
GetEnv : os .Getenv ,
ReadFile : os .ReadFile ,
Namespace : "OTEL_EXPORTER_OTLP" ,
}
func ApplyGRPCEnvConfigs (cfg Config ) Config {
opts := getOptionsFromEnv ()
for _ , opt := range opts {
cfg = opt .ApplyGRPCOption (cfg )
}
return cfg
}
func ApplyHTTPEnvConfigs (cfg Config ) Config {
opts := getOptionsFromEnv ()
for _ , opt := range opts {
cfg = opt .ApplyHTTPOption (cfg )
}
return cfg
}
func getOptionsFromEnv() []GenericOption {
opts := []GenericOption {}
tlsConf := &tls .Config {}
DefaultEnvOptionsReader .Apply (
envconfig .WithURL ("ENDPOINT" , func (u *url .URL ) {
opts = append (opts , withEndpointScheme (u ))
opts = append (opts , newSplitOption (func (cfg Config ) Config {
cfg .Traces .Endpoint = u .Host
cfg .Traces .URLPath = path .Join (u .Path , DefaultTracesPath )
return cfg
}, withEndpointForGRPC (u )))
}),
envconfig .WithURL ("TRACES_ENDPOINT" , func (u *url .URL ) {
opts = append (opts , withEndpointScheme (u ))
opts = append (opts , newSplitOption (func (cfg Config ) Config {
cfg .Traces .Endpoint = u .Host
path := u .Path
if path == "" {
path = "/"
}
cfg .Traces .URLPath = path
return cfg
}, withEndpointForGRPC (u )))
}),
envconfig .WithCertPool ("CERTIFICATE" , func (p *x509 .CertPool ) { tlsConf .RootCAs = p }),
envconfig .WithCertPool ("TRACES_CERTIFICATE" , func (p *x509 .CertPool ) { tlsConf .RootCAs = p }),
envconfig .WithClientCert (
"CLIENT_CERTIFICATE" ,
"CLIENT_KEY" ,
func (c tls .Certificate ) { tlsConf .Certificates = []tls .Certificate {c } },
),
envconfig .WithClientCert (
"TRACES_CLIENT_CERTIFICATE" ,
"TRACES_CLIENT_KEY" ,
func (c tls .Certificate ) { tlsConf .Certificates = []tls .Certificate {c } },
),
withTLSConfig (tlsConf , func (c *tls .Config ) { opts = append (opts , WithTLSClientConfig (c )) }),
envconfig .WithBool ("INSECURE" , func (b bool ) { opts = append (opts , withInsecure (b )) }),
envconfig .WithBool ("TRACES_INSECURE" , func (b bool ) { opts = append (opts , withInsecure (b )) }),
envconfig .WithHeaders ("HEADERS" , func (h map [string ]string ) { opts = append (opts , WithHeaders (h )) }),
envconfig .WithHeaders ("TRACES_HEADERS" , func (h map [string ]string ) { opts = append (opts , WithHeaders (h )) }),
WithEnvCompression ("COMPRESSION" , func (c Compression ) { opts = append (opts , WithCompression (c )) }),
WithEnvCompression ("TRACES_COMPRESSION" , func (c Compression ) { opts = append (opts , WithCompression (c )) }),
envconfig .WithDuration ("TIMEOUT" , func (d time .Duration ) { opts = append (opts , WithTimeout (d )) }),
envconfig .WithDuration ("TRACES_TIMEOUT" , func (d time .Duration ) { opts = append (opts , WithTimeout (d )) }),
)
return opts
}
func withEndpointScheme(u *url .URL ) GenericOption {
switch strings .ToLower (u .Scheme ) {
case "http" , "unix" :
return WithInsecure ()
default :
return WithSecure ()
}
}
func withEndpointForGRPC(u *url .URL ) func (cfg Config ) Config {
return func (cfg Config ) Config {
cfg .Traces .Endpoint = path .Join (u .Host , u .Path )
return cfg
}
}
func WithEnvCompression (n string , fn func (Compression )) func (e *envconfig .EnvOptionsReader ) {
return func (e *envconfig .EnvOptionsReader ) {
if v , ok := e .GetEnvValue (n ); ok {
cp := NoCompression
if v == "gzip" {
cp = GzipCompression
}
fn (cp )
}
}
}
func withInsecure(b bool ) GenericOption {
if b {
return WithInsecure ()
}
return WithSecure ()
}
func withTLSConfig(c *tls .Config , fn func (*tls .Config )) func (e *envconfig .EnvOptionsReader ) {
return func (e *envconfig .EnvOptionsReader ) {
if c .RootCAs != nil || len (c .Certificates ) > 0 {
fn (c )
}
}
}
The pages are generated with Golds v0.8.2 . (GOOS=linux GOARCH=amd64)
Golds is a Go 101 project developed by Tapir Liu .
PR and bug reports are welcome and can be submitted to the issue list .
Please follow @zigo_101 (reachable from the left QR code) to get the latest news of Golds .