From 556e8980832b8dd1c4142171865c4d604bbc47f6 Mon Sep 17 00:00:00 2001 From: Ruben Ruiz de Gauna Date: Thu, 24 Feb 2022 12:07:36 +0100 Subject: [PATCH] Use the agent license for self instrumentation. Added APM collector url logic --- .../agent_instrumentation_apm.go | 5 ++- .../agent_instrumentation_factory.go | 8 ++--- pkg/config/config.go | 33 ++++++++++++++----- pkg/config/defaults.go | 32 ++++++++++-------- 4 files changed, 51 insertions(+), 27 deletions(-) diff --git a/internal/agent/instrumentation/agent_instrumentation_apm.go b/internal/agent/instrumentation/agent_instrumentation_apm.go index 8b2ba184f..31d7d7c11 100644 --- a/internal/agent/instrumentation/agent_instrumentation_apm.go +++ b/internal/agent/instrumentation/agent_instrumentation_apm.go @@ -10,7 +10,10 @@ import ( const transactionInContextKey = iota -const appName = "New Relic Infrastructure Agent" +const ( + appName = "New Relic Infrastructure Agent" + apmInstrumentationName = "newrelic" +) type agentInstrumentationApm struct { nrApp *newrelic.Application diff --git a/internal/agent/instrumentation/agent_instrumentation_factory.go b/internal/agent/instrumentation/agent_instrumentation_factory.go index 275a34d5b..5fe165f09 100644 --- a/internal/agent/instrumentation/agent_instrumentation_factory.go +++ b/internal/agent/instrumentation/agent_instrumentation_factory.go @@ -7,11 +7,11 @@ import ( ) func InitSelfInstrumentation(c *config.Config, resolver hostname.Resolver) { - if strings.ToLower(c.SelfInstrumentation) == "apm" && c.SelfInstrumentationLicenseKey != "" { + if strings.ToLower(c.SelfInstrumentation) == apmInstrumentationName { apmSelfInstrumentation, err := NewAgentInstrumentationApm( - c.SelfInstrumentationLicenseKey, - c.SelfInstrumentationApmEndpoint, - c.SelfInstrumentationTelemetryEndpoint, + c.License, + c.SelfInstrumentationApmHost, + c.MetricURL, resolver, ) if err == nil { diff --git a/pkg/config/config.go b/pkg/config/config.go index d9021f813..894ff8556 100644 --- a/pkg/config/config.go +++ b/pkg/config/config.go @@ -1065,15 +1065,10 @@ type Config struct { // Public: No SelfInstrumentation string `yaml:"self_instrumentation" envconfig:"self_instrumentation"` - // SelfInstrumentationLicenseKey license key for apm self instrumentation - // Default: empty + // SelfInstrumentationApmHost defines the url for APM collector host + // Default: collector.newrelic.com // Public: No - SelfInstrumentationLicenseKey string `yaml:"self_instrumentation_license_key" envconfig:"self_instrumentation_license_key"` - - // SelfInstrumentationApmEndpoint OpenTelemetry endpoint for self instrumentation - // Default: empty - // Public: No - SelfInstrumentationApmEndpoint string `yaml:"self_instrumentation_apm_endpoint" envconfig:"self_instrumentation_apm_endpoint"` + SelfInstrumentationApmHost string `yaml:"self_instrumentation_apm_host" envconfig:"self_instrumentation_apm_host"` // SelfInstrumentationTelemetryEndpoint OpenTelemetry endpoint for self instrumentation // Default: empty @@ -1548,6 +1543,24 @@ func calculateCmdChannelStagingURL(licenseKey string) string { return defaultCmdChannelStagingURL } +func calculateSelfInstrumentationApmHost(licenseKey string, staging bool, fedramp bool) string { + if staging { + return defaultAPMCollectorHostStaging + } + if fedramp { + return defaultSecureFederalAPMCollectorHost + } + return calculateSelfInstrumentationApmProductionHost(licenseKey) +} + +func calculateSelfInstrumentationApmProductionHost(licenseKey string) string { + // only EU supported + if license.IsRegionEU(licenseKey) { + return defaultAPMCollectorHostEu + } + return defaultAPMCollectorHost +} + func calculateDimensionalMetricURL(collectorURL string, licenseKey string, staging, fedramp bool) string { if collectorURL != "" { return collectorURL @@ -1880,6 +1893,10 @@ func NormalizeConfig(cfg *Config, cfgMetadata config_loader.YAMLMetadata) (err e cfg.MemProfileInterval = defaultMemProfileInterval } + if cfg.SelfInstrumentationApmHost == "" { + cfg.SelfInstrumentationApmHost = calculateSelfInstrumentationApmHost(cfg.License, cfg.Staging, cfg.Fedramp) + } + return } diff --git a/pkg/config/defaults.go b/pkg/config/defaults.go index ba4a7b7ec..ac48be8d4 100644 --- a/pkg/config/defaults.go +++ b/pkg/config/defaults.go @@ -15,20 +15,24 @@ const ( LogFormatJSON = "json" // Non configurable stuff - defaultIdentityURLEu = "https://identity-api.eu.newrelic.com" - defaultIdentityStagingURLEu = "https://staging-identity-api.eu.newrelic.com" - defaultCmdChannelURLEu = "https://infrastructure-command-api.eu.newrelic.com" - defaultCmdChannelStagingURLEu = "https://staging-infrastructure-command-api.eu.newrelic.com" - defaultCmdChannelURL = "https://infrastructure-command-api.newrelic.com" - defaultCmdChannelStagingURL = "https://staging-infrastructure-command-api.newrelic.com" - defaultIdentityURL = "https://identity-api.newrelic.com" - defaultIdentityStagingURL = "https://staging-identity-api.newrelic.com" - baseCollectorURL = "https://%sinfra-api.%snewrelic.com" - baseDimensionalMetricURL = "https://%smetric-api.%snewrelic.com" - defaultSecureFederalURL = "https://gov-infra-api.newrelic.com" - defaultSecureFederalMetricURL = "https://gov-metric-api.newrelic.com" - defaultSecureFedralIdentityURL = "https://gov-identity-api.newrelic.com" - defaultSecureFedralCmdChannelURL = "https://gov-infrastructure-command-api.newrelic.com" + defaultIdentityURLEu = "https://identity-api.eu.newrelic.com" + defaultIdentityStagingURLEu = "https://staging-identity-api.eu.newrelic.com" + defaultCmdChannelURLEu = "https://infrastructure-command-api.eu.newrelic.com" + defaultCmdChannelStagingURLEu = "https://staging-infrastructure-command-api.eu.newrelic.com" + defaultCmdChannelURL = "https://infrastructure-command-api.newrelic.com" + defaultCmdChannelStagingURL = "https://staging-infrastructure-command-api.newrelic.com" + defaultIdentityURL = "https://identity-api.newrelic.com" + defaultIdentityStagingURL = "https://staging-identity-api.newrelic.com" + baseCollectorURL = "https://%sinfra-api.%snewrelic.com" + baseDimensionalMetricURL = "https://%smetric-api.%snewrelic.com" + defaultSecureFederalURL = "https://gov-infra-api.newrelic.com" + defaultSecureFederalMetricURL = "https://gov-metric-api.newrelic.com" + defaultSecureFedralIdentityURL = "https://gov-identity-api.newrelic.com" + defaultSecureFedralCmdChannelURL = "https://gov-infrastructure-command-api.newrelic.com" + defaultAPMCollectorHost = "collector.newrelic.com" + defaultAPMCollectorHostEu = "collector.eu.newrelic.com" + defaultSecureFederalAPMCollectorHost = "gov-collector.newrelic.com" + defaultAPMCollectorHostStaging = "staging-collector.newrelic.com" ) // Default configurable values