diff --git a/README.md b/README.md
index 5ba7112..1e6c21f 100644
--- a/README.md
+++ b/README.md
@@ -1,40 +1,46 @@
# Apache Exporter for Prometheus
Exports server statistics provided by mod_status of an Apache HTTP server to Prometheus metrics format
+## Usage
+
It can be built with:
```
mvn clean install
```
-## Usage
-
It can be importer in your Maven project with
```
com.github.postfinance.prometheus
apache-exporter
- 1.0.0
+ VERSION
```
+### ModStatus URL
+
The Apache ModStatus URL where the metrics are read, can be configured with
+* A parameter in the ApacheExporter constructor
* A System Property: httpdModStatusUrl
* An Environment Property: HTTPD_MOD_STATUS_URL
The default value is http://localhost/server-status?auto
-To obtain the metrics, use the method:
+### Apache HTTPD Metrics
+
+To obtain the metrics for apache, an istance of the ApacheExporter class should be created
+and one of the following methods called:
+* First method
```
public String export() throws IOException
```
-
-It returns a String in the format:
+returns a String in the format:
```
# HELP apache_exporter_build_info A metric with a constant '1' value labeled by version, revision, branch, and goversion from which apache_exporter was built.
@@ -68,3 +74,11 @@ apache_workers{state="busy"} 2
apache_workers{state="idle"} 6
```
+
+* Second method
+
+```
+public ArrayList exportSamplesList()
+```
+
+It returns a list of samples of type Collector.MetricFamilySamples
diff --git a/src/main/java/ch/postfinance/prometheus/ApacheExporter.java b/src/main/java/ch/postfinance/prometheus/ApacheExporter.java
index 1cbe3be..f8a502e 100644
--- a/src/main/java/ch/postfinance/prometheus/ApacheExporter.java
+++ b/src/main/java/ch/postfinance/prometheus/ApacheExporter.java
@@ -1,5 +1,6 @@
package ch.postfinance.prometheus;
+import io.prometheus.client.Collector;
import io.prometheus.client.CollectorRegistry;
import io.prometheus.client.Counter;
import io.prometheus.client.Gauge;
@@ -14,6 +15,8 @@
import java.net.http.HttpResponse;
import java.time.Duration;
import java.time.Instant;
+import java.util.ArrayList;
+import java.util.Collections;
public class ApacheExporter {
@@ -107,6 +110,19 @@ public String export() throws IOException {
return new String(stream.toByteArray());
}
+ public ArrayList exportSamplesList() {
+ try {
+ mapStatusToMetrics(readApacheStatus());
+ serverUpGauge.set(1);
+ } catch (InterruptedException | IOException e) {
+ scrapeErrorsTotal.inc();
+ serverUpGauge.set(0);
+ }
+
+ return Collections.list(registry.metricFamilySamples());
+
+ }
+
private void mapStatusToMetrics(String statusData) {
statusData.lines().parallel().forEach(line -> {
String[] elems = line.split(":");
diff --git a/src/test/java/ch/postfinance/prometheus/ApacheExporterTest.java b/src/test/java/ch/postfinance/prometheus/ApacheExporterTest.java
index 2c0afe7..705b601 100644
--- a/src/test/java/ch/postfinance/prometheus/ApacheExporterTest.java
+++ b/src/test/java/ch/postfinance/prometheus/ApacheExporterTest.java
@@ -1,8 +1,10 @@
package ch.postfinance.prometheus;
+import io.prometheus.client.Collector;
import org.junit.Test;
import java.io.IOException;
+import java.util.List;
import static org.junit.Assert.*;
@@ -21,20 +23,46 @@ public void export() {
}
}
+ @Test
+ public void exportList() {
+ ApacheExporter exporter = new ApacheExporter("http://fakehost/status");
+
+ List mfsList;
+
+ try {
+ for (int i = 0; i < 5; i++) {
+ mfsList = exporter.exportSamplesList();
+
+ for(Collector.MetricFamilySamples element : mfsList) {
+ if(element.name.equals("apache_scrape_errors_total")){
+ for(Collector.MetricFamilySamples.Sample sample : element.samples){
+ assertEquals(i+1, (int)sample.value);
+ }
+ }
+
+ }
+
+ Thread.sleep(1000);
+ }
+ } catch (InterruptedException e) {
+ fail(e.getMessage());
+ }
+ }
+
@Test
public void testProperty(){
ApacheExporter exporter = new ApacheExporter();
- assertEquals(exporter.getStatusUrl(), "http://localhost/server-status?auto");
+ assertEquals( "http://localhost/server-status?auto", exporter.getStatusUrl());
System.setProperty("httpdModStatusUrl", "http://e1-xxx-alsu001/server-status?auto");
- assertEquals(exporter.getStatusUrl(), "http://e1-xxx-alsu001/server-status?auto");
+ assertEquals("http://e1-xxx-alsu001/server-status?auto", exporter.getStatusUrl());
exporter = new ApacheExporter("http://host:port/midw-status");
- assertEquals(exporter.getStatusUrl(), "http://host:port/midw-status");
+ assertEquals( "http://host:port/midw-status", exporter.getStatusUrl());
}