Skip to content

Commit

Permalink
metrics: fork palominolabs/metrics-guice
Browse files Browse the repository at this point in the history
The library isn't maintained anymore:

palominolabs/metrics-guice#47 (comment)

Signed-off-by: Pierre-Alexandre Meyer <[email protected]>
  • Loading branch information
pierre committed Feb 23, 2022
1 parent 85c41dd commit 06d1f53
Show file tree
Hide file tree
Showing 46 changed files with 2,915 additions and 11 deletions.
8 changes: 8 additions & 0 deletions metrics/README.md
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
```
45 changes: 39 additions & 6 deletions metrics/pom.xml
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
~ Copyright 2010-2014 Ning, Inc.
~ Copyright 2014-2020 Groupon, Inc
~ Copyright 2020-2020 Equinix, Inc
~ Copyright 2014-2020 The Billing Project, LLC
~ 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
Expand All @@ -24,10 +22,45 @@
<artifactId>killbill-commons</artifactId>
<version>0.25.1-SNAPSHOT</version>
</parent>

<artifactId>killbill-metrics</artifactId>

<properties>
<check.spotbugs-exclude-filter-file>spotbugs-exclude.xml</check.spotbugs-exclude-filter-file>
</properties>
<dependencies>
<dependency>
<groupId>aopalliance</groupId>
<artifactId>aopalliance</artifactId>
</dependency>
<dependency>
<groupId>com.google.code.findbugs</groupId>
<artifactId>jsr305</artifactId>
</dependency>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
</dependency>
<dependency>
<groupId>com.google.inject</groupId>
<artifactId>guice</artifactId>
</dependency>
<dependency>
<groupId>io.dropwizard.metrics</groupId>
<artifactId>metrics-annotation</artifactId>
</dependency>
<dependency>
<groupId>io.dropwizard.metrics</groupId>
<artifactId>metrics-core</artifactId>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
Expand Down
23 changes: 23 additions & 0 deletions metrics/spotbugs-exclude.xml
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>
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();
}
}
}
}
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;
}
}
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);
}
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());
}
}
Loading

0 comments on commit 06d1f53

Please sign in to comment.