Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -266,13 +266,14 @@ func init() {
rootCmd.PersistentFlags().String("auth0-domain", "om-prod.eu.auth0.com", "Auth0 domain to connect to")

// tracing
rootCmd.PersistentFlags().String("honeycomb-api-key", "", "If specified, configures opentelemetry libraries to submit traces to honeycomb")
rootCmd.PersistentFlags().String("sentry-dsn", "", "If specified, configures sentry libraries to capture errors")
rootCmd.PersistentFlags().Bool("otel", false, "If specified, configures opentelemetry and - optionally, see --sentry-dsn - sentry using their default environment configs.")
rootCmd.PersistentFlags().String("honeycomb-api-key", "", "If specified, configures opentelemetry libraries to submit traces to honeycomb. This requires --otel to be set.")
rootCmd.PersistentFlags().String("sentry-dsn", "", "If specified, configures sentry libraries to capture errors. This requires --otel to be set.")
rootCmd.PersistentFlags().String("run-mode", "release", "Set the run mode for this service, 'release', 'debug' or 'test'. Defaults to 'release'.")
rootCmd.PersistentFlags().Bool("json-log", false, "Set to true to emit logs as json for easier parsing")
rootCmd.PersistentFlags().Bool("json-log", false, "Set to true to emit logs as json for easier parsing.")

// debugging
rootCmd.PersistentFlags().Bool("stdout-trace-dump", false, "Dump all otel traces to stdout for debugging")
rootCmd.PersistentFlags().Bool("stdout-trace-dump", false, "Dump all otel traces to stdout for debugging. This requires --otel to be set.")

// Run this before we do anything to set up the loglevel
rootCmd.PersistentPreRun = func(cmd *cobra.Command, args []string) {
Expand Down
79 changes: 44 additions & 35 deletions tracing/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ func tracingResource() *resource.Resource {
var tp *sdktrace.TracerProvider

func InitTracerWithHoneycomb(key string, opts ...otlptracehttp.Option) error {
if key != "" {
if key != "" && viper.GetBool("otel") {
opts = append(opts,
otlptracehttp.WithEndpoint("api.honeycomb.io"),
otlptracehttp.WithHeaders(map[string]string{"x-honeycomb-team": key}),
Expand All @@ -96,30 +96,32 @@ func InitTracerWithHoneycomb(key string, opts ...otlptracehttp.Option) error {
}

func InitTracer(opts ...otlptracehttp.Option) error {
if sentry_dsn := viper.GetString("sentry-dsn"); sentry_dsn != "" {
var environment string
if viper.GetString("run-mode") == "release" {
environment = "prod"
} else {
environment = "dev"
}
err := sentry.Init(sentry.ClientOptions{
Dsn: sentry_dsn,
AttachStacktrace: true,
EnableTracing: false,
Environment: environment,
// Set TracesSampleRate to 1.0 to capture 100%
// of transactions for performance monitoring.
// We recommend adjusting this value in production,
TracesSampleRate: 1.0,
})
if err != nil {
log.Errorf("sentry.Init: %s", err)
if viper.GetBool("otel") {
if sentry_dsn := viper.GetString("sentry-dsn"); sentry_dsn != "" {
var environment string
if viper.GetString("run-mode") == "release" {
environment = "prod"
} else {
environment = "dev"
}
err := sentry.Init(sentry.ClientOptions{
Dsn: sentry_dsn,
AttachStacktrace: true,
EnableTracing: false,
Environment: environment,
// Set TracesSampleRate to 1.0 to capture 100%
// of transactions for performance monitoring.
// We recommend adjusting this value in production,
TracesSampleRate: 1.0,
})
if err != nil {
log.Errorf("sentry.Init: %s", err)
}
// setup recovery for an unexpected panic in this function
defer sentry.Flush(2 * time.Second)
defer sentry.Recover()
log.Info("sentry configured")
}
// setup recovery for an unexpected panic in this function
defer sentry.Flush(2 * time.Second)
defer sentry.Recover()
log.Info("sentry configured")
}

client := otlptracehttp.NewClient(opts...)
Expand All @@ -129,21 +131,28 @@ func InitTracer(opts ...otlptracehttp.Option) error {
}
log.Infof("otlptracehttp client configured itself: %v", client)

tracerOpts := []sdktrace.TracerProviderOption{
sdktrace.WithBatcher(otlpExp, sdktrace.WithMaxQueueSize(50000)),
sdktrace.WithResource(tracingResource()),
sdktrace.WithSampler(sdktrace.ParentBased(NewUserAgentSampler("ELB-HealthChecker/2.0", 200))),
}
if viper.GetBool("stdout-trace-dump") {
stdoutExp, err := stdouttrace.New(stdouttrace.WithPrettyPrint())
if err != nil {
return err
tracerOpts := []sdktrace.TracerProviderOption{}

if viper.GetBool("otel") {
tracerOpts = []sdktrace.TracerProviderOption{
sdktrace.WithBatcher(otlpExp, sdktrace.WithMaxQueueSize(50000)),
sdktrace.WithResource(tracingResource()),
sdktrace.WithSampler(sdktrace.ParentBased(NewUserAgentSampler("ELB-HealthChecker/2.0", 200))),
}

if viper.GetBool("stdout-trace-dump") {
stdoutExp, err := stdouttrace.New(stdouttrace.WithPrettyPrint())
if err != nil {
return err
}
tracerOpts = append(tracerOpts, sdktrace.WithBatcher(stdoutExp))
}
tracerOpts = append(tracerOpts, sdktrace.WithBatcher(stdoutExp))
}
tp = sdktrace.NewTracerProvider(tracerOpts...)
otel.SetTracerProvider(tp)
otel.SetTextMapPropagator(propagation.NewCompositeTextMapPropagator(propagation.TraceContext{}, propagation.Baggage{}))
if viper.GetBool("otel") {
otel.SetTextMapPropagator(propagation.NewCompositeTextMapPropagator(propagation.TraceContext{}, propagation.Baggage{}))
}
return nil
}

Expand Down