Skip to content

Commit

Permalink
renamed AsyncTimed to AsyncSimplyTimed in analogy to MicroProfile's S…
Browse files Browse the repository at this point in the history
…implyTimed

this is to avoid confusion as the underlying timer is a SimpleTimer
  • Loading branch information
schulzp committed Sep 16, 2020
1 parent 3b6509e commit 265f3ca
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 26 deletions.
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# SmallRye Metrics Extension: AsyncTimed
# SmallRye Metrics Extension: AsyncSimplyTimed

This library provides annotation-based timing for methods returning promised values, namely
`java.util.concurrent.CompletionStage` and `io.smallrye.mutiny.Uni`.
Expand Down Expand Up @@ -40,4 +40,5 @@ Therefore, the minimum set of dependencies looks as follows:

## Usage

Annotate any bean method returning one of the aforementioned types with `com.traum.microprofile.metrics.annotation.AsyncTimed`.
Annotate any bean method returning one of the aforementioned types with `com.traum.microprofile.metrics.annotation.AsyncSimplyTimed`,
instead of MicroProfile's [`SimplyTimed`](https://download.eclipse.org/microprofile/microprofile-metrics-2.3/microprofile-metrics-spec-2.3.html#_simplytimed).
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.traum.metrics.interceptors;

import com.traum.microprofile.metrics.annotation.AsyncTimed;
import com.traum.microprofile.metrics.annotation.AsyncSimplyTimed;
import io.smallrye.metrics.TagsUtils;
import io.smallrye.metrics.elementdesc.AnnotationInfo;
import io.smallrye.metrics.elementdesc.MemberInfo;
Expand All @@ -27,16 +27,16 @@
import org.eclipse.microprofile.metrics.Tag;

