Skip to content

Commit

Permalink
Rework MeteredInterceptor
Browse files Browse the repository at this point in the history
  • Loading branch information
kifj committed Dec 24, 2023
1 parent 552588e commit 1666023
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 25 deletions.
4 changes: 2 additions & 2 deletions src/main/java/x1/stomp/boundary/QuoteResource.java
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ public class QuoteResource {
@APIResponse(responseCode = "200", description = "Quote received",
content = @Content(schema = @Schema(implementation = Quote.class)))
@APIResponse(responseCode = "404", description = "Subscription not found")
@Timed(value = "get-quote", extraTags = { "interface", "QuoteResource" })
@Timed
@Bulkhead(value = 5)
public Response getQuote(@Parameter(description = "Stock symbol, see [quote.cnbc.com](https://quote.cnbc.com)",
example = "BMW.DE") @PathParam("key") @MDCKey(MDC_KEY) String key) {
Expand All @@ -133,7 +133,7 @@ public Response getQuote(@Parameter(description = "Stock symbol, see [quote.cnbc
@Content(schema = @Schema(type = SchemaType.ARRAY, implementation = Quote.class),
mediaType = APPLICATION_JSON) })
@APIResponse(responseCode = "404", description = "No subscription found")
@Timed(value = "get-quotes", extraTags = { "interface", "QuoteResource" })
@Timed
@Bulkhead(value = 5)
public void getQuotes(
@Parameter(description = "Stock symbols", example = "[\"GOOG\"]") @QueryParam("key") @MDCKey(MDC_KEY) List<String> keys,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ public void filter(ContainerRequestContext requestContext, ContainerResponseCont
var methodTag = Tag.of("method", resourceInfo.getResourceMethod().getName());
var statusTag = Tag.of("status", String.valueOf(responseContext.getStatus()));
createMetrics(metricID, classTag, methodTag, statusTag);
requestContext.removeProperty(METRIC_ID_PARAM);
}
}

Expand Down
8 changes: 4 additions & 4 deletions src/main/java/x1/stomp/boundary/ShareResource.java
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ public class ShareResource {
@Content(schema = @Schema(type = SchemaType.ARRAY, implementation = Share.class),
mediaType = APPLICATION_JSON),
@Content(schema = @Schema(implementation = ShareWrapper.class), mediaType = APPLICATION_XML) })
@Timed(value = "get-shares", extraTags = { "interface", "ShareResource" })
@Timed
@Bulkhead(value = 5)
public List<Share> listAllShares() {
var shares = shareSubscription.list();
Expand All @@ -131,7 +131,7 @@ public List<Share> listAllShares() {
@APIResponse(responseCode = "200", description = "Subscription found",
content = @Content(schema = @Schema(implementation = Share.class)))
@APIResponse(responseCode = "404", description = "Subscription not found")
@Timed(value = "get-share", extraTags = { "interface", "ShareResource" })
@Timed
@Bulkhead(value = 5)
public Response findShare(@Parameter(description = "Stock symbol, see [quote.cnbc.com](https://quote.cnbc.com)",
example = "BMW.DE") @PathParam("key") @MDCKey(MDC_KEY) String key) {
Expand All @@ -153,7 +153,7 @@ public Response findShare(@Parameter(description = "Stock symbol, see [quote.cnb
@APIResponse(responseCode = "500", description = "Queuing failed")
@APIResponse(responseCode = "400", description = "Invalid data",
content = @Content(schema = @Schema(implementation = ErrorResponse.class)))
@Timed(value = "add-share", extraTags = { "interface", "ShareResource" })
@Timed
public Response addShare(
@Parameter(required = true,
description = "The share which is will be added for subscription") @NotNull @Valid Share share,
Expand Down Expand Up @@ -184,7 +184,7 @@ public Response addShare(
@APIResponse(responseCode = "200", description = "Subscription removed",
content = @Content(schema = @Schema(implementation = Share.class)))
@APIResponse(responseCode = "404", description = "Subscription was not found")
@Timed(value = "remove-share", extraTags = { "interface", "ShareResource" })
@Timed
public Response removeShare(
@Parameter(description = "Stock symbol", example = "GOOG") @PathParam("key") @MDCKey(MDC_KEY) String key) {
var candidate = shareSubscription.find(key);
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/x1/stomp/control/QuoteRetriever.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,12 @@ public class QuoteRetriever {
@ConfigProperty(name = "x1.stomp.control.QuickQuoteService/mp-rest/format", defaultValue = "json")
private String format;

@Timed(value = "rest_calls", extraTags = { "class", "QuoteRetriever", "method", "retrieveQuote" })
@Timed
public Optional<Quote> retrieveQuote(Share share) {
return createQuote(retrieveQuotes(share.getKey()), share);
}

@Timed(value = "rest_calls", extraTags = { "class", "QuoteRetriever", "method", "retrieveQuotes" })
@Timed
public List<Quote> retrieveQuotes(List<Share> shares) {
return (shares.isEmpty()) ? Collections.emptyList() : extractQuotes(shares, retrieveQuotes(joinKeys(shares)));
}
Expand Down
62 changes: 45 additions & 17 deletions src/main/java/x1/stomp/util/MeteredInterceptor.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,38 +19,56 @@
@Interceptor
@Metered
public class MeteredInterceptor {
public static final String DEFAULT_METRIC_NAME = "method.timed";
public static final String DEFAULT_EXCEPTION_TAG_VALUE = "none";
public static final String EXCEPTION_TAG = "exception";

@Inject
private MeterRegistry registry;

@AroundInvoke
public Object meter(InvocationContext ctx) throws Exception {
Class<?> type = ctx.getMethod().getDeclaringClass();
String method = ctx.getMethod().getName();

var timed = timed(ctx);
var counted = counted(ctx);

Sample sample = null;
Timer timer = null;
Counter counter = null;
Exception exception = null;
try {
var counted = counted(ctx);
if (counted != null) {
counter = registry.counter(metricId(counted, ctx.getMethod().getDeclaringClass(), ctx.getMethod().getName()),
counted.extraTags());
}
var timed = timed(ctx);
if (timed != null) {
timer = registry.timer(metricId(timed, ctx.getMethod().getDeclaringClass(), ctx.getMethod().getName()),
timed.extraTags());
sample = Timer.start(registry);
}
return ctx.proceed();
} catch (Exception e) {
exception = e;
throw e;
} finally {
if (sample != null) {
sample.stop(timer);
stopTimer(type, method, timed, sample, exception);
}
if (counter != null) {
counter.increment();
if (counted != null) {
increaseCounter(type, method, counted, exception);
}
}
}

private void increaseCounter(Class<?> type, String method, Counted counted, Exception exception) {
if (!counted.recordFailuresOnly() || exception != null) {
Counter.builder(metricId(counted)).description(StringUtils.defaultIfEmpty(counted.description(), null))
.tags(counted.extraTags()).tag("class", type.getSimpleName()).tag("method", method)
.tags(EXCEPTION_TAG, getExceptionTag(exception)).register(registry).increment();
}
}

private void stopTimer(Class<?> type, String method, Timed timed, Sample sample, Exception exception) {
var timer = Timer.builder(metricId(timed)).description(StringUtils.defaultIfEmpty(timed.description(), null))
.tags(timed.extraTags()).tag("class", type.getSimpleName()).tag("method", method)
.tags(EXCEPTION_TAG, getExceptionTag(exception)).register(registry);
sample.stop(timer);
}

private Timed timed(InvocationContext ctx) {
var annotation = ctx.getMethod().getAnnotation(Timed.class);
if (annotation == null) {
Expand All @@ -67,11 +85,21 @@ private Counted counted(InvocationContext ctx) {
return annotation;
}

private String metricId(Timed annotation, Class<?> type, String methodName) {
return StringUtils.defaultIfEmpty(annotation.value(), type.getSimpleName() + "." + methodName);
private String metricId(Timed annotation) {
return StringUtils.defaultIfEmpty(annotation.value(), DEFAULT_METRIC_NAME);
}

private String metricId(Counted annotation) {
return annotation.value();
}

private String metricId(Counted annotation, Class<?> type, String methodName) {
return StringUtils.defaultIfEmpty(annotation.value(), type.getSimpleName() + "." + methodName);
private String getExceptionTag(Throwable throwable) {
if (throwable == null) {
return DEFAULT_EXCEPTION_TAG_VALUE;
}
if (throwable.getCause() == null) {
return throwable.getClass().getSimpleName();
}
return throwable.getCause().getClass().getSimpleName();
}
}

0 comments on commit 1666023

Please sign in to comment.