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

prepend tag names in kafka metric if using prometheus reporter #586

Closed
Closed
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
2 changes: 2 additions & 0 deletions kafka/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ dependencies {
api libs.managed.kafka.clients

compileOnly mn.micronaut.micrometer.core
compileOnly mn.micronaut.micrometer.registry.prometheus
compileOnly libs.opentracing.kafka.client
compileOnly libs.zipkin.brave.kafka.clients
compileOnly mn.micronaut.graal
Expand All @@ -27,4 +28,5 @@ dependencies {

testRuntimeOnly mn.micronaut.micrometer.registry.statsd
testRuntimeOnly mn.micronaut.tracing.core
testRuntimeOnly mn.micronaut.micrometer.registry.prometheus
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import io.micrometer.core.instrument.Tag;
import io.micrometer.core.instrument.binder.MeterBinder;
import io.micrometer.core.lang.NonNull;
import io.micrometer.prometheus.PrometheusMeterRegistry;
import io.micronaut.configuration.kafka.metrics.builder.KafkaMetricMeterTypeBuilder;
import io.micronaut.core.annotation.Internal;
import org.apache.kafka.common.MetricName;
Expand Down Expand Up @@ -99,7 +100,7 @@ public void close() {

private void registerMetric(MeterRegistry meterRegistry, KafkaMetric metric) {
KafkaMetricMeterTypeBuilder.newBuilder()
.prefix(getMetricPrefix())
.prefix(getMetricPrefix(meterRegistry, metric))
.metric(metric)
.tagFunction(getTagFunction())
.registry(meterRegistry)
Expand Down Expand Up @@ -129,6 +130,22 @@ protected Set<String> getIncludedTags() {
return tags;
}

/**
* Customize metric prefix with tag names
* @param meterRegistry
* @param metric
* @return
*/
private String getMetricPrefix(MeterRegistry meterRegistry, KafkaMetric metric) {
String prefix = getMetricPrefix();
if (meterRegistry instanceof PrometheusMeterRegistry) {
StringBuilder sb = new StringBuilder(prefix);
metric.metricName().tags().keySet().forEach(v -> sb.append(".").append(v));
return sb.toString();
}
return prefix;
}

/**
* Abstract method to implement with the metric prefix for the reporter.
*
Expand Down