@Dependent
@AsyncTimed
@Interceptor
public class AsyncTimedInterceptor {
@AsyncSimplyTimed
public class AsyncSimplyTimedInterceptor {

private final MetricRegistry registry;

private final Map<String, SimpleTimer> cache = new HashMap<>();

@Inject
AsyncTimedInterceptor(MetricRegistry registry) {
AsyncSimplyTimedInterceptor(MetricRegistry registry) {
this.registry = registry;
}

Expand Down Expand Up @@ -81,7 +81,7 @@ private <E extends Member & AnnotatedElement> SimpleTimer registerTimer(E elemen
return cache.computeIfAbsent(
element.getName(),
key -> {
Of<AsyncTimed> resolvedAnnotation = AsyncTimedOf.of(element);
Of<AsyncSimplyTimed> resolvedAnnotation = AsyncSimplyTimedOf.of(element);

final Metadata metadata =
Metadata.builder()
Expand All @@ -98,32 +98,32 @@ private <E extends Member & AnnotatedElement> SimpleTimer registerTimer(E elemen
}
}

class AsyncTimedOf implements Of<AsyncTimed> {
class AsyncSimplyTimedOf implements Of<AsyncSimplyTimed> {

private static final MemberInfoAdapter<Member> MEMBER_INFO_ADAPTER = new CDIMemberInfoAdapter();

private final AnnotationInfo annotationInfo;
private final String metricName;
private final Tag[] tags;

private AsyncTimedOf(AnnotationInfo annotationInfo, String metricName, Tag[] tags) {
private AsyncSimplyTimedOf(AnnotationInfo annotationInfo, String metricName, Tag[] tags) {
this.annotationInfo = annotationInfo;
this.metricName = metricName;
this.tags = tags;
}

static <E extends Member & AnnotatedElement> AsyncTimedOf of(E element) {
final AsyncTimed annotation = element.getAnnotation(AsyncTimed.class);
static <E extends Member & AnnotatedElement> AsyncSimplyTimedOf of(E element) {
final AsyncSimplyTimed annotation = element.getAnnotation(AsyncSimplyTimed.class);
final MemberInfo memberInfo = MEMBER_INFO_ADAPTER.convert(element);
final String name = annotation.name().isEmpty() ? memberInfo.getName() : annotation.name();
final String metricName =
annotation.absolute()
? name
: MetricRegistry.name(memberInfo.getDeclaringClassName(), name);
final AnnotationInfo annotationInfo = new AsyncTimedAnnotationInfo(annotation);
final AnnotationInfo annotationInfo = new AsyncSimplyTimedAnnotationInfo(annotation);
final Tag[] tags = TagsUtils.parseTagsAsArray(annotation.tags());

return new AsyncTimedOf(annotationInfo, metricName, tags);
return new AsyncSimplyTimedOf(annotationInfo, metricName, tags);
}

@Override
Expand All @@ -147,11 +147,11 @@ public AnnotationInfo metricAnnotation() {
}
}

class AsyncTimedAnnotationInfo implements AnnotationInfo {
class AsyncSimplyTimedAnnotationInfo implements AnnotationInfo {

private final AsyncTimed instance;
private final AsyncSimplyTimed instance;

public <T extends Annotation> AsyncTimedAnnotationInfo(AsyncTimed instance) {
public <T extends Annotation> AsyncSimplyTimedAnnotationInfo(AsyncSimplyTimed instance) {
this.instance = instance;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
ElementType.METHOD,
ElementType.ANNOTATION_TYPE
})
public @interface AsyncTimed {
public @interface AsyncSimplyTimed {

/** @return The name of the simple timer. */
@Nonbinding
Expand Down
12 changes: 6 additions & 6 deletions src/test/java/com/traum/metrics/interceptors/AsyncService.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.traum.metrics.interceptors;

import com.traum.microprofile.metrics.annotation.AsyncTimed;
import com.traum.microprofile.metrics.annotation.AsyncSimplyTimed;
import java.time.Duration;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.CompletionStage;
Expand All @@ -14,27 +14,27 @@ public class AsyncService {

ScheduledExecutorService executorService = Executors.newScheduledThreadPool(1);

@AsyncTimed
@AsyncSimplyTimed
public CompletionStage<Duration> returnCompletionStage(Duration delay) {
return execute(delay);
}

@AsyncTimed(name = "relative_name")
@AsyncSimplyTimed(name = "relative_name")
public CompletionStage<Duration> returnCompletionStageWithRelativeName(Duration delay) {
return execute(delay);
}

@AsyncTimed(name = "absolute_name", absolute = true)
@AsyncSimplyTimed(name = "absolute_name", absolute = true)
public CompletionStage<Duration> returnCompletionStageWithAbsoluteName(Duration delay) {
return execute(delay);
}

@AsyncTimed(name = "with_exception", absolute = true)
@AsyncSimplyTimed(name = "with_exception", absolute = true)
public CompletionStage<Duration> returnCompletionStageWithException(Duration delay) {
throw new RuntimeException("Failed fast");
}

@AsyncTimed(name = "with_failure", absolute = true)
@AsyncSimplyTimed(name = "with_failure", absolute = true)
public CompletionStage<Duration> returnCompletionStageWithFailure(Duration delay) {
return CompletableFuture.failedStage(new RuntimeException("Failed later"));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,15 @@
import org.junit.jupiter.api.extension.ExtendWith;

@ExtendWith(WeldJunit5Extension.class)
class AsyncTimedInterceptorTest {
class AsyncSimplyTimedInterceptorTest {

@WeldSetup
WeldInitiator initiator =
WeldInitiator.of(
WeldInitiator.createWeld()
.beanClasses(AsyncService.class, MetricRegistryProducer.class)
.packages(AsyncTimedInterceptor.class)
.interceptors(AsyncTimedInterceptor.class));
.packages(AsyncSimplyTimedInterceptor.class)
.interceptors(AsyncSimplyTimedInterceptor.class));

@Inject AsyncService service;

Expand Down

0 comments on commit 265f3ca

Please sign in to comment.