Skip to content

Commit

Permalink
Merge pull request #12478 from ebullient/debug
Browse files Browse the repository at this point in the history
Warn for weird cases handling @timed methods
  • Loading branch information
mkouba authored Oct 5, 2020
2 parents c74ec29 + 2f87efa commit a9b13e4
Showing 1 changed file with 14 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
import javax.interceptor.Interceptor;
import javax.interceptor.InvocationContext;

import org.jboss.logging.Logger;

import io.micrometer.core.annotation.Timed;
import io.micrometer.core.instrument.LongTaskTimer;
import io.micrometer.core.instrument.MeterRegistry;
Expand All @@ -23,6 +25,7 @@
@MicrometerTimed
@Priority(Interceptor.Priority.LIBRARY_BEFORE + 10)
public class MicrometerTimedInterceptor {
private static final Logger log = Logger.getLogger(MicrometerTimedInterceptor.class);
public static final String DEFAULT_METRIC_NAME = "method.timed";

private final MeterRegistry meterRegistry;
Expand Down Expand Up @@ -94,7 +97,9 @@ private void record(Timed timed, String metricName, Timer.Sample sample, String

sample.stop(meterRegistry, builder);
} catch (Exception e) {
// ignoring on purpose
// ignoring on purpose: possible meter registration error should not interrupt main code flow.
log.warnf(e, "Unable to record observed timer value for %s with exceptionClass %s",
metricName, exceptionClass);
}
}

Expand All @@ -107,17 +112,18 @@ private Object processWithLongTaskTimer(InvocationContext context, Timed timed,

if (stopWhenCompleted) {
try {
return ((CompletionStage<?>) context.proceed()).whenComplete((result, throwable) -> stopLongTaskTimer(sample));
return ((CompletionStage<?>) context.proceed())
.whenComplete((result, throwable) -> stopLongTaskTimer(metricName, sample));
} catch (Exception ex) {
stopLongTaskTimer(sample);
stopLongTaskTimer(metricName, sample);
throw ex;
}
}

try {
return context.proceed();
} finally {
stopLongTaskTimer(sample);
stopLongTaskTimer(metricName, sample);
}
}

Expand All @@ -132,15 +138,18 @@ LongTaskTimer.Sample startLongTaskTimer(Timed timed, Tags commonTags, String met
.register(meterRegistry)
.start();
} catch (Exception e) {
// ignoring on purpose: possible meter registration error should not interrupt main code flow.
log.warnf(e, "Unable to create long task timer named %s", metricName);
return null;
}
}

private void stopLongTaskTimer(LongTaskTimer.Sample sample) {
private void stopLongTaskTimer(String metricName, LongTaskTimer.Sample sample) {
try {
sample.stop();
} catch (Exception e) {
// ignoring on purpose
log.warnf(e, "Unable to update long task timer named %s", metricName);
}
}

Expand Down

0 comments on commit a9b13e4

Please sign in to comment.