github.com/blend/go-sdk@v1.20240719.1/datadog/config.go (about)

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