This application shows how to export metrics from DataStax Java driver 4.x to Prometheus via Prometheus Java Client.
Contributors: Alex Ott
- To demonstrate how to configure the DataStax Java Driver 4.x to report metrics
- To demonstrate how to configure the Prometheus client to push those metrics to Prometheus
A list of key files within this repo and a short 1-2 sentence description of why they are important to the project
e.g.
- MetricsWithPrometheus - The main application file which contains all the logic to switch between the configurations
To configure the Prometheus Java Client you need to add the following dependencies to your pom.xml file.
<dependency>
<groupId>io.prometheus</groupId>
<artifactId>simpleclient_dropwizard</artifactId>
<version>${prometheus.version}</version>
</dependency>
<dependency>
<groupId>io.prometheus</groupId>
<artifactId>simpleclient_common</artifactId>
<version>${prometheus.version}</version>
</dependency>
<dependency>
<groupId>io.prometheus</groupId>
<artifactId>simpleclient_hotspot</artifactId>
<version>${prometheus.version}</version>
</dependency>
<dependency>
<groupId>io.prometheus</groupId>
<artifactId>simpleclient_httpserver</artifactId>
<version>${prometheus.version}</version>
</dependency>
Once that is added exporting of Java driver metrics is straightforward.
To begin exporting our metrics we just need to add following lines to our Session
object after we :
MetricRegistry registry = session.getMetrics()
.orElseThrow(() -> new IllegalStateException("Metrics are disabled"))
.getRegistry();
CollectorRegistry.defaultRegistry.register(new DropwizardExports(registry));
This then expose metrics to Prometheus by specific implementation - this example uses
Prometheus's HTTPServer
, running on the port 9095 (overridable via prometheusPort
Java
property).
The prerequisites required for this application to run are:
e.g.
- A DSE Cluster
- A Prometheus server (Install instruction are available here)
Run example with following command:
mvn clean compile exec:java -Dexec.mainClass="com.datastax.alexott.demos.MetricsWithPrometheus" \
-DcontactPoint=XX.XX.XX.XX -DdcName=dc1
You need to pass contact point & data center name as Java properties (contactPoint
and
dcName
correspondingly)