From d626bf69eed164060d5db609af1866e413446ea3 Mon Sep 17 00:00:00 2001 From: Matt Joiner Date: Tue, 23 Apr 2019 11:41:59 +1000 Subject: [PATCH] Move stackdriver stuff into its own file --- cmd/ipfs/main.go | 43 ++------------------------------------- cmd/ipfs/stackdriver.go | 45 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 47 insertions(+), 41 deletions(-) create mode 100644 cmd/ipfs/stackdriver.go diff --git a/cmd/ipfs/main.go b/cmd/ipfs/main.go index bb611c97dad..79bc974aed5 100644 --- a/cmd/ipfs/main.go +++ b/cmd/ipfs/main.go @@ -9,13 +9,10 @@ import ( "os" "path/filepath" "runtime/pprof" - "strconv" "strings" "time" - "contrib.go.opencensus.io/exporter/stackdriver" - "go.opencensus.io/trace" - + cmds "github.com/ipfs/go-ipfs-cmds" util "github.com/ipfs/go-ipfs/cmd/ipfs/util" oldcmds "github.com/ipfs/go-ipfs/commands" core "github.com/ipfs/go-ipfs/core" @@ -26,7 +23,7 @@ import ( fsrepo "github.com/ipfs/go-ipfs/repo/fsrepo" osh "github.com/Kubuxu/go-os-helper" - "github.com/ipfs/go-ipfs-cmds" + cmds "github.com/ipfs/go-ipfs-cmds" "github.com/ipfs/go-ipfs-cmds/cli" "github.com/ipfs/go-ipfs-cmds/http" config "github.com/ipfs/go-ipfs-config" @@ -91,42 +88,6 @@ func main() { os.Exit(exitCode) } -func withStackdriverTracing(f func()) { - sd, err := stackdriver.NewExporter(stackdriver.Options{ - Location: func() string { - s, ok := os.LookupEnv("STACKDRIVER_LOCATION") - if ok { - return s - } - s, _ = os.Hostname() - return s - }(), - ProjectID: os.Getenv("GOOGLE_CLOUD_PROJECT"), - }) - if err != nil { - log.Warningf("error creating the stackdriver exporter: %v", err) - goto noExporter - } - // It is imperative to invoke flush before your main function exits - defer sd.Flush() - - if s, ok := os.LookupEnv("OCTRACE_SAMPLE_PROB"); ok { - f, err := strconv.ParseFloat(s, 64) - if err != nil { - log.Errorf("parsing OC trace sample probability: %v", err) - } else { - trace.ApplyConfig(trace.Config{ - DefaultSampler: trace.ProbabilitySampler(f), - }) - } - } - - // Register it as a trace exporter - trace.RegisterExporter(sd) -noExporter: - f() -} - func mainRet() int { rand.Seed(time.Now().UnixNano()) ctx := logging.ContextWithLoggable(context.Background(), loggables.Uuid("session")) diff --git a/cmd/ipfs/stackdriver.go b/cmd/ipfs/stackdriver.go new file mode 100644 index 00000000000..7bdb8e33e22 --- /dev/null +++ b/cmd/ipfs/stackdriver.go @@ -0,0 +1,45 @@ +package main + +import ( + "os" + "strconv" + + "contrib.go.opencensus.io/exporter/stackdriver" + "go.opencensus.io/trace" +) + +func withStackdriverTracing(f func()) { + sd, err := stackdriver.NewExporter(stackdriver.Options{ + Location: func() string { + s, ok := os.LookupEnv("STACKDRIVER_LOCATION") + if ok { + return s + } + s, _ = os.Hostname() + return s + }(), + ProjectID: os.Getenv("GOOGLE_CLOUD_PROJECT"), + }) + if err != nil { + log.Warningf("error creating the stackdriver exporter: %v", err) + goto noExporter + } + // It is imperative to invoke flush before your main function exits + defer sd.Flush() + + if s, ok := os.LookupEnv("OCTRACE_SAMPLE_PROB"); ok { + f, err := strconv.ParseFloat(s, 64) + if err != nil { + log.Errorf("parsing OC trace sample probability: %v", err) + } else { + trace.ApplyConfig(trace.Config{ + DefaultSampler: trace.ProbabilitySampler(f), + }) + } + } + + // Register it as a trace exporter + trace.RegisterExporter(sd) +noExporter: + f() +}