Skip to content

Commit

Permalink
Core: Support prometheus namespace and subsystem prefixes (prebid#1802)
Browse files Browse the repository at this point in the history
  • Loading branch information
And1sS authored May 16, 2022
1 parent aad5adb commit ccaf47f
Show file tree
Hide file tree
Showing 13 changed files with 529 additions and 332 deletions.
7 changes: 5 additions & 2 deletions docs/config-app.md
Original file line number Diff line number Diff line change
Expand Up @@ -249,8 +249,11 @@ For `console` backend type available next options:
- `metrics.console.interval` - interval in seconds between successive sending metrics.

For `prometheus` backend type available next options:
- `metrics.prometheus.port` - if a port is specified a prometheus reporter will start on that port
- `metrics.prometheus.labels.enabled` - If set to `true` it enables tags/labels for prometheus metrics instead of including them in the metrics path
- `metrics.prometheus.enabled` - if equals to `true` then prometheus reporter will be started
- `metrics.prometheus.port` - prometheus reporter port
- `metrics.prometheus.namespace` - optional namespace prefix for metrics
- `metrics.prometheus.subsystem` - optional subsystem prefix for metrics
- `metrics.prometheus.custom-labels-enabled` - If set to `true` it enables tags/labels for prometheus metrics instead of including them in the metrics path

It is possible to define how many account-level metrics will be submitted on per-account basis.
See [metrics documentation](metrics.md) for complete list of metrics submitted at each verbosity level.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package org.prebid.server.metric.prometheus;

import io.prometheus.client.Collector;
import io.prometheus.client.dropwizard.samplebuilder.SampleBuilder;
import org.apache.commons.lang3.StringUtils;

import java.util.List;
import java.util.Objects;
import java.util.regex.Pattern;

public class NamespaceSubsystemSampleBuilder implements SampleBuilder {

private static final String VALID_PREFIX_REGEX = "[a-zA-Z_:]?[a-zA-Z0-9_:]*";

private final SampleBuilder delegate;
private final String prefix;

public NamespaceSubsystemSampleBuilder(SampleBuilder sampleBuilder, String namespace, String subsystem) {
delegate = Objects.requireNonNull(sampleBuilder);
prefix = toPrefix(namespace) + toPrefix(subsystem);

final Pattern prefixPattern = Pattern.compile(VALID_PREFIX_REGEX);
if (!prefixPattern.matcher(prefix).matches()) {
throw new IllegalArgumentException(String.format(
"Invalid prefix: %s, namespace and subsystem should match regex: %s", prefix, VALID_PREFIX_REGEX));
}
}

@Override
public Collector.MetricFamilySamples.Sample createSample(String dropwizardName,
String nameSuffix,
List<String> additionalLabelNames,
List<String> additionalLabelValues,
double value) {

return delegate.createSample(
prefix + dropwizardName,
nameSuffix,
additionalLabelNames,
additionalLabelValues,
value);
}

private static String toPrefix(String value) {
return StringUtils.isNotEmpty(value) ? value + "_" : "";
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import io.vertx.ext.dropwizard.Match;
import io.vertx.ext.dropwizard.MatchType;
import io.vertx.ext.web.handler.BodyHandler;
import org.prebid.server.spring.config.metrics.MetricsConfiguration;
import org.prebid.server.vertx.ContextRunner;
import org.prebid.server.vertx.LocalMessageCodec;
import org.springframework.beans.factory.annotation.Value;
Expand Down
Loading

0 comments on commit ccaf47f

Please sign in to comment.