config.go 9.8 KB

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