cmd/torrent: Time and log TracerProvider.Shutdown

This commit is contained in:
Matt Joiner 2022-07-11 11:37:14 +10:00
parent d9ca26dabb
commit 5d263855d9
No known key found for this signature in database
GPG Key ID: 6B990B8185E7F782
1 changed files with 19 additions and 15 deletions

View File

@ -4,19 +4,22 @@ package main
import ( import (
"context" "context"
"fmt" "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"
"go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracegrpc" "go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracegrpc"
"go.opentelemetry.io/otel/sdk/trace" "go.opentelemetry.io/otel/sdk/trace"
stdLog "log"
"net/http"
"github.com/anacrolix/bargle" "github.com/anacrolix/bargle"
"github.com/anacrolix/envpprof" "github.com/anacrolix/envpprof"
"github.com/anacrolix/log"
xprometheus "github.com/anacrolix/missinggo/v2/prometheus" xprometheus "github.com/anacrolix/missinggo/v2/prometheus"
"github.com/anacrolix/torrent/version" "github.com/anacrolix/torrent/version"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
) )
func init() { func init() {
@ -24,6 +27,13 @@ func init() {
http.Handle("/metrics", promhttp.Handler()) 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() { func main() {
defer stdLog.SetFlags(stdLog.Flags() | stdLog.Lshortfile) defer stdLog.SetFlags(stdLog.Flags() | stdLog.Lshortfile)
@ -32,19 +42,13 @@ func main() {
if err != nil { if err != nil {
log.Fatalf("creating tracing exporter: %v", err) log.Fatalf("creating tracing exporter: %v", err)
} }
tracingProvider := trace.NewTracerProvider(trace.WithBatcher(tracingExporter)) tracerProvider := trace.NewTracerProvider(trace.WithBatcher(tracingExporter))
defer tracingProvider.Shutdown(ctx) defer shutdownTracerProvider(ctx, tracerProvider)
otel.SetTracerProvider(tracingProvider) otel.SetTracerProvider(tracerProvider)
//otel.SetTextMapPropagator(
// propagation.NewCompositeTextMapPropagator(
// propagation.TraceContext{},
// propagation.Baggage{},
// ),
//)
main := bargle.Main{} main := bargle.Main{}
main.Defer(envpprof.Stop) main.Defer(envpprof.Stop)
main.Defer(func() { tracingProvider.Shutdown(ctx) }) main.Defer(func() { shutdownTracerProvider(ctx, tracerProvider) })
debug := false debug := false
debugFlag := bargle.NewFlag(&debug) debugFlag := bargle.NewFlag(&debug)
debugFlag.AddLong("debug") debugFlag.AddLong("debug")