Skip to content

Commit

Permalink
Support using a license key for ingestion
Browse files Browse the repository at this point in the history
  • Loading branch information
ankon committed May 15, 2022
1 parent 837f7f5 commit d12be59
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 17 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
16 changes: 14 additions & 2 deletions telemetry/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -55,14 +59,22 @@ 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) {
cfg.APIKey = key
}
}

// 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) {
Expand Down
22 changes: 14 additions & 8 deletions telemetry/harvester.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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
}

Expand Down Expand Up @@ -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),
Expand All @@ -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),
Expand All @@ -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),
Expand All @@ -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),
Expand All @@ -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,
Expand All @@ -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
}
Expand Down
10 changes: 5 additions & 5 deletions telemetry/harvester_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit d12be59

Please sign in to comment.