Skip to content

Commit

Permalink
Allow disabling of tracing
Browse files Browse the repository at this point in the history
  • Loading branch information
seanmcgary committed Jun 4, 2024
1 parent 2152f69 commit 2182efe
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 23 deletions.
5 changes: 3 additions & 2 deletions cmd/claim.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,13 +115,14 @@ var claimCmd = &cobra.Command{
Short: "Generate claim",
Long: ``,
Run: func(cmd *cobra.Command, args []string) {
tracer.StartTracer()
cfg := config.NewClaimConfig()

tracer.StartTracer(cfg.EnableTracing)
defer ddTracer.Stop()

span, ctx := ddTracer.StartSpanFromContext(context.Background(), "cmd::claim")
defer span.Finish()

cfg := config.NewClaimConfig()
logger, err := logger.NewLogger(&logger.LoggerConfig{
Debug: cfg.Debug,
})
Expand Down
5 changes: 3 additions & 2 deletions cmd/distribution.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,13 +84,14 @@ var distributionCmd = &cobra.Command{
Short: "Access distribution data",
Long: ``,
Run: func(cmd *cobra.Command, args []string) {
tracer.StartTracer()
cfg := config.NewDistributionConfig()

tracer.StartTracer(cfg.EnableTracing)
defer ddTracer.Stop()

span, ctx := ddTracer.StartSpanFromContext(context.Background(), "cmd::distribution")
defer span.Finish()

cfg := config.NewDistributionConfig()
logger, err := logger.NewLogger(&logger.LoggerConfig{
Debug: cfg.Debug,
})
Expand Down
1 change: 1 addition & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ func init() {
rootCmd.PersistentFlags().Bool("debug", false, "'true' or 'false'")
rootCmd.PersistentFlags().String("dd-statsd-url", "", "URL to use for DataDog StatsD. If empty, DD_DOGSTATSD_URL will be used")
rootCmd.PersistentFlags().Bool("enable-statsd", true, "Enable/disable statsd metrics collection")
rootCmd.PersistentFlags().Bool("enable-tracing", true, "Enable/disable tracing")

rootCmd.PersistentFlags().VisitAll(func(f *pflag.Flag) {
viper.BindPFlag(config.KebabToSnakeCase(f.Name), f)
Expand Down
2 changes: 1 addition & 1 deletion cmd/updater.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ var updaterCmd = &cobra.Command{
Run: func(cmd *cobra.Command, args []string) {
cfg := config.NewUpdaterConfig()

tracer.StartTracer()
tracer.StartTracer(cfg.EnableTracing)
defer ddTracer.Stop()

span, ctx := ddTracer.StartSpanFromContext(context.Background(), "cmd::updater")
Expand Down
34 changes: 17 additions & 17 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,11 @@ import (
)

type GlobalConfig struct {
Config string `mapstructure:"config"`
Debug bool `mapstructure:"debug"`
DDStatsdUrl string `mapstructure:"dd_statsd_url"`
EnableStatsd bool `mapstructure:"enable_statsd"`
Config string `mapstructure:"config"`
Debug bool `mapstructure:"debug"`
DDStatsdUrl string `mapstructure:"dd_statsd_url"`
EnableStatsd bool `mapstructure:"enable_statsd"`
EnableTracing bool `mapstructure:"enable_tracing"`
}

type Environment int
Expand Down Expand Up @@ -90,15 +91,20 @@ func StringEnvironmentFromEnum(env Environment) (string, error) {
return "", errors.New(fmt.Sprintf("String env not found for '%d'", env))
}

func GetGlobalConfig() GlobalConfig {
return GlobalConfig{
Config: viper.GetString("config"),
Debug: viper.GetBool("debug"),
DDStatsdUrl: viper.GetString("dd_statsd_url"),
EnableTracing: viper.GetBool("enable_tracing"),
}
}

// NewUpdaterConfig reads config values from viper and returns
// them in a struct
func NewUpdaterConfig() *UpdaterConfig {
updaterConfig = &UpdaterConfig{
GlobalConfig: GlobalConfig{
Config: viper.GetString("config"),
Debug: viper.GetBool("debug"),
DDStatsdUrl: viper.GetString("dd_statsd_url"),
},
GlobalConfig: GetGlobalConfig(),
Environment: parseEnvironment(viper.GetString("environment")),
Network: viper.GetString("network"),
RPCUrl: viper.GetString("rpc_url"),
Expand All @@ -111,10 +117,7 @@ func NewUpdaterConfig() *UpdaterConfig {

func NewDistributionConfig() *DistributionConfig {
distributionConfig = &DistributionConfig{
GlobalConfig: GlobalConfig{
Config: viper.GetString("config"),
Debug: viper.GetBool("debug"),
},
GlobalConfig: GetGlobalConfig(),
Environment: parseEnvironment(viper.GetString("environment")),
Network: viper.GetString("network"),
RPCUrl: viper.GetString("rpc_url"),
Expand All @@ -127,10 +130,7 @@ func NewDistributionConfig() *DistributionConfig {
}
func NewClaimConfig() *ClaimConfig {
claimConfig = &ClaimConfig{
GlobalConfig: GlobalConfig{
Config: viper.GetString("config"),
Debug: viper.GetBool("debug"),
},
GlobalConfig: GetGlobalConfig(),
Environment: parseEnvironment(viper.GetString("environment")),
Network: viper.GetString("network"),
RPCUrl: viper.GetString("rpc_url"),
Expand Down
8 changes: 7 additions & 1 deletion pkg/tracer/tracer.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
package tracer

import (
"gopkg.in/DataDog/dd-trace-go.v1/ddtrace/mocktracer"
ddTracer "gopkg.in/DataDog/dd-trace-go.v1/ddtrace/tracer"
)

func StartTracer() {
func StartTracer(enabled bool) {
if !enabled {
mocktracer.Start()
return
}
ddTracer.Start(
ddTracer.WithGlobalServiceName(true),
ddTracer.WithDebugMode(false),
ddTracer.WithLogStartup(false),
)
}

0 comments on commit 2182efe

Please sign in to comment.