Skip to content

Commit

Permalink
Merge pull request #1 from unidev-platform/metrics-labels
Browse files Browse the repository at this point in the history
Add default labels to metrics
  • Loading branch information
denis256 authored Mar 18, 2023
2 parents 263c3ba + 5992bae commit 149188c
Showing 1 changed file with 17 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;

import static java.util.Collections.emptyMap;

/**
* Service used to track metrics
*/
Expand All @@ -21,6 +23,8 @@ public class MetricService {

private final String prefix;

private final Map<String, String> defaultLabels;

private final ConcurrentMap<String, Counter> counters = new ConcurrentHashMap<>();

private final ConcurrentMap<String, Gauge> gauges = new ConcurrentHashMap<>();
Expand All @@ -29,13 +33,17 @@ public class MetricService {

private final ConcurrentMap<String, Histogram> histograms = new ConcurrentHashMap<>();


public MetricService(String prefix, int prometheusPort) throws IOException {
this(prefix, prometheusPort, emptyMap());
}

public MetricService(String prefix, int prometheusPort, Map<String, String> defaultLabels) throws IOException {
this.prefix = prefix;
DefaultExports.initialize();
server = new HTTPServer.Builder()
this.server = new HTTPServer.Builder()
.withPort(prometheusPort)
.build();
this.defaultLabels = defaultLabels;
}

/**
Expand All @@ -44,6 +52,7 @@ public MetricService(String prefix, int prometheusPort) throws IOException {
public Counter.Child counter(String name, Map<String, String> labels) {
try {
TreeMap<String, String> labelMap = new TreeMap<>(labels);
labelMap.putAll(defaultLabels);
String prometheusName = buildName(name);
counters.computeIfAbsent(prometheusName, s -> {
Counter value = Counter.build()
Expand Down Expand Up @@ -71,7 +80,7 @@ public void inc(String name, Map<String, String> labels) {
*/
public Gauge.Child gauge(String name, Map<String, String> labels) {
TreeMap<String, String> labelMap = new TreeMap<>(labels);

labelMap.putAll(defaultLabels);
try {
String prometheusName = buildName(name);
gauges.computeIfAbsent(prometheusName, s -> {
Expand All @@ -94,6 +103,7 @@ public Gauge.Child gauge(String name, Map<String, String> labels) {
*/
public Summary.Child summary(String name, Map<String, String> labels) {
TreeMap<String, String> labelMap = new TreeMap<>(labels);
labelMap.putAll(defaultLabels);
try {
String prometheusName = buildName(name);
summaries.computeIfAbsent(prometheusName, s -> {
Expand All @@ -116,6 +126,7 @@ public Summary.Child summary(String name, Map<String, String> labels) {
*/
public Histogram.Child histogram(String name, Map<String, String> labels) {
TreeMap<String, String> labelMap = new TreeMap<>(labels);
labelMap.putAll(defaultLabels);
try {
String prometheusName = buildName(name);
histograms.computeIfAbsent(prometheusName, s -> {
Expand Down Expand Up @@ -150,6 +161,9 @@ String[] mapValues(Map<String, String> map) {
}

String buildName(String name) {
if (prefix == null || prefix.isEmpty()) {
return name.toLowerCase().replaceAll("[^A-Za-z0-9]", "_");
}
return (prefix + "_" + name.toLowerCase()).replaceAll("[^A-Za-z0-9]", "_");
}

Expand Down

0 comments on commit 149188c

Please sign in to comment.