-
-
Notifications
You must be signed in to change notification settings - Fork 67
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
metrics: fork palominolabs/metrics-guice
The library isn't maintained anymore: palominolabs/metrics-guice#47 (comment) Signed-off-by: Pierre-Alexandre Meyer <[email protected]>
- Loading branch information
Showing
46 changed files
with
2,915 additions
and
11 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
killbill-metrics | ||
================ | ||
|
||
Contains a fork of metrics-guice from palominolabs optimized for Kill Bill (COIL-0.6 licensed). | ||
|
||
``` | ||
https://github.com/palominolabs/metrics-guice/commit/a7fc18f359f9d55963b321aff782f8008e11f692 | ||
``` |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
<!-- | ||
~ Copyright 2020-2022 Equinix, Inc | ||
~ Copyright 2014-2022 The Billing Project, LLC | ||
~ | ||
~ The Billing Project licenses this file to you under the Apache License, version 2.0 | ||
~ (the "License"); you may not use this file except in compliance with the | ||
~ License. You may obtain a copy of the License at: | ||
~ | ||
~ http://www.apache.org/licenses/LICENSE-2.0 | ||
~ | ||
~ Unless required by applicable law or agreed to in writing, software | ||
~ distributed under the License is distributed on an "AS IS" BASIS, WITHOUT | ||
~ WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the | ||
~ License for the specific language governing permissions and limitations | ||
~ under the License. | ||
--> | ||
|
||
<FindBugsFilter> | ||
<Match> | ||
<!-- Legacy code --> | ||
<Bug pattern="EI_EXPOSE_REP2"/> | ||
</Match> | ||
</FindBugsFilter> |
47 changes: 47 additions & 0 deletions
47
metrics/src/main/java/com/palominolabs/metrics/guice/CountedInterceptor.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,47 @@ | ||
/* | ||
* Copyright 2020-2022 Equinix, Inc | ||
* Copyright 2014-2022 The Billing Project, LLC | ||
* | ||
* The Billing Project licenses this file to you under the Apache License, version 2.0 | ||
* (the "License"); you may not use this file except in compliance with the | ||
* License. You may obtain a copy of the License at: | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT | ||
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the | ||
* License for the specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
|
||
package com.palominolabs.metrics.guice; | ||
|
||
import org.aopalliance.intercept.MethodInterceptor; | ||
import org.aopalliance.intercept.MethodInvocation; | ||
|
||
import com.codahale.metrics.Counter; | ||
import com.codahale.metrics.annotation.Counted; | ||
|
||
class CountedInterceptor implements MethodInterceptor { | ||
|
||
private final Counter counter; | ||
private final boolean decrementAfterMethod; | ||
|
||
CountedInterceptor(final Counter counter, final Counted annotation) { | ||
this.counter = counter; | ||
decrementAfterMethod = !annotation.monotonic(); | ||
} | ||
|
||
@Override | ||
public Object invoke(final MethodInvocation invocation) throws Throwable { | ||
counter.inc(); | ||
try { | ||
return invocation.proceed(); | ||
} finally { | ||
if (decrementAfterMethod) { | ||
counter.dec(); | ||
} | ||
} | ||
} | ||
} |
57 changes: 57 additions & 0 deletions
57
metrics/src/main/java/com/palominolabs/metrics/guice/CountedListener.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 2020-2022 Equinix, Inc | ||
* Copyright 2014-2022 The Billing Project, LLC | ||
* | ||
* The Billing Project licenses this file to you under the Apache License, version 2.0 | ||
* (the "License"); you may not use this file except in compliance with the | ||
* License. You may obtain a copy of the License at: | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT | ||
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the | ||
* License for the specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
|
||
package com.palominolabs.metrics.guice; | ||
|
||
import java.lang.reflect.Method; | ||
|
||
import javax.annotation.Nullable; | ||
|
||
import org.aopalliance.intercept.MethodInterceptor; | ||
|
||
import com.codahale.metrics.Counter; | ||
import com.codahale.metrics.MetricRegistry; | ||
import com.codahale.metrics.annotation.Counted; | ||
import com.palominolabs.metrics.guice.annotation.AnnotationResolver; | ||
|
||
/** | ||
* A listener which adds method interceptors to counted methods. | ||
*/ | ||
public class CountedListener extends DeclaredMethodsTypeListener { | ||
|
||
private final MetricRegistry metricRegistry; | ||
private final MetricNamer metricNamer; | ||
private final AnnotationResolver annotationResolver; | ||
|
||
public CountedListener(final MetricRegistry metricRegistry, final MetricNamer metricNamer, | ||
final AnnotationResolver annotationResolver) { | ||
this.metricRegistry = metricRegistry; | ||
this.metricNamer = metricNamer; | ||
this.annotationResolver = annotationResolver; | ||
} | ||
|
||
@Nullable | ||
@Override | ||
protected MethodInterceptor getInterceptor(final Method method) { | ||
final Counted annotation = annotationResolver.findAnnotation(Counted.class, method); | ||
if (annotation != null) { | ||
final Counter counter = metricRegistry.counter(metricNamer.getNameForCounted(method, annotation)); | ||
return new CountedInterceptor(counter, annotation); | ||
} | ||
return null; | ||
} | ||
} |
61 changes: 61 additions & 0 deletions
61
metrics/src/main/java/com/palominolabs/metrics/guice/DeclaredMethodsTypeListener.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,61 @@ | ||
/* | ||
* Copyright 2020-2022 Equinix, Inc | ||
* Copyright 2014-2022 The Billing Project, LLC | ||
* | ||
* The Billing Project licenses this file to you under the Apache License, version 2.0 | ||
* (the "License"); you may not use this file except in compliance with the | ||
* License. You may obtain a copy of the License at: | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT | ||
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the | ||
* License for the specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
|
||
package com.palominolabs.metrics.guice; | ||
|
||
import java.lang.reflect.Method; | ||
|
||
import javax.annotation.Nullable; | ||
|
||
import org.aopalliance.intercept.MethodInterceptor; | ||
|
||
import com.google.inject.TypeLiteral; | ||
import com.google.inject.matcher.Matchers; | ||
import com.google.inject.spi.TypeEncounter; | ||
import com.google.inject.spi.TypeListener; | ||
|
||
/** | ||
* A TypeListener which delegates to {@link DeclaredMethodsTypeListener#getInterceptor(Method)} for each method in the | ||
* class's declared methods. | ||
*/ | ||
abstract class DeclaredMethodsTypeListener implements TypeListener { | ||
|
||
@Override | ||
public <T> void hear(final TypeLiteral<T> literal, final TypeEncounter<T> encounter) { | ||
final Class<? super T> klass = literal.getRawType(); | ||
|
||
for (final Method method : klass.getDeclaredMethods()) { | ||
if (method.isSynthetic()) { | ||
continue; | ||
} | ||
|
||
final MethodInterceptor interceptor = getInterceptor(method); | ||
if (interceptor != null) { | ||
encounter.bindInterceptor(Matchers.only(method), interceptor); | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* Called for every method on every class in the type hierarchy of the visited type | ||
* | ||
* @param method method to get interceptor for | ||
* @return null if no interceptor should be applied, else an interceptor | ||
*/ | ||
@Nullable | ||
protected abstract MethodInterceptor getInterceptor(Method method); | ||
} |
117 changes: 117 additions & 0 deletions
117
metrics/src/main/java/com/palominolabs/metrics/guice/DeclaringClassMetricNamer.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,117 @@ | ||
/* | ||
* Copyright 2020-2022 Equinix, Inc | ||
* Copyright 2014-2022 The Billing Project, LLC | ||
* | ||
* The Billing Project licenses this file to you under the Apache License, version 2.0 | ||
* (the "License"); you may not use this file except in compliance with the | ||
* License. You may obtain a copy of the License at: | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT | ||
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the | ||
* License for the specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
|
||
package com.palominolabs.metrics.guice; | ||
|
||
import java.lang.reflect.Method; | ||
|
||
import javax.annotation.Nonnull; | ||
|
||
import com.codahale.metrics.annotation.Counted; | ||
import com.codahale.metrics.annotation.ExceptionMetered; | ||
import com.codahale.metrics.annotation.Gauge; | ||
import com.codahale.metrics.annotation.Metered; | ||
import com.codahale.metrics.annotation.Timed; | ||
|
||
import static com.codahale.metrics.MetricRegistry.name; | ||
|
||
/** | ||
* Uses the name fields in the metric annotations, if present, or the method declaring class and method name. | ||
*/ | ||
public class DeclaringClassMetricNamer implements MetricNamer { | ||
|
||
static final String COUNTER_SUFFIX = "counter"; | ||
static final String COUNTER_SUFFIX_MONOTONIC = "current"; | ||
static final String GAUGE_SUFFIX = "gauge"; | ||
static final String METERED_SUFFIX = "meter"; | ||
static final String TIMED_SUFFIX = "timer"; | ||
|
||
@Nonnull | ||
@Override | ||
public String getNameForCounted(@Nonnull final Method method, @Nonnull final Counted counted) { | ||
if (counted.absolute()) { | ||
return name(counted.name()); | ||
} | ||
|
||
if (counted.name().isEmpty()) { | ||
if (counted.monotonic()) { | ||
return name(method.getDeclaringClass(), method.getName(), COUNTER_SUFFIX_MONOTONIC); | ||
} else { | ||
return name(method.getDeclaringClass(), method.getName(), COUNTER_SUFFIX); | ||
} | ||
} | ||
|
||
return name(method.getDeclaringClass(), counted.name()); | ||
} | ||
|
||
@Nonnull | ||
@Override | ||
public String getNameForExceptionMetered(@Nonnull final Method method, @Nonnull final ExceptionMetered exceptionMetered) { | ||
if (exceptionMetered.absolute()) { | ||
return name(exceptionMetered.name()); | ||
} | ||
|
||
if (exceptionMetered.name().isEmpty()) { | ||
return | ||
name(method.getDeclaringClass(), method.getName(), ExceptionMetered.DEFAULT_NAME_SUFFIX); | ||
} | ||
|
||
return name(method.getDeclaringClass(), exceptionMetered.name()); | ||
} | ||
|
||
@Nonnull | ||
@Override | ||
public String getNameForGauge(@Nonnull final Class<?> instanceClass, @Nonnull final Method method, @Nonnull final Gauge gauge) { | ||
if (gauge.absolute()) { | ||
return name(gauge.name()); | ||
} | ||
|
||
if (gauge.name().isEmpty()) { | ||
return name(method.getDeclaringClass(), method.getName(), GAUGE_SUFFIX); | ||
} | ||
|
||
return name(method.getDeclaringClass(), gauge.name()); | ||
} | ||
|
||
@Nonnull | ||
@Override | ||
public String getNameForMetered(@Nonnull final Method method, @Nonnull final Metered metered) { | ||
if (metered.absolute()) { | ||
return name(metered.name()); | ||
} | ||
|
||
if (metered.name().isEmpty()) { | ||
return name(method.getDeclaringClass(), method.getName(), METERED_SUFFIX); | ||
} | ||
|
||
return name(method.getDeclaringClass(), metered.name()); | ||
} | ||
|
||
@Nonnull | ||
@Override | ||
public String getNameForTimed(@Nonnull final Method method, @Nonnull final Timed timed) { | ||
if (timed.absolute()) { | ||
return name(timed.name()); | ||
} | ||
|
||
if (timed.name().isEmpty()) { | ||
return name(method.getDeclaringClass(), method.getName(), TIMED_SUFFIX); | ||
} | ||
|
||
return name(method.getDeclaringClass(), timed.name()); | ||
} | ||
} |
Oops, something went wrong.