1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416
|
// Copyright 2020 New Relic Corporation. All rights reserved.
// SPDX-License-Identifier: Apache-2.0
package newrelic
import (
"errors"
"fmt"
"net/http"
"strings"
"time"
"github.com/newrelic/go-agent/internal"
)
// Config contains Application and Transaction behavior settings.
// Use NewConfig to create a Config with proper defaults.
type Config struct {
// AppName is used by New Relic to link data across servers.
//
// https://docs.newrelic.com/docs/apm/new-relic-apm/installation-configuration/naming-your-application
AppName string
// License is your New Relic license key.
//
// https://docs.newrelic.com/docs/accounts/install-new-relic/account-setup/license-key
License string
// Logger controls go-agent logging. For info level logging to stdout:
//
// cfg.Logger = newrelic.NewLogger(os.Stdout)
//
// For debug level logging to stdout:
//
// cfg.Logger = newrelic.NewDebugLogger(os.Stdout)
//
// See https://github.com/newrelic/go-agent/blob/master/GUIDE.md#logging
// for more examples and logging integrations.
Logger Logger
// Enabled controls whether the agent will communicate with the New Relic
// servers and spawn goroutines. Setting this to be false is useful in
// testing and staging situations.
Enabled bool
// Labels are key value pairs used to roll up applications into specific
// categories.
//
// https://docs.newrelic.com/docs/using-new-relic/user-interface-functions/organize-your-data/labels-categories-organize-apps-monitors
Labels map[string]string
// HighSecurity guarantees that certain agent settings can not be made
// more permissive. This setting must match the corresponding account
// setting in the New Relic UI.
//
// https://docs.newrelic.com/docs/agents/manage-apm-agents/configuration/high-security-mode
HighSecurity bool
// SecurityPoliciesToken enables security policies if set to a non-empty
// string. Only set this if security policies have been enabled on your
// account. This cannot be used in conjunction with HighSecurity.
//
// https://docs.newrelic.com/docs/agents/manage-apm-agents/configuration/enable-configurable-security-policies
SecurityPoliciesToken string
// CustomInsightsEvents controls the behavior of
// Application.RecordCustomEvent.
//
// https://docs.newrelic.com/docs/insights/new-relic-insights/adding-querying-data/inserting-custom-events-new-relic-apm-agents
CustomInsightsEvents struct {
// Enabled controls whether RecordCustomEvent will collect
// custom analytics events. High security mode overrides this
// setting.
Enabled bool
}
// TransactionEvents controls the behavior of transaction analytics
// events.
TransactionEvents struct {
// Enabled controls whether transaction events are captured.
Enabled bool
// Attributes controls the attributes included with transaction
// events.
Attributes AttributeDestinationConfig
// MaxSamplesStored allows you to limit the number of Transaction
// Events stored/reported in a given 60-second period
MaxSamplesStored int
}
// ErrorCollector controls the capture of errors.
ErrorCollector struct {
// Enabled controls whether errors are captured. This setting
// affects both traced errors and error analytics events.
Enabled bool
// CaptureEvents controls whether error analytics events are
// captured.
CaptureEvents bool
// IgnoreStatusCodes controls which http response codes are
// automatically turned into errors. By default, response codes
// greater than or equal to 400, with the exception of 404, are
// turned into errors.
IgnoreStatusCodes []int
// Attributes controls the attributes included with errors.
Attributes AttributeDestinationConfig
}
// TransactionTracer controls the capture of transaction traces.
TransactionTracer struct {
// Enabled controls whether transaction traces are captured.
Enabled bool
// Threshold controls whether a transaction trace will be
// considered for capture. Of the traces exceeding the
// threshold, the slowest trace every minute is captured.
Threshold struct {
// If IsApdexFailing is true then the trace threshold is
// four times the apdex threshold.
IsApdexFailing bool
// If IsApdexFailing is false then this field is the
// threshold, otherwise it is ignored.
Duration time.Duration
}
// SegmentThreshold is the threshold at which segments will be
// added to the trace. Lowering this setting may increase
// overhead. Decrease this duration if your Transaction Traces are
// missing segments.
SegmentThreshold time.Duration
// StackTraceThreshold is the threshold at which segments will
// be given a stack trace in the transaction trace. Lowering
// this setting will increase overhead.
StackTraceThreshold time.Duration
// Attributes controls the attributes included with transaction
// traces.
Attributes AttributeDestinationConfig
// Segments.Attributes controls the attributes included with
// each trace segment.
Segments struct {
Attributes AttributeDestinationConfig
}
}
// BrowserMonitoring contains settings which control the behavior of
// Transaction.BrowserTimingHeader.
BrowserMonitoring struct {
// Enabled controls whether or not the Browser monitoring feature is
// enabled.
Enabled bool
// Attributes controls the attributes included with Browser monitoring.
// BrowserMonitoring.Attributes.Enabled is false by default, to include
// attributes in the Browser timing Javascript:
//
// cfg.BrowserMonitoring.Attributes.Enabled = true
Attributes AttributeDestinationConfig
}
// HostDisplayName gives this server a recognizable name in the New
// Relic UI. This is an optional setting.
HostDisplayName string
// Transport customizes communication with the New Relic servers. This may
// be used to configure a proxy.
Transport http.RoundTripper
// Utilization controls the detection and gathering of system
// information.
Utilization struct {
// DetectAWS controls whether the Application attempts to detect
// AWS.
DetectAWS bool
// DetectAzure controls whether the Application attempts to detect
// Azure.
DetectAzure bool
// DetectPCF controls whether the Application attempts to detect
// PCF.
DetectPCF bool
// DetectGCP controls whether the Application attempts to detect
// GCP.
DetectGCP bool
// DetectDocker controls whether the Application attempts to
// detect Docker.
DetectDocker bool
// DetectKubernetes controls whether the Application attempts to
// detect Kubernetes.
DetectKubernetes bool
// These settings provide system information when custom values
// are required.
LogicalProcessors int
TotalRAMMIB int
BillingHostname string
}
// CrossApplicationTracer controls behaviour relating to cross application
// tracing (CAT), available since Go Agent v0.11. The
// CrossApplicationTracer and the DistributedTracer cannot be
// simultaneously enabled.
//
// https://docs.newrelic.com/docs/apm/transactions/cross-application-traces/introduction-cross-application-traces
CrossApplicationTracer struct {
Enabled bool
}
// DistributedTracer controls behaviour relating to Distributed Tracing,
// available since Go Agent v2.1. The DistributedTracer and the
// CrossApplicationTracer cannot be simultaneously enabled.
//
// https://docs.newrelic.com/docs/apm/distributed-tracing/getting-started/introduction-distributed-tracing
DistributedTracer struct {
Enabled bool
}
// SpanEvents controls behavior relating to Span Events. Span Events
// require that DistributedTracer is enabled.
SpanEvents struct {
Enabled bool
Attributes AttributeDestinationConfig
}
// DatastoreTracer controls behavior relating to datastore segments.
DatastoreTracer struct {
// InstanceReporting controls whether the host and port are collected
// for datastore segments.
InstanceReporting struct {
Enabled bool
}
// DatabaseNameReporting controls whether the database name is
// collected for datastore segments.
DatabaseNameReporting struct {
Enabled bool
}
QueryParameters struct {
Enabled bool
}
// SlowQuery controls the capture of slow query traces. Slow
// query traces show you instances of your slowest datastore
// segments.
SlowQuery struct {
Enabled bool
Threshold time.Duration
}
}
// Attributes controls which attributes are enabled and disabled globally.
// This setting affects all attribute destinations: Transaction Events,
// Error Events, Transaction Traces and segments, Traced Errors, Span
// Events, and Browser timing header.
Attributes AttributeDestinationConfig
// RuntimeSampler controls the collection of runtime statistics like
// CPU/Memory usage, goroutine count, and GC pauses.
RuntimeSampler struct {
// Enabled controls whether runtime statistics are captured.
Enabled bool
}
// ServerlessMode contains fields which control behavior when running in
// AWS Lambda.
//
// https://docs.newrelic.com/docs/serverless-function-monitoring/aws-lambda-monitoring/get-started/introduction-new-relic-monitoring-aws-lambda
ServerlessMode struct {
// Enabling ServerlessMode will print each transaction's data to
// stdout. No agent goroutines will be spawned in serverless mode, and
// no data will be sent directly to the New Relic backend.
// nrlambda.NewConfig sets Enabled to true.
Enabled bool
// ApdexThreshold sets the Apdex threshold when in ServerlessMode. The
// default is 500 milliseconds. nrlambda.NewConfig populates this
// field using the NEW_RELIC_APDEX_T environment variable.
//
// https://docs.newrelic.com/docs/apm/new-relic-apm/apdex/apdex-measure-user-satisfaction
ApdexThreshold time.Duration
// AccountID, TrustedAccountKey, and PrimaryAppID are used for
// distributed tracing in ServerlessMode. AccountID and
// TrustedAccountKey must be populated for distributed tracing to be
// enabled. nrlambda.NewConfig populates these fields using the
// NEW_RELIC_ACCOUNT_ID, NEW_RELIC_TRUSTED_ACCOUNT_KEY, and
// NEW_RELIC_PRIMARY_APPLICATION_ID environment variables.
AccountID string
TrustedAccountKey string
PrimaryAppID string
}
}
// AttributeDestinationConfig controls the attributes sent to each destination.
// For more information, see:
// https://docs.newrelic.com/docs/agents/manage-apm-agents/agent-data/agent-attributes
type AttributeDestinationConfig struct {
// Enabled controls whether or not this destination will get any
// attributes at all. For example, to prevent any attributes from being
// added to errors, set:
//
// cfg.ErrorCollector.Attributes.Enabled = false
//
Enabled bool
Include []string
// Exclude allows you to prevent the capture of certain attributes. For
// example, to prevent the capture of the request URL attribute
// "request.uri", set:
//
// cfg.Attributes.Exclude = append(cfg.Attributes.Exclude, newrelic.AttributeRequestURI)
//
// The '*' character acts as a wildcard. For example, to prevent the
// capture of all request related attributes, set:
//
// cfg.Attributes.Exclude = append(cfg.Attributes.Exclude, "request.*")
//
Exclude []string
}
// NewConfig creates a Config populated with default settings and the given
// appname and license.
func NewConfig(appname, license string) Config {
c := Config{}
c.AppName = appname
c.License = license
c.Enabled = true
c.Labels = make(map[string]string)
c.CustomInsightsEvents.Enabled = true
c.TransactionEvents.Enabled = true
c.TransactionEvents.Attributes.Enabled = true
c.TransactionEvents.MaxSamplesStored = internal.MaxTxnEvents
c.HighSecurity = false
c.ErrorCollector.Enabled = true
c.ErrorCollector.CaptureEvents = true
c.ErrorCollector.IgnoreStatusCodes = []int{
// https://github.com/grpc/grpc/blob/master/doc/statuscodes.md
0, // gRPC OK
5, // gRPC NOT_FOUND
http.StatusNotFound, // 404
}
c.ErrorCollector.Attributes.Enabled = true
c.Utilization.DetectAWS = true
c.Utilization.DetectAzure = true
c.Utilization.DetectPCF = true
c.Utilization.DetectGCP = true
c.Utilization.DetectDocker = true
c.Utilization.DetectKubernetes = true
c.Attributes.Enabled = true
c.RuntimeSampler.Enabled = true
c.TransactionTracer.Enabled = true
c.TransactionTracer.Threshold.IsApdexFailing = true
c.TransactionTracer.Threshold.Duration = 500 * time.Millisecond
c.TransactionTracer.SegmentThreshold = 2 * time.Millisecond
c.TransactionTracer.StackTraceThreshold = 500 * time.Millisecond
c.TransactionTracer.Attributes.Enabled = true
c.TransactionTracer.Segments.Attributes.Enabled = true
c.BrowserMonitoring.Enabled = true
// browser monitoring attributes are disabled by default
c.BrowserMonitoring.Attributes.Enabled = false
c.CrossApplicationTracer.Enabled = true
c.DistributedTracer.Enabled = false
c.SpanEvents.Enabled = true
c.SpanEvents.Attributes.Enabled = true
c.DatastoreTracer.InstanceReporting.Enabled = true
c.DatastoreTracer.DatabaseNameReporting.Enabled = true
c.DatastoreTracer.QueryParameters.Enabled = true
c.DatastoreTracer.SlowQuery.Enabled = true
c.DatastoreTracer.SlowQuery.Threshold = 10 * time.Millisecond
c.ServerlessMode.ApdexThreshold = 500 * time.Millisecond
c.ServerlessMode.Enabled = false
return c
}
const (
licenseLength = 40
appNameLimit = 3
)
// The following errors will be returned if your Config fails to validate.
var (
errLicenseLen = fmt.Errorf("license length is not %d", licenseLength)
errAppNameMissing = errors.New("string AppName required")
errAppNameLimit = fmt.Errorf("max of %d rollup application names", appNameLimit)
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")
)
// Validate checks the config for improper fields. If the config is invalid,
// newrelic.NewApplication returns an error.
func (c Config) Validate() error {
if c.Enabled && !c.ServerlessMode.Enabled {
if len(c.License) != licenseLength {
return errLicenseLen
}
} else {
// The License may be empty when the agent is not enabled.
if len(c.License) != licenseLength && len(c.License) != 0 {
return errLicenseLen
}
}
if "" == c.AppName && c.Enabled && !c.ServerlessMode.Enabled {
return errAppNameMissing
}
if c.HighSecurity && "" != c.SecurityPoliciesToken {
return errHighSecurityWithSecurityPolicies
}
if strings.Count(c.AppName, ";") >= appNameLimit {
return errAppNameLimit
}
return nil
}
// MaxTxnEvents returns the configured maximum number of Transaction Events if it has been configured
// and is less than the default maximum; otherwise it returns the default max.
func (c Config) MaxTxnEvents() int {
configured := c.TransactionEvents.MaxSamplesStored
if configured < 0 || configured > internal.MaxTxnEvents {
return internal.MaxTxnEvents
}
return configured
}
|