Skip to content

Commit

Permalink
Add Datadog tracer as a supported tracer (sourcegraph#315)
Browse files Browse the repository at this point in the history
Co-authored-by: Keegan Carruthers-Smith <[email protected]>

Zoekt will use the Datadog tracer when the env var DD_ENV is set. That env var will also enable the Datadog Continous profiler in its low overhead mode.
  • Loading branch information
daxmc99 authored Apr 8, 2022
1 parent acffe79 commit ea82fcb
Show file tree
Hide file tree
Showing 5 changed files with 190 additions and 86 deletions.
93 changes: 25 additions & 68 deletions cmd/zoekt-webserver/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ import (
"os"
"os/signal"
"path/filepath"
"reflect"
"strconv"
"strings"
"syscall"
Expand All @@ -39,18 +38,18 @@ import (
"github.com/google/zoekt/build"
"github.com/google/zoekt/debugserver"
"github.com/google/zoekt/internal/profiler"
"github.com/google/zoekt/internal/tracer"
"github.com/google/zoekt/query"
"github.com/google/zoekt/shards"
"github.com/google/zoekt/stream"
"github.com/google/zoekt/web"

"github.com/opentracing/opentracing-go"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promauto"
"go.uber.org/automaxprocs/maxprocs"

"github.com/uber/jaeger-client-go"
jaegercfg "github.com/uber/jaeger-client-go/config"
jaegermetrics "github.com/uber/jaeger-lib/metrics"
"go.uber.org/automaxprocs/maxprocs"
"gopkg.in/DataDog/dd-trace-go.v1/ddtrace"
)

const logFormat = "2006-01-02T15-04-05.999999999Z07"
Expand Down Expand Up @@ -117,7 +116,6 @@ func writeTemplates(dir string) error {
}

func main() {
logDir := flag.String("log_dir", "", "log to this directory rather than stderr.")
logRefresh := flag.Duration("log_refresh", 24*time.Hour, "if using --log_dir, start writing a new file this often.")

listen := flag.String("listen", ":6070", "listen on this address.")
Expand All @@ -137,6 +135,20 @@ func main() {
version := flag.Bool("version", false, "Print version number")

flag.Parse()
// avoid a panic due to log_dir flag already being defined in glog (a transient dependency)
logDirFlag := flag.Lookup("log_dir")
if logDirFlag != nil {
logDir := logDirFlag.Value.String()
if logDir != "" {
if fi, err := os.Lstat(logDir); err != nil || !fi.IsDir() {
log.Fatalf("%s is not a directory", logDir)
}
// We could do fdup acrobatics to also redirect
// stderr, but it is simpler and more portable for the
// caller to divert stderr output if necessary.
go divertLogs(logDir, *logRefresh)
}
}

if *version {
fmt.Printf("zoekt-webserver version %q\n", zoekt.Version)
Expand All @@ -150,19 +162,9 @@ func main() {
os.Exit(0)
}

initializeJaeger()
tracer.Init("zoekt-webserver", zoekt.Version)
profiler.Init("zoekt-webserver", zoekt.Version, -1)

if *logDir != "" {
if fi, err := os.Lstat(*logDir); err != nil || !fi.IsDir() {
log.Fatalf("%s is not a directory", *logDir)
}
// We could do fdup acrobatics to also redirect
// stderr, but it is simpler and more portable for the
// caller to divert stderr output if necessary.
go divertLogs(*logDir, *logRefresh)
}

// Tune GOMAXPROCS to match Linux container CPU quota.
_, _ = maxprocs.Set()

Expand Down Expand Up @@ -474,59 +476,14 @@ func traceID(ctx context.Context) string {

// traceIDFromSpan returns a trace ID, if any, found in the given span.
func traceIDFromSpan(span opentracing.Span) string {
spanCtx, ok := span.Context().(jaeger.SpanContext)
if !ok {
return ""
}
return spanCtx.TraceID().String()
}
switch v := span.Context().(type) {
case jaeger.SpanContext:
return v.TraceID().String()

func initializeJaeger() {
jaegerDisabled := os.Getenv("JAEGER_DISABLED")
if jaegerDisabled == "" {
return
case ddtrace.SpanContext:
return strconv.FormatUint(v.TraceID(), 10)
}
isJaegerDisabled, err := strconv.ParseBool(jaegerDisabled)
if err != nil {
log.Printf("EROR: failed to parse JAEGER_DISABLED: %s", err)
return
}
if isJaegerDisabled {
return
}
cfg, err := jaegercfg.FromEnv()
cfg.ServiceName = "zoekt"
if err != nil {
log.Printf("EROR: could not initialize jaeger tracer from env, error: %v", err.Error())
return
}
cfg.Tags = append(cfg.Tags, opentracing.Tag{Key: "service.version", Value: zoekt.Version})
if reflect.DeepEqual(cfg.Sampler, &jaegercfg.SamplerConfig{}) {
// Default sampler configuration for when it is not specified via
// JAEGER_SAMPLER_* env vars. In most cases, this is sufficient
// enough to connect to Jaeger without any env vars.
cfg.Sampler.Type = jaeger.SamplerTypeConst
cfg.Sampler.Param = 1
}
tracer, _, err := cfg.NewTracer(
jaegercfg.Logger(&jaegerLogger{}),
jaegercfg.Metrics(jaegermetrics.NullFactory),
)
if err != nil {
log.Printf("could not initialize jaeger tracer, error: %v", err.Error())
}
opentracing.SetGlobalTracer(tracer)
}

type jaegerLogger struct{}

func (l *jaegerLogger) Error(msg string) {
log.Printf("ERROR: %s", msg)
}

// Infof logs a message at info priority
func (l *jaegerLogger) Infof(msg string, args ...interface{}) {
log.Printf(msg, args...)
return ""
}

var (
Expand Down
14 changes: 14 additions & 0 deletions cmd/zoekt-webserver/main_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package main

import (
"flag"
"testing"
)

func TestLogDirFlag(t *testing.T) {

logDirFlag := flag.Lookup("log_dir")
if logDirFlag == nil {
t.Fatal("log_dir flag not found, this breaks OSS users. Was a dependency modified?")
}
}
69 changes: 61 additions & 8 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,9 @@ module github.com/google/zoekt

require (
cloud.google.com/go v0.82.0
github.com/ProtonMail/go-crypto v0.0.0-20210707164159-52430bf6b52c // indirect
github.com/RoaringBitmap/roaring v0.9.4
github.com/andygrunwald/go-gerrit v0.0.0-20191101112536-3f5e365ccf57
github.com/bits-and-blooms/bitset v1.2.1 // indirect
github.com/bmatcuk/doublestar v1.3.4
github.com/codahale/hdrhistogram v0.0.0-20161010025455-3a0bb77429bd // indirect
github.com/fsnotify/fsnotify v1.4.9
github.com/gfleury/go-bitbucket-v1 v0.0.0-20200312180434-e5170e3280fb
github.com/go-enry/go-enry/v2 v2.8.0
Expand All @@ -20,15 +17,12 @@ require (
github.com/hashicorp/go-retryablehttp v0.6.6
github.com/keegancsmith/rpc v1.1.0
github.com/keegancsmith/tmpfriend v0.0.0-20180423180255-86e88902a513
github.com/kevinburke/ssh_config v1.1.0 // indirect
github.com/kylelemons/godebug v1.1.0
github.com/mxk/go-flowrate v0.0.0-20140419014527-cca7078d478f
github.com/opentracing/opentracing-go v1.2.0
github.com/peterbourgon/ff/v3 v3.1.2
github.com/prometheus/client_golang v1.5.1
github.com/prometheus/procfs v0.0.10 // indirect
github.com/rs/xid v1.3.0
github.com/sergi/go-diff v1.2.0 // indirect
github.com/sourcegraph/go-ctags v0.0.0-20220210084826-96f4236f0a78
github.com/uber/jaeger-client-go v2.25.0+incompatible
github.com/uber/jaeger-lib v2.2.0+incompatible
Expand All @@ -37,9 +31,68 @@ require (
golang.org/x/net v0.0.0-20211020060615-d418f374d309
golang.org/x/oauth2 v0.0.0-20210514164344-f6687ab2804c
golang.org/x/sync v0.0.0-20210220032951-036812b2e83c
gopkg.in/DataDog/dd-trace-go.v1 v1.37.0
gopkg.in/DataDog/dd-trace-go.v1 v1.37.1
gopkg.in/natefinch/lumberjack.v2 v2.0.0
humungus.tedunangst.com/r/gerc v0.1.2
)

go 1.13
require (
github.com/DataDog/datadog-agent/pkg/obfuscate v0.0.0-20211129110424-6491aa3bf583 // indirect
github.com/DataDog/datadog-go v4.8.2+incompatible // indirect
github.com/DataDog/datadog-go/v5 v5.0.2 // indirect
github.com/DataDog/gostackparse v0.5.0 // indirect
github.com/DataDog/sketches-go v1.0.0 // indirect
github.com/Microsoft/go-winio v0.5.1 // indirect
github.com/ProtonMail/go-crypto v0.0.0-20210707164159-52430bf6b52c // indirect
github.com/acomagu/bufpipe v1.0.3 // indirect
github.com/beorn7/perks v1.0.1 // indirect
github.com/bits-and-blooms/bitset v1.2.1 // indirect
github.com/cespare/xxhash/v2 v2.1.2 // indirect
github.com/codahale/hdrhistogram v0.0.0-20161010025455-3a0bb77429bd // indirect
github.com/dgraph-io/ristretto v0.1.0 // indirect
github.com/dustin/go-humanize v1.0.0 // indirect
github.com/emirpasic/gods v1.12.0 // indirect
github.com/go-enry/go-oniguruma v1.2.1 // indirect
github.com/go-git/gcfg v1.5.0 // indirect
github.com/go-git/go-billy/v5 v5.3.1 // indirect
github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b // indirect
github.com/golang/groupcache v0.0.0-20200121045136-8c9f03a8e57e // indirect
github.com/golang/protobuf v1.5.2 // indirect
github.com/google/go-querystring v1.0.0 // indirect
github.com/google/pprof v0.0.0-20210506205249-923b5ab0fc1a // indirect
github.com/google/uuid v1.3.0 // indirect
github.com/googleapis/gax-go/v2 v2.0.5 // indirect
github.com/hashicorp/go-cleanhttp v0.5.2 // indirect
github.com/imdario/mergo v0.3.12 // indirect
github.com/jbenet/go-context v0.0.0-20150711004518-d14ea06fba99 // indirect
github.com/josharian/intern v1.0.0 // indirect
github.com/kevinburke/ssh_config v1.1.0 // indirect
github.com/mailru/easyjson v0.7.7 // indirect
github.com/matttproud/golang_protobuf_extensions v1.0.1 // indirect
github.com/mitchellh/go-homedir v1.1.0 // indirect
github.com/mitchellh/mapstructure v1.4.2 // indirect
github.com/mschoch/smat v0.2.0 // indirect
github.com/philhofer/fwd v1.1.1 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/prometheus/client_model v0.2.0 // indirect
github.com/prometheus/common v0.9.1 // indirect
github.com/prometheus/procfs v0.0.10 // indirect
github.com/sergi/go-diff v1.2.0 // indirect
github.com/tinylib/msgp v1.1.2 // indirect
github.com/xanzy/ssh-agent v0.3.0 // indirect
go.opencensus.io v0.23.0 // indirect
go.uber.org/atomic v1.9.0 // indirect
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519 // indirect
golang.org/x/sys v0.0.0-20220227234510-4e6760a101f9 // indirect
golang.org/x/text v0.3.7 // indirect
golang.org/x/time v0.0.0-20211116232009-f0f3c7e86c11 // indirect
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 // indirect
google.golang.org/api v0.46.0 // indirect
google.golang.org/appengine v1.6.7 // indirect
google.golang.org/genproto v0.0.0-20210517163617-5e0236093d7a // indirect
google.golang.org/grpc v1.37.1 // indirect
google.golang.org/protobuf v1.27.1 // indirect
gopkg.in/warnings.v0 v0.1.2 // indirect
)

go 1.17
12 changes: 2 additions & 10 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,6 @@ github.com/fatih/color v1.7.0/go.mod h1:Zm6kSWBoL9eyXnKyktHP6abPY2pDugNf5Kwzbycv
github.com/fatih/color v1.9.0 h1:8xPHl4/q1VyqGIPif1F+1V3Y3lSmrq01EabUW3CoW5s=
github.com/fatih/color v1.9.0/go.mod h1:eQcE1qtQxscV5RaZvpXrrb8Drkc3/DdQ+uUYCNjL+zU=
github.com/fatih/structs v1.1.0/go.mod h1:9NiDSp5zOcgEDl+j00MP/WkGVPOlPRLejGD8Ga6PJ7M=
github.com/flynn/go-shlex v0.0.0-20150515145356-3f9db97f8568 h1:BHsljHzVlRcyQhjrss6TZTdY2VfCqZPbv5k3iBFa2ZQ=
github.com/flynn/go-shlex v0.0.0-20150515145356-3f9db97f8568/go.mod h1:xEzjJPgXI435gkrCt3MPfRiAkVrwSbHsst4LCFVfpJc=
github.com/fortytw2/leaktest v1.3.0/go.mod h1:jDsjWgpAGjm2CA7WthBh/CdZYEPF31XHquHwclZch5g=
github.com/frankban/quicktest v1.13.0/go.mod h1:qLE0fzW0VuyUAJgPU19zByoIr0HtCHN/r/VLSOOIySU=
Expand Down Expand Up @@ -427,10 +426,8 @@ github.com/google/gofuzz v0.0.0-20161122191042-44d81051d367/go.mod h1:HP5RmnzzSN
github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
github.com/google/gofuzz v1.2.0 h1:xRy4A+RhZaiKjJ1bPfwQ8sedCA+YS2YcCHW6ec7JMi0=
github.com/google/gofuzz v1.2.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
github.com/google/martian v2.1.0+incompatible h1:/CP5g8u/VJHijgedC/Legn3BAbAaWPgecwXBIDzw5no=
github.com/google/martian v2.1.0+incompatible/go.mod h1:9I4somxYTbIHy5NJKHRl3wXiIaQGbYVAs8BPL6v8lEs=
github.com/google/martian/v3 v3.0.0/go.mod h1:y5Zk1BBys9G+gd6Jrk0W3cC1+ELVxBWuIGO+w/tUAp0=
github.com/google/martian/v3 v3.1.0 h1:wCKgOCHuUEVfsaQLpPSJb7VdYCdTVZQAuOdYm1yc/60=
github.com/google/martian/v3 v3.1.0/go.mod h1:y5Zk1BBys9G+gd6Jrk0W3cC1+ELVxBWuIGO+w/tUAp0=
github.com/google/pprof v0.0.0-20181206194817-3ea8567a2e57/go.mod h1:zfwlbNMJ+OItoe0UupaVj+oy1omPYYDuagoSzA8v9mc=
github.com/google/pprof v0.0.0-20190515194954-54271f7e092f/go.mod h1:zfwlbNMJ+OItoe0UupaVj+oy1omPYYDuagoSzA8v9mc=
Expand Down Expand Up @@ -661,12 +658,9 @@ github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
github.com/kylelemons/godebug v1.1.0 h1:RPNrshWIDI6G2gRW9EHilWtl7Z6Sb1BR0xunSBf0SNc=
github.com/kylelemons/godebug v1.1.0/go.mod h1:9/0rRGxNHcop5bhtWyNeEfOS8JIWk580+fNqagV/RAw=
github.com/labstack/echo v3.3.10+incompatible h1:pGRcYk231ExFAyoAjAfD85kQzRJCRI8bbnE7CX5OEgg=
github.com/labstack/echo v3.3.10+incompatible/go.mod h1:0INS7j/VjnFxD4E2wkz67b8cVwCLbBmJyDaka6Cmk1s=
github.com/labstack/echo/v4 v4.2.0 h1:jkCSsjXmBmapVXF6U4BrSz/cgofWM0CU3Q74wQvXkIc=
github.com/labstack/echo/v4 v4.2.0/go.mod h1:AA49e0DZ8kk5jTOOCKNuPR6oTnBS0dYiM4FW1e6jwpg=
github.com/labstack/gommon v0.3.0/go.mod h1:MULnywXg0yavhxWKc+lOruYdAhDwPK9wf0OL7NoOu+k=
github.com/labstack/gommon v0.3.1 h1:OomWaJXm7xR6L1HmEtGyQf26TEn7V6X88mktX9kee9o=
github.com/labstack/gommon v0.3.1/go.mod h1:uW6kP17uPlLJsD3ijUYn3/M5bAxtlZhMI6m3MFxTMTM=
github.com/leodido/go-urn v1.2.0/go.mod h1:+8+nEpDfqqsY+g338gtMEUOtuK+4dEMhiQEgxpxOKII=
github.com/lib/pq v1.0.0/go.mod h1:5WUZQaWbwv1U+lTReE5YruASi9Al49XbQIvNi/34Woo=
Expand Down Expand Up @@ -956,13 +950,11 @@ github.com/ultraware/whitespace v0.0.4/go.mod h1:aVMh/gQve5Maj9hQ/hg+F75lr/X5A89
github.com/urfave/cli v0.0.0-20171014202726-7bc6a0acffa5/go.mod h1:70zkFmudgCuE/ngEzBv17Jvp/497gISqfk5gWijbERA=
github.com/urfave/negroni v1.0.0/go.mod h1:Meg73S6kFm/4PpbYdq35yYWoCZ9mS/YSx+lKnmiohz4=
github.com/uudashr/gocognit v1.0.1/go.mod h1:j44Ayx2KW4+oB6SWMv8KsmHzZrOInQav7D3cQMJ5JUM=
github.com/valyala/bytebufferpool v1.0.0 h1:GqA5TC/0021Y/b9FG4Oi9Mr3q7XYx6KllzawFIhcdPw=
github.com/valyala/bytebufferpool v1.0.0/go.mod h1:6bBcMArwyJ5K/AmCkWv1jt77kVWyCJ6HpOuEn7z0Csc=
github.com/valyala/fasthttp v1.2.0/go.mod h1:4vX61m6KN+xDduDNwXrhIAVZaZaZiQ1luJk8LWSxF3s=
github.com/valyala/fasthttp v1.26.0/go.mod h1:cmWIqlu99AO/RKcp1HWaViTqc57FswJOfYYdPJBl8BA=
github.com/valyala/fasthttp v1.32.0/go.mod h1:2rsYD01CKFrjjsvFxx75KlEUNpWNBY9JWD3K/7o2Cus=
github.com/valyala/fasttemplate v1.0.1/go.mod h1:UQGH1tvbgY+Nz5t2n7tXsz52dQxojPUpymEIMZ47gx8=
github.com/valyala/fasttemplate v1.2.1 h1:TVEnxayobAdVkhQfrfes2IzOB6o+z4roRkPF52WA1u4=
github.com/valyala/fasttemplate v1.2.1/go.mod h1:KHLXt3tVN2HBp8eijSv/kGJopbvo7S+qRAEEKiv+SiQ=
github.com/valyala/quicktemplate v1.2.0/go.mod h1:EH+4AkTd43SvgIbQHYu59/cJyxDoOVRUAfrukLPuGJ4=
github.com/valyala/tcplisten v0.0.0-20161114210144-ceec8f93295a/go.mod h1:v3UYOV9WzVtRmSR+PDvWpU/qWl4Wa5LApYYX4ZtKbio=
Expand Down Expand Up @@ -1491,8 +1483,8 @@ google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp0
google.golang.org/protobuf v1.26.0/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQnmE0givc=
google.golang.org/protobuf v1.27.1 h1:SnqbnDw1V7RiZcXPx5MEeqPv2s79L9i7BJUlG/+RurQ=
google.golang.org/protobuf v1.27.1/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQnmE0givc=
gopkg.in/DataDog/dd-trace-go.v1 v1.37.0 h1:V485ujNZdVc9pBQgpalgSpTRlr3PdxcX5v60YuiDFWc=
gopkg.in/DataDog/dd-trace-go.v1 v1.37.0/go.mod h1:HMpV5TQ38YAfRJ8OlM7UfLyGg6D01MPNHRFwsnrGMB0=
gopkg.in/DataDog/dd-trace-go.v1 v1.37.1 h1:rgO9oC3Mr7es0zKIsaooL50UY0qboUDdueP7MzH1fUI=
gopkg.in/DataDog/dd-trace-go.v1 v1.37.1/go.mod h1:HMpV5TQ38YAfRJ8OlM7UfLyGg6D01MPNHRFwsnrGMB0=
gopkg.in/airbrake/gobrake.v2 v2.0.9/go.mod h1:/h5ZAUhDkGaJfjzjKLSjv6zCL6O0LLBxU4K+aSYdM/U=
gopkg.in/alecthomas/kingpin.v2 v2.2.6/go.mod h1:FMv+mEhP44yOT+4EoQTLFTRgOQ1FBLkstjWtayDeSgw=
gopkg.in/asn1-ber.v1 v1.0.0-20181015200546-f715ec2f112d/go.mod h1:cuepJuh7vyXfUyUwEgHQXw849cJrilpS5NeIjOWESAw=
Expand Down
Loading

0 comments on commit ea82fcb

Please sign in to comment.