Skip to content

Commit

Permalink
Move stackdriver stuff into its own file
Browse files Browse the repository at this point in the history
  • Loading branch information
anacrolix committed Apr 23, 2019
1 parent 9c6a9d6 commit d626bf6
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 41 deletions.
43 changes: 2 additions & 41 deletions cmd/ipfs/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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"
Expand Down Expand Up @@ -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"))
Expand Down
45 changes: 45 additions & 0 deletions cmd/ipfs/stackdriver.go
Original file line number Diff line number Diff line change
@@ -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()
}

0 comments on commit d626bf6

Please sign in to comment.