github.com/blend/go-sdk@v1.20220411.3/sentry/config.go (about)

     1  /*
     2  
     3  Copyright (c) 2022 - Present. Blend Labs, Inc. All rights reserved
     4  Use of this source code is governed by a MIT license that can be found in the LICENSE file.
     5  
     6  */
     7  
     8  package sentry
     9  
    10  import (
    11  	"context"
    12  	"fmt"
    13  	"net/url"
    14  	"time"
    15  
    16  	"github.com/blend/go-sdk/configutil"
    17  	"github.com/blend/go-sdk/env"
    18  )
    19  
    20  // Config is the sentry config.
    21  type Config struct {
    22  	// The DSN to use. If the DSN is not set, the client is effectively disabled.
    23  	DSN string `json:"dsn" yaml:"dsn"`
    24  	// The server name to be reported.
    25  	ServerName string `json:"serverName" yaml:"serverName"`
    26  	// The dist to be sent with events.
    27  	Dist string `json:"dist" yaml:"dist"`
    28  	// The release to be sent with events.
    29  	Release string `json:"release" yaml:"release"`
    30  	// The environment to be sent with events.
    31  	Environment string `json:"environment" yaml:"environment"`
    32  	// Maximum number of breadcrumbs.
    33  	MaxBreadcrumbs int `json:"maxBreadCrumbs" yaml:"maxBreadCrumbs"`
    34  	// Debug prints debugging information to the screen.
    35  	Debug bool `json:"debug" yaml:"debug"`
    36  	// FlushTimeout is the timeout for flushing exceptions to sentry.
    37  	FlushTimeout time.Duration `json:"flushTimeout" yaml:"flushTimeout"`
    38  }
    39  
    40  // IsZero returns if the config is unset.
    41  func (c Config) IsZero() bool {
    42  	return c.DSN == ""
    43  }
    44  
    45  // Resolve applies configutil resoltion steps.
    46  func (c *Config) Resolve(ctx context.Context) error {
    47  	return configutil.Resolve(ctx,
    48  		configutil.SetString(&c.DSN, configutil.String(c.DSN), configutil.Env("SENTRY_DSN")),
    49  		configutil.SetString(&c.ServerName, configutil.String(c.ServerName), configutil.Env(env.VarServiceName)),
    50  		configutil.SetString(&c.Environment, configutil.String(c.Environment), configutil.Env(env.VarServiceEnv)),
    51  		configutil.SetDuration(&c.FlushTimeout, configutil.Duration(c.FlushTimeout), configutil.Duration(5*time.Second)),
    52  	)
    53  }
    54  
    55  // GetDSNHost returns just the scheme and hostname for the dsn.
    56  func (c *Config) GetDSNHost() string {
    57  	if c.DSN == "" {
    58  		return ""
    59  	}
    60  
    61  	parsedURL, _ := url.Parse(c.DSN)
    62  	if parsedURL == nil {
    63  		return ""
    64  	}
    65  	return fmt.Sprintf("%s://%s", parsedURL.Scheme, parsedURL.Host)
    66  }
    67  
    68  // FlushTimeoutOrDefault returns the flush timeout or a default.
    69  func (c Config) FlushTimeoutOrDefault() time.Duration {
    70  	if c.FlushTimeout > 0 {
    71  		return c.FlushTimeout
    72  	}
    73  	return 5 * time.Second
    74  }