diff --git a/telemetry-core/src/main/java/com/newrelic/telemetry/SenderConfiguration.java b/telemetry-core/src/main/java/com/newrelic/telemetry/SenderConfiguration.java index bbb0af0d..a56ee796 100644 --- a/telemetry-core/src/main/java/com/newrelic/telemetry/SenderConfiguration.java +++ b/telemetry-core/src/main/java/com/newrelic/telemetry/SenderConfiguration.java @@ -13,10 +13,13 @@ /** 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, @@ -24,7 +27,14 @@ public SenderConfiguration( 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( @@ -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() { @@ -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); } @@ -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) { @@ -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. @@ -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. @@ -160,7 +195,8 @@ public SenderConfiguration build() { getOrDefaultSendUrl(), auditLoggingEnabled, secondaryUserAgent, - useLicenseKey); + useLicenseKey, + endpointRegion); } private URL getOrDefaultSendUrl() { diff --git a/telemetry-core/src/main/java/com/newrelic/telemetry/events/EventBatchSender.java b/telemetry-core/src/main/java/com/newrelic/telemetry/events/EventBatchSender.java index 8a555f11..d6970df0 100644 --- a/telemetry-core/src/main/java/com/newrelic/telemetry/events/EventBatchSender.java +++ b/telemetry-core/src/main/java/com/newrelic/telemetry/events/EventBatchSender.java @@ -15,6 +15,7 @@ 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; @@ -22,7 +23,8 @@ 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); @@ -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(); @@ -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); } diff --git a/telemetry-core/src/main/java/com/newrelic/telemetry/logs/LogBatchSender.java b/telemetry-core/src/main/java/com/newrelic/telemetry/logs/LogBatchSender.java index 83686077..de66fad6 100644 --- a/telemetry-core/src/main/java/com/newrelic/telemetry/logs/LogBatchSender.java +++ b/telemetry-core/src/main/java/com/newrelic/telemetry/logs/LogBatchSender.java @@ -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; @@ -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; @@ -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( @@ -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); } diff --git a/telemetry-core/src/main/java/com/newrelic/telemetry/metrics/MetricBatchSender.java b/telemetry-core/src/main/java/com/newrelic/telemetry/metrics/MetricBatchSender.java index 087decbf..d0232985 100644 --- a/telemetry-core/src/main/java/com/newrelic/telemetry/metrics/MetricBatchSender.java +++ b/telemetry-core/src/main/java/com/newrelic/telemetry/metrics/MetricBatchSender.java @@ -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; @@ -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); @@ -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( @@ -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); } diff --git a/telemetry-core/src/main/java/com/newrelic/telemetry/spans/SpanBatchSender.java b/telemetry-core/src/main/java/com/newrelic/telemetry/spans/SpanBatchSender.java index 82624a22..c1858acd 100644 --- a/telemetry-core/src/main/java/com/newrelic/telemetry/spans/SpanBatchSender.java +++ b/telemetry-core/src/main/java/com/newrelic/telemetry/spans/SpanBatchSender.java @@ -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; @@ -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; @@ -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( @@ -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); } diff --git a/telemetry-core/src/test/java/com/newrelic/telemetry/SenderConfigurationTest.java b/telemetry-core/src/test/java/com/newrelic/telemetry/SenderConfigurationTest.java new file mode 100644 index 00000000..7e180466 --- /dev/null +++ b/telemetry-core/src/test/java/com/newrelic/telemetry/SenderConfigurationTest.java @@ -0,0 +1,52 @@ +package com.newrelic.telemetry; + +import static org.junit.jupiter.api.Assertions.*; + +import java.net.URL; +import org.junit.jupiter.api.Test; + +class SenderConfigurationTest { + + String testURL = "https://fun.com"; + String testPath = "/account/metric"; + + @Test + void defaultRegionTest() { + SenderConfiguration testConfig = SenderConfiguration.builder(testURL, testPath).build(); + assertEquals("US", testConfig.getRegion()); + } + + @Test + void defaultEURegionTest() { + SenderConfiguration testEUConfig = + SenderConfiguration.builder(testURL, testPath).setRegion("EU").build(); + assertEquals("EU", testEUConfig.getRegion()); + } + + @Test + void exceptionTest() { + Exception testIllegalArgumentException = + assertThrows( + IllegalArgumentException.class, + () -> { + SenderConfiguration.builder(testURL, testPath).setRegion("ASIA").build(); + }); + String expectedExceptionMessage = "The only supported regions are the US and EU regions"; + assertEquals(expectedExceptionMessage, testIllegalArgumentException.getMessage()); + } + + @Test + void defaultEndpointTest() throws Exception { + URL testEndpointURL = new URL(testURL + testPath); + SenderConfiguration testConfig = SenderConfiguration.builder(testURL, testPath).build(); + assertEquals(testEndpointURL, testConfig.getEndpointUrl()); + } + + @Test + void userProvidedEndpointTest() throws Exception { + URL testEndpointURL = new URL("https://google.com"); + SenderConfiguration testConfig = + SenderConfiguration.builder(testURL, testPath).endpoint(testEndpointURL).build(); + assertEquals(testEndpointURL, testConfig.getEndpointUrl()); + } +} diff --git a/telemetry-core/src/test/java/com/newrelic/telemetry/events/EventBatchSenderTest.java b/telemetry-core/src/test/java/com/newrelic/telemetry/events/EventBatchSenderTest.java index d52a695f..b140a150 100644 --- a/telemetry-core/src/test/java/com/newrelic/telemetry/events/EventBatchSenderTest.java +++ b/telemetry-core/src/test/java/com/newrelic/telemetry/events/EventBatchSenderTest.java @@ -18,6 +18,7 @@ import com.newrelic.telemetry.http.HttpPoster; import com.newrelic.telemetry.http.HttpResponse; import com.newrelic.telemetry.transport.BatchDataSender; +import java.net.MalformedURLException; import java.net.URI; import java.net.URL; import java.util.ArrayList; @@ -95,4 +96,43 @@ public void sendBatchViaCreate() throws Exception { assertEquals(expected, result); assertTrue(((String) headersCaptor.getValue().get("User-Agent")).endsWith(" second")); } + + @Test + public void testDefaultEndpoint() throws Exception { + URL testURL = new URL("https://insights-collector.newrelic.com/v1/accounts/events"); + + EventBatchMarshaller testMarshaller = mock(EventBatchMarshaller.class); + BatchDataSender testSender = mock(BatchDataSender.class); + + EventBatchSender testEventBatchSender = new EventBatchSender(testMarshaller, testSender); + assertEquals(testURL, testEventBatchSender.returnEndpoint("US")); + } + + @Test + public void testEUEndpoint() throws Exception { + URL testEUURL = new URL("https://insights-collector.eu01.nr-data.net/v1/accounts/events"); + + EventBatchMarshaller testMarshaller = mock(EventBatchMarshaller.class); + BatchDataSender testSender = mock(BatchDataSender.class); + + EventBatchSender testEventBatchSender = new EventBatchSender(testMarshaller, testSender); + assertEquals(testEUURL, testEventBatchSender.returnEndpoint("EU")); + } + + @Test + public void testException() { + EventBatchMarshaller testMarshaller = mock(EventBatchMarshaller.class); + BatchDataSender testSender = mock(BatchDataSender.class); + + EventBatchSender testEventBatchSender = new EventBatchSender(testMarshaller, testSender); + Exception testMalformedURLException = + assertThrows( + MalformedURLException.class, + () -> { + testEventBatchSender.returnEndpoint("random"); + }); + String expectedExceptionMessage = + "A valid region (EU or US) needs to be added to generate the right endpoint"; + assertEquals(expectedExceptionMessage, testMalformedURLException.getMessage()); + } } diff --git a/telemetry-core/src/test/java/com/newrelic/telemetry/logs/LogBatchSenderTest.java b/telemetry-core/src/test/java/com/newrelic/telemetry/logs/LogBatchSenderTest.java index 1d909d57..a9c88c17 100644 --- a/telemetry-core/src/test/java/com/newrelic/telemetry/logs/LogBatchSenderTest.java +++ b/telemetry-core/src/test/java/com/newrelic/telemetry/logs/LogBatchSenderTest.java @@ -4,8 +4,7 @@ */ package com.newrelic.telemetry.logs; -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.junit.jupiter.api.Assertions.*; import static org.mockito.ArgumentMatchers.anyString; import static org.mockito.ArgumentMatchers.eq; import static org.mockito.ArgumentMatchers.isA; @@ -19,6 +18,7 @@ import com.newrelic.telemetry.http.HttpResponse; import com.newrelic.telemetry.logs.json.LogBatchMarshaller; import com.newrelic.telemetry.transport.BatchDataSender; +import java.net.MalformedURLException; import java.net.URI; import java.net.URL; import java.util.Collection; @@ -86,4 +86,43 @@ void testEmptyBatch() throws Exception { Response response = testClass.sendBatch(batch); assertEquals(202, response.getStatusCode()); } + + @Test + public void testDefaultEndpoint() throws Exception { + URL testURL = new URL("https://log-api.newrelic.com/log/v1"); + + LogBatchMarshaller testMarshaller = mock(LogBatchMarshaller.class); + BatchDataSender testSender = mock(BatchDataSender.class); + + LogBatchSender testLogBatchSender = new LogBatchSender(testMarshaller, testSender); + assertEquals(testURL, testLogBatchSender.returnEndpoint("US")); + } + + @Test + public void testEUEndpoint() throws Exception { + URL testEUURL = new URL("https://log-api.eu.newrelic.com/log/v1"); + + LogBatchMarshaller testMarshaller = mock(LogBatchMarshaller.class); + BatchDataSender testSender = mock(BatchDataSender.class); + + LogBatchSender testLogBatchSender = new LogBatchSender(testMarshaller, testSender); + assertEquals(testEUURL, testLogBatchSender.returnEndpoint("EU")); + } + + @Test + public void testException() { + LogBatchMarshaller testMarshaller = mock(LogBatchMarshaller.class); + BatchDataSender testSender = mock(BatchDataSender.class); + + LogBatchSender testLogBatchSender = new LogBatchSender(testMarshaller, testSender); + Exception testMalformedURLException = + assertThrows( + MalformedURLException.class, + () -> { + testLogBatchSender.returnEndpoint("random"); + }); + String expectedExceptionMessage = + "A valid region (EU or US) needs to be added to generate the right endpoint"; + assertEquals(expectedExceptionMessage, testMalformedURLException.getMessage()); + } } diff --git a/telemetry-core/src/test/java/com/newrelic/telemetry/metrics/MetricBatchSenderTest.java b/telemetry-core/src/test/java/com/newrelic/telemetry/metrics/MetricBatchSenderTest.java index 30e7589a..e109d6d3 100644 --- a/telemetry-core/src/test/java/com/newrelic/telemetry/metrics/MetricBatchSenderTest.java +++ b/telemetry-core/src/test/java/com/newrelic/telemetry/metrics/MetricBatchSenderTest.java @@ -4,21 +4,19 @@ */ package com.newrelic.telemetry.metrics; -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.junit.jupiter.api.Assertions.*; import static org.mockito.ArgumentMatchers.anyString; import static org.mockito.ArgumentMatchers.eq; import static org.mockito.ArgumentMatchers.isA; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; -import com.newrelic.telemetry.Attributes; -import com.newrelic.telemetry.BaseConfig; -import com.newrelic.telemetry.Response; +import com.newrelic.telemetry.*; import com.newrelic.telemetry.http.HttpPoster; import com.newrelic.telemetry.http.HttpResponse; import com.newrelic.telemetry.metrics.json.MetricBatchMarshaller; import com.newrelic.telemetry.transport.BatchDataSender; +import java.net.MalformedURLException; import java.net.URI; import java.net.URL; import java.util.Collection; @@ -87,4 +85,43 @@ public void sendBatchViaCreate() throws Exception { assertEquals(expected, result); assertTrue(((String) headersCaptor.getValue().get("User-Agent")).endsWith(" second")); } + + @Test + public void testDefaultEndpoint() throws Exception { + URL testURL = new URL("https://metric-api.newrelic.com/metric/v1"); + + MetricBatchMarshaller testMarshaller = mock(MetricBatchMarshaller.class); + BatchDataSender testSender = mock(BatchDataSender.class); + + MetricBatchSender testMetricBatchSender = new MetricBatchSender(testMarshaller, testSender); + assertEquals(testURL, testMetricBatchSender.returnEndpoint("US")); + } + + @Test + public void testEUEndpoint() throws Exception { + URL testEUURL = new URL("https://metric-api.eu.newrelic.com/metric/v1"); + + MetricBatchMarshaller testMarshaller = mock(MetricBatchMarshaller.class); + BatchDataSender testSender = mock(BatchDataSender.class); + + MetricBatchSender testMetricBatchSender = new MetricBatchSender(testMarshaller, testSender); + assertEquals(testEUURL, testMetricBatchSender.returnEndpoint("EU")); + } + + @Test + public void testException() { + MetricBatchMarshaller testMarshaller = mock(MetricBatchMarshaller.class); + BatchDataSender testSender = mock(BatchDataSender.class); + + MetricBatchSender testMetricBatchSender = new MetricBatchSender(testMarshaller, testSender); + Exception testMalformedURLException = + assertThrows( + MalformedURLException.class, + () -> { + testMetricBatchSender.returnEndpoint("random"); + }); + String expectedExceptionMessage = + "A valid region (EU or US) needs to be added to generate the right endpoint"; + assertEquals(expectedExceptionMessage, testMalformedURLException.getMessage()); + } } diff --git a/telemetry-core/src/test/java/com/newrelic/telemetry/spans/SpanBatchSenderTest.java b/telemetry-core/src/test/java/com/newrelic/telemetry/spans/SpanBatchSenderTest.java index 489c07f2..bb26d43d 100644 --- a/telemetry-core/src/test/java/com/newrelic/telemetry/spans/SpanBatchSenderTest.java +++ b/telemetry-core/src/test/java/com/newrelic/telemetry/spans/SpanBatchSenderTest.java @@ -4,8 +4,7 @@ */ package com.newrelic.telemetry.spans; -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.junit.jupiter.api.Assertions.*; import static org.mockito.ArgumentMatchers.anyString; import static org.mockito.ArgumentMatchers.eq; import static org.mockito.ArgumentMatchers.isA; @@ -19,6 +18,7 @@ import com.newrelic.telemetry.http.HttpResponse; import com.newrelic.telemetry.spans.json.SpanBatchMarshaller; import com.newrelic.telemetry.transport.BatchDataSender; +import java.net.MalformedURLException; import java.net.URI; import java.net.URL; import java.util.Collection; @@ -87,4 +87,43 @@ public void sendBatchViaCreate() throws Exception { assertEquals(expected, result); assertTrue(((String) headersCaptor.getValue().get("User-Agent")).endsWith(" second")); } + + @Test + public void testDefaultEndpoint() throws Exception { + URL testURL = new URL("https://trace-api.newrelic.com/trace/v1"); + + SpanBatchMarshaller testMarshaller = mock(SpanBatchMarshaller.class); + BatchDataSender testSender = mock(BatchDataSender.class); + + SpanBatchSender testSpanBatchSender = new SpanBatchSender(testMarshaller, testSender); + assertEquals(testURL, testSpanBatchSender.returnEndpoint("US")); + } + + @Test + public void testEUEndpoint() throws Exception { + URL testEUURL = new URL("https://trace-api.eu.newrelic.com/trace/v1"); + + SpanBatchMarshaller testMarshaller = mock(SpanBatchMarshaller.class); + BatchDataSender testSender = mock(BatchDataSender.class); + + SpanBatchSender testSpanBatchSender = new SpanBatchSender(testMarshaller, testSender); + assertEquals(testEUURL, testSpanBatchSender.returnEndpoint("EU")); + } + + @Test + public void testException() { + SpanBatchMarshaller testMarshaller = mock(SpanBatchMarshaller.class); + BatchDataSender testSender = mock(BatchDataSender.class); + + SpanBatchSender testSpanBatchSender = new SpanBatchSender(testMarshaller, testSender); + Exception testMalformedURLException = + assertThrows( + MalformedURLException.class, + () -> { + testSpanBatchSender.returnEndpoint("random"); + }); + String expectedExceptionMessage = + "A valid region (EU or US) needs to be added to generate the right endpoint"; + assertEquals(expectedExceptionMessage, testMalformedURLException.getMessage()); + } }