diff --git a/waggle-dance-core/src/main/java/com/hotels/bdp/waggledance/metrics/MonitoredAspect.java b/waggle-dance-core/src/main/java/com/hotels/bdp/waggledance/metrics/MonitoredAspect.java index 166bba7f7..6efc96656 100644 --- a/waggle-dance-core/src/main/java/com/hotels/bdp/waggledance/metrics/MonitoredAspect.java +++ b/waggle-dance-core/src/main/java/com/hotels/bdp/waggledance/metrics/MonitoredAspect.java @@ -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; @@ -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(); @@ -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); } } @@ -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 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 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(); } @@ -101,5 +144,4 @@ private String clean(String string) { private String buildMetricPath(String... parts) { return DOT_JOINER.join(parts); } - }