Configure Datadog analytics rate(#33984)

This commit is contained in:
Dax McDonald 2022-04-19 11:23:24 -07:00 committed by GitHub
parent 526e9002d2
commit 2a20aea813
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 21 additions and 5 deletions

View File

@ -16,8 +16,6 @@ import (
"github.com/inconshreveable/log15"
"github.com/opentracing/opentracing-go"
"github.com/opentracing/opentracing-go/ext"
muxtrace "gopkg.in/DataDog/dd-trace-go.v1/contrib/gorilla/mux"
"github.com/sourcegraph/sourcegraph/cmd/frontend/envvar"
"github.com/sourcegraph/sourcegraph/cmd/frontend/globals"
"github.com/sourcegraph/sourcegraph/cmd/frontend/graphqlbackend"
@ -29,7 +27,9 @@ import (
"github.com/sourcegraph/sourcegraph/internal/env"
"github.com/sourcegraph/sourcegraph/internal/randstring"
"github.com/sourcegraph/sourcegraph/internal/trace"
"github.com/sourcegraph/sourcegraph/internal/tracer"
"github.com/sourcegraph/sourcegraph/lib/errors"
muxtrace "gopkg.in/DataDog/dd-trace-go.v1/contrib/gorilla/mux"
)
const (
@ -128,7 +128,7 @@ func InitRouter(db database.DB, codeIntelResolver graphqlbackend.CodeIntelResolv
var mockServeRepo func(w http.ResponseWriter, r *http.Request)
func newRouter() *muxtrace.Router {
r := muxtrace.NewRouter()
r := muxtrace.NewRouter(muxtrace.WithAnalyticsRate(tracer.MUX_ANALYTICS_TRACE_RATE))
r.StrictSlash(true)
// Top-level routes.

View File

@ -3,7 +3,9 @@ package tracer
import (
"fmt"
"io"
"os"
"reflect"
"strconv"
"sync"
"github.com/inconshreveable/log15"
@ -32,14 +34,25 @@ func init() {
if _, err := maxprocs.Set(); err != nil {
log15.Error("automaxprocs failed", "error", err)
}
if r := os.Getenv("MUX_ANALYTICS_TRACE_RATE"); r != "" {
rate, err := strconv.ParseFloat(r, 64)
if err != nil {
log15.Error("Failed to parse MUX_ANALYTICS_TRACE_RATE", "error", err)
return
}
MUX_ANALYTICS_TRACE_RATE = rate
}
}
var MUX_ANALYTICS_TRACE_RATE = 0.1
// options control the behavior of a TracerType
type options struct {
TracerType
externalURL string
debug bool
// these values are not configurable at runtime
// these values are not configurable by site config
serviceName string
version string
env string
@ -70,6 +83,9 @@ func Init(c conftypes.WatchableSiteConfig) {
if version.IsDev(version.Version()) {
opts.env = "dev"
}
if d := os.Getenv("DD_ENV"); d != "" {
opts.env = d
}
opts.version = version.Version()
initTracer(opts, c)
@ -173,7 +189,7 @@ func newTracer(opts *options, prevTracer TracerType) (opentracing.Tracer, io.Clo
if err != nil {
return nil, nil, errors.Wrap(err, "jaegercfg.FromEnv failed")
}
cfg.Tags = append(cfg.Tags, opentracing.Tag{Key: "service.version", Value: version.Version()})
cfg.Tags = append(cfg.Tags, opentracing.Tag{Key: "service.version", Value: opts.version}, opentracing.Tag{Key: "service.env", Value: opts.env})
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