Skip to content

Commit

Permalink
Added the metrics with tags
Browse files Browse the repository at this point in the history
  • Loading branch information
Dhrubajyoti Sadhu committed Aug 14, 2023
1 parent 46a5e1a commit c5b17ea
Showing 1 changed file with 44 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@
import org.springframework.beans.factory.annotation.Configurable;

import io.micrometer.core.instrument.MeterRegistry;
import io.micrometer.core.instrument.Tag;
import io.micrometer.core.instrument.Tags;

import com.google.common.annotations.VisibleForTesting;
import com.google.common.base.Joiner;
Expand All @@ -51,6 +53,8 @@ public Object monitor(ProceedingJoinPoint pjp) throws Throwable {
public Object monitor(ProceedingJoinPoint pjp, Monitored monitored) throws Throwable {

String metricBasePath = buildMetricBasePath(pjp);
String newMetricBasePath = buildNewMetricBasePath(pjp);
String methodName = getMethodName(pjp);

String result = null;
Stopwatch stopwatch = Stopwatch.createStarted();
Expand All @@ -67,6 +71,12 @@ public Object monitor(ProceedingJoinPoint pjp, Monitored monitored) throws Throw
increment(buildMetricPath(COUNTER, metricBasePath, getMonitorMetastore(), result));
submit(buildMetricPath(TIMER, metricBasePath, getMonitorMetastore(), "duration"),
stopwatch.elapsed(TimeUnit.MILLISECONDS));

// Sends metrics with Tags: federation_namespace and method_name
incrementWithTags(buildMetricPath(COUNTER, newMetricBasePath, "calls"), methodName);
incrementWithTags(buildMetricPath(COUNTER, newMetricBasePath, result), methodName);
submitWithTags(buildMetricPath(TIMER, newMetricBasePath, "duration"),
stopwatch.elapsed(TimeUnit.MILLISECONDS), methodName);
}
}

Expand All @@ -75,21 +85,54 @@ void setMeterRegistry(MeterRegistry meterRegistry) {
this.meterRegistry = meterRegistry;
}

private void incrementWithTags(String metricName, String methodName) {
if (meterRegistry != null) {
Tag federationTag = Tag.of("federation_namespace", getMonitorMetastore());
Tag methodTag = Tag.of("method_name", methodName);
Iterable<Tag> tags = Tags.of(federationTag, methodTag);
meterRegistry.counter(metricName, tags).increment();
}
}

private void increment(String metricName) {
if (meterRegistry != null) {
meterRegistry.counter(metricName).increment();
}
}

private void submitWithTags(String metricName, long value, String methodName) {
if (meterRegistry != null) {
Tag federationTag = Tag.of("federation_namespace", getMonitorMetastore());
Tag methodTag = Tag.of("method_name", methodName);
Iterable<Tag> tags = Tags.of(federationTag).and(methodTag);
meterRegistry.timer(metricName, tags).record(Duration.ofMillis(value));
}
}

private void submit(String metricName, long value) {
if (meterRegistry != null) {
meterRegistry.timer(metricName).record(Duration.ofMillis(value));
}
}

private String buildMetricBasePath(ProceedingJoinPoint pjp) {
private String buildNewMetricBasePath(ProceedingJoinPoint pjp) {
String className = clean(pjp.getSignature().getDeclaringTypeName());
return new StringBuilder(className).toString();
}

private String getMethodName(ProceedingJoinPoint pjp) {
String methodName = clean(pjp.getSignature().getName());
return new StringBuilder(methodName).toString();
}

private String getClassName(ProceedingJoinPoint pjp) {
String className = clean(pjp.getSignature().getDeclaringTypeName());
return new StringBuilder(className).toString();
}

private String buildMetricBasePath(ProceedingJoinPoint pjp) {
String className = getClassName(pjp);
String methodName = getMethodName(pjp);
return new StringBuilder(className).append(".").append(methodName).toString();
}

Expand All @@ -101,5 +144,4 @@ private String clean(String string) {
private String buildMetricPath(String... parts) {
return DOT_JOINER.join(parts);
}

}

0 comments on commit c5b17ea

Please sign in to comment.