From 8964bd2df468f9b7170db9fd3edb4a896a57eacd Mon Sep 17 00:00:00 2001 From: maxday Date: Mon, 1 Nov 2021 13:00:59 -0400 Subject: [PATCH 1/8] support hello and flush route --- build.gradle | 2 + .../datadog_lambda_java/DDLambda.java | 34 +++++++--- .../datadog_lambda_java/Extension.java | 65 +++++++++++++++++++ .../datadog_lambda_java/ExtensionTest.java | 57 ++++++++++++++++ src/test/resources/fakeExtension | 0 5 files changed, 149 insertions(+), 9 deletions(-) create mode 100644 src/main/java/com/datadoghq/datadog_lambda_java/Extension.java create mode 100644 src/test/java/com/datadoghq/datadog_lambda_java/ExtensionTest.java create mode 100644 src/test/resources/fakeExtension diff --git a/build.gradle b/build.gradle index 8405fb2..84ea3b0 100644 --- a/build.gradle +++ b/build.gradle @@ -43,6 +43,8 @@ dependencies { testImplementation 'com.github.stefanbirkner:system-rules:1.19.0' testImplementation 'com.amazonaws:aws-java-sdk-kinesis:1.11.980' + // Use wiremock for stubing http calls + testCompile 'com.github.tomakehurst:wiremock-jre8:2.31.0' } sourceCompatibility = 1.8 diff --git a/src/main/java/com/datadoghq/datadog_lambda_java/DDLambda.java b/src/main/java/com/datadoghq/datadog_lambda_java/DDLambda.java index 6f3b021..bc28810 100644 --- a/src/main/java/com/datadoghq/datadog_lambda_java/DDLambda.java +++ b/src/main/java/com/datadoghq/datadog_lambda_java/DDLambda.java @@ -42,6 +42,15 @@ public class DDLambda { private Tracing tracing; private boolean enhanced = true; private Scope tracingScope; + private boolean shouldUseExtension = false; + + + /** + * Private constructor, called from existing constructors to detect whether the extension is present + */ + private DDLambda() { + this.shouldUseExtension = Extension.setup(); + } /** * Create a new DDLambda instrumenter given some Lambda context @@ -49,6 +58,7 @@ public class DDLambda { * @param cxt Enhanced Metrics pulls information from the Lambda context. */ public DDLambda(Context cxt) { + this(); this.tracing = new Tracing(); this.enhanced = checkEnhanced(); recordEnhanced(INVOCATION, cxt); @@ -63,6 +73,7 @@ public DDLambda(Context cxt) { * @param xrayTraceInfo This would normally be the contents of the "_X_AMZN_TRACE_ID" env var */ protected DDLambda(Context cxt, String xrayTraceInfo) { + this(); this.tracing = new Tracing(xrayTraceInfo); this.enhanced = checkEnhanced(); recordEnhanced(INVOCATION, cxt); @@ -78,6 +89,7 @@ protected DDLambda(Context cxt, String xrayTraceInfo) { * @param cxt Enhanced Metrics pulls information from the Lambda context. */ public DDLambda(APIGatewayProxyRequestEvent req, Context cxt) { + this(); this.enhanced = checkEnhanced(); recordEnhanced(INVOCATION, cxt); this.tracing = new Tracing(req); @@ -94,6 +106,7 @@ public DDLambda(APIGatewayProxyRequestEvent req, Context cxt) { * @param cxt Enhanced Metrics pulls information from the Lambda context. */ public DDLambda(APIGatewayV2ProxyRequestEvent req, Context cxt) { + this(); this.enhanced = checkEnhanced(); recordEnhanced(INVOCATION, cxt); this.tracing = new Tracing(req); @@ -110,6 +123,7 @@ public DDLambda(APIGatewayV2ProxyRequestEvent req, Context cxt) { * @param cxt Enhanced Metrics pulls information from the Lambda context. */ public DDLambda(SQSEvent event, Context cxt) { + this(); this.enhanced = checkEnhanced(); recordEnhanced(INVOCATION, cxt); SQSHeaderable headerable = new SQSHeaderable(event); @@ -127,6 +141,7 @@ public DDLambda(SQSEvent event, Context cxt) { * @param cxt Enhanced Metrics pulls information from the Lambda context. */ public DDLambda(KinesisEvent event, Context cxt) { + this(); this.enhanced = checkEnhanced(); recordEnhanced(INVOCATION, cxt); KinesisHeaderable headerable = new KinesisHeaderable(event); @@ -144,6 +159,7 @@ public DDLambda(KinesisEvent event, Context cxt) { * @param cxt Enhanced Metrics pulls information from the Lambda context. */ public DDLambda(Headerable req, Context cxt) { + this(); this.enhanced = checkEnhanced(); recordEnhanced(INVOCATION, cxt); this.tracing = new Tracing(req); @@ -197,18 +213,18 @@ private void startSpan(Map headers, Context cxt) { */ public void finish() { Span span = GlobalTracer.get().activeSpan(); - if (this.tracingScope == null) { DDLogger.getLoggerImpl().debug("Unable to close tracing scope because it is null."); - return; - } - this.tracingScope.close(); - - if (span != null) { - span.finish(); } else { - DDLogger.getLoggerImpl().debug("Unable to finish span because it is null."); - return; + this.tracingScope.close(); + if (span != null) { + span.finish(); + } else { + DDLogger.getLoggerImpl().debug("Unable to finish span because it is null."); + } + } + if(this.shouldUseExtension) { + Extension.flush(); } } diff --git a/src/main/java/com/datadoghq/datadog_lambda_java/Extension.java b/src/main/java/com/datadoghq/datadog_lambda_java/Extension.java new file mode 100644 index 0000000..b5eb6eb --- /dev/null +++ b/src/main/java/com/datadoghq/datadog_lambda_java/Extension.java @@ -0,0 +1,65 @@ +package com.datadoghq.datadog_lambda_java; + +import java.io.File; +import java.io.IOException; +import java.net.HttpURLConnection; +import java.net.MalformedURLException; +import java.net.URL; + +class Extension { + + private final static String AGENT_URL = "http://127.0.0.1:8124"; + private final static String HELLO_PATH = "/lambda/hello"; + private final static String FLUSH_PATH = "/lambda/flush"; + private final static String EXTENSION_PATH = "/opt/extensions/datadog-agent"; + + public static boolean setup() { + boolean shouldUseExtension = false; + if(isExtensionRuning(EXTENSION_PATH)) { + DDLogger.getLoggerImpl().debug("Extension has been detected"); + if(hitHelloRoute(AGENT_URL, FLUSH_PATH)) { + shouldUseExtension = true; + } else { + DDLogger.getLoggerImpl().debug("Impossible to call the hello route"); + } + } + return shouldUseExtension; + } + + public static void flush() { + if(!hitFlushRoute(AGENT_URL, FLUSH_PATH)) { + DDLogger.getLoggerImpl().debug("Impossible to flush"); + } + } + + public static boolean isExtensionRuning(final String extensionPath) { + File f = new File(extensionPath); + return (f.exists() && !f.isDirectory()); + } + + public static boolean hitHelloRoute(final String agentUrl, final String helloPath) { + try { + URL url = new URL(agentUrl + helloPath); + HttpURLConnection http = (HttpURLConnection) url.openConnection(); + return http.getResponseCode() == HttpURLConnection.HTTP_OK; + } catch (MalformedURLException e) { + return false; + } catch (IOException e) { + return false; + } + } + + public static boolean hitFlushRoute(final String agentUrl, final String flushPath) { + try { + URL url = new URL(agentUrl + flushPath); + HttpURLConnection http = (HttpURLConnection) url.openConnection(); + http.setRequestMethod("POST"); + return http.getResponseCode() == HttpURLConnection.HTTP_OK; + } catch (MalformedURLException e) { + return false; + } catch (IOException e) { + return false; + } + } + +} \ No newline at end of file diff --git a/src/test/java/com/datadoghq/datadog_lambda_java/ExtensionTest.java b/src/test/java/com/datadoghq/datadog_lambda_java/ExtensionTest.java new file mode 100644 index 0000000..8279144 --- /dev/null +++ b/src/test/java/com/datadoghq/datadog_lambda_java/ExtensionTest.java @@ -0,0 +1,57 @@ +package com.datadoghq.datadog_lambda_java; + +import com.github.tomakehurst.wiremock.junit.WireMockRule; +import org.junit.Assert; +import org.junit.Rule; +import org.junit.Test; + +import java.nio.file.Path; +import java.nio.file.Paths; + +import static com.github.tomakehurst.wiremock.client.WireMock.*; + + +public class ExtensionTest { + + @Rule + public WireMockRule wireMockRule = new WireMockRule(9999); + + @Test public void testDetectExtensionSuccess() { + Path resourceDirectory = Paths.get("src","test","resources"); + String fakeExtensionPath = resourceDirectory.toFile().getAbsolutePath() + "/fakeExtension"; + Assert.assertTrue(Extension.isExtensionRuning(fakeExtensionPath)); + } + @Test public void testDetectExtensionFailure() { + String invalidPath = "/fakeExtension"; + Assert.assertFalse(Extension.isExtensionRuning(invalidPath)); + } + + @Test public void testHitHelloRouteIncorrectUrl() { + Assert.assertFalse(Extension.hitHelloRoute("toto", "titi")); + } + + @Test public void testHitHelloRouteNotRespondingEndpoint() { + Assert.assertFalse(Extension.hitHelloRoute("http://localhost:1111", "/invalid")); + } + + @Test public void testHitHelloRouteValidEndpoint() { + stubFor(get(urlEqualTo("/valid")) + .willReturn(aResponse())); + Assert.assertTrue(Extension.hitHelloRoute("http://localhost:9999", "/valid")); + } + + @Test public void testHitFlushRouteIncorrectUrl() { + Assert.assertFalse(Extension.hitFlushRoute("toto", "titi")); + } + + @Test public void testHitFlushRouteNotRespondingEndpoint() { + Assert.assertFalse(Extension.hitFlushRoute("http://localhost:1111", "/invalid")); + } + + @Test public void testHitFlushRouteValidEndpoint() { + stubFor(post(urlEqualTo("/valid")) + .willReturn(aResponse())); + Assert.assertTrue(Extension.hitFlushRoute("http://localhost:9999", "/valid")); + } + +} \ No newline at end of file diff --git a/src/test/resources/fakeExtension b/src/test/resources/fakeExtension new file mode 100644 index 0000000..e69de29 From 17313aba31f2c431f5c8907d7dcaaa92d68ee7d5 Mon Sep 17 00:00:00 2001 From: maxday Date: Mon, 1 Nov 2021 16:09:12 -0400 Subject: [PATCH 2/8] add metric support --- build.gradle | 3 +- .../datadog_lambda_java/CustomMetric.java | 12 +++++ .../datadog_lambda_java/DDLambda.java | 6 +++ .../datadog_lambda_java/Extension.java | 2 +- .../datadog_lambda_java/MetricWriter.java | 30 ++++++++++++ .../datadog_lambda_java/CustomMetricTest.java | 48 +++++++++++++++++++ 6 files changed, 99 insertions(+), 2 deletions(-) diff --git a/build.gradle b/build.gradle index 84ea3b0..07a1f7b 100644 --- a/build.gradle +++ b/build.gradle @@ -34,7 +34,8 @@ dependencies { implementation 'org.jetbrains:annotations:15.0' implementation 'io.opentracing:opentracing-api:0.33.0' implementation 'io.opentracing:opentracing-util:0.33.0' - implementation 'com.datadoghq:dd-trace-api:0.72.0' + implementation 'com.datadoghq:dd-trace-api:0.89.0' + implementation 'com.datadoghq:java-dogstatsd-client:2.13.0' // Use JUnit test framework testImplementation 'junit:junit:4.12' diff --git a/src/main/java/com/datadoghq/datadog_lambda_java/CustomMetric.java b/src/main/java/com/datadoghq/datadog_lambda_java/CustomMetric.java index 75b61dd..236daa1 100644 --- a/src/main/java/com/datadoghq/datadog_lambda_java/CustomMetric.java +++ b/src/main/java/com/datadoghq/datadog_lambda_java/CustomMetric.java @@ -56,6 +56,18 @@ public void write(){ MetricWriter writer = MetricWriter.getMetricWriterImpl(); writer.write(this); } + + public String getName() { + return this.name; + } + + public double getValue() { + return this.value; + } + + public Map getTags() { + return this.tags; + } } class PersistedCustomMetric{ diff --git a/src/main/java/com/datadoghq/datadog_lambda_java/DDLambda.java b/src/main/java/com/datadoghq/datadog_lambda_java/DDLambda.java index bc28810..e18351a 100644 --- a/src/main/java/com/datadoghq/datadog_lambda_java/DDLambda.java +++ b/src/main/java/com/datadoghq/datadog_lambda_java/DDLambda.java @@ -50,6 +50,11 @@ public class DDLambda { */ private DDLambda() { this.shouldUseExtension = Extension.setup(); + if(this.shouldUseExtension) { + DDLogger.getLoggerImpl().debug("Setting the writer to extension"); + ExtensionMetricWriter emw = new ExtensionMetricWriter(); + MetricWriter.setMetricWriter(emw); + } } /** @@ -360,6 +365,7 @@ private void recordEnhanced(String basename, Context cxt) { metricName = ENHANCED_PREFIX + basename; tags = EnhancedMetric.makeTagsFromContext(cxt); } + new CustomMetric(metricName, 1, tags).write(); } diff --git a/src/main/java/com/datadoghq/datadog_lambda_java/Extension.java b/src/main/java/com/datadoghq/datadog_lambda_java/Extension.java index b5eb6eb..4bd29a3 100644 --- a/src/main/java/com/datadoghq/datadog_lambda_java/Extension.java +++ b/src/main/java/com/datadoghq/datadog_lambda_java/Extension.java @@ -17,7 +17,7 @@ public static boolean setup() { boolean shouldUseExtension = false; if(isExtensionRuning(EXTENSION_PATH)) { DDLogger.getLoggerImpl().debug("Extension has been detected"); - if(hitHelloRoute(AGENT_URL, FLUSH_PATH)) { + if(hitHelloRoute(AGENT_URL, HELLO_PATH)) { shouldUseExtension = true; } else { DDLogger.getLoggerImpl().debug("Impossible to call the hello route"); diff --git a/src/main/java/com/datadoghq/datadog_lambda_java/MetricWriter.java b/src/main/java/com/datadoghq/datadog_lambda_java/MetricWriter.java index 415f335..b912e14 100644 --- a/src/main/java/com/datadoghq/datadog_lambda_java/MetricWriter.java +++ b/src/main/java/com/datadoghq/datadog_lambda_java/MetricWriter.java @@ -1,5 +1,7 @@ package com.datadoghq.datadog_lambda_java; +import com.timgroup.statsd.NonBlockingStatsDClientBuilder; +import com.timgroup.statsd.StatsDClient; abstract class MetricWriter { private static MetricWriter IMPL; @@ -33,3 +35,31 @@ public void write(CustomMetric cm){ public void flush(){} } +class ExtensionMetricWriter extends MetricWriter{ + + private StatsDClient client; + + public ExtensionMetricWriter() { + this.client = new NonBlockingStatsDClientBuilder() + .prefix("") + .hostname("127.0.0.1") + .port(8125) + .build(); + } + + @Override + public void write(CustomMetric cm){ + StringBuilder tagsSb = new StringBuilder(); + if (cm.getTags() != null) { + cm.getTags().forEach((k, val) -> + tagsSb.append(k.toLowerCase()) + .append(":") + .append(val.toString().toLowerCase())); + } + client.distribution(cm.getName(), cm.getValue(), tagsSb.toString()); + } + + @Override + public void flush(){} +} + diff --git a/src/test/java/com/datadoghq/datadog_lambda_java/CustomMetricTest.java b/src/test/java/com/datadoghq/datadog_lambda_java/CustomMetricTest.java index 9afd959..8133039 100644 --- a/src/test/java/com/datadoghq/datadog_lambda_java/CustomMetricTest.java +++ b/src/test/java/com/datadoghq/datadog_lambda_java/CustomMetricTest.java @@ -5,9 +5,17 @@ import org.junit.Test; +import java.io.IOException; +import java.net.DatagramPacket; +import java.net.DatagramSocket; +import java.net.InetAddress; +import java.net.SocketException; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; +import java.util.HashMap; +import java.util.LinkedHashMap; +import java.util.Map; import static org.junit.Assert.*; @@ -43,6 +51,46 @@ public class CustomMetricTest { assertEquals("{\"m\":\"foo\",\"v\":24.3,\"t\":[],\"e\":1559152800}",smw.getMetricsWritten()); } + @Test public void testExtensionMetricWriter() { + Map map = new LinkedHashMap<>(); // to save the order to avoid flaky test + map.put("firstTag", "firstTagValue"); + map.put("secondTag", 100.34); + CustomMetric ddm = new CustomMetric("foo", 24.3, map); + ExtensionMetricWriter emw = new ExtensionMetricWriter(); + MetricWriter.setMetricWriter(emw); + final String[] text = new String[1]; + + new Thread(new Runnable() { + public void run() { + byte[] msg = new byte[256]; + DatagramPacket dp = new DatagramPacket(msg, msg.length); + DatagramSocket ds = null; + try { + ds = new DatagramSocket(8125); + ds.receive(dp); + text[0] = new String(dp.getData()); + } catch (SocketException e) { + e.printStackTrace(); + } catch (IOException e) { + e.printStackTrace(); + } finally { + if (ds != null) { + ds.close(); + } + } + } + }).start(); + + ddm.write(); + + try { + Thread.sleep(1000); + assertTrue(text[0].startsWith("foo:24.3|d|#firsttag:firsttagvaluesecondtag:100.34")); + } catch (InterruptedException e) { + fail(); + } + } + /** * For tests! */ From 0c8ec9d41313f4482c845b78ebf3ece344dd63b2 Mon Sep 17 00:00:00 2001 From: maxday Date: Mon, 1 Nov 2021 16:33:25 -0400 Subject: [PATCH 3/8] pr comments --- build.gradle | 2 +- .../java/com/datadoghq/datadog_lambda_java/DDLambda.java | 3 +-- .../java/com/datadoghq/datadog_lambda_java/Extension.java | 8 ++++---- 3 files changed, 6 insertions(+), 7 deletions(-) diff --git a/build.gradle b/build.gradle index 07a1f7b..a5bb0cd 100644 --- a/build.gradle +++ b/build.gradle @@ -44,7 +44,7 @@ dependencies { testImplementation 'com.github.stefanbirkner:system-rules:1.19.0' testImplementation 'com.amazonaws:aws-java-sdk-kinesis:1.11.980' - // Use wiremock for stubing http calls + // Use wiremock for stubbing http calls testCompile 'com.github.tomakehurst:wiremock-jre8:2.31.0' } diff --git a/src/main/java/com/datadoghq/datadog_lambda_java/DDLambda.java b/src/main/java/com/datadoghq/datadog_lambda_java/DDLambda.java index e18351a..141de66 100644 --- a/src/main/java/com/datadoghq/datadog_lambda_java/DDLambda.java +++ b/src/main/java/com/datadoghq/datadog_lambda_java/DDLambda.java @@ -364,9 +364,8 @@ private void recordEnhanced(String basename, Context cxt) { if (this.enhanced) { metricName = ENHANCED_PREFIX + basename; tags = EnhancedMetric.makeTagsFromContext(cxt); + new CustomMetric(metricName, 1, tags).write(); } - - new CustomMetric(metricName, 1, tags).write(); } /** diff --git a/src/main/java/com/datadoghq/datadog_lambda_java/Extension.java b/src/main/java/com/datadoghq/datadog_lambda_java/Extension.java index 4bd29a3..b2d471c 100644 --- a/src/main/java/com/datadoghq/datadog_lambda_java/Extension.java +++ b/src/main/java/com/datadoghq/datadog_lambda_java/Extension.java @@ -15,12 +15,12 @@ class Extension { public static boolean setup() { boolean shouldUseExtension = false; - if(isExtensionRuning(EXTENSION_PATH)) { + if(isExtensionRunning(EXTENSION_PATH)) { DDLogger.getLoggerImpl().debug("Extension has been detected"); if(hitHelloRoute(AGENT_URL, HELLO_PATH)) { shouldUseExtension = true; } else { - DDLogger.getLoggerImpl().debug("Impossible to call the hello route"); + DDLogger.getLoggerImpl().debug("Could not call the hello route"); } } return shouldUseExtension; @@ -28,11 +28,11 @@ public static boolean setup() { public static void flush() { if(!hitFlushRoute(AGENT_URL, FLUSH_PATH)) { - DDLogger.getLoggerImpl().debug("Impossible to flush"); + DDLogger.getLoggerImpl().debug("Could not call the flush routeg"); } } - public static boolean isExtensionRuning(final String extensionPath) { + public static boolean isExtensionRunning(final String extensionPath) { File f = new File(extensionPath); return (f.exists() && !f.isDirectory()); } From d973a47904b5b071b6a1d37bc6a213b28f573faf Mon Sep 17 00:00:00 2001 From: maxday Date: Mon, 1 Nov 2021 16:38:09 -0400 Subject: [PATCH 4/8] tests --- .../java/com/datadoghq/datadog_lambda_java/ExtensionTest.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/test/java/com/datadoghq/datadog_lambda_java/ExtensionTest.java b/src/test/java/com/datadoghq/datadog_lambda_java/ExtensionTest.java index 8279144..51a49bf 100644 --- a/src/test/java/com/datadoghq/datadog_lambda_java/ExtensionTest.java +++ b/src/test/java/com/datadoghq/datadog_lambda_java/ExtensionTest.java @@ -19,11 +19,11 @@ public class ExtensionTest { @Test public void testDetectExtensionSuccess() { Path resourceDirectory = Paths.get("src","test","resources"); String fakeExtensionPath = resourceDirectory.toFile().getAbsolutePath() + "/fakeExtension"; - Assert.assertTrue(Extension.isExtensionRuning(fakeExtensionPath)); + Assert.assertTrue(Extension.isExtensionRunning(fakeExtensionPath)); } @Test public void testDetectExtensionFailure() { String invalidPath = "/fakeExtension"; - Assert.assertFalse(Extension.isExtensionRuning(invalidPath)); + Assert.assertFalse(Extension.isExtensionRunning(invalidPath)); } @Test public void testHitHelloRouteIncorrectUrl() { From 4f98806e30436f65d3be08e7b108ed90a4d76e05 Mon Sep 17 00:00:00 2001 From: maxday Date: Mon, 1 Nov 2021 17:27:03 -0400 Subject: [PATCH 5/8] pr comment public -> protected --- .../java/com/datadoghq/datadog_lambda_java/Extension.java | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/main/java/com/datadoghq/datadog_lambda_java/Extension.java b/src/main/java/com/datadoghq/datadog_lambda_java/Extension.java index b2d471c..a32a05e 100644 --- a/src/main/java/com/datadoghq/datadog_lambda_java/Extension.java +++ b/src/main/java/com/datadoghq/datadog_lambda_java/Extension.java @@ -26,18 +26,18 @@ public static boolean setup() { return shouldUseExtension; } - public static void flush() { + protected static void flush() { if(!hitFlushRoute(AGENT_URL, FLUSH_PATH)) { DDLogger.getLoggerImpl().debug("Could not call the flush routeg"); } } - public static boolean isExtensionRunning(final String extensionPath) { + protected static boolean isExtensionRunning(final String extensionPath) { File f = new File(extensionPath); return (f.exists() && !f.isDirectory()); } - public static boolean hitHelloRoute(final String agentUrl, final String helloPath) { + protected static boolean hitHelloRoute(final String agentUrl, final String helloPath) { try { URL url = new URL(agentUrl + helloPath); HttpURLConnection http = (HttpURLConnection) url.openConnection(); @@ -49,7 +49,7 @@ public static boolean hitHelloRoute(final String agentUrl, final String helloPat } } - public static boolean hitFlushRoute(final String agentUrl, final String flushPath) { + protected static boolean hitFlushRoute(final String agentUrl, final String flushPath) { try { URL url = new URL(agentUrl + flushPath); HttpURLConnection http = (HttpURLConnection) url.openConnection(); From 2c3f76a8742b768bbdf797db087f043dcd41d871 Mon Sep 17 00:00:00 2001 From: maxday Date: Mon, 1 Nov 2021 17:30:21 -0400 Subject: [PATCH 6/8] pr comment public -> protected --- src/main/java/com/datadoghq/datadog_lambda_java/Extension.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/com/datadoghq/datadog_lambda_java/Extension.java b/src/main/java/com/datadoghq/datadog_lambda_java/Extension.java index a32a05e..8313e24 100644 --- a/src/main/java/com/datadoghq/datadog_lambda_java/Extension.java +++ b/src/main/java/com/datadoghq/datadog_lambda_java/Extension.java @@ -13,7 +13,7 @@ class Extension { private final static String FLUSH_PATH = "/lambda/flush"; private final static String EXTENSION_PATH = "/opt/extensions/datadog-agent"; - public static boolean setup() { + protected static boolean setup() { boolean shouldUseExtension = false; if(isExtensionRunning(EXTENSION_PATH)) { DDLogger.getLoggerImpl().debug("Extension has been detected"); From 830dbd58b3ccbe1917acda1fb16718eb3382d63c Mon Sep 17 00:00:00 2001 From: maxday Date: Mon, 1 Nov 2021 17:42:09 -0400 Subject: [PATCH 7/8] prevent flaky test --- .../datadog_lambda_java/CustomMetricTest.java | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/src/test/java/com/datadoghq/datadog_lambda_java/CustomMetricTest.java b/src/test/java/com/datadoghq/datadog_lambda_java/CustomMetricTest.java index 8133039..455f047 100644 --- a/src/test/java/com/datadoghq/datadog_lambda_java/CustomMetricTest.java +++ b/src/test/java/com/datadoghq/datadog_lambda_java/CustomMetricTest.java @@ -66,6 +66,7 @@ public void run() { DatagramPacket dp = new DatagramPacket(msg, msg.length); DatagramSocket ds = null; try { + text[0] = "notYetReceived"; ds = new DatagramSocket(8125); ds.receive(dp); text[0] = new String(dp.getData()); @@ -83,10 +84,20 @@ public void run() { ddm.write(); - try { - Thread.sleep(1000); - assertTrue(text[0].startsWith("foo:24.3|d|#firsttag:firsttagvaluesecondtag:100.34")); - } catch (InterruptedException e) { + int i = 0; + for(; i < 10; ++i) { + try { + if (text[0].equals("notYetReceived")) { + Thread.sleep(1000); + } else { + assertTrue(text[0].startsWith("foo:24.3|d|#firsttag:firsttagvaluesecondtag:100.34")); + break; + } + } catch (InterruptedException e) { + fail(); + } + } + if( i == 10) { fail(); } } From ab2e6e23180db74f1755a559e02ab3e9f62a3be8 Mon Sep 17 00:00:00 2001 From: maxday Date: Mon, 1 Nov 2021 17:48:09 -0400 Subject: [PATCH 8/8] null check in tests --- .../com/datadoghq/datadog_lambda_java/CustomMetricTest.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/test/java/com/datadoghq/datadog_lambda_java/CustomMetricTest.java b/src/test/java/com/datadoghq/datadog_lambda_java/CustomMetricTest.java index 455f047..a1ba4eb 100644 --- a/src/test/java/com/datadoghq/datadog_lambda_java/CustomMetricTest.java +++ b/src/test/java/com/datadoghq/datadog_lambda_java/CustomMetricTest.java @@ -87,7 +87,7 @@ public void run() { int i = 0; for(; i < 10; ++i) { try { - if (text[0].equals("notYetReceived")) { + if (null== text[0] || text[0].equals("notYetReceived")) { Thread.sleep(1000); } else { assertTrue(text[0].startsWith("foo:24.3|d|#firsttag:firsttagvaluesecondtag:100.34"));