This repository has been archived by the owner on Dec 5, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 587
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
cf23f23
commit 64e8e11
Showing
5 changed files
with
169 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
40 changes: 40 additions & 0 deletions
40
...java/works/weave/socks/shipping/configuration/PrometheusEndpointContextConfiguration.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package works.weave.socks.shipping.configuration; | ||
|
||
import io.prometheus.client.CollectorRegistry; | ||
import org.springframework.boot.actuate.autoconfigure.ExportMetricWriter; | ||
import org.springframework.boot.actuate.autoconfigure.ManagementContextConfiguration; | ||
import org.springframework.boot.actuate.condition.ConditionalOnEnabledEndpoint; | ||
import org.springframework.boot.actuate.metrics.writer.MetricWriter; | ||
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean; | ||
import org.springframework.context.annotation.Bean; | ||
import works.weave.socks.shipping.controllers.PrometheusEndpoint; | ||
import works.weave.socks.shipping.controllers.PrometheusMvcEndpoint; | ||
import works.weave.socks.shipping.monitoring.PrometheusMetricWriter; | ||
|
||
@ManagementContextConfiguration | ||
public class PrometheusEndpointContextConfiguration { | ||
|
||
@Bean | ||
public PrometheusEndpoint prometheusEndpoint(CollectorRegistry registry) { | ||
return new PrometheusEndpoint(registry); | ||
} | ||
|
||
@Bean | ||
@ConditionalOnBean(PrometheusEndpoint.class) | ||
@ConditionalOnEnabledEndpoint("prometheus") | ||
PrometheusMvcEndpoint prometheusMvcEndpoint(PrometheusEndpoint prometheusEndpoint) { | ||
return new PrometheusMvcEndpoint(prometheusEndpoint); | ||
} | ||
|
||
@Bean | ||
CollectorRegistry collectorRegistry() { | ||
return new CollectorRegistry(); | ||
} | ||
|
||
@Bean | ||
@ExportMetricWriter | ||
MetricWriter prometheusMetricWriter(CollectorRegistry registry) { | ||
return new PrometheusMetricWriter(registry); | ||
} | ||
|
||
} |
30 changes: 30 additions & 0 deletions
30
src/main/java/works/weave/socks/shipping/controllers/PrometheusEndpoint.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package works.weave.socks.shipping.controllers; | ||
|
||
import io.prometheus.client.CollectorRegistry; | ||
import io.prometheus.client.exporter.common.TextFormat; | ||
import org.springframework.boot.actuate.endpoint.AbstractEndpoint; | ||
|
||
import java.io.IOException; | ||
import java.io.StringWriter; | ||
import java.io.Writer; | ||
|
||
public class PrometheusEndpoint extends AbstractEndpoint<String> { | ||
|
||
private CollectorRegistry registry; | ||
|
||
public PrometheusEndpoint(CollectorRegistry registry) { | ||
super("prometheus", false, true); | ||
this.registry = registry; | ||
} | ||
|
||
@Override | ||
public String invoke() { | ||
Writer writer = new StringWriter(); | ||
try { | ||
TextFormat.write004(writer, registry.metricFamilySamples()); | ||
} catch (IOException e) { | ||
e.printStackTrace(); | ||
} | ||
return writer.toString(); | ||
} | ||
} |
31 changes: 31 additions & 0 deletions
31
src/main/java/works/weave/socks/shipping/controllers/PrometheusMvcEndpoint.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package works.weave.socks.shipping.controllers; | ||
|
||
import io.prometheus.client.exporter.common.TextFormat; | ||
import org.springframework.boot.actuate.endpoint.mvc.AbstractEndpointMvcAdapter; | ||
import org.springframework.boot.actuate.endpoint.mvc.HypermediaDisabled; | ||
import org.springframework.http.HttpStatus; | ||
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.ResponseBody; | ||
|
||
import java.util.Collections; | ||
|
||
public class PrometheusMvcEndpoint extends AbstractEndpointMvcAdapter<PrometheusEndpoint> { | ||
|
||
public PrometheusMvcEndpoint(PrometheusEndpoint delegate) { | ||
super(delegate); | ||
} | ||
|
||
@RequestMapping(method = RequestMethod.GET, produces = TextFormat.CONTENT_TYPE_004) | ||
@ResponseBody | ||
@HypermediaDisabled | ||
protected Object invoke() { | ||
if (!getDelegate().isEnabled()) { | ||
return new ResponseEntity<>( | ||
Collections.singletonMap("message", "This endpoint is disabled"), | ||
HttpStatus.NOT_FOUND); | ||
} | ||
return super.invoke(); | ||
} | ||
} |
54 changes: 54 additions & 0 deletions
54
src/main/java/works/weave/socks/shipping/monitoring/PrometheusMetricWriter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
package works.weave.socks.shipping.monitoring; | ||
|
||
import io.prometheus.client.CollectorRegistry; | ||
import io.prometheus.client.Counter; | ||
import io.prometheus.client.Gauge; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.boot.actuate.metrics.Metric; | ||
import org.springframework.boot.actuate.metrics.writer.Delta; | ||
import org.springframework.boot.actuate.metrics.writer.MetricWriter; | ||
|
||
import java.util.concurrent.ConcurrentHashMap; | ||
import java.util.concurrent.ConcurrentMap; | ||
|
||
public class PrometheusMetricWriter implements MetricWriter { | ||
|
||
private final ConcurrentMap<String, Counter> counters = new ConcurrentHashMap<>(); | ||
private final ConcurrentMap<String, Gauge> gauges = new ConcurrentHashMap<>(); | ||
private CollectorRegistry registry; | ||
|
||
@Autowired | ||
public PrometheusMetricWriter(CollectorRegistry registry) { | ||
this.registry = registry; | ||
} | ||
|
||
@Override | ||
public void increment(Delta<?> delta) { | ||
counter(delta.getName()).inc(delta.getValue().doubleValue()); | ||
} | ||
|
||
@Override | ||
public void reset(String metricName) { | ||
counter(metricName).clear(); | ||
} | ||
|
||
@Override | ||
public void set(Metric<?> value) { | ||
gauge(value.getName()).set(value.getValue().doubleValue()); | ||
} | ||
|
||
private Counter counter(String name) { | ||
String key = sanitizeName(name); | ||
return counters.computeIfAbsent(key, k -> Counter.build().name(k).help(k).register(registry)); | ||
} | ||
|
||
private Gauge gauge(String name) { | ||
String key = sanitizeName(name); | ||
return gauges.computeIfAbsent(key, k -> Gauge.build().name(k).help(k).register(registry)); | ||
} | ||
|
||
private String sanitizeName(String name) { | ||
return name.replaceAll("[^a-zA-Z0-9_]", "_"); | ||
} | ||
|
||
} |