From 5d263855d9ff45f2b2c29f3609a98dfa73cd3d8b Mon Sep 17 00:00:00 2001 From: Matt Joiner Date: Mon, 11 Jul 2022 11:37:14 +1000 Subject: [PATCH] cmd/torrent: Time and log TracerProvider.Shutdown --- cmd/torrent/main.go | 34 +++++++++++++++++++--------------- 1 file changed, 19 insertions(+), 15 deletions(-) diff --git a/cmd/torrent/main.go b/cmd/torrent/main.go index 51f5f84a..33e4eea9 100644 --- a/cmd/torrent/main.go +++ b/cmd/torrent/main.go @@ -4,19 +4,22 @@ package main import ( "context" "fmt" - "github.com/anacrolix/log" + stdLog "log" + "net/http" + "time" + + "github.com/prometheus/client_golang/prometheus" + "github.com/prometheus/client_golang/prometheus/promhttp" "go.opentelemetry.io/otel" "go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracegrpc" "go.opentelemetry.io/otel/sdk/trace" - stdLog "log" - "net/http" "github.com/anacrolix/bargle" "github.com/anacrolix/envpprof" + "github.com/anacrolix/log" xprometheus "github.com/anacrolix/missinggo/v2/prometheus" + "github.com/anacrolix/torrent/version" - "github.com/prometheus/client_golang/prometheus" - "github.com/prometheus/client_golang/prometheus/promhttp" ) func init() { @@ -24,6 +27,13 @@ func init() { http.Handle("/metrics", promhttp.Handler()) } +func shutdownTracerProvider(ctx context.Context, tp *trace.TracerProvider) { + started := time.Now() + err := tp.Shutdown(ctx) + elapsed := time.Since(started) + log.Levelf(log.Error, "shutting down tracer provider (took %v): %v", elapsed, err) +} + func main() { defer stdLog.SetFlags(stdLog.Flags() | stdLog.Lshortfile) @@ -32,19 +42,13 @@ func main() { if err != nil { log.Fatalf("creating tracing exporter: %v", err) } - tracingProvider := trace.NewTracerProvider(trace.WithBatcher(tracingExporter)) - defer tracingProvider.Shutdown(ctx) - otel.SetTracerProvider(tracingProvider) - //otel.SetTextMapPropagator( - // propagation.NewCompositeTextMapPropagator( - // propagation.TraceContext{}, - // propagation.Baggage{}, - // ), - //) + tracerProvider := trace.NewTracerProvider(trace.WithBatcher(tracingExporter)) + defer shutdownTracerProvider(ctx, tracerProvider) + otel.SetTracerProvider(tracerProvider) main := bargle.Main{} main.Defer(envpprof.Stop) - main.Defer(func() { tracingProvider.Shutdown(ctx) }) + main.Defer(func() { shutdownTracerProvider(ctx, tracerProvider) }) debug := false debugFlag := bargle.NewFlag(&debug) debugFlag.AddLong("debug")