github.com/blend/go-sdk@v1.20220411.3/datadog/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 datadog
     9  
    10  import (
    11  	"context"
    12  	"fmt"
    13  
    14  	"github.com/blend/go-sdk/env"
    15  )
    16  
    17  const (
    18  	// DefaultDatadogBufferDepth is the default number of statsd messages to buffer.
    19  	DefaultDatadogBufferDepth = 128
    20  )
    21  
    22  // Config is the datadog config.
    23  type Config struct {
    24  	// Address is the address of the datadog collector in the form of "hostname:port" or "unix:///path/to/socket"
    25  	// It will supercede `Hostname` and `Port`.
    26  	Address string `json:"address,omitempty" yaml:"address,omitempty" env:"DATADOG_ADDRESS"`
    27  	// TraceAddress is the address of the datadog collector in the form of "hostname:port" or "unix:///path/to/trace-socket"
    28  	// It will supercede `TraceHostname` and `TracePort`
    29  	TraceAddress string `json:"traceAddress,omitempty" yaml:"traceAddress,omitempty" env:"DATADOG_TRACE_ADDRESS"`
    30  	// ProfilerAddress is the address of the datadog collector in the form of "hostname:port" or "unix:///path/to/profiler-socket"
    31  	ProfilerAddress string `json:"profilerAddress,omitempty" yaml:"profilerAddress,omitempty" env:"DATADOG_PROFILER_ADDRESS"`
    32  
    33  	// Hostname is the host portion of a <host>:<port> address. It will be used in conjunction with `Port`
    34  	// to form the default `Address`.
    35  	Hostname string `json:"hostname,omitempty" yaml:"hostname,omitempty" env:"DATADOG_HOSTNAME"`
    36  	// Port is the port portion of a <host>:<port> address. It will be used in conjunction with `Host`
    37  	// to form the default `Address`.
    38  	Port string `json:"port,omitempty" yaml:"port,omitempty" env:"DATADOG_PORT"`
    39  
    40  	// TraceHostname is the host portion of a <host>:<port> address. It will be used in conjunction with `TracePort`
    41  	// to form the default `TraceAddress`.
    42  	TraceHostname string `json:"traceHostname,omitempty" yaml:"traceHostname,omitempty" env:"DATADOG_TRACE_HOSTNAME"`
    43  	// TracePort is the port portion of a <host>:<port> address. It will be used in conjunction with `TraceHost`
    44  	// to form the default `TraceAddress`.
    45  	TracePort string `json:"tracePort,omitempty" yaml:"tracePort,omitempty" env:"DATADOG_TRACE_PORT"`
    46  
    47  	// TracingEnabled returns if we should use tracing or not.
    48  	TracingEnabled *bool `json:"tracingEnabled,omitempty" yaml:"tracingEnabled,omitempty" env:"DATADOG_APM_ENABLED"`
    49  	// TracingSampleRate is the default tracing sample rate, on the interval [0-1]
    50  	TraceSampleRate *float64 `json:"traceSampleRate,omitempty" yaml:"traceSampleRate,omitempty" env:"DATADOG_APM_SAMPLE_RATE"`
    51  
    52  	// ProfilingEnabled returns if we should use profiling or not.
    53  	ProfilingEnabled *bool `json:"profilingEnabled,omitempty" yaml:"profilingEnabled,omitempty" env:"DATADOG_PROFILING_ENABLED"`
    54  
    55  	// Buffered indicates if we should buffer statsd metrics.
    56  	Buffered *bool `json:"buffered,omitempty" yaml:"buffered,omitempty" env:"DATADOG_BUFFERED"`
    57  	// BufferDepth is the depth of the buffer for statsd metrics.
    58  	BufferDepth int `json:"bufferDepth,omitempty" yaml:"bufferDepth,omitempty" env:"DATADOG_BUFFER_DEPTH"`
    59  
    60  	// Namespace is an optional namespace.
    61  	// The namespace is a prefix on all statsd metric names submitted to the collector.
    62  	Namespace string `json:"namespace,omitempty" yaml:"namespace,omitempty" env:"DATADOG_NAMESPACE"`
    63  	// DefaultTags are the default tags associated with any stat metric.
    64  	DefaultTags []string `json:"defaultTags,omitempty" yaml:"defaultTags,omitempty" env:"DATADOG_DEFAULT_TAGS,csv"`
    65  }
    66  
    67  // Resolve implements configutil.ConfigResolver.
    68  func (c *Config) Resolve(ctx context.Context) error {
    69  	return env.GetVars(ctx).ReadInto(c)
    70  }
    71  
    72  // IsZero returns if the config is unset.
    73  func (c Config) IsZero() bool {
    74  	return c.GetAddress() == ""
    75  }
    76  
    77  // GetAddress returns the datadog collector address string.
    78  func (c Config) GetAddress() string {
    79  	if c.Address != "" {
    80  		return c.Address
    81  	}
    82  	if c.Hostname != "" {
    83  		return fmt.Sprintf("%s:%s", c.Hostname, c.PortOrDefault())
    84  	}
    85  	return DefaultAddress
    86  }
    87  
    88  // GetTraceAddress returns the datadog collector address string.
    89  func (c Config) GetTraceAddress() string {
    90  	if c.TraceAddress != "" {
    91  		return c.TraceAddress
    92  	}
    93  	if c.TraceHostname != "" {
    94  		return fmt.Sprintf("%s:%s", c.TraceHostname, c.TracePortOrDefault())
    95  	}
    96  	if c.Hostname != "" {
    97  		return fmt.Sprintf("%s:%s", c.Hostname, c.TracePortOrDefault())
    98  	}
    99  	return ""
   100  }
   101  
   102  // GetProfilerAddress gets the profiler address.
   103  func (c Config) GetProfilerAddress() string {
   104  	if c.ProfilerAddress != "" {
   105  		return c.ProfilerAddress
   106  	}
   107  	return c.GetTraceAddress()
   108  }
   109  
   110  // PortOrDefault returns the port or a default.
   111  func (c Config) PortOrDefault() string {
   112  	if c.Port != "" {
   113  		return c.Port
   114  	}
   115  	return DefaultPort
   116  }
   117  
   118  // TracePortOrDefault returns the trace port or a default.
   119  func (c Config) TracePortOrDefault() string {
   120  	if c.TracePort != "" {
   121  		return c.TracePort
   122  	}
   123  	return DefaultTracePort
   124  }
   125  
   126  // TracingEnabledOrDefault returns if tracing is enabled.
   127  func (c Config) TracingEnabledOrDefault() bool {
   128  	if c.TracingEnabled != nil {
   129  		return *c.TracingEnabled
   130  	}
   131  	return DefaultTracingEnabled
   132  }
   133  
   134  // TraceSampleRateOrDefault returns the trace sample rate or a default.
   135  func (c Config) TraceSampleRateOrDefault() float64 {
   136  	if c.TraceSampleRate != nil {
   137  		return *c.TraceSampleRate
   138  	}
   139  	return DefaultTraceSampleRate
   140  }
   141  
   142  // ProfilingEnabledOrDefault returns if tracing is enabled.
   143  func (c Config) ProfilingEnabledOrDefault() bool {
   144  	if c.ProfilingEnabled != nil {
   145  		return *c.ProfilingEnabled
   146  	}
   147  	return DefaultProfilingEnabled
   148  }
   149  
   150  // BufferedOrDefault returns if the client should buffer messages or not.
   151  func (c Config) BufferedOrDefault() bool {
   152  	if c.Buffered != nil {
   153  		return *c.Buffered
   154  	}
   155  	return false
   156  }
   157  
   158  // BufferDepthOrDefault returns the buffer depth.
   159  func (c Config) BufferDepthOrDefault() int {
   160  	if c.BufferDepth > 0 {
   161  		return c.BufferDepth
   162  	}
   163  	return DefaultDatadogBufferDepth
   164  }