github.com/lulzWill/go-agent@v2.1.2+incompatible/config.go (about)

     1  package newrelic
     2  
     3  import (
     4  	"errors"
     5  	"fmt"
     6  	"net/http"
     7  	"strings"
     8  	"time"
     9  )
    10  
    11  // Config contains Application and Transaction behavior settings.
    12  // Use NewConfig to create a Config with proper defaults.
    13  type Config struct {
    14  	// AppName is used by New Relic to link data across servers.
    15  	//
    16  	// https://docs.newrelic.com/docs/apm/new-relic-apm/installation-configuration/naming-your-application
    17  	AppName string
    18  
    19  	// License is your New Relic license key.
    20  	//
    21  	// https://docs.newrelic.com/docs/accounts-partnerships/accounts/account-setup/license-key
    22  	License string
    23  
    24  	// Logger controls go-agent logging.  See log.go.
    25  	Logger Logger
    26  
    27  	// Enabled determines whether the agent will communicate with the New
    28  	// Relic servers and spawn goroutines.  Setting this to be false can be
    29  	// useful in testing and staging situations.
    30  	Enabled bool
    31  
    32  	// Labels are key value pairs used to roll up applications into specific
    33  	// categories.
    34  	//
    35  	// https://docs.newrelic.com/docs/apm/new-relic-apm/maintenance/labels-categories-organizing-your-apps-servers
    36  	Labels map[string]string
    37  
    38  	// HighSecurity guarantees that certain agent settings can not be made
    39  	// more permissive.  This setting must match the corresponding account
    40  	// setting in the New Relic UI.
    41  	//
    42  	// https://docs.newrelic.com/docs/accounts-partnerships/accounts/security/high-security
    43  	HighSecurity bool
    44  
    45  	// SecurityPoliciesToken enables security policies if set to a non-empty
    46  	// string.  Only set this if security policies have been enabled on your
    47  	// account.  This cannot be used in conjuction with HighSecurity.
    48  	SecurityPoliciesToken string
    49  
    50  	// CustomInsightsEvents controls the behavior of
    51  	// Application.RecordCustomEvent.
    52  	//
    53  	// https://docs.newrelic.com/docs/insights/new-relic-insights/adding-querying-data/inserting-custom-events-new-relic-apm-agents
    54  	CustomInsightsEvents struct {
    55  		// Enabled controls whether RecordCustomEvent will collect
    56  		// custom analytics events.  High security mode overrides this
    57  		// setting.
    58  		Enabled bool
    59  	}
    60  
    61  	// TransactionEvents controls the behavior of transaction analytics
    62  	// events.
    63  	TransactionEvents struct {
    64  		// Enabled controls whether transaction events are captured.
    65  		Enabled bool
    66  		// Attributes controls the attributes included with transaction
    67  		// events.
    68  		Attributes AttributeDestinationConfig
    69  	}
    70  
    71  	// ErrorCollector controls the capture of errors.
    72  	ErrorCollector struct {
    73  		// Enabled controls whether errors are captured.  This setting
    74  		// affects both traced errors and error analytics events.
    75  		Enabled bool
    76  		// CaptureEvents controls whether error analytics events are
    77  		// captured.
    78  		CaptureEvents bool
    79  		// IgnoreStatusCodes controls which http response codes are
    80  		// automatically turned into errors.  By default, response codes
    81  		// greater than or equal to 400, with the exception of 404, are
    82  		// turned into errors.
    83  		IgnoreStatusCodes []int
    84  		// Attributes controls the attributes included with errors.
    85  		Attributes AttributeDestinationConfig
    86  	}
    87  
    88  	// TransactionTracer controls the capture of transaction traces.
    89  	TransactionTracer struct {
    90  		// Enabled controls whether transaction traces are captured.
    91  		Enabled bool
    92  		// Threshold controls whether a transaction trace will be
    93  		// considered for capture.  Of the traces exceeding the
    94  		// threshold, the slowest trace every minute is captured.
    95  		Threshold struct {
    96  			// If IsApdexFailing is true then the trace threshold is
    97  			// four times the apdex threshold.
    98  			IsApdexFailing bool
    99  			// If IsApdexFailing is false then this field is the
   100  			// threshold, otherwise it is ignored.
   101  			Duration time.Duration
   102  		}
   103  		// SegmentThreshold is the threshold at which segments will be
   104  		// added to the trace.  Lowering this setting may increase
   105  		// overhead.
   106  		SegmentThreshold time.Duration
   107  		// StackTraceThreshold is the threshold at which segments will
   108  		// be given a stack trace in the transaction trace.  Lowering
   109  		// this setting will drastically increase overhead.
   110  		StackTraceThreshold time.Duration
   111  		// Attributes controls the attributes included with transaction
   112  		// traces.
   113  		Attributes AttributeDestinationConfig
   114  	}
   115  
   116  	// HostDisplayName gives this server a recognizable name in the New
   117  	// Relic UI.  This is an optional setting.
   118  	HostDisplayName string
   119  
   120  	// Transport customizes http.Client communication with New Relic
   121  	// servers.  This may be used to configure a proxy.
   122  	Transport http.RoundTripper
   123  
   124  	// Utilization controls the detection and gathering of system
   125  	// information.
   126  	Utilization struct {
   127  		// DetectAWS controls whether the Application attempts to detect
   128  		// AWS.
   129  		DetectAWS bool
   130  		// DetectAzure controls whether the Application attempts to detect
   131  		// Azure.
   132  		DetectAzure bool
   133  		// DetectPCF controls whether the Application attempts to detect
   134  		// PCF.
   135  		DetectPCF bool
   136  		// DetectGCP controls whether the Application attempts to detect
   137  		// GCP.
   138  		DetectGCP bool
   139  		// DetectDocker controls whether the Application attempts to
   140  		// detect Docker.
   141  		DetectDocker bool
   142  
   143  		// These settings provide system information when custom values
   144  		// are required.
   145  		LogicalProcessors int
   146  		TotalRAMMIB       int
   147  		BillingHostname   string
   148  	}
   149  
   150  	// CrossApplicationTracer controls behaviour relating to cross application
   151  	// tracing (CAT).
   152  	CrossApplicationTracer struct {
   153  		Enabled bool
   154  	}
   155  
   156  	// DatastoreTracer controls behavior relating to datastore segments.
   157  	DatastoreTracer struct {
   158  		InstanceReporting struct {
   159  			Enabled bool
   160  		}
   161  		DatabaseNameReporting struct {
   162  			Enabled bool
   163  		}
   164  		QueryParameters struct {
   165  			Enabled bool
   166  		}
   167  		// SlowQuery controls the capture of slow query traces.  Slow
   168  		// query traces show you instances of your slowest datastore
   169  		// segments.
   170  		SlowQuery struct {
   171  			Enabled   bool
   172  			Threshold time.Duration
   173  		}
   174  	}
   175  
   176  	// Attributes controls the attributes included with errors and
   177  	// transaction events.
   178  	Attributes AttributeDestinationConfig
   179  
   180  	// RuntimeSampler controls the collection of runtime statistics like
   181  	// CPU/Memory usage, goroutine count, and GC pauses.
   182  	RuntimeSampler struct {
   183  		// Enabled controls whether runtime statistics are captured.
   184  		Enabled bool
   185  	}
   186  }
   187  
   188  // AttributeDestinationConfig controls the attributes included with errors and
   189  // transaction events.
   190  type AttributeDestinationConfig struct {
   191  	Enabled bool
   192  	Include []string
   193  	Exclude []string
   194  }
   195  
   196  // NewConfig creates an Config populated with the given appname, license,
   197  // and expected default values.
   198  func NewConfig(appname, license string) Config {
   199  	c := Config{}
   200  
   201  	c.AppName = appname
   202  	c.License = license
   203  	c.Enabled = true
   204  	c.Labels = make(map[string]string)
   205  	c.CustomInsightsEvents.Enabled = true
   206  	c.TransactionEvents.Enabled = true
   207  	c.TransactionEvents.Attributes.Enabled = true
   208  	c.HighSecurity = false
   209  	c.ErrorCollector.Enabled = true
   210  	c.ErrorCollector.CaptureEvents = true
   211  	c.ErrorCollector.IgnoreStatusCodes = []int{
   212  		http.StatusNotFound, // 404
   213  	}
   214  	c.ErrorCollector.Attributes.Enabled = true
   215  	c.Utilization.DetectAWS = true
   216  	c.Utilization.DetectAzure = true
   217  	c.Utilization.DetectPCF = true
   218  	c.Utilization.DetectGCP = true
   219  	c.Utilization.DetectDocker = true
   220  	c.Attributes.Enabled = true
   221  	c.RuntimeSampler.Enabled = true
   222  
   223  	c.TransactionTracer.Enabled = true
   224  	c.TransactionTracer.Threshold.IsApdexFailing = true
   225  	c.TransactionTracer.Threshold.Duration = 500 * time.Millisecond
   226  	c.TransactionTracer.SegmentThreshold = 2 * time.Millisecond
   227  	c.TransactionTracer.StackTraceThreshold = 500 * time.Millisecond
   228  	c.TransactionTracer.Attributes.Enabled = true
   229  
   230  	c.CrossApplicationTracer.Enabled = true
   231  
   232  	c.DatastoreTracer.InstanceReporting.Enabled = true
   233  	c.DatastoreTracer.DatabaseNameReporting.Enabled = true
   234  	c.DatastoreTracer.QueryParameters.Enabled = true
   235  	c.DatastoreTracer.SlowQuery.Enabled = true
   236  	c.DatastoreTracer.SlowQuery.Threshold = 10 * time.Millisecond
   237  
   238  	return c
   239  }
   240  
   241  const (
   242  	licenseLength = 40
   243  	appNameLimit  = 3
   244  )
   245  
   246  // The following errors will be returned if your Config fails to validate.
   247  var (
   248  	errLicenseLen                       = fmt.Errorf("license length is not %d", licenseLength)
   249  	errAppNameMissing                   = errors.New("string AppName required")
   250  	errAppNameLimit                     = fmt.Errorf("max of %d rollup application names", appNameLimit)
   251  	errHighSecurityWithSecurityPolicies = errors.New("SecurityPoliciesToken and HighSecurity are incompatible; please ensure HighSecurity is set to false if SecurityPoliciesToken is a non-empty string and a security policy has been set for your account")
   252  )
   253  
   254  // Validate checks the config for improper fields.  If the config is invalid,
   255  // newrelic.NewApplication returns an error.
   256  func (c Config) Validate() error {
   257  	if c.Enabled {
   258  		if len(c.License) != licenseLength {
   259  			return errLicenseLen
   260  		}
   261  	} else {
   262  		// The License may be empty when the agent is not enabled.
   263  		if len(c.License) != licenseLength && len(c.License) != 0 {
   264  			return errLicenseLen
   265  		}
   266  	}
   267  	if "" == c.AppName && c.Enabled {
   268  		return errAppNameMissing
   269  	}
   270  	if c.HighSecurity && "" != c.SecurityPoliciesToken {
   271  		return errHighSecurityWithSecurityPolicies
   272  	}
   273  	if strings.Count(c.AppName, ";") >= appNameLimit {
   274  		return errAppNameLimit
   275  	}
   276  	return nil
   277  }