Skip to content

Commit 4b71408

Browse files
authored
add metrics example (#578)
* add metrics example * correct for metrics in arguments
1 parent e7e76cc commit 4b71408

File tree

3 files changed

+93
-11
lines changed

3 files changed

+93
-11
lines changed

examples/troubleshooting/README.md

+21-11
Original file line numberDiff line numberDiff line change
@@ -16,26 +16,36 @@ dependencies will allow for easy compilation
1616

1717
(Manual compilation would require the jars and their dependencies downloaded, and then javac -cp <list of jars> path-to-java-file.)
1818

19-
# Traces
19+
# Traces, Metrics, Logs
2020

21-
The [`TestOtelSdkTrace`](./src/main/java/elastic/troubleshooting/TestOtelSdkTrace.java) class is a standalone class that creates
22-
a span (named `test span`) in a service that you name. It takes
23-
three arguments
21+
The examples in this section all take the same three arguments:
2422
1. The name of the service you want to have displayed in the APM UI
2523
2. The endpoint to send traces to, normally the Elastic APM server or the OpenTelemetry collector.
26-
The url would typically look like `http://localhost:4318/v1/traces` or `https://somewhere:443/v1/traces`
27-
but if `/v1/traces` is missing from the argument, it is added to the endpoint
28-
3. The secret token or apikey in the format secret:<token> or apikey:<apikey>.
29-
If the argument doesn't start with neither `secret:` nor `apikey:`, then the full argument is assumed to be a secret token
24+
The url would typically look like `http://localhost:4318/v1/traces` or `https://somewhere:443/v1/traces`
25+
but if `/v1/traces` is missing from the argument, it is added to the endpoint
26+
- for metrics, replace `/traces` with `/metrics` in the above paths
27+
3. The secret token or apikey in the format secret:<token> or apikey:<apikey>.
28+
If the argument doesn't start with neither `secret:` nor `apikey:`, then the full argument is
29+
assumed to be a secret token
30+
31+
## Traces
32+
33+
The [`TestOtelSdkTrace`](./src/main/java/elastic/troubleshooting/TestOtelSdkTrace.java) class is a standalone class that creates
34+
a span (named `test span`) in a service that you name.
3035

3136
After running the class, you should see the trace in the APM UI, eg with service name set to `test1` and
3237
correct endpoint and token, you should see something similar to
3338
![this](images\test-trace.png)
3439

35-
# Metrics
40+
## Metrics
3641

37-
To do
42+
The [`TestOtelSdkMetrics`](./src/main/java/elastic/troubleshooting/TestOtelSdkMetrics.java) class is a standalone class that generates
43+
metrics for `jvm.thread.count` for a little over 3 minutes in a service that you name.
44+
45+
After running the class, you should see the metrics in the APM UI, eg with service name set to `test1` and
46+
correct endpoint and token, you should see something similar to
47+
![this](images\test-metrics.png)
3848

39-
# Logs
49+
## Logs
4050

4151
To do
Loading
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
package elastic.troubleshooting;
2+
3+
import java.util.concurrent.TimeUnit;
4+
5+
import io.opentelemetry.api.metrics.LongCounter;
6+
import io.opentelemetry.api.metrics.Meter;
7+
import io.opentelemetry.exporter.otlp.http.metrics.OtlpHttpMetricExporter;
8+
import io.opentelemetry.sdk.OpenTelemetrySdk;
9+
import io.opentelemetry.sdk.metrics.SdkMeterProvider;
10+
import io.opentelemetry.sdk.metrics.export.MetricReader;
11+
import io.opentelemetry.sdk.metrics.export.PeriodicMetricReader;
12+
import io.opentelemetry.sdk.resources.Resource;
13+
14+
public class TestOtelSdkMetrics {
15+
16+
public static void main(String[] args) throws Exception {
17+
String servicename = args[0];
18+
String endpoint = args[1];
19+
String secretOrApikey = args[2];
20+
if (!endpoint.endsWith("/v1/metrics")) {
21+
if (endpoint.endsWith("/")) {
22+
endpoint = endpoint + "v1/metrics";
23+
} else {
24+
endpoint = endpoint + "/v1/metrics";
25+
}
26+
}
27+
boolean isSecret = true;
28+
if (secretOrApikey.startsWith("secret:")) {
29+
secretOrApikey = secretOrApikey.substring(7);
30+
} else if (secretOrApikey.startsWith("apikey:")) {
31+
secretOrApikey = secretOrApikey.substring(7);
32+
isSecret = false;
33+
}
34+
35+
System.out.println("Starting test ... initializing OpenTelemetry");
36+
37+
Resource resource = Resource.getDefault().toBuilder().put("service.name", servicename).build();
38+
39+
OtlpHttpMetricExporter metricExporter = OtlpHttpMetricExporter.builder().setEndpoint(endpoint)
40+
.addHeader("Authorization", (isSecret ? "Bearer " : "ApiKey ") + secretOrApikey).build();
41+
42+
MetricReader metricReader = PeriodicMetricReader.builder(metricExporter)
43+
.setInterval(3, TimeUnit.SECONDS)
44+
.build();
45+
46+
SdkMeterProvider meterProvider = SdkMeterProvider.builder().registerMetricReader(metricReader)
47+
.setResource(resource).build();
48+
49+
OpenTelemetrySdk openTelemetry = OpenTelemetrySdk.builder().setMeterProvider(meterProvider)
50+
.build();
51+
52+
System.out.println("Starting test ... creating metrics");
53+
54+
Meter meter = openTelemetry.getMeter("TestOtelSdktrace");
55+
LongCounter counter = meter.counterBuilder("jvm.thread.count").build();
56+
for (int i = 0; i < 100; i++) {
57+
counter.add(3);
58+
Thread.sleep(1000L);
59+
}
60+
for (int i = 0; i < 100; i++) {
61+
counter.add(0);
62+
Thread.sleep(1000L);
63+
}
64+
65+
openTelemetry.shutdown().whenComplete(() -> {
66+
System.out.println("Ending test ... shutdown OpenTelemetry");
67+
});
68+
69+
Thread.sleep(60_000L);
70+
System.out.println("Ending test ... terminating the JVM");
71+
}
72+
}

0 commit comments

Comments
 (0)