Skip to content

Commit

Permalink
added method
Browse files Browse the repository at this point in the history
  • Loading branch information
antobaldu committed Aug 18, 2020
1 parent 5b29481 commit cae1b89
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 9 deletions.
26 changes: 20 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
@@ -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

```
<dependency>
<groupId>com.github.postfinance.prometheus</groupId>
<artifactId>apache-exporter</artifactId>
<version>1.0.0</version>
<version>VERSION</version>
</dependency>
```

### 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.
Expand Down Expand Up @@ -68,3 +74,11 @@ apache_workers{state="busy"} 2
apache_workers{state="idle"} 6
```

* Second method

```
public ArrayList<Collector.MetricFamilySamples> exportSamplesList()
```

It returns a list of samples of type Collector.MetricFamilySamples
16 changes: 16 additions & 0 deletions src/main/java/ch/postfinance/prometheus/ApacheExporter.java
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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 {

Expand Down Expand Up @@ -107,6 +110,19 @@ public String export() throws IOException {
return new String(stream.toByteArray());
}

public ArrayList<Collector.MetricFamilySamples> 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(":");
Expand Down
34 changes: 31 additions & 3 deletions src/test/java/ch/postfinance/prometheus/ApacheExporterTest.java
Original file line number Diff line number Diff line change
@@ -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.*;

Expand All @@ -21,20 +23,46 @@ public void export() {
}
}

@Test
public void exportList() {
ApacheExporter exporter = new ApacheExporter("http://fakehost/status");

List<Collector.MetricFamilySamples> 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());


}
Expand Down

0 comments on commit cae1b89

Please sign in to comment.