Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use upstream Prometheus client and Spring integration #1609

Merged
merged 5 commits into from
Oct 10, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 14 additions & 4 deletions zipkin-autoconfigure/metrics-prometheus/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
Exposes [Spring Actuator metrics](http://docs.spring.io/spring-boot/docs/current/reference/html/production-ready-metrics.html)
on `/prometheus` using the Prometheus exposition [text format version 0.0.4](https://prometheus.io/docs/instrumenting/exposition_formats/).

All metrics defaults to the `gauge` type, unless a known format is specified by a prefix in the metric name. Currently the only two detected types are `gauge_` and `counter_`.

## Scrape configuration example

```yaml
Expand All @@ -13,6 +11,18 @@ All metrics defaults to the `gauge` type, unless a known format is specified by
metrics_path: '/prometheus'
static_configs:
- targets: ['localhost:9411']

metric_relabel_configs:
# Response code count
- source_labels: [__name__]
regex: '^status_(\d+)_(.*)$'
replacement: '${1}'
target_label: status
- source_labels: [__name__]
regex: '^status_(\d+)_(.*)$'
replacement: '${2}'
target_label: path
- source_labels: [__name__]
regex: '^status_(\d+)_(.*)$'
replacement: 'http_requests_total'
target_label: __name__
```

96 changes: 51 additions & 45 deletions zipkin-autoconfigure/metrics-prometheus/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,50 +14,56 @@
the License.

-->
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>zipkin-autoconfigure</artifactId>
<groupId>io.zipkin.java</groupId>
<version>2.1.1-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>

<artifactId>zipkin-autoconfigure-metrics-prometheus</artifactId>
<name>Auto Configuration: Prometheus Metrics</name>

<properties>
<main.basedir>${project.basedir}/../..</main.basedir>
</properties>

<dependencyManagement>
<dependencies>
<dependency>
<!-- Import dependency management from Spring Boot -->
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>${spring-boot.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>

<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>

<!-- Test dependencies -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>zipkin-autoconfigure</artifactId>
<groupId>io.zipkin.java</groupId>
<version>2.1.1-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>

<artifactId>zipkin-autoconfigure-metrics-prometheus</artifactId>
<name>Auto Configuration: Prometheus Metrics</name>

<properties>
<main.basedir>${project.basedir}/../..</main.basedir>
<prometheus_simpleclient.version>0.0.26</prometheus_simpleclient.version>
</properties>

<dependencies>
<!-- prometheusMetricsFilter is a servlet filter -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.0.1</version>
<scope>provided</scope>
</dependency>

<!-- Prometheus client -->
<dependency>
<groupId>io.prometheus</groupId>
<artifactId>simpleclient</artifactId>
<version>${prometheus_simpleclient.version}</version>
</dependency>
<!-- Prometheus Hotspot JVM metrics-->
<dependency>
<groupId>io.prometheus</groupId>
<artifactId>simpleclient_hotspot</artifactId>
<version>${prometheus_simpleclient.version}</version>
</dependency>
<!-- Expose request times -->
<dependency>
<groupId>io.prometheus</groupId>
<artifactId>simpleclient_servlet</artifactId>
<version>${prometheus_simpleclient.version}</version>
</dependency>
<!-- Prometheus Spring Boot integration -->
<dependency>
<groupId>io.prometheus</groupId>
<artifactId>simpleclient_spring_boot</artifactId>
<version>${prometheus_simpleclient.version}</version>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* Copyright 2015-2016 The OpenZipkin Authors
* Copyright 2015-2017 The OpenZipkin Authors
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
* in compliance with the License. You may obtain a copy of the License at
Expand All @@ -14,89 +14,29 @@

package zipkin.autoconfigure.metrics;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.actuate.endpoint.PublicMetrics;
import org.springframework.boot.actuate.metrics.Metric;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.Collection;
import java.util.regex.Pattern;

@RestController
@RequestMapping("/prometheus")
import io.prometheus.client.hotspot.DefaultExports;
import io.prometheus.client.spring.boot.EnablePrometheusEndpoint;
import io.prometheus.client.spring.boot.EnableSpringBootMetricsCollector;
import io.prometheus.client.spring.web.EnablePrometheusTiming;
import javax.servlet.Filter;
import javax.servlet.ServletException;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@EnablePrometheusEndpoint
@EnableSpringBootMetricsCollector
@EnablePrometheusTiming
@Configuration
public class PrometheusMetricsAutoConfiguration {

private static final Pattern SANITIZE_PREFIX_PATTERN = Pattern.compile("^[^a-zA-Z_]");
private static final Pattern SANITIZE_BODY_PATTERN = Pattern.compile("[^a-zA-Z0-9_]");

private final Collection<PublicMetrics> publicMetrics;

@Autowired
public PrometheusMetricsAutoConfiguration(Collection<PublicMetrics> publicMetrics) {
this.publicMetrics = publicMetrics;
}

private static String sanitizeMetricName(String metricName) {
return SANITIZE_BODY_PATTERN.matcher(
SANITIZE_PREFIX_PATTERN.matcher(metricName).replaceFirst("_")
).replaceAll("_");
}

@RequestMapping(method = RequestMethod.GET)
public ResponseEntity<String> prometheusMetrics() {
StringBuilder sb = new StringBuilder();

for (PublicMetrics publicMetrics : this.publicMetrics) {
for (Metric<?> metric : publicMetrics.metrics()) {
final String sanitizedName = sanitizeMetricName(metric.getName());
final String type = typeForName(sanitizedName);
final String metricName = metricName(sanitizedName, type);
double value = metric.getValue().doubleValue();

sb.append(String.format("#TYPE %s %s\n", metricName, type));
sb.append(String.format("#HELP %s %s\n", metricName, metricName));
sb.append(String.format("%s %s\n", metricName, prometheusDouble(value)));
}
}
return ResponseEntity.ok()
.contentType(MediaType.parseMediaType("text/plain; version=0.0.4; charset=utf-8"))
.body(sb.toString());

}

private String prometheusDouble(double value) {
if (value == Double.POSITIVE_INFINITY) {
return "+Inf";
} else if (value == Double.NEGATIVE_INFINITY) {
return "-Inf";
} else {
return String.valueOf(value);
}
}

private String metricName(String name, String type) {
switch (type) {
case "counter":
return name.replaceFirst("^counter_", "");
case "gauge":
return name.replaceFirst("^gauge_", "");
default:
return name;
}
}

private String typeForName(String name) {
if (name.startsWith("gauge")) {
return "gauge";
}
if (name.startsWith("counter")) {
return "counter";
}

return "gauge";
}
PrometheusMetricsAutoConfiguration() {
DefaultExports.initialize();
}

@Bean
public Filter prometheusMetricsFilter() throws ServletException {
return new io.prometheus.client.filter.MetricsFilter("http_request_duration_seconds",
"Response time histogram",
0,
null);
}
}

This file was deleted.