-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #8 from kumuluz/mp-metrics-2.3
Updated to mp-metrics 2.3, implemented SimpleTimer.
- Loading branch information
Showing
13 changed files
with
330 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
57 changes: 57 additions & 0 deletions
57
core/src/main/java/com/kumuluz/ee/metrics/api/SimpleTimerContextImpl.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
/* | ||
* Copyright (c) 2014-2020 Kumuluz and/or its affiliates | ||
* and other contributors as indicated by the @author tags and | ||
* the contributor list. | ||
* | ||
* Licensed under the MIT License (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://opensource.org/licenses/MIT | ||
* | ||
* The software is provided "AS IS", WITHOUT WARRANTY OF ANY KIND, express or | ||
* implied, including but not limited to the warranties of merchantability, | ||
* fitness for a particular purpose and noninfringement. in no event shall the | ||
* authors or copyright holders be liable for any claim, damages or other | ||
* liability, whether in an action of contract, tort or otherwise, arising from, | ||
* out of or in connection with the software or the use or other dealings in the | ||
* software. See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package com.kumuluz.ee.metrics.api; | ||
|
||
import com.codahale.metrics.Clock; | ||
import org.eclipse.microprofile.metrics.SimpleTimer; | ||
|
||
import java.util.concurrent.TimeUnit; | ||
|
||
/** | ||
* Microprofile SimpleTimer.Context implementation. | ||
* | ||
* @author Aljaž Pavišič | ||
* @since 2.3.0 | ||
*/ | ||
public class SimpleTimerContextImpl implements SimpleTimer.Context, AutoCloseable { | ||
|
||
private final SimpleTimerImpl simpleTimer; | ||
private final Clock clock; | ||
private final long startTime; | ||
|
||
public SimpleTimerContextImpl(SimpleTimerImpl simpleTimer, Clock clock){ | ||
this.simpleTimer = simpleTimer; | ||
this.clock = clock; | ||
this.startTime = clock.getTick(); | ||
} | ||
|
||
@Override | ||
public long stop() { | ||
final long elapsed = clock.getTick() - startTime; | ||
simpleTimer.update(elapsed, TimeUnit.NANOSECONDS); | ||
return elapsed; | ||
} | ||
|
||
@Override | ||
public void close() { | ||
stop(); | ||
} | ||
} |
111 changes: 111 additions & 0 deletions
111
core/src/main/java/com/kumuluz/ee/metrics/api/SimpleTimerImpl.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
/* | ||
* Copyright (c) 2014-2020 Kumuluz and/or its affiliates | ||
* and other contributors as indicated by the @author tags and | ||
* the contributor list. | ||
* | ||
* Licensed under the MIT License (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://opensource.org/licenses/MIT | ||
* | ||
* The software is provided "AS IS", WITHOUT WARRANTY OF ANY KIND, express or | ||
* implied, including but not limited to the warranties of merchantability, | ||
* fitness for a particular purpose and noninfringement. in no event shall the | ||
* authors or copyright holders be liable for any claim, damages or other | ||
* liability, whether in an action of contract, tort or otherwise, arising from, | ||
* out of or in connection with the software or the use or other dealings in the | ||
* software. See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package com.kumuluz.ee.metrics.api; | ||
|
||
import com.codahale.metrics.Clock; | ||
import org.eclipse.microprofile.metrics.SimpleTimer; | ||
|
||
import java.time.Duration; | ||
import java.util.concurrent.Callable; | ||
import java.util.concurrent.TimeUnit; | ||
import java.util.concurrent.atomic.LongAdder; | ||
|
||
/** | ||
* Microprofile SimpleTimer implementation. | ||
* | ||
* @author Aljaž Pavišič | ||
* @since 2.3.0 | ||
*/ | ||
public class SimpleTimerImpl implements SimpleTimer { | ||
|
||
private Clock clock; | ||
|
||
private Duration elapsedTime; | ||
|
||
private LongAdder count; | ||
|
||
public SimpleTimerImpl() { | ||
this.clock = Clock.defaultClock(); | ||
this.elapsedTime = Duration.ZERO; | ||
this.count = new LongAdder(); | ||
} | ||
|
||
@Override | ||
public void update(Duration duration) { | ||
inc(1); | ||
this.elapsedTime = this.elapsedTime.plus(duration); | ||
} | ||
|
||
@Override | ||
public <T> T time(Callable<T> callable) throws Exception { | ||
final long startTime = clock.getTick(); | ||
try { | ||
return callable.call(); | ||
} | ||
finally { | ||
update(clock.getTick() - startTime); | ||
} | ||
} | ||
|
||
@Override | ||
public void time(Runnable runnable) { | ||
final long startTime = clock.getTick(); | ||
try { | ||
runnable.run(); | ||
} finally { | ||
update(clock.getTick() - startTime); | ||
} | ||
} | ||
|
||
@Override | ||
public Context time() { | ||
return new SimpleTimerContextImpl(this, clock); | ||
} | ||
|
||
@Override | ||
public Duration getElapsedTime() { | ||
return this.elapsedTime; | ||
} | ||
|
||
@Override | ||
public long getCount() { | ||
return this.count.sum(); | ||
} | ||
|
||
public void update(long duration, TimeUnit timeUnit){ | ||
update(timeUnit.toNanos(duration)); | ||
} | ||
|
||
public void inc(long n){ | ||
this.count.add(n); | ||
} | ||
|
||
public void dec(long n){ | ||
this.count.add(-n); | ||
} | ||
|
||
private void update(long duration) { | ||
if (duration > 0) { | ||
inc(1); | ||
this.elapsedTime = this.elapsedTime.plusNanos(duration); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
88 changes: 88 additions & 0 deletions
88
core/src/main/java/com/kumuluz/ee/metrics/interceptors/SimplyTimedInterceptor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
/* | ||
* Copyright (c) 2014-2020 Kumuluz and/or its affiliates | ||
* and other contributors as indicated by the @author tags and | ||
* the contributor list. | ||
* | ||
* Licensed under the MIT License (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://opensource.org/licenses/MIT | ||
* | ||
* The software is provided "AS IS", WITHOUT WARRANTY OF ANY KIND, express or | ||
* implied, including but not limited to the warranties of merchantability, | ||
* fitness for a particular purpose and noninfringement. in no event shall the | ||
* authors or copyright holders be liable for any claim, damages or other | ||
* liability, whether in an action of contract, tort or otherwise, arising from, | ||
* out of or in connection with the software or the use or other dealings in the | ||
* software. See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package com.kumuluz.ee.metrics.interceptors; | ||
|
||
import com.kumuluz.ee.metrics.utils.AnnotationMetadata; | ||
import com.kumuluz.ee.metrics.utils.MetadataWithTags; | ||
import org.eclipse.microprofile.metrics.MetricRegistry; | ||
import org.eclipse.microprofile.metrics.MetricType; | ||
import org.eclipse.microprofile.metrics.SimpleTimer; | ||
import org.eclipse.microprofile.metrics.annotation.SimplyTimed; | ||
|
||
import javax.annotation.Priority; | ||
import javax.enterprise.inject.Intercepted; | ||
import javax.enterprise.inject.spi.Bean; | ||
import javax.inject.Inject; | ||
import javax.interceptor.AroundConstruct; | ||
import javax.interceptor.AroundInvoke; | ||
import javax.interceptor.Interceptor; | ||
import javax.interceptor.InvocationContext; | ||
import java.lang.reflect.AnnotatedElement; | ||
import java.lang.reflect.Member; | ||
|
||
/** | ||
* Interceptor for SimplyTimed annotation. | ||
* | ||
* @author Aljaž Pavišič | ||
* @since 2.3.0 | ||
*/ | ||
@Interceptor | ||
@SimplyTimed | ||
@Priority(Interceptor.Priority.LIBRARY_BEFORE) | ||
public class SimplyTimedInterceptor { | ||
@Inject | ||
private MetricRegistry applicationRegistry; | ||
|
||
private Bean<?> bean; | ||
|
||
@Inject | ||
private SimplyTimedInterceptor(@Intercepted Bean<?> bean) { | ||
this.bean = bean; | ||
} | ||
|
||
@AroundConstruct | ||
private Object simplyTimedConstructor(InvocationContext context) throws Exception { | ||
return applyInterceptor(context, context.getConstructor()); | ||
} | ||
|
||
@AroundInvoke | ||
private Object simplyTimedMethod(InvocationContext context) throws Exception { | ||
return applyInterceptor(context, context.getMethod()); | ||
} | ||
|
||
private <E extends Member & AnnotatedElement> Object applyInterceptor(InvocationContext context, E member) | ||
throws Exception { | ||
MetadataWithTags metadata = AnnotationMetadata.buildMetadata(bean.getBeanClass(), member, SimplyTimed.class, MetricType.SIMPLE_TIMER); | ||
SimpleTimer simpleTimer = applicationRegistry.getSimpleTimers().get(metadata.getMetricID()); | ||
if (simpleTimer == null) { | ||
throw new IllegalStateException("No simple timer with ID [" + metadata.getMetricID() + "] found in registry [" | ||
+ applicationRegistry + "]"); | ||
} | ||
|
||
SimpleTimer.Context simpleTimerContext = simpleTimer.time(); | ||
|
||
try { | ||
return context.proceed(); | ||
} finally { | ||
simpleTimerContext.stop(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.