From d12be597dbcc0d1bef08350fb36236943f915029 Mon Sep 17 00:00:00 2001 From: Andreas Kohn Date: Thu, 12 May 2022 09:00:55 +0200 Subject: [PATCH] Support using a license key for ingestion Fixes: https://github.com/newrelic/newrelic-telemetry-sdk-go/issues/75 --- CHANGELOG.md | 3 +++ README.md | 4 ++-- telemetry/config.go | 16 ++++++++++++++-- telemetry/harvester.go | 22 ++++++++++++++-------- telemetry/harvester_test.go | 10 +++++----- 5 files changed, 38 insertions(+), 17 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 41a1b2a..77d1dcb 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,9 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/). ## [Unreleased] +### Added +* Allow using a license key instead of an API key ([#77](https://github.com/newrelic/newrelic-telemetry-sdk-go/pull/77)) + ## [0.8.1] - 2021-07-29 ### Added diff --git a/README.md b/README.md index a8bff49..551eaec 100644 --- a/README.md +++ b/README.md @@ -51,8 +51,8 @@ import ( ) func main() { - // First create a Harvester. APIKey is the only required field. - h, err := telemetry.NewHarvester(telemetry.ConfigAPIKey(os.Getenv("NEW_RELIC_LICENSE_KEY"))) + // First create a Harvester. Either License or APIKey must be provided. + h, err := telemetry.NewHarvester(telemetry.ConfigLicense(os.Getenv("NEW_RELIC_LICENSE_KEY"))) if err != nil { fmt.Println(err) } diff --git a/telemetry/config.go b/telemetry/config.go index 56c8ce1..999b471 100644 --- a/telemetry/config.go +++ b/telemetry/config.go @@ -13,8 +13,12 @@ import ( // Config customizes the behavior of a Harvester. type Config struct { - // APIKey is required and refers to your New Relic Insert API key. + // APIKey refers to your New Relic Insert API key. + // One of APIKey or License is required APIKey string + // License refers to your New Relic License key. + // One of APIKey or License is required + License string // Client is the http.Client used for making requests. Client *http.Client // HarvestTimeout is the total amount of time including retries that the @@ -55,7 +59,7 @@ type Config struct { ProductVersion string } -// ConfigAPIKey sets the Config's APIKey which is required and refers to your +// ConfigAPIKey sets the Config's APIKey which refers to your // New Relic Insert API key. func ConfigAPIKey(key string) func(*Config) { return func(cfg *Config) { @@ -63,6 +67,14 @@ func ConfigAPIKey(key string) func(*Config) { } } +// ConfigLicense sets the Config's License which refers to your +// New Relic License key. +func ConfigLicense(license string) func(*Config) { + return func(cfg *Config) { + cfg.License = license + } +} + // ConfigCommonAttributes adds the given attributes to the Config's // CommonAttributes. func ConfigCommonAttributes(attributes map[string]interface{}) func(*Config) { diff --git a/telemetry/harvester.go b/telemetry/harvester.go index 5e81ad9..13bf33e 100644 --- a/telemetry/harvester.go +++ b/telemetry/harvester.go @@ -51,7 +51,7 @@ const ( ) var ( - errAPIKeyUnset = errors.New("APIKey is required") + errAPIKeyUnset = errors.New("APIKey or License is required") ) // NewHarvester creates a new harvester. @@ -65,7 +65,12 @@ func NewHarvester(options ...func(*Config)) (*Harvester, error) { opt(&cfg) } - if cfg.APIKey == "" { + var apiKeyOption ClientOption + if cfg.APIKey != "" { + apiKeyOption = WithInsertKey(cfg.APIKey) + } else if cfg.License != "" { + apiKeyOption = WithLicenseKey(cfg.License) + } else { return nil, errAPIKeyUnset } @@ -97,7 +102,7 @@ func NewHarvester(options ...func(*Config)) (*Harvester, error) { userAgent := "harvester " + h.config.userAgent() h.spanRequestFactory, err = NewSpanRequestFactory( - WithInsertKey(h.config.APIKey), + apiKeyOption, withScheme(spanURL.Scheme), WithEndpoint(spanURL.Host), WithUserAgent(userAgent), @@ -112,7 +117,7 @@ func NewHarvester(options ...func(*Config)) (*Harvester, error) { } h.metricRequestFactory, err = NewMetricRequestFactory( - WithInsertKey(h.config.APIKey), + apiKeyOption, withScheme(metricURL.Scheme), WithEndpoint(metricURL.Host), WithUserAgent(userAgent), @@ -127,7 +132,7 @@ func NewHarvester(options ...func(*Config)) (*Harvester, error) { } h.eventRequestFactory, err = NewEventRequestFactory( - WithInsertKey(h.config.APIKey), + apiKeyOption, withScheme(eventURL.Scheme), WithEndpoint(eventURL.Host), WithUserAgent(userAgent), @@ -142,7 +147,7 @@ func NewHarvester(options ...func(*Config)) (*Harvester, error) { } h.logRequestFactory, err = NewLogRequestFactory( - WithInsertKey(h.config.APIKey), + apiKeyOption, withScheme(logURL.Scheme), WithEndpoint(logURL.Host), WithUserAgent(userAgent), @@ -153,7 +158,8 @@ func NewHarvester(options ...func(*Config)) (*Harvester, error) { h.config.logDebug(map[string]interface{}{ "event": "harvester created", - "api-key": sanitizeAPIKeyForLogging(h.config.APIKey), + "api-key": sanitizeKeyForLogging(h.config.APIKey), + "license-key": sanitizeKeyForLogging(h.config.License), "harvest-period-seconds": h.config.HarvestPeriod.Seconds(), "metrics-url-override": h.config.MetricsURLOverride, "spans-url-override": h.config.SpansURLOverride, @@ -169,7 +175,7 @@ func NewHarvester(options ...func(*Config)) (*Harvester, error) { return h, nil } -func sanitizeAPIKeyForLogging(apiKey string) string { +func sanitizeKeyForLogging(apiKey string) string { if len(apiKey) <= 8 { return apiKey } diff --git a/telemetry/harvester_test.go b/telemetry/harvester_test.go index 6a599ff..9cba12c 100644 --- a/telemetry/harvester_test.go +++ b/telemetry/harvester_test.go @@ -135,11 +135,11 @@ func TestSanitizeApiKeyForLogging(t *testing.T) { t.Errorf("Got %s but expected %s", actual, expected) } } - assertEqual("", sanitizeAPIKeyForLogging("")) - assertEqual("", sanitizeAPIKeyForLogging("")) - assertEqual("foo", sanitizeAPIKeyForLogging("foo")) - assertEqual("foobarba", sanitizeAPIKeyForLogging("foobarbazqux")) - assertEqual("eu01xxfoobarba", sanitizeAPIKeyForLogging("eu01xxfoobarbazqux")) + assertEqual("", sanitizeKeyForLogging("")) + assertEqual("", sanitizeKeyForLogging("")) + assertEqual("foo", sanitizeKeyForLogging("foo")) + assertEqual("foobarba", sanitizeKeyForLogging("foobarbazqux")) + assertEqual("eu01xxfoobarba", sanitizeKeyForLogging("eu01xxfoobarbazqux")) } func TestHarvesterRecordSpan(t *testing.T) {