Skip to content

Commit

Permalink
Initial Work toward HealthCheckRegistry Support
Browse files Browse the repository at this point in the history
* Updated Metrics Versions to latest version [4.0.3].
* Refactored the MetricRegistryBean to DefaultRegistryBean<T>
    * The lack of a common interface for MetricsRegistry and HealthCheckRegistry forced a decision between being more generic or more duplicated code. I chose the former.
    * Added scoped static methods for creating the DefaultRegistryBeans the library expects to work with, keeping the magic strings describing the beans close to the class definition.
* Added two test classes, currently empty... (I'm stubbing things as I go)
  • Loading branch information
bvarner committed Aug 3, 2018
1 parent 997ec7b commit 8e33d9a
Show file tree
Hide file tree
Showing 6 changed files with 96 additions and 13 deletions.
@@ -0,0 +1,19 @@
/**
* Copyright © 2013 Antonin Stefanutti (antonin.stefanutti@gmail.com)
*
* Licensed 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 io.astefanutti.metrics.cdi.se;

public class HealthCheckRegistryProducerFieldBeanTest {
}
@@ -0,0 +1,19 @@
/**
* Copyright © 2013 Antonin Stefanutti (antonin.stefanutti@gmail.com)
*
* Licensed 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 io.astefanutti.metrics.cdi.se;

public class HealthCheckRegistryProducerMethodBeanTest {
}
4 changes: 4 additions & 0 deletions impl/pom.xml
Expand Up @@ -47,6 +47,10 @@
<groupId>io.dropwizard.metrics</groupId>
<artifactId>metrics-annotation</artifactId>
</dependency>
<dependency>
<groupId>io.dropwizard.metrics</groupId>
<artifactId>metrics-healthchecks</artifactId>
</dependency>

<!-- provided dependencies -->

Expand Down
Expand Up @@ -16,6 +16,7 @@
package io.astefanutti.metrics.cdi;

import com.codahale.metrics.MetricRegistry;
import com.codahale.metrics.health.HealthCheckRegistry;

import javax.enterprise.context.ApplicationScoped;
import javax.enterprise.context.spi.CreationalContext;
Expand All @@ -35,20 +36,38 @@
import java.util.HashSet;
import java.util.Set;

/* package-private */ final class MetricRegistryBean implements Bean<MetricRegistry>, PassivationCapable {
/* package-private */ final class DefaultRegistryBean<T> implements Bean<T>, PassivationCapable {

private final Set<Annotation> qualifiers = new HashSet<>(Arrays.<Annotation>asList(new AnnotationLiteral<Any>(){}, new AnnotationLiteral<Default>(){}));

private final Set<Type> types;

private final InjectionTarget<MetricRegistry> target;
private final Class<T> clazz;

MetricRegistryBean(BeanManager manager) {
AnnotatedType<MetricRegistry> annotatedType = manager.createAnnotatedType(MetricRegistry.class);
private final InjectionTarget<T> target;

private final String name;

private final String description;

DefaultRegistryBean(BeanManager manager, Class<T> clazz, String name, String description) {
AnnotatedType<T> annotatedType = manager.createAnnotatedType(clazz);
this.clazz = clazz;
this.name = name;
this.description = description;
this.types = annotatedType.getTypeClosure();
this.target = manager.createInjectionTarget(annotatedType);
}

/** Factory method to return properly typed and described RegistryBean */
static DefaultRegistryBean<MetricRegistry> createDefaultMetricRegistry(BeanManager manager) {
return new DefaultRegistryBean<>(manager, MetricRegistry.class, "metric-registry", "Default Metric Registry Bean");
}

static DefaultRegistryBean<HealthCheckRegistry> createDefaultHealthCheckRegistry(BeanManager manager) {
return new DefaultRegistryBean<>(manager, HealthCheckRegistry.class, "health-check-registry", "Default Health Check Registry Bean");
}

@Override
public Class<? extends Annotation> getScope() {
return ApplicationScoped.class;
Expand All @@ -60,24 +79,24 @@ public Set<Annotation> getQualifiers() {
}

@Override
public MetricRegistry create(CreationalContext<MetricRegistry> context) {
MetricRegistry registry = target.produce(context);
public T create(CreationalContext<T> context) {
T registry = target.produce(context);
target.inject(registry, context);
target.postConstruct(registry);
context.push(registry);
return registry;
}

@Override
public void destroy(MetricRegistry instance, CreationalContext<MetricRegistry> context) {
public void destroy(T instance, CreationalContext<T> context) {
target.preDestroy(instance);
target.dispose(instance);
context.release();
}

@Override
public Class<MetricRegistry> getBeanClass() {
return MetricRegistry.class;
public Class<T> getBeanClass() {
return clazz;
}

@Override
Expand All @@ -87,12 +106,12 @@ public Set<InjectionPoint> getInjectionPoints() {

@Override
public String getName() {
return "metric-registry";
return this.name;
}

@Override
public String toString() {
return "Default Metric Registry Bean";
return this.description;
}

@Override
Expand Down
Expand Up @@ -24,7 +24,9 @@
import com.codahale.metrics.annotation.Gauge;
import com.codahale.metrics.annotation.Metered;
import com.codahale.metrics.annotation.Timed;
import com.codahale.metrics.health.HealthCheck;

import javax.enterprise.context.ApplicationScoped;
import javax.enterprise.event.Observes;
import javax.enterprise.inject.Default;
import javax.enterprise.inject.spi.AfterBeanDiscovery;
Expand Down Expand Up @@ -83,6 +85,13 @@ private <X> void metricsAnnotations(@Observes @WithAnnotations({CachedGauge.clas
pat.setAnnotatedType(new AnnotatedTypeDecorator<>(pat.getAnnotatedType(), METRICS_BINDING));
}

private <X> void healthchecks(@Observes @WithAnnotations({ApplicationScoped.class}) ProcessAnnotatedType<X> pat) {
if (pat.getAnnotatedType().getJavaClass().isAssignableFrom(HealthCheck.class)) {
// TODO: Intercept / decorate the HealthCheck to add it to the registry.
return;
}
}

private void metricProducerField(@Observes ProcessProducerField<? extends Metric, ?> ppf) {
metrics.put(ppf.getBean(), ppf.getAnnotatedProducerField());
}
Expand All @@ -95,7 +104,9 @@ private void metricProducerMethod(@Observes ProcessProducerMethod<? extends Metr

private void defaultMetricRegistry(@Observes AfterBeanDiscovery abd, BeanManager manager) {
if (manager.getBeans(MetricRegistry.class).isEmpty())
abd.addBean(new MetricRegistryBean(manager));
abd.addBean(DefaultRegistryBean.createDefaultMetricRegistry(manager));
if (manager.getBeans(HealthCheck.class).isEmpty())
abd.addBean(DefaultRegistryBean.createDefaultHealthCheckRegistry(manager));
}

private void configuration(@Observes AfterDeploymentValidation adv, BeanManager manager) {
Expand Down
13 changes: 12 additions & 1 deletion pom.xml
Expand Up @@ -58,7 +58,7 @@

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<metrics.version>3.2.4</metrics.version>
<metrics.version>4.0.3</metrics.version>
<cdi.version>1.2</cdi.version>
<cdi2.version>2.0</cdi2.version>
<javaee7.version>7.0</javaee7.version>
Expand Down Expand Up @@ -216,6 +216,17 @@
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>io.dropwizard.metrics</groupId>
<artifactId>metrics-healthchecks</artifactId>
<version>${metrics.version}</version>
<exclusions>
<exclusion>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
</exclusion>
</exclusions>
</dependency>

<!-- provided dependencies -->

Expand Down

0 comments on commit 8e33d9a

Please sign in to comment.