Skip to content

Commit

Permalink
Merge pull request #276 from newrelic/eu-endpoint-feature-branch
Browse files Browse the repository at this point in the history
Added EU endpoint support and Unit tests in SenderConfiguration and the BatchSenders for all data types.
  • Loading branch information
yamnihcg authored Jun 28, 2021
2 parents f65b4c7 + 2f265b9 commit e848562
Show file tree
Hide file tree
Showing 10 changed files with 413 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,28 @@

/** Configuration options for the various classes that send data to the New Relic ingest APIs. */
public class SenderConfiguration {
private static final String DEFAULT_US_REGION = "US";

private final BaseConfig baseConfig;
private final HttpPoster httpPoster;
private final URL endpointUrl;
private final boolean useLicenseKey;
private final String endpointRegion;

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

public SenderConfiguration(
Expand All @@ -33,11 +43,13 @@ public SenderConfiguration(
URL endpointUrl,
boolean auditLoggingEnabled,
String secondaryUserAgent,
boolean useLicenseKey) {
boolean useLicenseKey,
String endpointRegion) {
this.httpPoster = httpPoster;
this.endpointUrl = endpointUrl;
this.baseConfig = new BaseConfig(apiKey, auditLoggingEnabled, secondaryUserAgent);
this.useLicenseKey = useLicenseKey;
this.endpointRegion = endpointRegion;
}

public String getApiKey() {
Expand All @@ -64,6 +76,10 @@ public boolean useLicenseKey() {
return useLicenseKey;
}

public String getRegion() {
return endpointRegion;
}

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

public SenderConfigurationBuilder(String defaultUrl, String basePath) {
Expand Down Expand Up @@ -107,8 +124,8 @@ public SenderConfigurationBuilder httpPoster(HttpPoster httpPoster) {

/**
* Configure the *full* endpoint URL for data to be sent to, including the path. You should only
* use this method if you wish to modify the default behavior, which is to send data to the
* Portland production US endpoints.
* use this method if you wish to send data to endpoints other than the US and EU production
* endpoints.
*
* @param endpoint A full {@link URL}, including the path.
* @return this builder.
Expand All @@ -128,6 +145,24 @@ public SenderConfigurationBuilder useLicenseKey(boolean useLicenseKey) {
return this;
}

/**
* Sets the region so that it is used in the individual batch senders (e.x. MetricBatchSender,
* LogBatchSender, SpanBatchSenders) to configure regional endpoints and send data to New Relic
*
* @param region String to indicate whether the account is in an American or European region. US
* (for American Region), EU (for European Region)
* @return this builder
*/
public SenderConfigurationBuilder setRegion(String region) throws IllegalArgumentException {

region = region.toUpperCase();
if (!region.equals("US") && !region.equals("EU")) {
throw new IllegalArgumentException("The only supported regions are the US and EU regions");
}
this.endpointRegion = region;
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 @@ -160,7 +195,8 @@ public SenderConfiguration build() {
getOrDefaultSendUrl(),
auditLoggingEnabled,
secondaryUserAgent,
useLicenseKey);
useLicenseKey,
endpointRegion);
}

private URL getOrDefaultSendUrl() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,16 @@
import com.newrelic.telemetry.http.HttpPoster;
import com.newrelic.telemetry.transport.BatchDataSender;
import com.newrelic.telemetry.util.Utils;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.function.Supplier;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class EventBatchSender {
private static final String EVENTS_PATH = "/v1/accounts/events";
private static final String DEFAULT_URL = "https://insights-collector.newrelic.com/";
private static final String DEFAULT_URL = "https://insights-collector.newrelic.com";
private static final String EUROPEAN_URL = "https://insights-collector.eu01.nr-data.net";
private static final Response EMPTY_BATCH_RESPONSE = new Response(202, "Ignored", "Empty batch");

private static final Logger logger = LoggerFactory.getLogger(EventBatchSender.class);
Expand Down Expand Up @@ -79,7 +81,21 @@ public static EventBatchSender create(SenderConfiguration configuration) {
Utils.verifyNonNull(configuration.getApiKey(), "API key cannot be null");
Utils.verifyNonNull(configuration.getHttpPoster(), "an HttpPoster implementation is required.");

URL url = configuration.getEndpointUrl();
String userRegion = configuration.getRegion();

String defaultUrl = DEFAULT_URL + EVENTS_PATH;
String endpointUrlToString = configuration.getEndpointUrl().toString();

URL url = null;
if (!endpointUrlToString.equals(defaultUrl)) {
url = configuration.getEndpointUrl();
} else {
try {
url = returnEndpoint(userRegion);
} catch (MalformedURLException e) {
e.printStackTrace();
}
}

EventBatchMarshaller marshaller = new EventBatchMarshaller();

Expand All @@ -95,6 +111,28 @@ public static EventBatchSender create(SenderConfiguration configuration) {
return new EventBatchSender(marshaller, sender);
}

public static URL returnEndpoint(String userRegion) throws MalformedURLException {
URL url = null;

if (userRegion.equals("US")) {
try {
url = new URL(DEFAULT_URL + EVENTS_PATH);
return url;
} catch (MalformedURLException e) {
e.printStackTrace();
}
} else if (userRegion.equals("EU")) {
try {
url = new URL(EUROPEAN_URL + EVENTS_PATH);
return url;
} catch (MalformedURLException e) {
e.printStackTrace();
}
}
throw new MalformedURLException(
"A valid region (EU or US) needs to be added to generate the right endpoint");
}

public static SenderConfigurationBuilder configurationBuilder() {
return SenderConfiguration.builder(DEFAULT_URL, EVENTS_PATH);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import com.newrelic.telemetry.logs.json.LogJsonTelemetryBlockWriter;
import com.newrelic.telemetry.transport.BatchDataSender;
import com.newrelic.telemetry.util.Utils;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.function.Supplier;
import org.slf4j.Logger;
Expand All @@ -25,8 +26,8 @@
public class LogBatchSender {

private static final String LOGS_PATH = "/log/v1";
private static final String DEFAULT_URL = "https://log-api.newrelic.com/";

private static final String DEFAULT_URL = "https://log-api.newrelic.com";
private static final String EUROPEAN_URL = "https://log-api.eu.newrelic.com";
private static final Logger logger = LoggerFactory.getLogger(LogBatchSender.class);

private final LogBatchMarshaller marshaller;
Expand Down Expand Up @@ -91,7 +92,21 @@ public static LogBatchSender create(SenderConfiguration configuration) {
Utils.verifyNonNull(configuration.getApiKey(), "API key cannot be null");
Utils.verifyNonNull(configuration.getHttpPoster(), "an HttpPoster implementation is required.");

URL url = configuration.getEndpointUrl();
String userRegion = configuration.getRegion();

String defaultUrl = DEFAULT_URL + LOGS_PATH;
String endpointUrlToString = configuration.getEndpointUrl().toString();

URL url = null;
if (!endpointUrlToString.equals(defaultUrl)) {
url = configuration.getEndpointUrl();
} else {
try {
url = returnEndpoint(userRegion);
} catch (MalformedURLException e) {
e.printStackTrace();
}
}

LogBatchMarshaller marshaller =
new LogBatchMarshaller(
Expand All @@ -109,6 +124,27 @@ public static LogBatchSender create(SenderConfiguration configuration) {
return new LogBatchSender(marshaller, sender);
}

public static URL returnEndpoint(String userRegion) throws MalformedURLException {
URL url = null;
if (userRegion.equals("US")) {
try {
url = new URL(DEFAULT_URL + LOGS_PATH);
return url;
} catch (MalformedURLException e) {
e.printStackTrace();
}
} else if (userRegion.equals("EU")) {
try {
url = new URL(EUROPEAN_URL + LOGS_PATH);
return url;
} catch (MalformedURLException e) {
e.printStackTrace();
}
}
throw new MalformedURLException(
"A valid region (EU or US) needs to be added to generate the right endpoint");
}

public static SenderConfiguration.SenderConfigurationBuilder configurationBuilder() {
return SenderConfiguration.builder(DEFAULT_URL, LOGS_PATH);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import com.newrelic.telemetry.metrics.json.MetricToJson;
import com.newrelic.telemetry.transport.BatchDataSender;
import com.newrelic.telemetry.util.Utils;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.function.Supplier;
import org.slf4j.Logger;
Expand All @@ -27,7 +28,8 @@
public class MetricBatchSender {

private static final String METRICS_PATH = "/metric/v1";
private static final String DEFAULT_URL = "https://metric-api.newrelic.com/";
private static final String DEFAULT_URL = "https://metric-api.newrelic.com";
private static final String EUROPEAN_URL = "https://metric-api.eu.newrelic.com";

private static final Logger logger = LoggerFactory.getLogger(MetricBatchSender.class);

Expand Down Expand Up @@ -88,7 +90,21 @@ public static MetricBatchSender create(SenderConfiguration configuration) {
Utils.verifyNonNull(configuration.getApiKey(), "API key cannot be null");
Utils.verifyNonNull(configuration.getHttpPoster(), "an HttpPoster implementation is required.");

URL url = configuration.getEndpointUrl();
String userRegion = configuration.getRegion();

String defaultUrl = DEFAULT_URL + METRICS_PATH;
String endpointUrlToString = configuration.getEndpointUrl().toString();

URL url = null;
if (!endpointUrlToString.equals(defaultUrl)) {
url = configuration.getEndpointUrl();
} else {
try {
url = returnEndpoint(userRegion);
} catch (MalformedURLException e) {
e.printStackTrace();
}
}

MetricBatchMarshaller marshaller =
new MetricBatchMarshaller(
Expand All @@ -106,6 +122,27 @@ public static MetricBatchSender create(SenderConfiguration configuration) {
return new MetricBatchSender(marshaller, sender);
}

public static URL returnEndpoint(String userRegion) throws MalformedURLException {
URL url = null;
if (userRegion.equals("US")) {
try {
url = new URL(DEFAULT_URL + METRICS_PATH);
return url;
} catch (MalformedURLException e) {
e.printStackTrace();
}
} else if (userRegion.equals("EU")) {
try {
url = new URL(EUROPEAN_URL + METRICS_PATH);
return url;
} catch (MalformedURLException e) {
e.printStackTrace();
}
}
throw new MalformedURLException(
"A valid region (EU or US) needs to be added to generate the right endpoint");
}

public static SenderConfigurationBuilder configurationBuilder() {
return SenderConfiguration.builder(DEFAULT_URL, METRICS_PATH);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import com.newrelic.telemetry.spans.json.SpanJsonTelemetryBlockWriter;
import com.newrelic.telemetry.transport.BatchDataSender;
import com.newrelic.telemetry.util.Utils;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.function.Supplier;
import org.slf4j.Logger;
Expand All @@ -25,8 +26,8 @@
public class SpanBatchSender {

private static final String SPANS_PATH = "/trace/v1";
private static final String DEFAULT_URL = "https://trace-api.newrelic.com/";

private static final String DEFAULT_URL = "https://trace-api.newrelic.com";
private static final String EUROPEAN_URL = "https://trace-api.eu.newrelic.com";
private static final Logger logger = LoggerFactory.getLogger(SpanBatchSender.class);

private final SpanBatchMarshaller marshaller;
Expand Down Expand Up @@ -91,7 +92,20 @@ public static SpanBatchSender create(SenderConfiguration configuration) {
Utils.verifyNonNull(configuration.getApiKey(), "API key cannot be null");
Utils.verifyNonNull(configuration.getHttpPoster(), "an HttpPoster implementation is required.");

URL url = configuration.getEndpointUrl();
String userRegion = configuration.getRegion();
String defaultUrl = DEFAULT_URL + SPANS_PATH;
String endpointUrlToString = configuration.getEndpointUrl().toString();

URL url = null;
if (!endpointUrlToString.equals(defaultUrl)) {
url = configuration.getEndpointUrl();
} else {
try {
url = returnEndpoint(userRegion);
} catch (MalformedURLException e) {
e.printStackTrace();
}
}

SpanBatchMarshaller marshaller =
new SpanBatchMarshaller(
Expand All @@ -109,6 +123,27 @@ public static SpanBatchSender create(SenderConfiguration configuration) {
return new SpanBatchSender(marshaller, sender);
}

public static URL returnEndpoint(String userRegion) throws MalformedURLException {
URL url = null;
if (userRegion.equals("US")) {
try {
url = new URL(DEFAULT_URL + SPANS_PATH);
return url;
} catch (MalformedURLException e) {
e.printStackTrace();
}
} else if (userRegion.equals("EU")) {
try {
url = new URL(EUROPEAN_URL + SPANS_PATH);
return url;
} catch (MalformedURLException e) {
e.printStackTrace();
}
}
throw new MalformedURLException(
"A valid region (EU or US) needs to be added to generate the right endpoint");
}

public static SenderConfiguration.SenderConfigurationBuilder configurationBuilder() {
return SenderConfiguration.builder(DEFAULT_URL, SPANS_PATH);
}
Expand Down
Loading

0 comments on commit e848562

Please sign in to comment.