-
Notifications
You must be signed in to change notification settings - Fork 14
Support for the extension - Add hello and flush routes and metric support #61
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
8964bd2
support hello and flush route
maxday 17313ab
add metric support
maxday 0c8ec9d
pr comments
maxday d973a47
tests
maxday 4f98806
pr comment public -> protected
maxday 2c3f76a
pr comment public -> protected
maxday 830dbd5
prevent flaky test
maxday ab2e6e2
null check in tests
maxday File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
65 changes: 65 additions & 0 deletions
65
src/main/java/com/datadoghq/datadog_lambda_java/Extension.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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"; | ||
|
|
||
| protected static boolean setup() { | ||
| boolean shouldUseExtension = false; | ||
| if(isExtensionRunning(EXTENSION_PATH)) { | ||
| DDLogger.getLoggerImpl().debug("Extension has been detected"); | ||
| if(hitHelloRoute(AGENT_URL, HELLO_PATH)) { | ||
| shouldUseExtension = true; | ||
| } else { | ||
| DDLogger.getLoggerImpl().debug("Could not call the hello route"); | ||
| } | ||
| } | ||
| return shouldUseExtension; | ||
| } | ||
|
|
||
| protected static void flush() { | ||
| if(!hitFlushRoute(AGENT_URL, FLUSH_PATH)) { | ||
| DDLogger.getLoggerImpl().debug("Could not call the flush routeg"); | ||
| } | ||
| } | ||
|
|
||
| protected static boolean isExtensionRunning(final String extensionPath) { | ||
| File f = new File(extensionPath); | ||
| return (f.exists() && !f.isDirectory()); | ||
| } | ||
|
|
||
| protected 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; | ||
| } | ||
| } | ||
|
|
||
| protected 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; | ||
| } | ||
| } | ||
|
|
||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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,57 @@ public class CustomMetricTest { | |
| assertEquals("{\"m\":\"foo\",\"v\":24.3,\"t\":[],\"e\":1559152800}",smw.getMetricsWritten()); | ||
| } | ||
|
|
||
| @Test public void testExtensionMetricWriter() { | ||
| Map<String, Object> 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 { | ||
| text[0] = "notYetReceived"; | ||
| 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(); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. awesome |
||
| } | ||
| } | ||
| } | ||
| }).start(); | ||
|
|
||
| ddm.write(); | ||
|
|
||
| int i = 0; | ||
| for(; i < 10; ++i) { | ||
| try { | ||
| if (null== text[0] || 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(); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * For tests! | ||
| */ | ||
|
|
||
57 changes: 57 additions & 0 deletions
57
src/test/java/com/datadoghq/datadog_lambda_java/ExtensionTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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.isExtensionRunning(fakeExtensionPath)); | ||
| } | ||
| @Test public void testDetectExtensionFailure() { | ||
| String invalidPath = "/fakeExtension"; | ||
| Assert.assertFalse(Extension.isExtensionRunning(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")); | ||
| } | ||
|
|
||
| } |
Empty file.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nice