Skip to content

Commit

Permalink
Merge pull request #271 from newrelic/spike_license_key_usage
Browse files Browse the repository at this point in the history
Initial cut of adding license key support
  • Loading branch information
Ben Evans authored Feb 16, 2021
2 parents e4c47c1 + 006e2f9 commit 3cae80f
Show file tree
Hide file tree
Showing 7 changed files with 64 additions and 7 deletions.
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,4 @@ jsonassertVersion=1.5.0
mockitoVersion=2.23.0
mockserverVersion=5.5.1
okhttpVersion=4.8.0
testContainerVersion=1.15.0-rc2
testContainerVersion=1.15.1
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,28 @@ public class SenderConfiguration {
private final BaseConfig baseConfig;
private final HttpPoster httpPoster;
private final URL endpointUrl;
private final boolean useLicenseKey;

public SenderConfiguration(
String apiKey,
HttpPoster httpPoster,
URL endpointUrl,
boolean auditLoggingEnabled,
String secondaryUserAgent) {
this(apiKey, httpPoster, endpointUrl, auditLoggingEnabled, secondaryUserAgent, false);
}

public SenderConfiguration(
String apiKey,
HttpPoster httpPoster,
URL endpointUrl,
boolean auditLoggingEnabled,
String secondaryUserAgent,
boolean useLicenseKey) {
this.httpPoster = httpPoster;
this.endpointUrl = endpointUrl;
this.baseConfig = new BaseConfig(apiKey, auditLoggingEnabled, secondaryUserAgent);
this.useLicenseKey = useLicenseKey;
}

public String getApiKey() {
Expand All @@ -48,6 +60,10 @@ public String getSecondaryUserAgent() {
return baseConfig.getSecondaryUserAgent();
}

public boolean useLicenseKey() {
return useLicenseKey;
}

public static SenderConfigurationBuilder builder(String defaultUrl, String basePath) {
return new SenderConfigurationBuilder(defaultUrl, basePath);
}
Expand All @@ -61,6 +77,7 @@ public static class SenderConfigurationBuilder {
private HttpPoster httpPoster;
private URL endpointUrl;
private boolean auditLoggingEnabled = false;
private boolean useLicenseKey = false;
private String secondaryUserAgent;

public SenderConfigurationBuilder(String defaultUrl, String basePath) {
Expand Down Expand Up @@ -101,6 +118,16 @@ public SenderConfigurationBuilder endpoint(URL endpoint) {
return this;
}

/**
* @param useLicenseKey flag to indicate the configured {@code apiKey} is a license key, not an
* insights api key
* @return this builder
*/
public SenderConfigurationBuilder useLicenseKey(boolean useLicenseKey) {
this.useLicenseKey = useLicenseKey;
return this;
}

/**
* Configure whether audit logging is enabled. Note: audit logging will log all data payloads
* sent to New Relic at DEBUG level, in plain text.
Expand Down Expand Up @@ -128,7 +155,12 @@ public SenderConfigurationBuilder secondaryUserAgent(String secondaryUserAgent)

public SenderConfiguration build() {
return new SenderConfiguration(
apiKey, httpPoster, getOrDefaultSendUrl(), auditLoggingEnabled, secondaryUserAgent);
apiKey,
httpPoster,
getOrDefaultSendUrl(),
auditLoggingEnabled,
secondaryUserAgent,
useLicenseKey);
}

private URL getOrDefaultSendUrl() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,8 @@ public static EventBatchSender create(SenderConfiguration configuration) {
configuration.getApiKey(),
url,
configuration.isAuditLoggingEnabled(),
configuration.getSecondaryUserAgent());
configuration.getSecondaryUserAgent(),
configuration.useLicenseKey());

return new EventBatchSender(marshaller, sender);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,8 @@ public static LogBatchSender create(SenderConfiguration configuration) {
configuration.getApiKey(),
url,
configuration.isAuditLoggingEnabled(),
configuration.getSecondaryUserAgent());
configuration.getSecondaryUserAgent(),
configuration.useLicenseKey());

return new LogBatchSender(marshaller, sender);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,8 @@ public static MetricBatchSender create(SenderConfiguration configuration) {
configuration.getApiKey(),
url,
configuration.isAuditLoggingEnabled(),
configuration.getSecondaryUserAgent());
configuration.getSecondaryUserAgent(),
configuration.useLicenseKey());

return new MetricBatchSender(marshaller, sender);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,8 @@ public static SpanBatchSender create(SenderConfiguration configuration) {
configuration.getApiKey(),
url,
configuration.isAuditLoggingEnabled(),
configuration.getSecondaryUserAgent());
configuration.getSecondaryUserAgent(),
configuration.useLicenseKey());

return new SpanBatchSender(marshaller, sender);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ public class BatchDataSender {
private final URL endpointURl;
private final boolean auditLoggingEnabled;
private final String userAgent;
private final boolean useLicenseKey;

static {
String implementationVersion = readVersion();
Expand All @@ -56,15 +57,31 @@ public BatchDataSender(
URL endpointURl,
boolean auditLoggingEnabled,
String secondaryUserAgent) {
this(client, apiKey, endpointURl, auditLoggingEnabled, secondaryUserAgent, false);
}

public BatchDataSender(
HttpPoster client,
String apiKey,
URL endpointURl,
boolean auditLoggingEnabled,
String secondaryUserAgent,
boolean useLicenseKey) {
this.client = client;
this.apiKey = apiKey;
this.endpointURl = endpointURl;
this.auditLoggingEnabled = auditLoggingEnabled;
this.userAgent = buildUserAgent(secondaryUserAgent);
this.useLicenseKey = useLicenseKey;
logger.info("BatchDataSender configured with endpoint {}", endpointURl);
if (auditLoggingEnabled) {
logger.info("BatchDataSender configured with audit logging enabled.");
}
if (useLicenseKey) {
logger.info("BatchDataSender configured to use license keys");
} else {
logger.info("BatchDataSender configured to use insights keys");
}
}

private String buildUserAgent(String additionalUserAgent) {
Expand Down Expand Up @@ -124,7 +141,11 @@ private Response sendPayload(byte[] payload, UUID requestId, String batchType)
throws DiscardBatchException, RetryWithSplitException, RetryWithBackoffException,
RetryWithRequestedWaitException {
Map<String, String> headers = new HashMap<>();
headers.put("Api-Key", apiKey);
if (useLicenseKey) {
headers.put("X-License-Key", apiKey);
} else {
headers.put("Api-Key", apiKey);
}
headers.put("Content-Encoding", "gzip");
if (requestId != null) {
headers.put("X-Request-Id", requestId.toString());
Expand Down

0 comments on commit 3cae80f

Please sign in to comment.