From db63ce76993c3a5f1a41cf2db4643647a8905189 Mon Sep 17 00:00:00 2001 From: Thomas Meire Date: Fri, 23 Aug 2024 22:13:20 +0200 Subject: [PATCH] Add env var to disable traces to stdout with test --- go.mod | 5 +++++ go.sum | 3 +++ main.go | 5 ++++- pkg/environment/environment.go | 14 ++++++++++++++ pkg/o11y/otel.go | 18 ++++++++++-------- 5 files changed, 36 insertions(+), 9 deletions(-) diff --git a/go.mod b/go.mod index 4c335cd..367cad4 100644 --- a/go.mod +++ b/go.mod @@ -18,15 +18,19 @@ require ( github.com/beorn7/perks v1.0.1 // indirect github.com/cenkalti/backoff/v4 v4.3.0 // indirect github.com/cespare/xxhash/v2 v2.3.0 // indirect + github.com/davecgh/go-spew v1.1.1 // indirect github.com/felixge/httpsnoop v1.0.4 // indirect github.com/go-logr/logr v1.4.2 // indirect github.com/go-logr/stdr v1.2.2 // indirect github.com/google/uuid v1.6.0 // indirect github.com/grpc-ecosystem/grpc-gateway/v2 v2.20.0 // indirect + github.com/kelseyhightower/envconfig v1.4.0 // indirect github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect + github.com/pmezard/go-difflib v1.0.0 // indirect github.com/prometheus/client_model v0.6.1 // indirect github.com/prometheus/common v0.55.0 // indirect github.com/prometheus/procfs v0.15.1 // indirect + github.com/stretchr/testify v1.9.0 // indirect go.opentelemetry.io/otel/exporters/otlp/otlptrace v1.28.0 // indirect go.opentelemetry.io/otel/metric v1.28.0 // indirect go.opentelemetry.io/otel/trace v1.28.0 // indirect @@ -38,4 +42,5 @@ require ( google.golang.org/genproto/googleapis/rpc v0.0.0-20240701130421-f6361c86f094 // indirect google.golang.org/grpc v1.64.1 // indirect google.golang.org/protobuf v1.34.2 // indirect + gopkg.in/yaml.v3 v3.0.1 // indirect ) diff --git a/go.sum b/go.sum index 11d3c8d..4f26e1e 100644 --- a/go.sum +++ b/go.sum @@ -19,6 +19,8 @@ github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0= github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= github.com/grpc-ecosystem/grpc-gateway/v2 v2.20.0 h1:bkypFPDjIYGfCYD5mRBvpqxfYX1YCS1PXdKYWi8FsN0= github.com/grpc-ecosystem/grpc-gateway/v2 v2.20.0/go.mod h1:P+Lt/0by1T8bfcF3z737NnSbmxQAppXMRziHUxPOC8k= +github.com/kelseyhightower/envconfig v1.4.0 h1:Im6hONhd3pLkfDFsbRgu68RDNkGF1r3dvMUtDTo2cv8= +github.com/kelseyhightower/envconfig v1.4.0/go.mod h1:cccZRl6mQpaq41TPp5QxidR+Sa3axMbJDNb//FQX6Gg= github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 h1:C3w9PqII01/Oq1c1nUAm88MOHcQC9l5mIlSMApZMrHA= github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822/go.mod h1:+n7T8mK8HuQTcFwEeznm/DIxMOiR9yIdICNftLE1DvQ= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= @@ -73,5 +75,6 @@ google.golang.org/grpc v1.64.1 h1:LKtvyfbX3UGVPFcGqJ9ItpVWW6oN/2XqTxfAnwRRXiA= google.golang.org/grpc v1.64.1/go.mod h1:hiQF4LFZelK2WKaP6W0L92zGHtiQdZxk8CrSdvyjeP0= google.golang.org/protobuf v1.34.2 h1:6xV6lTsCfpGD21XK49h7MhtcApnLqkfYgPcdHftf6hg= google.golang.org/protobuf v1.34.2/go.mod h1:qYOHts0dSfpeUzUFpOMr/WGzszTmLH+DiWniOlNbLDw= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/main.go b/main.go index bf98ee9..26548fd 100644 --- a/main.go +++ b/main.go @@ -7,6 +7,7 @@ import ( "os/signal" "syscall" + "github.com/blackskad/go-web-scaffold/pkg/environment" "github.com/blackskad/go-web-scaffold/pkg/o11y" "github.com/blackskad/go-web-scaffold/pkg/web" ) @@ -14,6 +15,8 @@ import ( func main() { ctx := context.Background() + conf := environment.Parse() + o11y.StartPProfServer() sigc := make(chan os.Signal, 1) @@ -28,7 +31,7 @@ func main() { server := &http.Server{ Addr: ":8080", - Handler: o11y.Register(ctx, mux), + Handler: o11y.Register(ctx, conf, mux), } done := make(chan struct{}) diff --git a/pkg/environment/environment.go b/pkg/environment/environment.go index 595acb2..fd365dd 100644 --- a/pkg/environment/environment.go +++ b/pkg/environment/environment.go @@ -1,4 +1,18 @@ package environment +import "github.com/kelseyhightower/envconfig" + const Name = "go-web-scaffold" const Version = "development" + +type Config struct { + EnableTracesStdout bool `default:"true" split_words:"true"` +} + +func Parse() Config { + var conf Config + + envconfig.MustProcess("go-web-scaffold", &conf) + + return conf +} diff --git a/pkg/o11y/otel.go b/pkg/o11y/otel.go index 0e477ab..2782a3e 100644 --- a/pkg/o11y/otel.go +++ b/pkg/o11y/otel.go @@ -23,7 +23,7 @@ import ( semconv "go.opentelemetry.io/otel/semconv/v1.26.0" ) -func initTracer(ctx context.Context) (*sdktrace.TracerProvider, error) { +func initTracer(ctx context.Context, conf environment.Config) (*sdktrace.TracerProvider, error) { opts := []sdktrace.TracerProviderOption{ // TODO: add ability to switch between sdktrace.AlwaysSample and sdktrace.ProbabilitySampler for production. sdktrace.WithSampler(sdktrace.AlwaysSample()), @@ -37,11 +37,13 @@ func initTracer(ctx context.Context) (*sdktrace.TracerProvider, error) { } // Always export to stdout - expStdout, err := stdouttrace.New() - if err != nil { - return nil, err + if conf.EnableTracesStdout { + expStdout, err := stdouttrace.New() + if err != nil { + return nil, err + } + opts = append(opts, sdktrace.WithBatcher(expStdout)) } - opts = append(opts, sdktrace.WithBatcher(expStdout)) // If the env var is set, also export to an otel collector if os.Getenv("OTEL_EXPORTER_OTLP_ENDPOINT") != "" { @@ -56,7 +58,7 @@ func initTracer(ctx context.Context) (*sdktrace.TracerProvider, error) { tp := sdktrace.NewTracerProvider(opts...) otel.SetTracerProvider(tp) otel.SetTextMapPropagator(propagation.NewCompositeTextMapPropagator(propagation.TraceContext{}, propagation.Baggage{})) - return tp, err + return tp, nil } func initMeter() (*sdkmetric.MeterProvider, error) { @@ -96,9 +98,9 @@ func runPrometheusServer() { var initOnce sync.Once -func Register(ctx context.Context, h http.Handler) http.Handler { +func Register(ctx context.Context, conf environment.Config, h http.Handler) http.Handler { initOnce.Do(func() { - _, err := initTracer(ctx) + _, err := initTracer(ctx, conf) if err != nil { log.Fatal(err) }