Skip to content

Commit

Permalink
Add support for prometheus listener
Browse files Browse the repository at this point in the history
  • Loading branch information
rabelenda-abstracta committed May 14, 2024
1 parent d5b75ae commit d639a06
Show file tree
Hide file tree
Showing 11 changed files with 3,437 additions and 0 deletions.
1 change: 1 addition & 0 deletions docs/guide/reporting/real-time/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@ When running tests with JMeter (and in particular with jmeter-java-dsl) a usual
<!-- @include: influxdb.md -->
<!-- @include: graphite.md -->
<!-- @include: elasticsearch.md -->
<!-- @include: prometheus.md -->
<!-- @include: datadog.md -->
86 changes: 86 additions & 0 deletions docs/guide/reporting/real-time/prometheus.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
#### Prometheus

As in previous scenarios, you can also use Prometheus and Grafana.

To use the module, you will need to include the following dependency in your project:

:::: code-group
::: code-group-item Maven
```xml
<dependency>
<groupId>us.abstracta.jmeter</groupId>
<artifactId>jmeter-java-dsl-prometheus</artifactId>
<version>1.27</version>
<scope>test</scope>
</dependency>
```
:::
::: code-group-item Gradle
```groovy
testImplementation 'us.abstracta.jmeter:jmeter-java-dsl-prometheus:1.27'
```
:::
::::

And use provided `prometheusListener()` method like in this example:

```java
import static org.assertj.core.api.Assertions.assertThat;
import static us.abstracta.jmeter.javadsl.JmeterDsl.*;
import static us.abstracta.jmeter.javadsl.prometheus.DslPrometheusListener.*;

import java.io.IOException;
import java.time.Duration;
import org.junit.jupiter.api.Test;
import us.abstracta.jmeter.javadsl.core.TestPlanStats;

public class PerformanceTest {

@Test
public void testPerformance() throws IOException {
TestPlanStats stats = testPlan(
threadGroup(2, 10,
httpSampler("http://my.service")
),
prometheusListener()
).run();
assertThat(stats.overall().sampleTimePercentile99()).isLessThan(Duration.ofSeconds(5));
}

}
```

As in previous cases, you can to try it locally by running `docker-compose up` inside [this directory](/docs/guide/reporting/real-time/prometheus). After containers are started, you can follow the same steps as in previous scenarios.

::: warning
Use the provided `docker-compose` settings for local tests only. It uses weak credentials and is not properly configured for production purposes.
:::

Check [DslPrometheusListener](/jmeter-java-dsl-prometheus/src/main/java/us/abstracta/jmeter/javadsl/prometheus/DslPrometheusListener.java) for details on listener settings.

Here is an example that shows the default settings used by `prometheusListener`:

```java
import us.abstracta.jmeter.javadsl.prometheus.DslPrometheusListener.PrometheusMetric;
...
prometheusListener()
.metrics(
PrometheusMetric.responseTime("ResponseTime", "the response time of samplers")
.labels(PrometheusMetric.SAMPLE_LABEL, PrometheusMetric.RESPONSE_CODE)
.quantile(0.75, 0.5)
.quantile(0.95, 0.1)
.quantile(0.99, 0.01)
.maxAge(Duration.ofMinutes(1)),
PrometheusMetric.successRatio("Ratio", "the success ratio of samplers")
.labels(PrometheusMetric.SAMPLE_LABEL, PrometheusMetric.RESPONSE_CODE)
)
.port(9270)
.host("0.0.0.0")
.endWait(Duration.ofSeconds(10))
...
```
> Note that the default settings are different from the used JMeter Prometheus Plugin, to allow easier usage and avoid missing metrics at the end of test plan execution.
::: tip
When configuring the `prometheusListener` always consider setting a `endWait` that is greater thant the Prometheus Server configured `scrape_interval` to avoid missing metrics at the end of test plan execution (e.g.: 2x the scrape interval value).
:::
24 changes: 24 additions & 0 deletions docs/guide/reporting/real-time/prometheus/docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
services:
prometheus:
image: prom/prometheus:v2.51.2
ports:
- '9090:9090'
volumes:
- prometheus-data:/prometheus
- ./prometheus.yml:/etc/prometheus/prometheus.yml
grafana:
image: grafana/grafana:10.4.2
ports:
- '3000:3000'
volumes:
- grafana-data:/var/lib/grafana
- ./grafana-provisioning/:/etc/grafana/provisioning
depends_on:
- prometheus
environment:
- GF_SECURITY_ADMIN_USER=admin
- GF_SECURITY_ADMIN_PASSWORD=1234
- GF_DASHBOARDS_DEFAULT_HOME_DASHBOARD_PATH=/etc/grafana/provisioning/dashboards/jmeter.json
volumes:
prometheus-data:
grafana-data:
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
apiVersion: 1
providers:
- name: JMeter Dashboards
allowUiUpdates: true
options:
path: /etc/grafana/provisioning/dashboards
Loading

0 comments on commit d639a06

Please sign in to comment.