-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
allow to configure Reservoir implementations
introduction of a setting in the MetricsConfiguration class to be able to configure the Reservoir implementation to use for produced Timer & Histogram instances. see #12
- Loading branch information
1 parent
3b64cfa
commit 9da35a7
Showing
8 changed files
with
233 additions
and
4 deletions.
There are no files selected for viewing
48 changes: 48 additions & 0 deletions
48
envs/se/src/main/java/io/astefanutti/metrics/cdi/se/ReservoirBuilderNullContributor.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,48 @@ | ||
/** | ||
* 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; | ||
|
||
import java.util.concurrent.TimeUnit; | ||
import java.util.concurrent.atomic.AtomicInteger; | ||
|
||
import javax.enterprise.context.ApplicationScoped; | ||
import javax.enterprise.event.Observes; | ||
|
||
import com.codahale.metrics.Reservoir; | ||
import com.codahale.metrics.SlidingTimeWindowArrayReservoir; | ||
|
||
import io.astefanutti.metrics.cdi.MetricsConfiguration; | ||
import io.astefanutti.metrics.cdi.ReservoirBuidler; | ||
import io.astefanutti.metrics.cdi.ReservoirUsage; | ||
|
||
@ApplicationScoped | ||
public class ReservoirBuilderNullContributor { | ||
AtomicInteger counter = new AtomicInteger(); | ||
|
||
public int calls() { | ||
return counter.get(); | ||
} | ||
|
||
void configuration(@Observes MetricsConfiguration configuration) { | ||
configuration.useReservoirBuilder(new ReservoirBuidler() { | ||
@Override | ||
public Reservoir build(String metricName, ReservoirUsage type) { | ||
counter.incrementAndGet(); | ||
return null; | ||
} | ||
}); | ||
} | ||
} |
69 changes: 69 additions & 0 deletions
69
envs/se/src/test/java/io/astefanutti/metrics/cdi/se/ReservoirBuilderTest.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,69 @@ | ||
/** | ||
* 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; | ||
|
||
import static org.hamcrest.CoreMatchers.is; | ||
import static org.junit.Assert.assertThat; | ||
|
||
import javax.inject.Inject; | ||
|
||
import org.jboss.arquillian.container.test.api.Deployment; | ||
import org.jboss.arquillian.junit.Arquillian; | ||
import org.jboss.shrinkwrap.api.Archive; | ||
import org.jboss.shrinkwrap.api.ShrinkWrap; | ||
import org.jboss.shrinkwrap.api.asset.EmptyAsset; | ||
import org.jboss.shrinkwrap.api.spec.JavaArchive; | ||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
|
||
import io.astefanutti.metrics.cdi.MetricsExtension; | ||
|
||
@RunWith(Arquillian.class) | ||
public class ReservoirBuilderTest { | ||
@Inject | ||
ReservoirBuilderNullContributor reservoirBuilder; | ||
|
||
@Inject | ||
TimerFieldBean timerBean; | ||
|
||
@Inject | ||
HistogramFieldBean histogramBean; | ||
|
||
|
||
@Deployment | ||
public static Archive<?> createTestArchive() { | ||
return ShrinkWrap.create(JavaArchive.class) | ||
// Test bean class with Timer injection | ||
.addClass(TimerFieldBean.class) | ||
// Test bean class with Histogram injection | ||
.addClass(HistogramFieldBean.class) | ||
// The ReservoirBuilder counting calls | ||
.addClass(ReservoirBuilderNullContributor.class) | ||
// Metrics CDI extension | ||
.addPackage(MetricsExtension.class.getPackage()) | ||
// Bean archive deployment descriptor | ||
.addAsManifestResource(EmptyAsset.INSTANCE, "beans.xml"); | ||
} | ||
|
||
@Test | ||
public void checkReservoirBuilderCalls() { | ||
int NB_TIMER_FIELDS_IN_BEAN = 6; | ||
int NB_HISTOGRAM_FIELDS_IN_BEAN = 1; | ||
|
||
int expectedCalls = NB_TIMER_FIELDS_IN_BEAN + NB_HISTOGRAM_FIELDS_IN_BEAN; | ||
assertThat("Number of ReservoirBuilder calls differ from expected # of Timer fields", reservoirBuilder.calls(), is(expectedCalls)); | ||
} | ||
} |
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
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
31 changes: 31 additions & 0 deletions
31
impl/src/main/java/io/astefanutti/metrics/cdi/ReservoirBuidler.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,31 @@ | ||
/** | ||
* 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; | ||
|
||
import com.codahale.metrics.Reservoir; | ||
|
||
/** | ||
* Builder class allowing the production of different {@link Reservoir} implementations based on the metric kind & names. | ||
*/ | ||
public interface ReservoirBuidler { | ||
/** | ||
* Builds a {@link Reservoir} for the given metric | ||
* @param metricName the name of the metric for which a Reservoir needs to be used | ||
* @param type the kind of metric for which a reservoir is required | ||
* @return the reservoir to use, or null if default reservoir implementation has to be used | ||
*/ | ||
Reservoir build(String metricName, ReservoirUsage type); | ||
} |
20 changes: 20 additions & 0 deletions
20
impl/src/main/java/io/astefanutti/metrics/cdi/ReservoirUsage.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,20 @@ | ||
/** | ||
* 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; | ||
|
||
public enum ReservoirUsage { | ||
HISTOGRAM, TIMER; | ||
} |