|
1 | 1 | # Using Testcontainers |
2 | 2 |
|
3 | | -The provided code demonstrates how to use **Testcontainers** with **Grafana's LGTM stack** to test OpenTelemetry metrics in a Java application. Here's a step-by-step explanation: |
| 3 | +The provided code demonstrates how to use **Testcontainers** with **Grafana's LGTM stack** to test OpenTelemetry metrics |
| 4 | +in a Java application. Here's a step-by-step explanation: |
4 | 5 |
|
5 | 6 | 1. **Set Up the Testcontainers Environment**: |
6 | 7 |
|
7 | | - - The `@Testcontainers` annotation enables the Testcontainers extension for JUnit 5. |
8 | | - - The `@Container` annotation is used to define a `LgtmStackContainer` that runs the Grafana LGTM stack in a Docker container. |
| 8 | +- The `@Testcontainers` annotation enables the Testcontainers extension for JUnit 5. |
| 9 | +- The `@Container` annotation is used to define a `LgtmStackContainer` that runs the Grafana LGTM stack in a Docker |
| 10 | + container. |
9 | 11 |
|
10 | 12 | 2. **Configure OpenTelemetry**: |
11 | 13 |
|
12 | | - - In the `@BeforeEach` method, system properties are set to configure the OpenTelemetry exporter to send metrics to the LGTM stack running in the container. |
| 14 | +- In the `@BeforeEach` method, system properties are set to configure the OpenTelemetry exporter to send metrics to |
| 15 | + the LGTM stack running in the container. |
13 | 16 |
|
14 | 17 | 3. **Run the Application**: |
15 | 18 |
|
16 | | - - The `OtelApp` class initializes OpenTelemetry and generates a custom metric (`sold_items`) with attributes (e.g., `tenant`). |
| 19 | +- The `OtelApp` class initializes OpenTelemetry and generates a custom metric (`sold_items`) with attributes (e.g., |
| 20 | + `tenant`) as well as a span representing the block the code. |
17 | 21 |
|
18 | | -4. **Test the Metrics Export**: |
| 22 | +4. **Test Exporting Metrics and Traces**: |
19 | 23 |
|
20 | | - - The test method `testExportMetric` runs the application and queries the Prometheus endpoint in the LGTM stack to verify that the metric (`sold_items`) has been exported successfully. |
21 | | - - The `Awaitility` library is used to poll the Prometheus endpoint until the metric is found or a timeout occurs. |
| 24 | +- The test method `testExportMetricsAndTraces` runs the application and queries the Prometheus and Tempo endpoints |
| 25 | + in the LGTM stack to verify that the metric (`sold_items`) and span have been exported successfully. |
| 26 | +- The `Awaitility` library is used to poll the endpoints until the telemetry is found or a timeout occurs. |
22 | 27 |
|
23 | 28 | 5. **Debugging with Grafana**: |
24 | | - - The test outputs the Grafana URL (`lgtm.getGrafanaHttpUrl()`) to the console, allowing you to manually inspect the metrics in the Grafana UI. |
| 29 | + |
| 30 | +- The test outputs the Grafana URL (`lgtm.getGrafanaHttpUrl()`) to the console, allowing you to manually inspect the |
| 31 | + telemetry in the Grafana UI if needed. |
25 | 32 |
|
26 | 33 | ## Example Usage |
27 | 34 |
|
28 | 35 | 1. Start the test using `mvn test`. |
29 | 36 | 2. Check the console output for the Grafana URL. |
30 | | -3. Open the Grafana UI, navigate to the Explore tab, and query the metrics. |
31 | | -4. The test will pass if the metric is successfully exported and found in Prometheus. |
| 37 | +3. Open the Grafana UI, navigate to the Explore tab, and query the metrics or traces. |
| 38 | +4. The test will pass if the metric and span are successfully exported and found in Prometheus and Tempo. |
32 | 39 |
|
33 | | -This setup is useful for validating OpenTelemetry instrumentation and ensuring metrics are correctly exported to a monitoring system. |
| 40 | +This setup is useful for validating OpenTelemetry instrumentation and ensuring metrics are correctly exported to a |
| 41 | +monitoring system. |
0 commit comments