Skip to content

Instantly share code, notes, and snippets.

@breedx-splk
Last active November 15, 2023 21:54
Show Gist options
  • Save breedx-splk/246962f7d1c180f924a530630a966c9f to your computer and use it in GitHub Desktop.
Save breedx-splk/246962f7d1c180f924a530630a966c9f to your computer and use it in GitHub Desktop.
In another window, run `nc -l 8000` (mac) or `nc -l -p 8000` (linux) to listen on port 8000. Then run this class with the otel java agent.
package io.opentelemetry.javaagent;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.concurrent.TimeUnit;
import static java.nio.charset.StandardCharsets.UTF_8;
public class HttpUrlConnectionCheck {
private HttpUrlConnectionCheck() {}
public static void main(String[] args) throws Exception {
while (true) {
doPost();
TimeUnit.SECONDS.sleep(5);
}
}
private static void doPost() throws Exception {
// URL url = new URL("http://example.com");
URL url = new URL("http://localhost:8000");
System.out.println("Opening connection...");
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setDoOutput(true);
conn.setRequestMethod("POST");
OutputStream out = conn.getOutputStream();
OutputStreamWriter writer = new OutputStreamWriter(out, UTF_8);
writer.write("this is a test\n\n");
writer.flush();
writer.close();
out.close();
System.out.println("All done.");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment