ids = ((MetricsRegistryImpl) registry).getMemberToMetricMappings()
74 | .getMeters(new CDIMemberInfoAdapter<>().convert(element));
75 | if (ids == null || ids.isEmpty()) {
76 | throw SmallRyeMetricsMessages.msg.noMetricMappedForMember(element);
77 | }
78 | ids.stream()
79 | .map(metricID -> {
80 | Meter metric = registry.getMeters().get(metricID);
81 | if (metric == null) {
82 | throw SmallRyeMetricsMessages.msg.noMetricFoundInRegistry(MetricType.METERED, metricID);
83 | }
84 | return metric;
85 | })
86 | .forEach(Meter::mark);
87 | return context.proceed();
88 | }
89 | }
90 |
--------------------------------------------------------------------------------
/implementation/src/main/java/io/smallrye/metrics/interceptors/MetricName.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright © 2013 Antonin Stefanutti (antonin.stefanutti@gmail.com)
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package io.smallrye.metrics.interceptors;
17 |
18 | import jakarta.enterprise.inject.spi.AnnotatedMember;
19 | import jakarta.enterprise.inject.spi.InjectionPoint;
20 |
21 | public interface MetricName {
22 |
23 | String of(InjectionPoint point);
24 |
25 | String of(AnnotatedMember> member);
26 |
27 | // TODO: expose an SPI so that external strategies can be provided. For example, Camel CDI could provide a property placeholder resolution strategy.
28 | String of(String attribute);
29 | }
30 |
--------------------------------------------------------------------------------
/implementation/src/main/java/io/smallrye/metrics/interceptors/MetricNameFactory.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Copyright © 2013 Antonin Stefanutti (antonin.stefanutti@gmail.com)
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package io.smallrye.metrics.interceptors;
17 |
18 | import java.util.Collections;
19 |
20 | import jakarta.enterprise.context.ApplicationScoped;
21 | import jakarta.enterprise.inject.Produces;
22 | import jakarta.enterprise.inject.spi.BeanManager;
23 |
24 | @ApplicationScoped
25 | public class MetricNameFactory {
26 |
27 | @Produces
28 | @ApplicationScoped
29 | MetricName metricName(BeanManager manager) {
30 | return new SeMetricName(Collections.emptySet()); // TODO
31 | }
32 | }
33 |
--------------------------------------------------------------------------------
/implementation/src/main/java/io/smallrye/metrics/interceptors/MetricsBinding.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright © 2013 Antonin Stefanutti (antonin.stefanutti@gmail.com)
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package io.smallrye.metrics.interceptors;
17 |
18 | import java.lang.annotation.ElementType;
19 | import java.lang.annotation.Retention;
20 | import java.lang.annotation.RetentionPolicy;
21 | import java.lang.annotation.Target;
22 |
23 | import jakarta.interceptor.InterceptorBinding;
24 |
25 | @InterceptorBinding
26 | @Target({ ElementType.TYPE })
27 | @Retention(RetentionPolicy.RUNTIME)
28 | public @interface MetricsBinding {
29 | }
30 |
--------------------------------------------------------------------------------
/implementation/src/main/java/io/smallrye/metrics/interceptors/MetricsParameter.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Copyright © 2013 Antonin Stefanutti (antonin.stefanutti@gmail.com)
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package io.smallrye.metrics.interceptors;
17 |
18 | /* package-private */ enum MetricsParameter {
19 |
20 | useAbsoluteName
21 | }
22 |
--------------------------------------------------------------------------------
/implementation/src/main/java/io/smallrye/metrics/interceptors/SimplyTimedInterceptor.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2017 Red Hat, Inc. and/or its affiliates
3 | * and other contributors as indicated by the @author tags.
4 | *
5 | * Licensed under the Apache License, Version 2.0 (the "License");
6 | * you may not use this file except in compliance with the License.
7 | * You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | *
17 | */
18 | package io.smallrye.metrics.interceptors;
19 |
20 | import java.lang.reflect.AnnotatedElement;
21 | import java.lang.reflect.Member;
22 | import java.util.List;
23 | import java.util.Set;
24 | import java.util.stream.Collectors;
25 |
26 | import jakarta.annotation.Priority;
27 | import jakarta.inject.Inject;
28 | import jakarta.interceptor.AroundConstruct;
29 | import jakarta.interceptor.AroundInvoke;
30 | import jakarta.interceptor.AroundTimeout;
31 | import jakarta.interceptor.Interceptor;
32 | import jakarta.interceptor.InvocationContext;
33 |
34 | import org.eclipse.microprofile.metrics.MetricID;
35 | import org.eclipse.microprofile.metrics.MetricRegistry;
36 | import org.eclipse.microprofile.metrics.MetricType;
37 | import org.eclipse.microprofile.metrics.SimpleTimer;
38 | import org.eclipse.microprofile.metrics.annotation.SimplyTimed;
39 |
40 | import io.smallrye.metrics.MetricRegistries;
41 | import io.smallrye.metrics.MetricsRegistryImpl;
42 | import io.smallrye.metrics.SmallRyeMetricsMessages;
43 | import io.smallrye.metrics.elementdesc.adapter.cdi.CDIMemberInfoAdapter;
44 |
45 | @SuppressWarnings("unused")
46 | @SimplyTimed
47 | @Interceptor
48 | @Priority(Interceptor.Priority.LIBRARY_BEFORE + 10)
49 | public class SimplyTimedInterceptor {
50 |
51 | private final MetricRegistry registry;
52 |
53 | @Inject
54 | SimplyTimedInterceptor() {
55 | this.registry = MetricRegistries.get(MetricRegistry.Type.APPLICATION);
56 | }
57 |
58 | @AroundConstruct
59 | Object simplyTimedConstructor(InvocationContext context) throws Exception {
60 | return timedCallable(context, context.getConstructor());
61 | }
62 |
63 | @AroundInvoke
64 | Object simplyTimedMethod(InvocationContext context) throws Exception {
65 | return timedCallable(context, context.getMethod());
66 | }
67 |
68 | @AroundTimeout
69 | Object simplyTimedTimeout(InvocationContext context) throws Exception {
70 | return timedCallable(context, context.getMethod());
71 | }
72 |
73 | private Object timedCallable(InvocationContext invocationContext, E element)
74 | throws Exception {
75 | Set ids = ((MetricsRegistryImpl) registry).getMemberToMetricMappings()
76 | .getSimpleTimers(new CDIMemberInfoAdapter<>().convert(element));
77 | if (ids == null || ids.isEmpty()) {
78 | throw SmallRyeMetricsMessages.msg.noMetricMappedForMember(element);
79 | }
80 | List contexts = ids.stream()
81 | .map(metricID -> {
82 | SimpleTimer metric = registry.getSimpleTimers().get(metricID);
83 | if (metric == null) {
84 | throw SmallRyeMetricsMessages.msg.noMetricFoundInRegistry(MetricType.SIMPLE_TIMER, metricID);
85 | }
86 | return metric;
87 | })
88 | .map(SimpleTimer::time)
89 | .collect(Collectors.toList());
90 | try {
91 | return invocationContext.proceed();
92 | } finally {
93 | for (SimpleTimer.Context timeContext : contexts) {
94 | timeContext.stop();
95 | }
96 | }
97 | }
98 | }
99 |
--------------------------------------------------------------------------------
/implementation/src/main/java/io/smallrye/metrics/mbean/MCounterImpl.java:
--------------------------------------------------------------------------------
1 | /*
2 | *
3 | * Copyright 2017 Red Hat, Inc, and individual contributors.
4 | *
5 | * Licensed under the Apache License, Version 2.0 (the "License");
6 | * you may not use this file except in compliance with the License.
7 | * You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | * /
17 | */
18 | package io.smallrye.metrics.mbean;
19 |
20 | import org.eclipse.microprofile.metrics.Counter;
21 |
22 | import io.smallrye.metrics.JmxWorker;
23 | import io.smallrye.metrics.SmallRyeMetricsMessages;
24 |
25 | /**
26 | * @author hrupp
27 | */
28 | public class MCounterImpl implements Counter {
29 | private final String mbeanExpression;
30 | private final JmxWorker worker;
31 |
32 | public MCounterImpl(JmxWorker worker, String mbeanExpression) {
33 | this.mbeanExpression = mbeanExpression;
34 | this.worker = worker;
35 | }
36 |
37 | @Override
38 | public void inc() {
39 | throw SmallRyeMetricsMessages.msg.mustNotBeCalled();
40 | }
41 |
42 | @Override
43 | public void inc(long n) {
44 | throw SmallRyeMetricsMessages.msg.mustNotBeCalled();
45 | }
46 |
47 | @Override
48 | public long getCount() {
49 | return worker.getValue(mbeanExpression).longValue();
50 | }
51 | }
52 |
--------------------------------------------------------------------------------
/implementation/src/main/java/io/smallrye/metrics/mbean/MGaugeImpl.java:
--------------------------------------------------------------------------------
1 | /*
2 | *
3 | * Copyright 2017 Red Hat, Inc, and individual contributors.
4 | *
5 | * Licensed under the Apache License, Version 2.0 (the "License");
6 | * you may not use this file except in compliance with the License.
7 | * You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | * /
17 | */
18 | package io.smallrye.metrics.mbean;
19 |
20 | import org.eclipse.microprofile.metrics.Gauge;
21 |
22 | import io.smallrye.metrics.JmxWorker;
23 |
24 | /**
25 | * @author hrupp
26 | */
27 | public class MGaugeImpl implements Gauge {
28 |
29 | private final String mBeanExpression;
30 | private final JmxWorker worker;
31 |
32 | public MGaugeImpl(JmxWorker worker, String mBeanExpression) {
33 | this.worker = worker;
34 | this.mBeanExpression = mBeanExpression;
35 | }
36 |
37 | @Override
38 | public Number getValue() {
39 | return worker.getValue(mBeanExpression);
40 | }
41 | }
42 |
--------------------------------------------------------------------------------
/implementation/src/main/java/io/smallrye/metrics/setup/AnnotatedDecorator.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Copyright © 2013 Antonin Stefanutti (antonin.stefanutti@gmail.com)
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package io.smallrye.metrics.setup;
17 |
18 | import java.lang.annotation.Annotation;
19 | import java.lang.reflect.Type;
20 | import java.util.Collections;
21 | import java.util.HashSet;
22 | import java.util.Set;
23 |
24 | import jakarta.enterprise.inject.spi.Annotated;
25 |
26 | /* package-private */ class AnnotatedDecorator implements Annotated {
27 |
28 | private final Annotated decorated;
29 |
30 | private final Set annotations;
31 |
32 | AnnotatedDecorator(Annotated decorated, Set annotations) {
33 | this.decorated = decorated;
34 | this.annotations = annotations;
35 | }
36 |
37 | @Override
38 | public Type getBaseType() {
39 | return decorated.getBaseType();
40 | }
41 |
42 | @Override
43 | public Set getTypeClosure() {
44 | return decorated.getTypeClosure();
45 | }
46 |
47 | @Override
48 | public T getAnnotation(Class annotationType) {
49 | T annotation = getDecoratingAnnotation(annotationType);
50 | if (annotation != null) {
51 | return annotation;
52 | } else {
53 | return decorated.getAnnotation(annotationType);
54 | }
55 | }
56 |
57 | @Override
58 | public Set getAnnotations(Class annotationType) {
59 | return decorated.getAnnotations(annotationType);
60 | }
61 |
62 | @Override
63 | public Set getAnnotations() {
64 | Set annotations = new HashSet<>(this.annotations);
65 | annotations.addAll(decorated.getAnnotations());
66 | return Collections.unmodifiableSet(annotations);
67 | }
68 |
69 | @Override
70 | public boolean isAnnotationPresent(Class extends Annotation> annotationType) {
71 | return getDecoratingAnnotation(annotationType) != null || decorated.isAnnotationPresent(annotationType);
72 | }
73 |
74 | @SuppressWarnings("unchecked")
75 | private T getDecoratingAnnotation(Class annotationType) {
76 | for (Annotation annotation : annotations) {
77 | if (annotationType.isAssignableFrom(annotation.annotationType())) {
78 | return (T) annotation;
79 | }
80 | }
81 |
82 | return null;
83 | }
84 | }
85 |
--------------------------------------------------------------------------------
/implementation/src/main/java/io/smallrye/metrics/setup/AnnotatedTypeDecorator.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Copyright © 2013 Antonin Stefanutti (antonin.stefanutti@gmail.com)
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package io.smallrye.metrics.setup;
17 |
18 | import java.lang.annotation.Annotation;
19 | import java.util.Collections;
20 | import java.util.HashSet;
21 | import java.util.Set;
22 |
23 | import jakarta.enterprise.inject.spi.AnnotatedConstructor;
24 | import jakarta.enterprise.inject.spi.AnnotatedField;
25 | import jakarta.enterprise.inject.spi.AnnotatedMethod;
26 | import jakarta.enterprise.inject.spi.AnnotatedType;
27 |
28 | /* package-private */ final class AnnotatedTypeDecorator extends AnnotatedDecorator implements AnnotatedType {
29 |
30 | private final AnnotatedType decoratedType;
31 |
32 | private final Set> decoratedMethods;
33 |
34 | AnnotatedTypeDecorator(AnnotatedType decoratedType, Annotation decoratingAnnotation) {
35 | this(decoratedType, decoratingAnnotation, Collections.> emptySet());
36 | }
37 |
38 | AnnotatedTypeDecorator(AnnotatedType decoratedType, Annotation decoratingAnnotation,
39 | Set> decoratedMethods) {
40 | super(decoratedType, Collections.singleton(decoratingAnnotation));
41 | this.decoratedType = decoratedType;
42 | this.decoratedMethods = decoratedMethods;
43 | }
44 |
45 | @Override
46 | public Class getJavaClass() {
47 | return decoratedType.getJavaClass();
48 | }
49 |
50 | @Override
51 | public Set> getConstructors() {
52 | return decoratedType.getConstructors();
53 | }
54 |
55 | @Override
56 | public Set> getMethods() {
57 | Set> methods = new HashSet<>(decoratedType.getMethods());
58 | for (AnnotatedMethod super X> method : decoratedMethods) {
59 | methods.remove(method);
60 | methods.add(method);
61 | }
62 |
63 | return Collections.unmodifiableSet(methods);
64 | }
65 |
66 | @Override
67 | public Set> getFields() {
68 | return decoratedType.getFields();
69 | }
70 | }
71 |
--------------------------------------------------------------------------------
/implementation/src/main/resources/META-INF/services/jakarta.enterprise.inject.spi.Extension:
--------------------------------------------------------------------------------
1 | io.smallrye.metrics.setup.MetricCdiInjectionExtension
2 |
--------------------------------------------------------------------------------
/implementation/src/main/resources/project.properties:
--------------------------------------------------------------------------------
1 | #
2 | # Copyright 2019 Red Hat, Inc. and/or its affiliates
3 | # and other contributors as indicated by the @author tags.
4 | #
5 | # Licensed under the Apache License, Version 2.0 (the "License");
6 | # you may not use this file except in compliance with the License.
7 | # You may obtain a copy of the License at
8 | #
9 | # http://www.apache.org/licenses/LICENSE-2.0
10 | #
11 | # Unless required by applicable law or agreed to in writing, software
12 | # distributed under the License is distributed on an "AS IS" BASIS,
13 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | # See the License for the specific language governing permissions and
15 | # limitations under the License.
16 | #
17 |
18 | smallrye.metrics.version=${project.version}
--------------------------------------------------------------------------------
/implementation/src/test/java/io/smallrye/metrics/ExtendedMetadataBuilderTest.java:
--------------------------------------------------------------------------------
1 | package io.smallrye.metrics;
2 |
3 | import org.eclipse.microprofile.metrics.MetricType;
4 | import org.junit.Assert;
5 | import org.junit.Test;
6 |
7 | public class ExtendedMetadataBuilderTest {
8 |
9 | @Test
10 | public void testBuilder() {
11 | //given
12 | String displayName = "display name";
13 | String name = "name";
14 | String description = "description";
15 | String mbean = "mbean";
16 | String openMetricsKeyOverride = "openMetricsKeyOverride";
17 | String unit = "unit";
18 |
19 | //when
20 | ExtendedMetadata extendedMetadata = ExtendedMetadata.builder()
21 | .withType(MetricType.CONCURRENT_GAUGE)
22 | .withDisplayName(displayName)
23 | .withName(name)
24 | .withDescription(description)
25 | .withMbean(mbean)
26 | .withOpenMetricsKeyOverride(openMetricsKeyOverride)
27 | .withUnit(unit)
28 | .skipsScopeInOpenMetricsExportCompletely(false)
29 | .prependsScopeToOpenMetricsName(false)
30 | .multi(false)
31 | .build();
32 |
33 | //then
34 | Assert.assertEquals(displayName, extendedMetadata.getDisplayName());
35 | Assert.assertEquals(name, extendedMetadata.getName());
36 | Assert.assertEquals(mbean, extendedMetadata.getMbean());
37 | Assert.assertEquals(openMetricsKeyOverride, extendedMetadata.getOpenMetricsKeyOverride().get());
38 | Assert.assertEquals(unit, extendedMetadata.unit().get());
39 | Assert.assertEquals(description, extendedMetadata.description().get());
40 | Assert.assertFalse(extendedMetadata.isSkipsScopeInOpenMetricsExportCompletely());
41 | Assert.assertFalse(extendedMetadata.isMulti());
42 | Assert.assertFalse(extendedMetadata.prependsScopeToOpenMetricsName().get());
43 | Assert.assertEquals(MetricType.CONCURRENT_GAUGE, extendedMetadata.getTypeRaw());
44 | }
45 | }
46 |
--------------------------------------------------------------------------------
/implementation/src/test/java/io/smallrye/metrics/app/WeightedSnapshotTest.java:
--------------------------------------------------------------------------------
1 | package io.smallrye.metrics.app;
2 |
3 | import static org.junit.Assert.*;
4 |
5 | import java.util.Collections;
6 |
7 | import org.junit.Test;
8 |
9 | /**
10 | * @author helloween
11 | */
12 | public class WeightedSnapshotTest {
13 |
14 | @Test
15 | public void init() {
16 | WeightedSnapshot.WeightedSample sample = new WeightedSnapshot.WeightedSample(1L, 0.0);
17 | WeightedSnapshot snapshot = new WeightedSnapshot(Collections.singletonList(sample));
18 | assertFalse(Double.isNaN(snapshot.getMean()));
19 | }
20 | }
21 |
--------------------------------------------------------------------------------
/implementation/src/test/java/io/smallrye/metrics/exporters/ExporterUtilTest.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2017 Red Hat, Inc. and/or its affiliates
3 | * and other contributors as indicated by the @author tags.
4 | *
5 | * Licensed under the Apache License, Version 2.0 (the "License");
6 | * you may not use this file except in compliance with the License.
7 | * You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 | package io.smallrye.metrics.exporters;
18 |
19 | import org.eclipse.microprofile.metrics.MetricUnits;
20 | import org.junit.Assert;
21 | import org.junit.Test;
22 |
23 | /**
24 | *
25 | * @author Michal Szynkiewicz, michal.l.szynkiewicz@gmail.com
26 | *
27 | * Date: 3/23/18
28 | */
29 | public class ExporterUtilTest {
30 | @Test
31 | public void shouldScaleNanosToSecods() {
32 | Double out = ExporterUtil.convertNanosTo(1.0, MetricUnits.MINUTES);
33 | Assert.assertEquals(out, 0.0, 1e-10);
34 | }
35 |
36 | @Test
37 | public void shouldScaleNanosToMillis() {
38 | Double out = ExporterUtil.convertNanosTo(1.0, MetricUnits.MILLISECONDS);
39 | Assert.assertEquals(out, 0.000001, 1e-10);
40 | }
41 |
42 | @Test
43 | public void testScaleHoursToSeconds() {
44 | String foo = MetricUnits.HOURS;
45 | Double out = ExporterUtil.convertNanosTo(3 * 3600 * 1000_000_000., foo);
46 | Assert.assertEquals(out, 3., 1e-10);
47 | }
48 | }
--------------------------------------------------------------------------------
/implementation/src/test/java/io/smallrye/metrics/exporters/OpenMetricsUnitScalingTest.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2017 Red Hat, Inc. and/or its affiliates
3 | * and other contributors as indicated by the @author tags.
4 | *
5 | * Licensed under the Apache License, Version 2.0 (the "License");
6 | * you may not use this file except in compliance with the License.
7 | * You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 | package io.smallrye.metrics.exporters;
18 |
19 | import static org.junit.Assert.*;
20 |
21 | import java.util.Optional;
22 |
23 | import org.eclipse.microprofile.metrics.MetricUnits;
24 | import org.junit.Test;
25 |
26 | /**
27 | * @author hrupp
28 | */
29 | public class OpenMetricsUnitScalingTest {
30 |
31 | @Test
32 | public void testScaleToSecondsForNanos() {
33 | String foo = MetricUnits.NANOSECONDS;
34 | double out = OpenMetricsUnit.scaleToBase(foo, 3.0);
35 | assertEquals("Out was " + out, 0.000_000_003, out, 0.001);
36 | }
37 |
38 | @Test
39 | public void testScaleToSeconds() {
40 | String foo = MetricUnits.SECONDS;
41 | double out = OpenMetricsUnit.scaleToBase(foo, 3.0);
42 | assertEquals("Out was " + out, 3, out, 0.001);
43 | }
44 |
45 | @Test
46 | public void testScaleToSecondsForDays() {
47 | String foo = MetricUnits.DAYS;
48 | double out = OpenMetricsUnit.scaleToBase(foo, 3.0);
49 | assertEquals("Out was " + out, 3 * 24 * 60 * 60, out, 0.001);
50 | }
51 |
52 | @Test
53 | public void testScaleMegabyteToByte() {
54 | String foo = MetricUnits.MEGABYTES;
55 | double out = OpenMetricsUnit.scaleToBase(foo, 1.0);
56 | assertEquals("Out was " + out, 1_000_000, out, 0.001);
57 | }
58 |
59 | @Test
60 | public void testScaleBitsToByte() {
61 | String foo = MetricUnits.BITS;
62 | double out = OpenMetricsUnit.scaleToBase(foo, 13.0);
63 | assertEquals("Out was " + out, 13.0 / 8.0, out, 0.001);
64 | }
65 |
66 | @Test
67 | public void testFindBaseUnit1() {
68 | String foo = MetricUnits.HOURS;
69 | String out = OpenMetricsUnit.getBaseUnitAsOpenMetricsString(Optional.ofNullable(foo));
70 | assertEquals(MetricUnits.SECONDS, out);
71 | String promUnit = OpenMetricsUnit.getBaseUnitAsOpenMetricsString(Optional.ofNullable(out));
72 | assertEquals("seconds", promUnit);
73 | }
74 |
75 | @Test
76 | public void testFindBaseUnit2() {
77 | String foo = MetricUnits.MILLISECONDS;
78 | String out = OpenMetricsUnit.getBaseUnitAsOpenMetricsString(Optional.ofNullable(foo));
79 | assertEquals(MetricUnits.SECONDS, out);
80 | String promUnit = OpenMetricsUnit.getBaseUnitAsOpenMetricsString(Optional.ofNullable(out));
81 | assertEquals("seconds", promUnit);
82 | }
83 |
84 | @Test
85 | public void testFindBaseUnit3() {
86 | String foo = MetricUnits.PERCENT;
87 | String out = OpenMetricsUnit.getBaseUnitAsOpenMetricsString(Optional.ofNullable(foo));
88 | assertEquals(MetricUnits.PERCENT, out);
89 | }
90 |
91 | @Test
92 | public void testFindBaseUnit4() {
93 | String foo = MetricUnits.NONE;
94 | String out = OpenMetricsUnit.getBaseUnitAsOpenMetricsString(Optional.ofNullable(foo));
95 | assertEquals(MetricUnits.NONE, out);
96 | }
97 | }
98 |
--------------------------------------------------------------------------------
/implementation/src/test/java/io/smallrye/metrics/exporters/SomeHistogram.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2019 Red Hat, Inc. and/or its affiliates
3 | * and other contributors as indicated by the @author tags.
4 | *
5 | * Licensed under the Apache License, Version 2.0 (the "License");
6 | * you may not use this file except in compliance with the License.
7 | * You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 |
18 | package io.smallrye.metrics.exporters;
19 |
20 | import org.eclipse.microprofile.metrics.Histogram;
21 | import org.eclipse.microprofile.metrics.Snapshot;
22 |
23 | public class SomeHistogram implements Histogram {
24 |
25 | private Snapshot snapshot = new SomeSnapshot();
26 |
27 | @Override
28 | public void update(int value) {
29 | }
30 |
31 | @Override
32 | public void update(long value) {
33 | }
34 |
35 | @Override
36 | public long getCount() {
37 | return 0;
38 | }
39 |
40 | @Override
41 | public long getSum() {
42 | return 0;
43 | }
44 |
45 | @Override
46 | public Snapshot getSnapshot() {
47 | return snapshot;
48 | }
49 | }
50 |
--------------------------------------------------------------------------------
/implementation/src/test/java/io/smallrye/metrics/exporters/SomeMeter.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2019 Red Hat, Inc. and/or its affiliates
3 | * and other contributors as indicated by the @author tags.
4 | *
5 | * Licensed under the Apache License, Version 2.0 (the "License");
6 | * you may not use this file except in compliance with the License.
7 | * You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 |
18 | package io.smallrye.metrics.exporters;
19 |
20 | import org.eclipse.microprofile.metrics.Meter;
21 |
22 | /**
23 | * Provides a second (useless) Meter implementation
24 | */
25 | public class SomeMeter implements Meter {
26 | @Override
27 | public void mark() {
28 |
29 | }
30 |
31 | @Override
32 | public void mark(long l) {
33 |
34 | }
35 |
36 | @Override
37 | public long getCount() {
38 | return 0;
39 | }
40 |
41 | @Override
42 | public double getFifteenMinuteRate() {
43 | return 0;
44 | }
45 |
46 | @Override
47 | public double getFiveMinuteRate() {
48 | return 0;
49 | }
50 |
51 | @Override
52 | public double getMeanRate() {
53 | return 0;
54 | }
55 |
56 | @Override
57 | public double getOneMinuteRate() {
58 | return 0;
59 | }
60 | }
61 |
--------------------------------------------------------------------------------
/implementation/src/test/java/io/smallrye/metrics/exporters/SomeSnapshot.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2019 Red Hat, Inc. and/or its affiliates
3 | * and other contributors as indicated by the @author tags.
4 | *
5 | * Licensed under the Apache License, Version 2.0 (the "License");
6 | * you may not use this file except in compliance with the License.
7 | * You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 |
18 | package io.smallrye.metrics.exporters;
19 |
20 | import java.io.OutputStream;
21 |
22 | import org.eclipse.microprofile.metrics.Snapshot;
23 |
24 | public class SomeSnapshot extends Snapshot {
25 | @Override
26 | public double getValue(double quantile) {
27 | return 0;
28 | }
29 |
30 | @Override
31 | public long[] getValues() {
32 | return new long[0];
33 | }
34 |
35 | @Override
36 | public int size() {
37 | return 0;
38 | }
39 |
40 | @Override
41 | public long getMax() {
42 | return 0;
43 | }
44 |
45 | @Override
46 | public double getMean() {
47 | return 0;
48 | }
49 |
50 | @Override
51 | public long getMin() {
52 | return 0;
53 | }
54 |
55 | @Override
56 | public double getStdDev() {
57 | return 0;
58 | }
59 |
60 | @Override
61 | public void dump(OutputStream output) {
62 | }
63 | }
64 |
--------------------------------------------------------------------------------
/implementation/src/test/java/io/smallrye/metrics/exporters/SomeTimer.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2019 Red Hat, Inc. and/or its affiliates
3 | * and other contributors as indicated by the @author tags.
4 | *
5 | * Licensed under the Apache License, Version 2.0 (the "License");
6 | * you may not use this file except in compliance with the License.
7 | * You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 |
18 | package io.smallrye.metrics.exporters;
19 |
20 | import java.time.Duration;
21 | import java.util.concurrent.Callable;
22 |
23 | import org.eclipse.microprofile.metrics.Snapshot;
24 | import org.eclipse.microprofile.metrics.Timer;
25 |
26 | public class SomeTimer implements Timer {
27 |
28 | private Snapshot snapshot = new SomeSnapshot();
29 |
30 | @Override
31 | public void update(Duration duration) {
32 | }
33 |
34 | @Override
35 | public T time(Callable event) throws Exception {
36 | return null;
37 | }
38 |
39 | @Override
40 | public void time(Runnable event) {
41 | }
42 |
43 | @Override
44 | public Context time() {
45 | return null;
46 | }
47 |
48 | @Override
49 | public Duration getElapsedTime() {
50 | return Duration.ZERO;
51 | }
52 |
53 | @Override
54 | public long getCount() {
55 | return 0;
56 | }
57 |
58 | @Override
59 | public double getFifteenMinuteRate() {
60 | return 0;
61 | }
62 |
63 | @Override
64 | public double getFiveMinuteRate() {
65 | return 0;
66 | }
67 |
68 | @Override
69 | public double getMeanRate() {
70 | return 0;
71 | }
72 |
73 | @Override
74 | public double getOneMinuteRate() {
75 | return 0;
76 | }
77 |
78 | @Override
79 | public Snapshot getSnapshot() {
80 | return snapshot;
81 | }
82 | }
83 |
--------------------------------------------------------------------------------
/implementation/src/test/java/io/smallrye/metrics/histogram/ExponentiallyWeightedReservoirTest.java:
--------------------------------------------------------------------------------
1 | package io.smallrye.metrics.histogram;
2 |
3 | import static org.hamcrest.CoreMatchers.equalTo;
4 | import static org.hamcrest.CoreMatchers.not;
5 |
6 | import org.eclipse.microprofile.metrics.Histogram;
7 | import org.junit.Assert;
8 | import org.junit.Test;
9 |
10 | import io.smallrye.metrics.app.ExponentiallyDecayingReservoir;
11 | import io.smallrye.metrics.app.HistogramImpl;
12 |
13 | public class ExponentiallyWeightedReservoirTest {
14 |
15 | @Test
16 | public void removeZeroWeightsInSamplesToPreventNaNInMeanValues() {
17 | final TestingClock clock = new TestingClock();
18 | final ExponentiallyDecayingReservoir reservoir = new ExponentiallyDecayingReservoir(1028, 0.015, clock);
19 | Histogram histogram = new HistogramImpl(reservoir);
20 |
21 | histogram.update(100);
22 |
23 | for (int i = 1; i < 48; i++) {
24 | clock.addHours(1);
25 | Assert.assertThat(reservoir.getSnapshot().getMean(), not(equalTo(Double.NaN)));
26 | }
27 | }
28 |
29 | }
30 |
--------------------------------------------------------------------------------
/implementation/src/test/java/io/smallrye/metrics/histogram/TestingClock.java:
--------------------------------------------------------------------------------
1 | package io.smallrye.metrics.histogram;
2 |
3 | import java.util.concurrent.TimeUnit;
4 |
5 | import io.smallrye.metrics.app.Clock;
6 |
7 | public class TestingClock extends Clock {
8 | private final long initialTicksInNanos;
9 | long ticksInNanos;
10 |
11 | public TestingClock(long initialTicksInNanos) {
12 | this.initialTicksInNanos = initialTicksInNanos;
13 | this.ticksInNanos = initialTicksInNanos;
14 | }
15 |
16 | public TestingClock() {
17 | this(0L);
18 | }
19 |
20 | public synchronized void addNanos(long nanos) {
21 | ticksInNanos += nanos;
22 | }
23 |
24 | public synchronized void addSeconds(long seconds) {
25 | ticksInNanos += TimeUnit.SECONDS.toNanos(seconds);
26 | }
27 |
28 | public synchronized void addMillis(long millis) {
29 | ticksInNanos += TimeUnit.MILLISECONDS.toNanos(millis);
30 | }
31 |
32 | public synchronized void addHours(long hours) {
33 | ticksInNanos += TimeUnit.HOURS.toNanos(hours);
34 | }
35 |
36 | @Override
37 | public synchronized long getTick() {
38 | return ticksInNanos;
39 | }
40 |
41 | @Override
42 | public synchronized long getTime() {
43 | return TimeUnit.NANOSECONDS.toMillis(ticksInNanos - initialTicksInNanos);
44 | }
45 |
46 | }
47 |
--------------------------------------------------------------------------------
/implementation/src/test/java/io/smallrye/metrics/registration/MetadataMismatchTest.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2019 Red Hat, Inc. and/or its affiliates
3 | * and other contributors as indicated by the @author tags.
4 | *
5 | * Licensed under the Apache License, Version 2.0 (the "License");
6 | * you may not use this file except in compliance with the License.
7 | * You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 | package io.smallrye.metrics.registration;
18 |
19 | import static org.hamcrest.CoreMatchers.equalTo;
20 | import static org.hamcrest.CoreMatchers.instanceOf;
21 | import static org.junit.Assert.assertEquals;
22 | import static org.junit.Assert.assertNotEquals;
23 | import static org.junit.Assert.assertThat;
24 | import static org.junit.Assert.fail;
25 |
26 | import org.eclipse.microprofile.metrics.Histogram;
27 | import org.eclipse.microprofile.metrics.Metadata;
28 | import org.eclipse.microprofile.metrics.MetricFilter;
29 | import org.eclipse.microprofile.metrics.MetricRegistry;
30 | import org.eclipse.microprofile.metrics.Tag;
31 | import org.junit.After;
32 | import org.junit.Test;
33 |
34 | import io.smallrye.metrics.MetricRegistries;
35 |
36 | public class MetadataMismatchTest {
37 |
38 | private MetricRegistry registry = MetricRegistries.get(MetricRegistry.Type.APPLICATION);
39 |
40 | @After
41 | public void cleanupApplicationMetrics() {
42 | registry.removeMatching(MetricFilter.ALL);
43 | }
44 |
45 | @Test
46 | public void metricsWithDifferentMetadata() {
47 | Metadata metadata1 = Metadata.builder().withName("myhistogram").withDescription("description1").build();
48 | Metadata metadata2 = Metadata.builder().withName("myhistogram").withDescription("description2").build();
49 |
50 | registry.histogram(metadata1);
51 | try {
52 | registry.histogram(metadata2);
53 | fail("Shouldn't be able to re-register a metric with different metadata");
54 | } catch (Exception e) {
55 | assertThat(e, instanceOf(IllegalStateException.class));
56 | assertEquals(1, registry.getMetrics().size());
57 | }
58 | }
59 |
60 | @Test
61 | public void reusingMetadata() {
62 | Metadata metadata1 = Metadata.builder().withName("myhistogram").withDescription("description1").build();
63 |
64 | Histogram histogram1 = registry.histogram(metadata1);
65 | Histogram histogram2 = registry.histogram("myhistogram", new Tag("color", "blue"));
66 |
67 | assertNotEquals(histogram1, histogram2);
68 | assertEquals(2, registry.getMetrics().size());
69 | assertThat(registry.getMetadata().get("myhistogram").description().get(), equalTo("description1"));
70 | }
71 |
72 | @Test
73 | public void metricsWithSameTypeAndMetadata() {
74 | Metadata metadata1 = Metadata.builder().withName("myhistogram").withDescription("description1").build();
75 | Metadata metadata2 = Metadata.builder().withName("myhistogram").withDescription("description1").build();
76 |
77 | Histogram histogram1 = registry.histogram(metadata1);
78 | Histogram histogram2 = registry.histogram(metadata2);
79 |
80 | assertEquals(histogram1, histogram2);
81 | assertEquals(1, registry.getMetrics().size());
82 | }
83 |
84 | }
85 |
--------------------------------------------------------------------------------
/implementation/src/test/java/io/smallrye/metrics/registration/MetricTypeMismatchTest.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2019 Red Hat, Inc. and/or its affiliates
3 | * and other contributors as indicated by the @author tags.
4 | *
5 | * Licensed under the Apache License, Version 2.0 (the "License");
6 | * you may not use this file except in compliance with the License.
7 | * You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 | package io.smallrye.metrics.registration;
18 |
19 | import static org.hamcrest.CoreMatchers.containsString;
20 | import static org.hamcrest.CoreMatchers.instanceOf;
21 | import static org.junit.Assert.assertEquals;
22 | import static org.junit.Assert.assertThat;
23 | import static org.junit.Assert.fail;
24 |
25 | import org.eclipse.microprofile.metrics.Metadata;
26 | import org.eclipse.microprofile.metrics.MetricFilter;
27 | import org.eclipse.microprofile.metrics.MetricRegistry;
28 | import org.eclipse.microprofile.metrics.MetricType;
29 | import org.junit.After;
30 | import org.junit.Test;
31 |
32 | import io.smallrye.metrics.MetricRegistries;
33 |
34 | public class MetricTypeMismatchTest {
35 |
36 | private MetricRegistry registry = MetricRegistries.get(MetricRegistry.Type.APPLICATION);
37 |
38 | @After
39 | public void cleanupApplicationMetrics() {
40 | registry.removeMatching(MetricFilter.ALL);
41 | }
42 |
43 | @Test
44 | public void metricsWithDifferentType() {
45 | Metadata metadata1 = Metadata.builder().withName("metric1")
46 | .withDescription("description1").build();
47 | Metadata metadata2 = Metadata.builder().withName("metric1")
48 | .withDescription("description2").build();
49 |
50 | registry.histogram(metadata1);
51 | try {
52 | registry.meter(metadata2);
53 | fail("Must not be able to register if a metric with different type is registered under the same name");
54 | } catch (Exception e) {
55 | assertThat(e, instanceOf(IllegalStateException.class));
56 | assertEquals(1, registry.getMetrics().size());
57 | }
58 | }
59 |
60 | @Test
61 | public void wrongTypeInMetadata() {
62 | Metadata metadata1 = Metadata.builder()
63 | .withName("metric1")
64 | .withType(MetricType.COUNTER)
65 | .build();
66 | try {
67 | registry.meter(metadata1);
68 | fail("Must not be able to register a metric if the type in its metadata is different than the what we specified by using a particular registration method.");
69 | } catch (Exception e) {
70 | assertThat(e, instanceOf(IllegalArgumentException.class));
71 | assertThat(e.getMessage(),
72 | containsString("Attempting to register a meter, but the passed metadata contains type=counter"));
73 | assertEquals(0, registry.getMetrics().size());
74 | }
75 | }
76 |
77 | }
78 |
--------------------------------------------------------------------------------
/implementation/src/test/resources/base-metrics.properties:
--------------------------------------------------------------------------------
1 | #
2 | # Copyright 2019 Red Hat, Inc. and/or its affiliates
3 | # and other contributors as indicated by the @author tags.
4 | #
5 | # Licensed under the Apache License, Version 2.0 (the "License");
6 | # you may not use this file except in compliance with the License.
7 | # You may obtain a copy of the License at
8 | #
9 | # http://www.apache.org/licenses/LICENSE-2.0
10 | #
11 | # Unless required by applicable law or agreed to in writing, software
12 | # distributed under the License is distributed on an "AS IS" BASIS,
13 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | # See the License for the specific language governing permissions and
15 | # limitations under the License.
16 | #
17 |
18 | classloader.currentLoadedClass.count.displayName: Current Loaded Class Count
19 | classloader.currentLoadedClass.count.type: counter
20 | classloader.currentLoadedClass.count.unit: none
21 | classloader.currentLoadedClass.count.description: Displays the number of classes that are currently loaded in the Java virtual machine.
22 | classloader.currentLoadedClass.count.mbean: java.lang:type=ClassLoading/LoadedClassCount
23 | classloader.totalLoadedClass.count.displayName: Total Loaded Class Count
24 | classloader.totalLoadedClass.count.type: counter
25 | classloader.totalLoadedClass.count.unit: none
26 | classloader.totalLoadedClass.count.description: Displays the total number of classes that have been loaded since the Java virtual machine has started execution.
27 | classloader.totalLoadedClass.count.mbean: java.lang:type=ClassLoading/TotalLoadedClassCount
28 | classloader.totalUnloadedClass.count.displayName: Total Unloaded Class Count
29 | classloader.totalUnloadedClass.count.type: counter
30 | classloader.totalUnloadedClass.count.unit: none
31 | classloader.totalUnloadedClass.count.description: Displays the total number of classes unloaded since the Java virtual machine has started execution.
32 | classloader.totalUnloadedClass.count.mbean: java.lang:type=ClassLoading/UnloadedClassCount
33 | gc.count.displayName: Garbage Collection Count
34 | gc.count.type: counter
35 | gc.count.unit: none
36 | gc.count.multi: true
37 | gc.count.description: Displays the total number of collections that have occurred. This attribute lists -1 if the collection count is undefined for this collector.
38 | gc.count.mbean: java.lang:type=GarbageCollector,name=%s/CollectionCount
39 | gc.count.tags: name=%s1
40 | gc.time.displayName: Garbage Collection Time
41 | gc.time.type: gauge
42 | gc.time.unit: milliseconds
43 | gc.time.multi: true
44 | gc.time.description: Displays the approximate accumulated collection elapsed time in milliseconds. This attribute displays -1 if the collection elapsed time is undefined for this collector. The Java virtual machine implementation may use a high resolution timer to measure the elapsed time. This attribute may display the same value even if the collection count has been incremented if the collection elapsed time is very short.
45 | gc.time.mbean: java.lang:type=GarbageCollector,name=%s/CollectionTime
46 | gc.time.tags: name=%s1
47 | test_key.mbean: java.nio:name=%s2,type=%s1/ObjectName
48 | test_key.displayName: Display Name %s1-%s2
49 | test_key.type: counter
50 | test_key.unit: none
51 | test_key.multi: true
52 | test_key.description: Description %s1-%s2
53 | test_key.tags: type=%s1;name=%s2
54 |
--------------------------------------------------------------------------------
/implementation/src/test/resources/logging.properties:
--------------------------------------------------------------------------------
1 | handlers = java.util.logging.ConsoleHandler
2 | java.util.logging.ConsoleHandler.level = FINE
3 | io.smallrye.metrics.level = FINE
--------------------------------------------------------------------------------
/release/pom.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 | 4.0.0
4 |
5 |
6 | io.smallrye
7 | smallrye-metrics-parent
8 | 4.0.1-SNAPSHOT
9 |
10 |
11 | smallrye-metrics-release
12 |
13 | Empty Release Project to Avoid Maven Bug
14 | Empty Release Project to Avoid Maven Bug
15 |
16 | pom
17 |
18 |
19 |
--------------------------------------------------------------------------------
/testsuite/api-tck/src/main/resources/META-INF/services/org.jboss.arquillian.core.spi.LoadableExtension:
--------------------------------------------------------------------------------
1 | io.smallrye.metrics.testsuite.MetricsExtension
--------------------------------------------------------------------------------
/testsuite/api-tck/src/test/resources/logging.properties:
--------------------------------------------------------------------------------
1 | #
2 | # Copyright 2019 Red Hat, Inc. and/or its affiliates
3 | # and other contributors as indicated by the @author tags.
4 | #
5 | # Licensed under the Apache License, Version 2.0 (the "License");
6 | # you may not use this file except in compliance with the License.
7 | # You may obtain a copy of the License at
8 | #
9 | # http://www.apache.org/licenses/LICENSE-2.0
10 | #
11 | # Unless required by applicable law or agreed to in writing, software
12 | # distributed under the License is distributed on an "AS IS" BASIS,
13 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | # See the License for the specific language governing permissions and
15 | # limitations under the License.
16 | #
17 |
18 | handlers = java.util.logging.ConsoleHandler
19 | java.util.logging.ConsoleHandler.level = FINE
20 | io.smallrye.metrics.level = FINE
21 | org.jboss.weld.level = WARN
--------------------------------------------------------------------------------
/testsuite/common/pom.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
18 |
19 |
20 | 4.0.0
21 |
22 |
23 | io.smallrye
24 | smallrye-metrics-testsuite-parent
25 | 4.0.1-SNAPSHOT
26 |
27 |
28 | smallrye-metrics-testsuite-common
29 |
30 | SmallRye: MicroProfile Metrics Test Suite Common
31 |
32 |
33 |
34 | io.smallrye
35 | smallrye-metrics
36 | ${project.version}
37 |
38 |
39 | jakarta.enterprise
40 | jakarta.enterprise.cdi-api
41 | provided
42 |
43 |
44 | org.jboss.arquillian.container
45 | arquillian-container-test-spi
46 | provided
47 |
48 |
49 |
50 |
51 |
--------------------------------------------------------------------------------
/testsuite/common/src/main/java/io/smallrye/metrics/testsuite/ArchiveProcessor.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2018 Red Hat, Inc, and individual contributors.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package io.smallrye.metrics.testsuite;
17 |
18 | import org.jboss.arquillian.container.test.spi.client.deployment.ApplicationArchiveProcessor;
19 | import org.jboss.arquillian.test.spi.TestClass;
20 | import org.jboss.shrinkwrap.api.Archive;
21 | import org.jboss.shrinkwrap.api.container.ResourceContainer;
22 |
23 | /**
24 | * @author Michal Szynkiewicz, michal.l.szynkiewicz@gmail.com
25 | *
26 | * Date: 6/19/18
27 | */
28 | public class ArchiveProcessor implements ApplicationArchiveProcessor {
29 |
30 | @Override
31 | public void process(Archive> applicationArchive, TestClass testClass) {
32 | if (applicationArchive instanceof ResourceContainer) {
33 | ResourceContainer archive = (ResourceContainer) applicationArchive;
34 | archive.addAsResource("io/smallrye/metrics/base-metrics.properties",
35 | "/io/smallrye/metrics/base-metrics.properties");
36 | } else {
37 | throw new IllegalStateException(this.getClass().getCanonicalName() + " works only with ResourceContainers");
38 | }
39 | }
40 | }
41 |
--------------------------------------------------------------------------------
/testsuite/common/src/main/java/io/smallrye/metrics/testsuite/MetricsExtension.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2018 Red Hat, Inc, and individual contributors.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package io.smallrye.metrics.testsuite;
17 |
18 | import org.jboss.arquillian.container.test.spi.client.deployment.ApplicationArchiveProcessor;
19 | import org.jboss.arquillian.core.spi.LoadableExtension;
20 |
21 | /**
22 | *
23 | * @author Michal Szynkiewicz, michal.l.szynkiewicz@gmail.com
24 | *
25 | * Date: 6/25/18
26 | */
27 | public class MetricsExtension implements LoadableExtension {
28 |
29 | @Override
30 | public void register(ExtensionBuilder extensionBuilder) {
31 | extensionBuilder.service(ApplicationArchiveProcessor.class, ArchiveProcessor.class);
32 | }
33 |
34 | }
35 |
--------------------------------------------------------------------------------
/testsuite/common/src/main/java/io/smallrye/metrics/testsuite/MetricsInitializer.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2018 Red Hat, Inc, and individual contributors.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package io.smallrye.metrics.testsuite;
17 |
18 | import java.io.IOException;
19 |
20 | import jakarta.enterprise.context.ApplicationScoped;
21 | import jakarta.enterprise.context.Initialized;
22 | import jakarta.enterprise.event.Observes;
23 |
24 | import io.smallrye.metrics.setup.JmxRegistrar;
25 |
26 | /**
27 | * @author Michal Szynkiewicz, michal.l.szynkiewicz@gmail.com
28 | *
29 | * Date: 6/29/18
30 | */
31 | @ApplicationScoped
32 | public class MetricsInitializer {
33 | void init(@Observes @Initialized(ApplicationScoped.class) Object ignored) throws IOException {
34 | JmxRegistrar registrar = new JmxRegistrar();
35 | registrar.init();
36 | }
37 | }
38 |
--------------------------------------------------------------------------------
/testsuite/common/src/main/resources/io/smallrye/metrics/vendor-metrics.properties:
--------------------------------------------------------------------------------
1 | BufferPool_used_memory_%s.mbean: java.nio:type=BufferPool,name=%s/MemoryUsed
2 | BufferPool_used_memory_%s.description: The memory used by the NIO pool: %s
3 | BufferPool_used_memory_%s.multi: true
4 | BufferPool_used_memory_%s.type: gauge
5 | BufferPool_used_memory_%s.unit: bytes
6 | memoryPool.%s.usage.mbean: java.lang:type=MemoryPool,name=%s/Usage#used
7 | memoryPool.%s.usage.unit: bytes
8 | memoryPool.%s.usage.description: Current usage of the %s memory pool
9 | memoryPool.%s.usage.multi: true
10 | memoryPool.%s.usage.type: gauge
11 | memoryPool.%s.usage.max.mbean: java.lang:type=MemoryPool,name=%s/PeakUsage#used
12 | memoryPool.%s.usage.max.unit: bytes
13 | memoryPool.%s.usage.max.description: Peak usage of the %s memory pool
14 | memoryPool.%s.usage.max.multi: true
15 | memoryPool.%s.usage.max.type: gauge
--------------------------------------------------------------------------------
/testsuite/extra/pom.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
18 |
19 |
20 | 4.0.0
21 |
22 |
23 | io.smallrye
24 | smallrye-metrics-testsuite-parent
25 | 4.0.1-SNAPSHOT
26 |
27 |
28 | smallrye-metrics-testsuite-extra
29 |
30 | SmallRye: MicroProfile Metrics Test Suite Additional Tests
31 |
32 |
33 |
34 | io.smallrye
35 | smallrye-metrics
36 | ${project.version}
37 |
38 |
39 | io.smallrye
40 | smallrye-config
41 |
42 |
43 | io.smallrye.config
44 | smallrye-config
45 |
46 |
47 | org.eclipse.microprofile.metrics
48 | microprofile-metrics-api
49 |
50 |
51 |
52 | io.smallrye
53 | smallrye-metrics-testsuite-common
54 | ${project.version}
55 |
56 |
57 | jakarta.enterprise
58 | jakarta.enterprise.cdi-api
59 | provided
60 |
61 |
62 | org.jboss.weld
63 | weld-core-impl
64 |
65 |
66 | org.jboss.arquillian.container
67 | arquillian-weld-embedded
68 |
69 |
70 | junit
71 | junit
72 | 4.13.2
73 |
74 |
75 | org.jboss.arquillian.junit
76 | arquillian-junit-container
77 |
78 |
79 |
80 |
81 |
82 | coverage
83 |
84 | @{jacocoArgLine}
85 |
86 |
87 |
88 |
89 |
90 | org.jacoco
91 | jacoco-maven-plugin
92 |
93 |
94 |
95 |
96 |
97 |
98 |
99 |
--------------------------------------------------------------------------------
/testsuite/extra/src/main/java/io/smallrye/metrics/test/HelloService.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2018 Red Hat, Inc, and individual contributors.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package io.smallrye.metrics.test;
17 |
18 | import jakarta.enterprise.context.ApplicationScoped;
19 |
20 | import org.eclipse.microprofile.metrics.MetricUnits;
21 | import org.eclipse.microprofile.metrics.annotation.Counted;
22 | import org.eclipse.microprofile.metrics.annotation.Timed;
23 |
24 | @ApplicationScoped
25 | public class HelloService {
26 |
27 | @Counted(name = "hello-count", absolute = true, displayName = "Hello Count", description = "Number of hello invocations")
28 | public String hello() {
29 | return "Hello from counted method";
30 | }
31 |
32 | @Timed(unit = MetricUnits.MILLISECONDS, name = "howdy-time", absolute = true, displayName = "Howdy Time", description = "Time of howdy invocations")
33 | public String howdy() {
34 | return "Howdy from timed method";
35 | }
36 | }
37 |
--------------------------------------------------------------------------------
/testsuite/extra/src/main/java/io/smallrye/metrics/test/MetricsSummary.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2018 Red Hat, Inc, and individual contributors.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package io.smallrye.metrics.test;
17 |
18 | import jakarta.enterprise.context.ApplicationScoped;
19 | import jakarta.inject.Inject;
20 |
21 | import org.eclipse.microprofile.metrics.MetricRegistry;
22 | import org.eclipse.microprofile.metrics.annotation.RegistryType;
23 |
24 | @ApplicationScoped
25 | public class MetricsSummary {
26 |
27 | @Inject
28 | @RegistryType(type = MetricRegistry.Type.BASE)
29 | private MetricRegistry baseMetrics;
30 |
31 | @Inject
32 | @RegistryType(type = MetricRegistry.Type.VENDOR)
33 | private MetricRegistry vendorMetrics;
34 |
35 | @Inject
36 | @RegistryType(type = MetricRegistry.Type.APPLICATION)
37 | private MetricRegistry appMetrics;
38 |
39 | @Inject
40 | private MetricRegistry defaultMetrics;
41 |
42 | public MetricRegistry getBaseMetrics() {
43 | return baseMetrics;
44 | }
45 |
46 | public MetricRegistry getVendorMetrics() {
47 | return vendorMetrics;
48 | }
49 |
50 | public MetricRegistry getAppMetrics() {
51 | return appMetrics;
52 | }
53 |
54 | public MetricRegistry getDefaultMetrics() {
55 | return defaultMetrics;
56 | }
57 |
58 | }
59 |
--------------------------------------------------------------------------------
/testsuite/extra/src/main/resources/META-INF/services/org.jboss.arquillian.core.spi.LoadableExtension:
--------------------------------------------------------------------------------
1 | io.smallrye.metrics.testsuite.MetricsExtension
--------------------------------------------------------------------------------
/testsuite/extra/src/test/java/io/smallrye/metrics/test/dependent/DependentScopedBeanWithGauge.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2019 Red Hat, Inc, and individual contributors.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package io.smallrye.metrics.test.dependent;
18 |
19 | import org.eclipse.microprofile.metrics.MetricUnits;
20 | import org.eclipse.microprofile.metrics.annotation.Gauge;
21 |
22 | public class DependentScopedBeanWithGauge {
23 |
24 | @Gauge(name = "gauge", absolute = true, unit = MetricUnits.DAYS)
25 | public Long gaugedMethod() {
26 | return 42L;
27 | }
28 |
29 | }
30 |
--------------------------------------------------------------------------------
/testsuite/extra/src/test/java/io/smallrye/metrics/test/dependent/DependentScopedBeanWithMetrics.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2019 Red Hat, Inc, and individual contributors.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package io.smallrye.metrics.test.dependent;
18 |
19 | import org.eclipse.microprofile.metrics.annotation.ConcurrentGauge;
20 | import org.eclipse.microprofile.metrics.annotation.Counted;
21 | import org.eclipse.microprofile.metrics.annotation.Metered;
22 | import org.eclipse.microprofile.metrics.annotation.Timed;
23 |
24 | public class DependentScopedBeanWithMetrics {
25 |
26 | @Counted(name = "counter", absolute = true)
27 | public void countedMethod() {
28 |
29 | }
30 |
31 | @Metered(name = "meter", absolute = true)
32 | public void meteredMethod() {
33 |
34 | }
35 |
36 | @Timed(name = "timer", absolute = true)
37 | public void timedMethod() {
38 |
39 | }
40 |
41 | @ConcurrentGauge(name = "cgauge", absolute = true)
42 | public void cGaugedMethod() {
43 |
44 | }
45 |
46 | }
47 |
--------------------------------------------------------------------------------
/testsuite/extra/src/test/java/io/smallrye/metrics/test/dependent/GaugeInDependentScopedBeanTest.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2019 Red Hat, Inc, and individual contributors.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package io.smallrye.metrics.test.dependent;
18 |
19 | import jakarta.enterprise.inject.Instance;
20 | import jakarta.inject.Inject;
21 |
22 | import org.eclipse.microprofile.metrics.MetricFilter;
23 | import org.eclipse.microprofile.metrics.MetricRegistry;
24 | import org.jboss.arquillian.container.test.api.Deployment;
25 | import org.jboss.arquillian.junit.Arquillian;
26 | import org.jboss.shrinkwrap.api.ShrinkWrap;
27 | import org.jboss.shrinkwrap.api.asset.EmptyAsset;
28 | import org.jboss.shrinkwrap.api.spec.WebArchive;
29 | import org.junit.After;
30 | import org.junit.Assert;
31 | import org.junit.Test;
32 | import org.junit.runner.RunWith;
33 |
34 | import io.smallrye.metrics.MetricRegistries;
35 |
36 | /**
37 | * Gauges can only be used with AnnotationScoped beans. If we place a gauge on a bean that creates multiple
38 | * instances during the application lifetime, this should be treated as an error, because a gauge
39 | * must always be bound to just one object, therefore creating multiple instances of the bean would
40 | * create ambiguity.
41 | */
42 | @RunWith(Arquillian.class)
43 | public class GaugeInDependentScopedBeanTest {
44 |
45 | @Deployment
46 | public static WebArchive deployment() {
47 | return ShrinkWrap.create(WebArchive.class)
48 | .addAsWebInfResource(EmptyAsset.INSTANCE, "beans.xml")
49 | .addClass(DependentScopedBeanWithGauge.class);
50 | }
51 |
52 | @After
53 | public void cleanupApplicationMetrics() {
54 | MetricRegistries.get(MetricRegistry.Type.APPLICATION).removeMatching(MetricFilter.ALL);
55 | }
56 |
57 | @Inject
58 | private Instance beanInstance;
59 |
60 | @Test
61 | public void gauge() {
62 | try {
63 | DependentScopedBeanWithGauge instance1 = beanInstance.get();
64 | DependentScopedBeanWithGauge instance2 = beanInstance.get();
65 | Assert.fail("Shouldn't be able to create multiple instances of a bean that contains a gauge");
66 | } catch (Exception e) {
67 |
68 | }
69 | }
70 |
71 | }
72 |
--------------------------------------------------------------------------------
/testsuite/extra/src/test/java/io/smallrye/metrics/test/initialization/Initialization_ConcurrentGauge_Method_Test.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2019 Red Hat, Inc. and/or its affiliates
3 | * and other contributors as indicated by the @author tags.
4 | *
5 | * Licensed under the Apache License, Version 2.0 (the "License");
6 | * you may not use this file except in compliance with the License.
7 | * You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 |
18 | package io.smallrye.metrics.test.initialization;
19 |
20 | import static org.junit.Assert.assertTrue;
21 |
22 | import jakarta.inject.Inject;
23 |
24 | import org.eclipse.microprofile.metrics.MetricID;
25 | import org.eclipse.microprofile.metrics.MetricRegistry;
26 | import org.eclipse.microprofile.metrics.annotation.ConcurrentGauge;
27 | import org.jboss.arquillian.container.test.api.Deployment;
28 | import org.jboss.arquillian.junit.Arquillian;
29 | import org.jboss.shrinkwrap.api.ShrinkWrap;
30 | import org.jboss.shrinkwrap.api.asset.EmptyAsset;
31 | import org.jboss.shrinkwrap.api.spec.WebArchive;
32 | import org.junit.Test;
33 | import org.junit.runner.RunWith;
34 |
35 | @RunWith(Arquillian.class)
36 | public class Initialization_ConcurrentGauge_Method_Test {
37 |
38 | @Deployment
39 | public static WebArchive deployment() {
40 | return ShrinkWrap.create(WebArchive.class)
41 | .addAsWebInfResource(EmptyAsset.INSTANCE, "beans.xml")
42 | .addClasses(BeanWithConcurrentGauge_Method.class);
43 | }
44 |
45 | @Inject
46 | MetricRegistry registry;
47 |
48 | @Test
49 | public void test() {
50 | assertTrue(registry.getConcurrentGauges().containsKey(new MetricID("cgauged_method")));
51 | }
52 |
53 | public static class BeanWithConcurrentGauge_Method {
54 |
55 | @ConcurrentGauge(name = "cgauged_method", absolute = true)
56 | public void cGaugedMethod() {
57 |
58 | }
59 |
60 | }
61 |
62 | }
63 |
--------------------------------------------------------------------------------
/testsuite/extra/src/test/java/io/smallrye/metrics/test/initialization/Initialization_Counter_ClassLevel_Test.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2019 Red Hat, Inc. and/or its affiliates
3 | * and other contributors as indicated by the @author tags.
4 | *
5 | * Licensed under the Apache License, Version 2.0 (the "License");
6 | * you may not use this file except in compliance with the License.
7 | * You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 |
18 | package io.smallrye.metrics.test.initialization;
19 |
20 | import static org.junit.Assert.assertFalse;
21 | import static org.junit.Assert.assertTrue;
22 |
23 | import jakarta.inject.Inject;
24 |
25 | import org.eclipse.microprofile.metrics.MetricID;
26 | import org.eclipse.microprofile.metrics.MetricRegistry;
27 | import org.eclipse.microprofile.metrics.Tag;
28 | import org.eclipse.microprofile.metrics.annotation.Counted;
29 | import org.jboss.arquillian.container.test.api.Deployment;
30 | import org.jboss.arquillian.junit.Arquillian;
31 | import org.jboss.shrinkwrap.api.ShrinkWrap;
32 | import org.jboss.shrinkwrap.api.asset.EmptyAsset;
33 | import org.jboss.shrinkwrap.api.spec.WebArchive;
34 | import org.junit.Test;
35 | import org.junit.runner.RunWith;
36 |
37 | @RunWith(Arquillian.class)
38 | public class Initialization_Counter_ClassLevel_Test {
39 |
40 | @Deployment
41 | public static WebArchive deployment() {
42 | return ShrinkWrap.create(WebArchive.class)
43 | .addAsWebInfResource(EmptyAsset.INSTANCE, "beans.xml")
44 | .addClasses(BeanWithCounter_ClassLevel.class);
45 | }
46 |
47 | @Inject
48 | MetricRegistry registry;
49 |
50 | @Test
51 | public void test() {
52 | // metric should be created for the constructor
53 | assertTrue(
54 | registry.getCounters().containsKey(new MetricID("customName.BeanWithCounter_ClassLevel", new Tag("t1", "v1"))));
55 |
56 | // metrics should be created for public methods
57 | assertTrue(registry.getCounters().containsKey(new MetricID("customName.publicMethod", new Tag("t1", "v1"))));
58 | assertTrue(registry.getCounters().containsKey(new MetricID("customName.publicMethod2", new Tag("t1", "v1"))));
59 |
60 | // but not for private methods
61 | assertFalse(registry.getCounters().keySet().stream()
62 | .anyMatch(metricID -> metricID.getName().toLowerCase().contains("private")));
63 | }
64 |
65 | @Counted(name = "customName", tags = "t1=v1", absolute = true)
66 | private static class BeanWithCounter_ClassLevel {
67 |
68 | public BeanWithCounter_ClassLevel() {
69 |
70 | }
71 |
72 | public void publicMethod() {
73 |
74 | }
75 |
76 | public void publicMethod2() {
77 |
78 | }
79 |
80 | private void privateMethod() {
81 |
82 | }
83 |
84 | }
85 |
86 | }
87 |
--------------------------------------------------------------------------------
/testsuite/extra/src/test/java/io/smallrye/metrics/test/initialization/Initialization_Counter_Constructor_Test.java:
--------------------------------------------------------------------------------
1 | package io.smallrye.metrics.test.initialization;
2 |
3 | import static org.junit.Assert.assertEquals;
4 | import static org.junit.Assert.assertNotNull;
5 |
6 | import jakarta.inject.Inject;
7 |
8 | import org.eclipse.microprofile.metrics.Counter;
9 | import org.eclipse.microprofile.metrics.MetricID;
10 | import org.eclipse.microprofile.metrics.MetricRegistry;
11 | import org.eclipse.microprofile.metrics.annotation.Counted;
12 | import org.jboss.arquillian.container.test.api.Deployment;
13 | import org.jboss.arquillian.junit.Arquillian;
14 | import org.jboss.shrinkwrap.api.ShrinkWrap;
15 | import org.jboss.shrinkwrap.api.asset.EmptyAsset;
16 | import org.jboss.shrinkwrap.api.spec.WebArchive;
17 | import org.junit.Test;
18 | import org.junit.runner.RunWith;
19 |
20 | @RunWith(Arquillian.class)
21 | public class Initialization_Counter_Constructor_Test {
22 |
23 | @Deployment
24 | public static WebArchive deployment() {
25 | return ShrinkWrap.create(WebArchive.class)
26 | .addAsWebInfResource(EmptyAsset.INSTANCE, "beans.xml")
27 | .addClasses(BeanWithCounter_Constructor.class);
28 | }
29 |
30 | @Inject
31 | MetricRegistry registry;
32 |
33 | @Test
34 | public void test() {
35 | Counter metricFromConstructor = registry.getCounters()
36 | .get(new MetricID(
37 | "io.smallrye.metrics.test.initialization.Initialization_Counter_Constructor_Test" +
38 | "$BeanWithCounter_Constructor.BeanWithCounter_Constructor"));
39 | assertNotNull(metricFromConstructor);
40 | assertEquals(1, registry.getCounters().size());
41 | }
42 |
43 | private static class BeanWithCounter_Constructor {
44 |
45 | @Counted
46 | public BeanWithCounter_Constructor() {
47 |
48 | }
49 |
50 | }
51 |
52 | }
53 |
--------------------------------------------------------------------------------
/testsuite/extra/src/test/java/io/smallrye/metrics/test/initialization/Initialization_Counter_Method_Reused_Test.java:
--------------------------------------------------------------------------------
1 | package io.smallrye.metrics.test.initialization;
2 |
3 | import static org.junit.Assert.assertEquals;
4 | import static org.junit.Assert.assertTrue;
5 |
6 | import jakarta.annotation.security.PermitAll;
7 | import jakarta.inject.Inject;
8 |
9 | import org.eclipse.microprofile.metrics.MetricID;
10 | import org.eclipse.microprofile.metrics.MetricRegistry;
11 | import org.eclipse.microprofile.metrics.annotation.Counted;
12 | import org.jboss.arquillian.container.test.api.Deployment;
13 | import org.jboss.arquillian.junit.Arquillian;
14 | import org.jboss.shrinkwrap.api.ShrinkWrap;
15 | import org.jboss.shrinkwrap.api.asset.EmptyAsset;
16 | import org.jboss.shrinkwrap.api.spec.WebArchive;
17 | import org.junit.Test;
18 | import org.junit.runner.RunWith;
19 |
20 | @RunWith(Arquillian.class)
21 | public class Initialization_Counter_Method_Reused_Test {
22 |
23 | @Deployment
24 | public static WebArchive deployment() {
25 | return ShrinkWrap.create(WebArchive.class)
26 | .addAsWebInfResource(EmptyAsset.INSTANCE, "beans.xml")
27 | .addClasses(BeanWithCounter_Method_Reused.class);
28 | }
29 |
30 | @Inject
31 | private MetricRegistry registry;
32 |
33 | @Inject
34 | private BeanWithCounter_Method_Reused bean;
35 |
36 | @Test
37 | public void test() {
38 | assertTrue(registry.getCounters().containsKey(new MetricID("counter_method")));
39 | assertTrue(registry.getCounters((metricID, metric) -> metricID.getName().contains("irrelevant")).isEmpty());
40 | bean.counterMethod();
41 | assertEquals(1, registry.getCounters().get(new MetricID("counter_method")).getCount());
42 | bean.counterMethod2();
43 | assertEquals(2, registry.getCounters().get(new MetricID("counter_method")).getCount());
44 | assertEquals(1, registry.getCounters().size());
45 | }
46 |
47 | public static class BeanWithCounter_Method_Reused {
48 |
49 | @Counted(name = "counter_method", absolute = true)
50 | public void counterMethod() {
51 |
52 | }
53 |
54 | @Counted(name = "counter_method", absolute = true)
55 | public void counterMethod2() {
56 |
57 | }
58 |
59 | @PermitAll
60 | public void irrelevantAnnotatedMethod() {
61 |
62 | }
63 |
64 | }
65 |
66 | }
67 |
--------------------------------------------------------------------------------
/testsuite/extra/src/test/java/io/smallrye/metrics/test/initialization/Initialization_Counter_Method_Test.java:
--------------------------------------------------------------------------------
1 | package io.smallrye.metrics.test.initialization;
2 |
3 | import static org.junit.Assert.assertTrue;
4 |
5 | import jakarta.annotation.security.PermitAll;
6 | import jakarta.enterprise.context.ApplicationScoped;
7 | import jakarta.inject.Inject;
8 |
9 | import org.eclipse.microprofile.metrics.MetricID;
10 | import org.eclipse.microprofile.metrics.MetricRegistry;
11 | import org.eclipse.microprofile.metrics.annotation.Counted;
12 | import org.jboss.arquillian.container.test.api.Deployment;
13 | import org.jboss.arquillian.junit.Arquillian;
14 | import org.jboss.shrinkwrap.api.ShrinkWrap;
15 | import org.jboss.shrinkwrap.api.asset.EmptyAsset;
16 | import org.jboss.shrinkwrap.api.spec.WebArchive;
17 | import org.junit.Assert;
18 | import org.junit.Test;
19 | import org.junit.runner.RunWith;
20 |
21 | @RunWith(Arquillian.class)
22 | public class Initialization_Counter_Method_Test {
23 |
24 | @Deployment
25 | public static WebArchive deployment() {
26 | return ShrinkWrap.create(WebArchive.class)
27 | .addAsWebInfResource(EmptyAsset.INSTANCE, "beans.xml")
28 | .addClasses(BeanWithCounter_Method.class);
29 | }
30 |
31 | @Inject
32 | MetricRegistry registry;
33 |
34 | @Inject
35 | BeanWithCounter_Method bean;
36 |
37 | @Test
38 | public void test() {
39 | assertTrue(registry.getCounters().containsKey(new MetricID("counter_method")));
40 | assertTrue(registry.getCounters((metricID, metric) -> metricID.getName().contains("irrelevant")).isEmpty());
41 | bean.counterMethod();
42 | Assert.assertEquals(1, registry.getCounters().get(new MetricID("counter_method")).getCount());
43 | }
44 |
45 | @ApplicationScoped
46 | public static class BeanWithCounter_Method {
47 |
48 | @Counted(name = "counter_method", absolute = true)
49 | public void counterMethod() {
50 |
51 | }
52 |
53 | @PermitAll
54 | public void irrelevantAnnotatedMethod() {
55 |
56 | }
57 |
58 | }
59 |
60 | }
61 |
--------------------------------------------------------------------------------
/testsuite/extra/src/test/java/io/smallrye/metrics/test/initialization/Initialization_Gauge_Method_Test.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2019 Red Hat, Inc. and/or its affiliates
3 | * and other contributors as indicated by the @author tags.
4 | *
5 | * Licensed under the Apache License, Version 2.0 (the "License");
6 | * you may not use this file except in compliance with the License.
7 | * You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 |
18 | package io.smallrye.metrics.test.initialization;
19 |
20 | import static org.junit.Assert.assertTrue;
21 |
22 | import jakarta.enterprise.context.ApplicationScoped;
23 | import jakarta.inject.Inject;
24 |
25 | import org.eclipse.microprofile.metrics.MetricID;
26 | import org.eclipse.microprofile.metrics.MetricRegistry;
27 | import org.eclipse.microprofile.metrics.MetricUnits;
28 | import org.eclipse.microprofile.metrics.annotation.Gauge;
29 | import org.jboss.arquillian.container.test.api.Deployment;
30 | import org.jboss.arquillian.junit.Arquillian;
31 | import org.jboss.shrinkwrap.api.ShrinkWrap;
32 | import org.jboss.shrinkwrap.api.asset.EmptyAsset;
33 | import org.jboss.shrinkwrap.api.spec.WebArchive;
34 | import org.junit.Assert;
35 | import org.junit.Test;
36 | import org.junit.runner.RunWith;
37 |
38 | @RunWith(Arquillian.class)
39 | public class Initialization_Gauge_Method_Test {
40 |
41 | @Deployment
42 | public static WebArchive deployment() {
43 | return ShrinkWrap.create(WebArchive.class)
44 | .addAsWebInfResource(EmptyAsset.INSTANCE, "beans.xml")
45 | .addClasses(BeanWithGauge_ApplicationScoped.class);
46 | }
47 |
48 | @Inject
49 | MetricRegistry registry;
50 |
51 | @Inject
52 | BeanWithGauge_ApplicationScoped applicationScopedBean;
53 |
54 | /**
55 | * With a gauge in an application-scoped bean, the metric will be registered once the bean is instantiated.
56 | */
57 | @Test
58 | public void testApplicationScoped() {
59 | applicationScopedBean.gauge(); // access the application-scoped bean so that an instance gets created
60 | assertTrue(registry.getGauges().containsKey(new MetricID("gaugeApp")));
61 | Assert.assertEquals(2L, registry.getGauges().get(new MetricID("gaugeApp")).getValue());
62 | Assert.assertEquals(3L, registry.getGauges().get(new MetricID("gaugeApp")).getValue());
63 | }
64 |
65 | @ApplicationScoped
66 | public static class BeanWithGauge_ApplicationScoped {
67 |
68 | Long i = 0L;
69 |
70 | @Gauge(name = "gaugeApp", absolute = true, unit = MetricUnits.NONE)
71 | public Long gauge() {
72 | return ++i;
73 | }
74 |
75 | }
76 |
77 | }
78 |
--------------------------------------------------------------------------------
/testsuite/extra/src/test/java/io/smallrye/metrics/test/initialization/Initialization_Injection_Test.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2019 Red Hat, Inc. and/or its affiliates
3 | * and other contributors as indicated by the @author tags.
4 | *
5 | * Licensed under the Apache License, Version 2.0 (the "License");
6 | * you may not use this file except in compliance with the License.
7 | * You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 |
18 | package io.smallrye.metrics.test.initialization;
19 |
20 | import static org.junit.Assert.assertEquals;
21 | import static org.junit.Assert.assertTrue;
22 |
23 | import jakarta.inject.Inject;
24 |
25 | import org.eclipse.microprofile.metrics.Histogram;
26 | import org.eclipse.microprofile.metrics.MetricID;
27 | import org.eclipse.microprofile.metrics.MetricRegistry;
28 | import org.eclipse.microprofile.metrics.annotation.Metric;
29 | import org.jboss.arquillian.container.test.api.Deployment;
30 | import org.jboss.arquillian.junit.Arquillian;
31 | import org.jboss.shrinkwrap.api.ShrinkWrap;
32 | import org.jboss.shrinkwrap.api.asset.EmptyAsset;
33 | import org.jboss.shrinkwrap.api.spec.WebArchive;
34 | import org.junit.Test;
35 | import org.junit.runner.RunWith;
36 |
37 | @RunWith(Arquillian.class)
38 | public class Initialization_Injection_Test {
39 |
40 | @Deployment
41 | public static WebArchive deployment() {
42 | return ShrinkWrap.create(WebArchive.class)
43 | .addAsWebInfResource(EmptyAsset.INSTANCE, "beans.xml")
44 | .addClasses(BeanWithMetricInjection.class);
45 | }
46 |
47 | @Inject
48 | MetricRegistry registry;
49 |
50 | @Inject
51 | BeanWithMetricInjection bean;
52 |
53 | @Test
54 | public void test() {
55 | MetricID metricID = new MetricID(
56 | "io.smallrye.metrics.test.initialization.Initialization_Injection_Test.BeanWithMetricInjection.histogram");
57 | // check that the injected histogram is registered eagerly
58 | assertTrue(registry.getHistograms().containsKey(metricID));
59 | bean.addDataToHistogram();
60 | assertEquals(10, registry.getHistograms().get(metricID).getSnapshot().getMax());
61 | }
62 |
63 | public static class BeanWithMetricInjection {
64 |
65 | @Inject
66 | @Metric
67 | Histogram histogram;
68 |
69 | public void addDataToHistogram() {
70 | histogram.update(10);
71 | }
72 |
73 | }
74 |
75 | }
76 |
--------------------------------------------------------------------------------
/testsuite/extra/src/test/java/io/smallrye/metrics/test/initialization/Initialization_Meter_Method_Test.java:
--------------------------------------------------------------------------------
1 | package io.smallrye.metrics.test.initialization;
2 |
3 | import static org.junit.Assert.assertEquals;
4 | import static org.junit.Assert.assertTrue;
5 |
6 | import jakarta.inject.Inject;
7 |
8 | import org.eclipse.microprofile.metrics.MetricID;
9 | import org.eclipse.microprofile.metrics.MetricRegistry;
10 | import org.eclipse.microprofile.metrics.annotation.Metered;
11 | import org.jboss.arquillian.container.test.api.Deployment;
12 | import org.jboss.arquillian.junit.Arquillian;
13 | import org.jboss.shrinkwrap.api.ShrinkWrap;
14 | import org.jboss.shrinkwrap.api.asset.EmptyAsset;
15 | import org.jboss.shrinkwrap.api.spec.WebArchive;
16 | import org.junit.Test;
17 | import org.junit.runner.RunWith;
18 |
19 | @RunWith(Arquillian.class)
20 | public class Initialization_Meter_Method_Test {
21 |
22 | @Deployment
23 | public static WebArchive deployment() {
24 | return ShrinkWrap.create(WebArchive.class)
25 | .addAsWebInfResource(EmptyAsset.INSTANCE, "beans.xml")
26 | .addClasses(BeanWithMeter_Method.class);
27 | }
28 |
29 | @Inject
30 | MetricRegistry registry;
31 |
32 | @Inject
33 | BeanWithMeter_Method bean;
34 |
35 | @Test
36 | public void test() {
37 | assertTrue(registry.getMeters().containsKey(new MetricID("meter_method")));
38 | bean.meterMethod();
39 | assertEquals(1, registry.getMeters().get(new MetricID("meter_method")).getCount());
40 | }
41 |
42 | public static class BeanWithMeter_Method {
43 |
44 | @Metered(name = "meter_method", absolute = true)
45 | public void meterMethod() {
46 |
47 | }
48 |
49 | }
50 |
51 | }
52 |
--------------------------------------------------------------------------------
/testsuite/extra/src/test/java/io/smallrye/metrics/test/initialization/Initialization_SimpleTimer_Method_Test.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2019 Red Hat, Inc. and/or its affiliates
3 | * and other contributors as indicated by the @author tags.
4 | *
5 | * Licensed under the Apache License, Version 2.0 (the "License");
6 | * you may not use this file except in compliance with the License.
7 | * You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 |
18 | package io.smallrye.metrics.test.initialization;
19 |
20 | import static org.junit.Assert.assertEquals;
21 | import static org.junit.Assert.assertTrue;
22 |
23 | import jakarta.inject.Inject;
24 |
25 | import org.eclipse.microprofile.metrics.MetricID;
26 | import org.eclipse.microprofile.metrics.MetricRegistry;
27 | import org.eclipse.microprofile.metrics.annotation.SimplyTimed;
28 | import org.jboss.arquillian.container.test.api.Deployment;
29 | import org.jboss.arquillian.junit.Arquillian;
30 | import org.jboss.shrinkwrap.api.ShrinkWrap;
31 | import org.jboss.shrinkwrap.api.asset.EmptyAsset;
32 | import org.jboss.shrinkwrap.api.spec.WebArchive;
33 | import org.junit.Test;
34 | import org.junit.runner.RunWith;
35 |
36 | @RunWith(Arquillian.class)
37 | public class Initialization_SimpleTimer_Method_Test {
38 |
39 | @Deployment
40 | public static WebArchive deployment() {
41 | return ShrinkWrap.create(WebArchive.class)
42 | .addAsWebInfResource(EmptyAsset.INSTANCE, "beans.xml")
43 | .addClasses(BeanWithSimpleTimer_Method.class);
44 | }
45 |
46 | @Inject
47 | MetricRegistry registry;
48 |
49 | @Inject
50 | BeanWithSimpleTimer_Method bean;
51 |
52 | @Test
53 | public void test() {
54 | assertTrue(registry.getSimpleTimers().containsKey(new MetricID("simplytimed_method")));
55 | bean.timedMethod();
56 | assertEquals(1, registry.getSimpleTimers().get(new MetricID("simplytimed_method")).getCount());
57 | }
58 |
59 | public static class BeanWithSimpleTimer_Method {
60 |
61 | @SimplyTimed(name = "simplytimed_method", absolute = true)
62 | public void timedMethod() {
63 |
64 | }
65 |
66 | }
67 |
68 | }
69 |
--------------------------------------------------------------------------------
/testsuite/extra/src/test/java/io/smallrye/metrics/test/initialization/Initialization_Timer_Method_Test.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2019 Red Hat, Inc. and/or its affiliates
3 | * and other contributors as indicated by the @author tags.
4 | *
5 | * Licensed under the Apache License, Version 2.0 (the "License");
6 | * you may not use this file except in compliance with the License.
7 | * You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 |
18 | package io.smallrye.metrics.test.initialization;
19 |
20 | import static org.junit.Assert.assertEquals;
21 | import static org.junit.Assert.assertTrue;
22 |
23 | import jakarta.inject.Inject;
24 |
25 | import org.eclipse.microprofile.metrics.MetricID;
26 | import org.eclipse.microprofile.metrics.MetricRegistry;
27 | import org.eclipse.microprofile.metrics.annotation.Timed;
28 | import org.jboss.arquillian.container.test.api.Deployment;
29 | import org.jboss.arquillian.junit.Arquillian;
30 | import org.jboss.shrinkwrap.api.ShrinkWrap;
31 | import org.jboss.shrinkwrap.api.asset.EmptyAsset;
32 | import org.jboss.shrinkwrap.api.spec.WebArchive;
33 | import org.junit.Test;
34 | import org.junit.runner.RunWith;
35 |
36 | @RunWith(Arquillian.class)
37 | public class Initialization_Timer_Method_Test {
38 |
39 | @Deployment
40 | public static WebArchive deployment() {
41 | return ShrinkWrap.create(WebArchive.class)
42 | .addAsWebInfResource(EmptyAsset.INSTANCE, "beans.xml")
43 | .addClasses(BeanWithTimer_Method.class);
44 | }
45 |
46 | @Inject
47 | MetricRegistry registry;
48 |
49 | @Inject
50 | BeanWithTimer_Method bean;
51 |
52 | @Test
53 | public void test() {
54 | assertTrue(registry.getTimers().containsKey(new MetricID("timed_method")));
55 | bean.timedMethod();
56 | assertEquals(1, registry.getTimers().get(new MetricID("timed_method")).getCount());
57 | }
58 |
59 | public static class BeanWithTimer_Method {
60 |
61 | @Timed(name = "timed_method", absolute = true)
62 | public void timedMethod() {
63 |
64 | }
65 |
66 | }
67 |
68 | }
69 |
--------------------------------------------------------------------------------
/testsuite/extra/src/test/java/io/smallrye/metrics/test/inject/NonReusableMetricInjectionBean.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2019 Red Hat, Inc. and/or its affiliates
3 | * and other contributors as indicated by the @author tags.
4 | *
5 | * Licensed under the Apache License, Version 2.0 (the "License");
6 | * you may not use this file except in compliance with the License.
7 | * You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 |
18 | package io.smallrye.metrics.test.inject;
19 |
20 | import jakarta.inject.Inject;
21 |
22 | import org.eclipse.microprofile.metrics.Counter;
23 | import org.eclipse.microprofile.metrics.annotation.Counted;
24 | import org.eclipse.microprofile.metrics.annotation.Metric;
25 |
26 | public class NonReusableMetricInjectionBean {
27 |
28 | @Counted(name = "mycounter", absolute = true, tags = { "k=v1" })
29 | public void counter() {
30 | }
31 |
32 | @Counted(name = "mycounter", absolute = true, tags = { "k=v2" })
33 | public void counter2() {
34 | }
35 |
36 | // each time this bean gets constructed and injected, change the values of metrics to verify that
37 | // we can inject metrics using an annotated method parameter
38 | @Inject
39 | public void increaseCountersOnInjecting(
40 | @Metric(name = "mycounter", absolute = true, tags = { "k=v1" }) Counter counter1,
41 | @Metric(name = "mycounter", absolute = true, tags = { "k=v2" }) Counter counter2) {
42 | counter1.inc(2);
43 | counter2.inc(3);
44 | }
45 |
46 | }
47 |
--------------------------------------------------------------------------------
/testsuite/extra/src/test/java/io/smallrye/metrics/test/inject/NonReusableMetricInjectionTest.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2019 Red Hat, Inc. and/or its affiliates
3 | * and other contributors as indicated by the @author tags.
4 | *
5 | * Licensed under the Apache License, Version 2.0 (the "License");
6 | * you may not use this file except in compliance with the License.
7 | * You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 |
18 | package io.smallrye.metrics.test.inject;
19 |
20 | import jakarta.enterprise.inject.Instance;
21 | import jakarta.inject.Inject;
22 |
23 | import org.eclipse.microprofile.metrics.MetricID;
24 | import org.eclipse.microprofile.metrics.MetricRegistry;
25 | import org.eclipse.microprofile.metrics.Tag;
26 | import org.jboss.arquillian.container.test.api.Deployment;
27 | import org.jboss.arquillian.junit.Arquillian;
28 | import org.jboss.shrinkwrap.api.ShrinkWrap;
29 | import org.jboss.shrinkwrap.api.asset.EmptyAsset;
30 | import org.jboss.shrinkwrap.api.spec.WebArchive;
31 | import org.junit.Assert;
32 | import org.junit.Test;
33 | import org.junit.runner.RunWith;
34 |
35 | /**
36 | * Test that it is possible to inject a metric using an annotated method parameter.
37 | */
38 | @RunWith(Arquillian.class)
39 | public class NonReusableMetricInjectionTest {
40 |
41 | @Inject
42 | private Instance bean;
43 |
44 | @Inject
45 | private MetricRegistry metricRegistry;
46 |
47 | @Deployment
48 | public static WebArchive deployment() {
49 | return ShrinkWrap.create(WebArchive.class)
50 | .addAsWebInfResource(EmptyAsset.INSTANCE, "beans.xml")
51 | .addClass(NonReusableMetricInjectionBean.class);
52 | }
53 |
54 | @Test
55 | public void test() {
56 | // force creating two instances of the bean
57 | bean.get();
58 | bean.get();
59 | Assert.assertEquals(4, metricRegistry.getCounters().get(new MetricID("mycounter", new Tag("k", "v1"))).getCount());
60 | Assert.assertEquals(6, metricRegistry.getCounters().get(new MetricID("mycounter", new Tag("k", "v2"))).getCount());
61 | }
62 |
63 | }
64 |
--------------------------------------------------------------------------------
/testsuite/extra/src/test/java/io/smallrye/metrics/test/registry/AllMetricsOfGivenTypeTest.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2018 Red Hat, Inc, and individual contributors.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package io.smallrye.metrics.test.registry;
17 |
18 | import static org.junit.Assert.assertEquals;
19 | import static org.junit.Assert.assertFalse;
20 | import static org.junit.Assert.assertTrue;
21 |
22 | import java.util.SortedMap;
23 |
24 | import jakarta.inject.Inject;
25 |
26 | import org.eclipse.microprofile.metrics.MetricID;
27 | import org.eclipse.microprofile.metrics.Timer;
28 | import org.jboss.arquillian.container.test.api.Deployment;
29 | import org.jboss.arquillian.junit.Arquillian;
30 | import org.jboss.shrinkwrap.api.ShrinkWrap;
31 | import org.jboss.shrinkwrap.api.asset.EmptyAsset;
32 | import org.jboss.shrinkwrap.api.spec.WebArchive;
33 | import org.junit.Test;
34 | import org.junit.runner.RunWith;
35 |
36 | import io.smallrye.metrics.test.HelloService;
37 | import io.smallrye.metrics.test.MetricsSummary;
38 |
39 | @RunWith(Arquillian.class)
40 | public class AllMetricsOfGivenTypeTest {
41 | @Inject
42 | HelloService hello;
43 |
44 | @Inject
45 | MetricsSummary summary;
46 |
47 | @Deployment
48 | public static WebArchive deployment() {
49 | return ShrinkWrap.create(WebArchive.class)
50 | .addAsWebInfResource(EmptyAsset.INSTANCE, "beans.xml")
51 | .addPackage(HelloService.class.getPackage())
52 | .addPackage(AllMetricsOfGivenTypeTest.class.getPackage());
53 | }
54 |
55 | @Test
56 | public void testGetMetricsOfGivenType() {
57 | hello.hello();
58 | hello.howdy();
59 | SortedMap timers = summary.getAppMetrics().getTimers();
60 | assertEquals(1, timers.size());
61 | assertTrue(timers.containsKey(new MetricID("howdy-time")));
62 | assertFalse(timers.containsKey(new MetricID(("hello-count"))));
63 | }
64 | }
65 |
--------------------------------------------------------------------------------
/testsuite/extra/src/test/java/io/smallrye/metrics/test/registry/MetricRegistryInjectionTest.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2018 Red Hat, Inc, and individual contributors.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package io.smallrye.metrics.test.registry;
17 |
18 | import static org.junit.Assert.assertEquals;
19 | import static org.junit.Assert.assertNotNull;
20 | import static org.junit.Assert.assertTrue;
21 |
22 | import jakarta.inject.Inject;
23 |
24 | import org.eclipse.microprofile.metrics.Counting;
25 | import org.eclipse.microprofile.metrics.Metric;
26 | import org.eclipse.microprofile.metrics.MetricID;
27 | import org.jboss.arquillian.container.test.api.Deployment;
28 | import org.jboss.arquillian.junit.Arquillian;
29 | import org.jboss.shrinkwrap.api.ShrinkWrap;
30 | import org.jboss.shrinkwrap.api.asset.EmptyAsset;
31 | import org.jboss.shrinkwrap.api.spec.WebArchive;
32 | import org.junit.Test;
33 | import org.junit.runner.RunWith;
34 |
35 | import io.smallrye.metrics.test.HelloService;
36 | import io.smallrye.metrics.test.MetricsSummary;
37 |
38 | /**
39 | *
40 | * @author Martin Kouba
41 | */
42 | @RunWith(Arquillian.class)
43 | public class MetricRegistryInjectionTest {
44 |
45 | @Inject
46 | HelloService hello;
47 |
48 | @Inject
49 | MetricsSummary summary;
50 |
51 | @Deployment
52 | public static WebArchive createTestArchive() {
53 | return ShrinkWrap.create(WebArchive.class).addAsWebInfResource(EmptyAsset.INSTANCE, "beans.xml")
54 | .addPackage(MetricsSummary.class.getPackage())
55 | .addPackage(MetricRegistryInjectionTest.class.getPackage());
56 | }
57 |
58 | @Test
59 | public void testInjection() {
60 | hello.hello();
61 | assertNotNull(summary.getBaseMetrics().getMetrics().containsKey("memory.usedHeap"));
62 | assertNotNull(summary.getVendorMetrics().getMetrics().containsKey("loadedModules"));
63 | Metric helloCountMetric = summary.getAppMetrics().getMetrics().get(new MetricID("hello-count"));
64 | assertNotNull(helloCountMetric);
65 | assertTrue(helloCountMetric instanceof Counting);
66 | Counting helloCount = (Counting) helloCountMetric;
67 | assertEquals(1, helloCount.getCount());
68 | }
69 |
70 | }
71 |
--------------------------------------------------------------------------------
/testsuite/extra/src/test/java/io/smallrye/metrics/test/reusability/ReuseMetricWithDifferingTagsBean.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2019 Red Hat, Inc. and/or its affiliates
3 | * and other contributors as indicated by the @author tags.
4 | *
5 | * Licensed under the Apache License, Version 2.0 (the "License");
6 | * you may not use this file except in compliance with the License.
7 | * You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 |
18 | package io.smallrye.metrics.test.reusability;
19 |
20 | import org.eclipse.microprofile.metrics.annotation.Counted;
21 |
22 | public class ReuseMetricWithDifferingTagsBean {
23 |
24 | @Counted(name = "colorCounter", absolute = true, tags = { "color=blue" })
25 | public void colorBlue1() {
26 | }
27 |
28 | @Counted(name = "colorCounter", absolute = true, tags = { "color=red" })
29 | public void colorRed1() {
30 | }
31 |
32 | @Counted(name = "colorCounter", absolute = true, tags = { "color=blue" })
33 | public void colorBlue2() {
34 | }
35 |
36 | @Counted(name = "colorCounter", absolute = true, tags = { "color=red" })
37 | public void colorRed2() {
38 | }
39 |
40 | }
41 |
--------------------------------------------------------------------------------
/testsuite/extra/src/test/java/io/smallrye/metrics/test/reusability/ReuseMetricWithDifferingTagsTest.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2019 Red Hat, Inc. and/or its affiliates
3 | * and other contributors as indicated by the @author tags.
4 | *
5 | * Licensed under the Apache License, Version 2.0 (the "License");
6 | * you may not use this file except in compliance with the License.
7 | * You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 |
18 | package io.smallrye.metrics.test.reusability;
19 |
20 | import jakarta.inject.Inject;
21 |
22 | import org.eclipse.microprofile.metrics.MetricID;
23 | import org.eclipse.microprofile.metrics.MetricRegistry;
24 | import org.eclipse.microprofile.metrics.Tag;
25 | import org.jboss.arquillian.container.test.api.Deployment;
26 | import org.jboss.arquillian.junit.Arquillian;
27 | import org.jboss.shrinkwrap.api.ShrinkWrap;
28 | import org.jboss.shrinkwrap.api.asset.EmptyAsset;
29 | import org.jboss.shrinkwrap.api.spec.WebArchive;
30 | import org.junit.Assert;
31 | import org.junit.Test;
32 | import org.junit.runner.RunWith;
33 |
34 | /**
35 | * Test that two metrics of the same name and differing tags can be created by annotations.
36 | */
37 | @RunWith(Arquillian.class)
38 | public class ReuseMetricWithDifferingTagsTest {
39 |
40 | @Inject
41 | MetricRegistry metricRegistry;
42 |
43 | @Inject
44 | private ReuseMetricWithDifferingTagsBean bean;
45 |
46 | @Deployment
47 | public static WebArchive deployment() {
48 | return ShrinkWrap.create(WebArchive.class)
49 | .addAsWebInfResource(EmptyAsset.INSTANCE, "beans.xml")
50 | .addClass(ReuseMetricWithDifferingTagsBean.class);
51 | }
52 |
53 | @Test
54 | public void test() {
55 | bean.colorBlue1();
56 | bean.colorBlue1();
57 | bean.colorBlue2();
58 | bean.colorRed1();
59 | bean.colorRed2();
60 | Assert.assertEquals(3,
61 | metricRegistry.getCounters().get(new MetricID("colorCounter", new Tag("color", "blue"))).getCount());
62 | Assert.assertEquals(2,
63 | metricRegistry.getCounters().get(new MetricID("colorCounter", new Tag("color", "red"))).getCount());
64 | }
65 |
66 | }
67 |
--------------------------------------------------------------------------------
/testsuite/extra/src/test/java/io/smallrye/metrics/test/stereotype/CountedClass.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2019 Red Hat, Inc. and/or its affiliates
3 | * and other contributors as indicated by the @author tags.
4 | *
5 | * Licensed under the Apache License, Version 2.0 (the "License");
6 | * you may not use this file except in compliance with the License.
7 | * You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 |
18 | package io.smallrye.metrics.test.stereotype;
19 |
20 | import io.smallrye.metrics.test.stereotype.stereotypes.CountMe;
21 |
22 | @CountMe
23 | public class CountedClass {
24 |
25 | public void foo() {
26 |
27 | }
28 |
29 | }
30 |
--------------------------------------------------------------------------------
/testsuite/extra/src/test/java/io/smallrye/metrics/test/stereotype/StereotypeCountedClassTest.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2019 Red Hat, Inc. and/or its affiliates
3 | * and other contributors as indicated by the @author tags.
4 | *
5 | * Licensed under the Apache License, Version 2.0 (the "License");
6 | * you may not use this file except in compliance with the License.
7 | * You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 |
18 | package io.smallrye.metrics.test.stereotype;
19 |
20 | import static org.junit.Assert.assertEquals;
21 | import static org.junit.Assert.assertTrue;
22 |
23 | import jakarta.inject.Inject;
24 |
25 | import org.eclipse.microprofile.metrics.MetricID;
26 | import org.eclipse.microprofile.metrics.MetricRegistry;
27 | import org.jboss.arquillian.container.test.api.Deployment;
28 | import org.jboss.arquillian.junit.Arquillian;
29 | import org.jboss.shrinkwrap.api.ShrinkWrap;
30 | import org.jboss.shrinkwrap.api.asset.EmptyAsset;
31 | import org.jboss.shrinkwrap.api.spec.WebArchive;
32 | import org.junit.Test;
33 | import org.junit.runner.RunWith;
34 |
35 | import io.smallrye.metrics.test.stereotype.stereotypes.CountMe;
36 |
37 | @RunWith(Arquillian.class)
38 | public class StereotypeCountedClassTest {
39 |
40 | @Deployment
41 | public static WebArchive createTestArchive() {
42 | return ShrinkWrap.create(WebArchive.class)
43 | .addAsWebInfResource(EmptyAsset.INSTANCE, "beans.xml")
44 | .addClasses(CountedClass.class, CountMe.class);
45 | }
46 |
47 | @Inject
48 | MetricRegistry metricRegistry;
49 |
50 | @Inject
51 | CountedClass bean;
52 |
53 | @Test
54 | public void test() {
55 | MetricID id_constructor = new MetricID(CountedClass.class.getName() + ".CountedClass");
56 | assertTrue(metricRegistry.getCounters().containsKey(id_constructor));
57 | MetricID id_method = new MetricID(CountedClass.class.getName() + ".foo");
58 | assertTrue(metricRegistry.getCounters().containsKey(id_method));
59 | bean.foo();
60 | assertEquals(1, metricRegistry.getCounters().get(id_method).getCount());
61 | }
62 |
63 | }
64 |
--------------------------------------------------------------------------------
/testsuite/extra/src/test/java/io/smallrye/metrics/test/stereotype/stereotypes/CountMe.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2019 Red Hat, Inc. and/or its affiliates
3 | * and other contributors as indicated by the @author tags.
4 | *
5 | * Licensed under the Apache License, Version 2.0 (the "License");
6 | * you may not use this file except in compliance with the License.
7 | * You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 |
18 | package io.smallrye.metrics.test.stereotype.stereotypes;
19 |
20 | import java.lang.annotation.ElementType;
21 | import java.lang.annotation.Retention;
22 | import java.lang.annotation.RetentionPolicy;
23 | import java.lang.annotation.Target;
24 |
25 | import jakarta.enterprise.inject.Stereotype;
26 |
27 | import org.eclipse.microprofile.metrics.annotation.Counted;
28 |
29 | @Stereotype
30 | @Retention(RetentionPolicy.RUNTIME)
31 | @Target({ ElementType.TYPE, ElementType.METHOD, ElementType.FIELD })
32 | @Counted
33 | public @interface CountMe {
34 |
35 | }
36 |
--------------------------------------------------------------------------------
/testsuite/optional-tck/src/test/java/io/smallrye/metrics/tck/optional/JaxRsMetricsActivatingProcessor.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2019 Red Hat, Inc. and/or its affiliates
3 | * and other contributors as indicated by the @author tags.
4 | *
5 | * Licensed under the Apache License, Version 2.0 (the "License");
6 | * you may not use this file except in compliance with the License.
7 | * You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 |
18 | package io.smallrye.metrics.tck.optional;
19 |
20 | import java.io.File;
21 |
22 | import jakarta.ws.rs.ext.Providers;
23 |
24 | import org.jboss.arquillian.container.test.spi.TestDeployment;
25 | import org.jboss.arquillian.container.test.spi.client.deployment.ProtocolArchiveProcessor;
26 | import org.jboss.shrinkwrap.api.Archive;
27 | import org.jboss.shrinkwrap.api.spec.WebArchive;
28 | import org.jboss.shrinkwrap.resolver.api.maven.Maven;
29 |
30 | import io.smallrye.metrics.jaxrs.JaxRsMetricsFilter;
31 | import io.smallrye.metrics.jaxrs.JaxRsMetricsServletFilter;
32 |
33 | public class JaxRsMetricsActivatingProcessor implements ProtocolArchiveProcessor {
34 |
35 | @Override
36 | public void process(TestDeployment testDeployment, Archive> archive) {
37 | WebArchive war = (WebArchive) archive;
38 |
39 | String[] deps = {
40 | // "io.smallrye:smallrye-config",
41 | "io.smallrye:smallrye-metrics",
42 | "io.smallrye:smallrye-metrics-testsuite-common",
43 | "org.eclipse.microprofile.metrics:microprofile-metrics-api",
44 | // "org.eclipse.microprofile.config:microprofile-config-api"
45 | };
46 | File[] dependencies = Maven.resolver().loadPomFromFile(new File("pom.xml")).resolve(deps).withTransitivity().asFile();
47 | war.addAsLibraries(dependencies);
48 |
49 | war.addClass(MetricsHttpServlet.class);
50 | war.addClass(JaxRsMetricsFilter.class);
51 | war.addClass(JaxRsMetricsServletFilter.class);
52 |
53 | // change application context root to '/' because the TCK assumes that the metrics
54 | // will be available at '/metrics', and in our case the metrics servlet is located
55 | // within the application itself, we don't use WildFly's built-in support for metrics
56 | war.addAsWebInfResource("WEB-INF/jboss-web.xml", "jboss-web.xml");
57 |
58 | // activate the servlet filter
59 | war.setWebXML("WEB-INF/web.xml");
60 |
61 | // activate the JAX-RS request filter
62 | war.addAsServiceProvider(Providers.class.getName(), JaxRsMetricsFilter.class.getName());
63 |
64 | // exclude built-in Metrics and Config from WildFly
65 | war.addAsManifestResource("META-INF/jboss-deployment-structure.xml", "jboss-deployment-structure.xml");
66 | }
67 |
68 | }
69 |
--------------------------------------------------------------------------------
/testsuite/optional-tck/src/test/java/io/smallrye/metrics/tck/optional/JaxRsMetricsExtension.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2018 Red Hat, Inc, and individual contributors.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package io.smallrye.metrics.tck.optional;
17 |
18 | import org.jboss.arquillian.container.test.spi.client.deployment.ProtocolArchiveProcessor;
19 | import org.jboss.arquillian.core.spi.LoadableExtension;
20 |
21 | public class JaxRsMetricsExtension implements LoadableExtension {
22 |
23 | @Override
24 | public void register(ExtensionBuilder extensionBuilder) {
25 | extensionBuilder.service(ProtocolArchiveProcessor.class, JaxRsMetricsActivatingProcessor.class);
26 | }
27 |
28 | }
29 |
--------------------------------------------------------------------------------
/testsuite/optional-tck/src/test/java/io/smallrye/metrics/tck/optional/MetricsHttpServlet.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2017 Red Hat, Inc. and/or its affiliates
3 | * and other contributors as indicated by the @author tags.
4 | *
5 | * Licensed under the Apache License, Version 2.0 (the "License");
6 | * you may not use this file except in compliance with the License.
7 | * You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 | package io.smallrye.metrics.tck.optional;
18 |
19 | import java.io.IOException;
20 | import java.util.Collections;
21 | import java.util.stream.Stream;
22 |
23 | import jakarta.inject.Inject;
24 | import jakarta.servlet.annotation.WebServlet;
25 | import jakarta.servlet.http.HttpServlet;
26 | import jakarta.servlet.http.HttpServletRequest;
27 | import jakarta.servlet.http.HttpServletResponse;
28 |
29 | import io.smallrye.metrics.MetricsRequestHandler;
30 |
31 | @WebServlet(name = "metrics-servlet", urlPatterns = "/metrics/*")
32 | public class MetricsHttpServlet extends HttpServlet {
33 |
34 | @Override
35 | protected void doOptions(HttpServletRequest req, HttpServletResponse resp) throws IOException {
36 | doGet(req, resp);
37 | }
38 |
39 | @Override
40 | protected void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
41 | String requestPath = request.getRequestURI();
42 | String method = request.getMethod();
43 | Stream acceptHeaders = Collections.list(request.getHeaders("Accept")).stream();
44 |
45 | metricsHandler.handleRequest(requestPath, method, acceptHeaders, (status, message, headers) -> {
46 | headers.forEach(response::addHeader);
47 | response.setStatus(status);
48 | response.getWriter().write(message);
49 | });
50 | }
51 |
52 | @Inject
53 | private MetricsRequestHandler metricsHandler;
54 | }
55 |
--------------------------------------------------------------------------------
/testsuite/optional-tck/src/test/resources/META-INF/jboss-deployment-structure.xml:
--------------------------------------------------------------------------------
1 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
--------------------------------------------------------------------------------
/testsuite/optional-tck/src/test/resources/META-INF/services/org.jboss.arquillian.core.spi.LoadableExtension:
--------------------------------------------------------------------------------
1 | io.smallrye.metrics.tck.optional.JaxRsMetricsExtension
--------------------------------------------------------------------------------
/testsuite/optional-tck/src/test/resources/WEB-INF/jboss-web.xml:
--------------------------------------------------------------------------------
1 |
17 |
18 |
19 | /
20 |
--------------------------------------------------------------------------------
/testsuite/optional-tck/src/test/resources/WEB-INF/web.xml:
--------------------------------------------------------------------------------
1 |
2 |
18 |
19 |
24 |
25 | jaxrsmetrics
26 | io.smallrye.metrics.jaxrs.JaxRsMetricsServletFilter
27 | true
28 |
29 |
30 | jaxrsmetrics
31 | /*
32 |
33 |
--------------------------------------------------------------------------------
/testsuite/pom.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
18 |
19 |
20 | 4.0.0
21 |
22 |
23 | io.smallrye
24 | smallrye-metrics-parent
25 | 4.0.1-SNAPSHOT
26 |
27 |
28 | smallrye-metrics-testsuite-parent
29 | pom
30 |
31 | SmallRye: MicroProfile Metrics Test Suite Parent
32 |
33 |
34 | common
35 | api-tck
36 | extra
37 | rest-tck
38 | optional-tck
39 |
40 |
41 |
42 |
43 |
44 | org.sonatype.plugins
45 | nexus-staging-maven-plugin
46 |
47 | true
48 |
49 |
50 |
51 |
52 |
53 |
--------------------------------------------------------------------------------
/testsuite/rest-tck/src/test/java/io/smallrye/metrics/tck/rest/ArchiveProcessor.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2018 Red Hat, Inc, and individual contributors.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package io.smallrye.metrics.tck.rest;
17 |
18 | import java.io.File;
19 |
20 | import org.jboss.arquillian.container.test.spi.TestDeployment;
21 | import org.jboss.arquillian.container.test.spi.client.deployment.ProtocolArchiveProcessor;
22 | import org.jboss.shrinkwrap.api.Archive;
23 | import org.jboss.shrinkwrap.api.spec.WebArchive;
24 | import org.jboss.shrinkwrap.resolver.api.maven.Maven;
25 |
26 | /**
27 | * @author Michal Szynkiewicz, michal.l.szynkiewicz@gmail.com
28 | *
29 | * Date: 6/19/18
30 | */
31 | public class ArchiveProcessor implements ProtocolArchiveProcessor {
32 | @Override
33 | public void process(TestDeployment testDeployment, Archive> protocolArchive) {
34 | WebArchive war = (WebArchive) protocolArchive;
35 | war.addAsWebInfResource("WEB-INF/jboss-web.xml", "jboss-web.xml");
36 | String[] deps = {
37 | "io.smallrye:smallrye-config",
38 | "io.smallrye:smallrye-metrics",
39 | "io.smallrye:smallrye-metrics-testsuite-common",
40 | "org.eclipse.microprofile.metrics:microprofile-metrics-api",
41 | // "org.jboss.weld.servlet:weld-servlet-core"
42 | };
43 |
44 | File[] dependencies = Maven.resolver().loadPomFromFile(new File("pom.xml")).resolve(deps).withTransitivity().asFile();
45 |
46 | war.addAsLibraries(dependencies);
47 |
48 | // war.addClass(SmallRyeBeanArchiveHandler.class);
49 | war.addClass(MetricsHttpServlet.class);
50 | // war.addClass(MetricsInitializer.class);
51 | war.addAsResource("io/smallrye/metrics/base-metrics.properties", "/io/smallrye/metrics/base-metrics.properties");
52 | // war.addAsServiceProvider(BeanArchiveHandler.class, SmallRyeBeanArchiveHandler.class);
53 | // war.addAsServiceProvider(Extension.class, ConfigExtension.class);
54 | // war.addAsServiceProvider(ConfigProviderResolver.class, SmallRyeConfigProviderResolver.class);
55 | }
56 | }
57 |
--------------------------------------------------------------------------------
/testsuite/rest-tck/src/test/java/io/smallrye/metrics/tck/rest/MetricsExtension.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2018 Red Hat, Inc, and individual contributors.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package io.smallrye.metrics.tck.rest;
17 |
18 | import org.jboss.arquillian.container.test.spi.client.deployment.ProtocolArchiveProcessor;
19 | import org.jboss.arquillian.core.spi.LoadableExtension;
20 |
21 | public class MetricsExtension implements LoadableExtension {
22 |
23 | @Override
24 | public void register(ExtensionBuilder extensionBuilder) {
25 | extensionBuilder.service(ProtocolArchiveProcessor.class, ArchiveProcessor.class);
26 | }
27 |
28 | }
29 |
--------------------------------------------------------------------------------
/testsuite/rest-tck/src/test/java/io/smallrye/metrics/tck/rest/MetricsHttpServlet.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2017 Red Hat, Inc. and/or its affiliates
3 | * and other contributors as indicated by the @author tags.
4 | *
5 | * Licensed under the Apache License, Version 2.0 (the "License");
6 | * you may not use this file except in compliance with the License.
7 | * You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 | package io.smallrye.metrics.tck.rest;
18 |
19 | import java.io.IOException;
20 | import java.util.Collections;
21 | import java.util.stream.Stream;
22 |
23 | import jakarta.inject.Inject;
24 | import jakarta.servlet.annotation.WebServlet;
25 | import jakarta.servlet.http.HttpServlet;
26 | import jakarta.servlet.http.HttpServletRequest;
27 | import jakarta.servlet.http.HttpServletResponse;
28 |
29 | import io.smallrye.metrics.MetricsRequestHandler;
30 |
31 | /**
32 | * @author hrupp
33 | * @author Michal Szynkiewicz, michal.l.szynkiewicz@gmail.com
34 | */
35 | @WebServlet(name = "metrics-servlet", urlPatterns = "/metrics/*")
36 | public class MetricsHttpServlet extends HttpServlet {
37 |
38 | @Override
39 | protected void doOptions(HttpServletRequest req, HttpServletResponse resp) throws IOException {
40 | doGet(req, resp);
41 | }
42 |
43 | @Override
44 | protected void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
45 | String requestPath = request.getRequestURI();
46 | String method = request.getMethod();
47 | Stream acceptHeaders = Collections.list(request.getHeaders("Accept")).stream();
48 |
49 | metricsHandler.handleRequest(requestPath, method, acceptHeaders, (status, message, headers) -> {
50 | headers.forEach(response::addHeader);
51 | response.setStatus(status);
52 | response.getWriter().write(message);
53 | });
54 | }
55 |
56 | @Inject
57 | private MetricsRequestHandler metricsHandler;
58 | }
59 |
--------------------------------------------------------------------------------
/testsuite/rest-tck/src/test/java/io/smallrye/metrics/tck/rest/SmallRyeBeanArchiveHandler.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2018 Red Hat, Inc, and individual contributors.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package io.smallrye.metrics.tck.rest;
17 |
18 | import java.io.IOException;
19 | import java.io.InputStream;
20 | import java.net.URL;
21 | import java.util.zip.ZipEntry;
22 | import java.util.zip.ZipInputStream;
23 |
24 | import jakarta.annotation.Priority;
25 |
26 | import org.jboss.weld.environment.deployment.discovery.BeanArchiveBuilder;
27 | import org.jboss.weld.environment.deployment.discovery.BeanArchiveHandler;
28 | import org.jboss.weld.environment.util.Files;
29 |
30 | /**
31 | * Copied from SmallRye Rest Client
32 | *
33 | * Special handler for WildFly VFS urls.
34 | *
35 | * @author Martin Kouba
36 | */
37 | @Priority(1)
38 | public class SmallRyeBeanArchiveHandler implements BeanArchiveHandler {
39 |
40 | @Override
41 | public BeanArchiveBuilder handle(String beanArchiveReference) {
42 |
43 | // An external form of a wildfly beans.xml URL will be something like:
44 | // vfs:/content/_DEFAULT___DEFAULT__1f4a3572-fc97-41f5-8fed-0e7e7f7895df.war/WEB-INF/lib/4e0a1b50-4a74-429d-b519-9eedcac11046.jar/META-INF/beans.xml
45 | beanArchiveReference = beanArchiveReference.substring(0, beanArchiveReference.lastIndexOf("/META-INF/beans.xml"));
46 |
47 | if (beanArchiveReference.endsWith(".war")) {
48 | // We only use this handler for libraries - WEB-INF classes are handled by ServletContextBeanArchiveHandler
49 | return null;
50 | }
51 |
52 | try {
53 | URL url = new URL(beanArchiveReference);
54 | try (ZipInputStream in = openStream(url)) {
55 | BeanArchiveBuilder builder = new BeanArchiveBuilder();
56 | handleLibrary(url, in, builder);
57 | return builder;
58 | }
59 | } catch (IOException e) {
60 | throw new IllegalStateException(e);
61 | }
62 | }
63 |
64 | ZipInputStream openStream(URL url) throws IOException {
65 | InputStream in = url.openStream();
66 | return (in instanceof ZipInputStream) ? (ZipInputStream) in : new ZipInputStream(in);
67 | }
68 |
69 | private void handleLibrary(URL url, ZipInputStream zip, BeanArchiveBuilder builder) throws IOException {
70 | ZipEntry entry = null;
71 | while ((entry = zip.getNextEntry()) != null) {
72 | if (Files.isClass(entry.getName())) {
73 | builder.addClass(Files.filenameToClassname(entry.getName()));
74 | }
75 | }
76 | }
77 | }
78 |
--------------------------------------------------------------------------------
/testsuite/rest-tck/src/test/resources/META-INF/services/org.jboss.arquillian.core.spi.LoadableExtension:
--------------------------------------------------------------------------------
1 | io.smallrye.metrics.tck.rest.MetricsExtension
--------------------------------------------------------------------------------
/testsuite/rest-tck/src/test/resources/WEB-INF/jboss-web.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 | /
4 |
--------------------------------------------------------------------------------
/testsuite/rest-tck/src/test/resources/logging.properties:
--------------------------------------------------------------------------------
1 | #
2 | # Copyright 2019 Red Hat, Inc. and/or its affiliates
3 | # and other contributors as indicated by the @author tags.
4 | #
5 | # Licensed under the Apache License, Version 2.0 (the "License");
6 | # you may not use this file except in compliance with the License.
7 | # You may obtain a copy of the License at
8 | #
9 | # http://www.apache.org/licenses/LICENSE-2.0
10 | #
11 | # Unless required by applicable law or agreed to in writing, software
12 | # distributed under the License is distributed on an "AS IS" BASIS,
13 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | # See the License for the specific language governing permissions and
15 | # limitations under the License.
16 | #
17 |
18 | handlers = java.util.logging.ConsoleHandler
19 | java.util.logging.ConsoleHandler.level = FINE
20 | io.smallrye.metrics.level = FINE
21 | org.jboss.weld.level = WARN
--------------------------------------------------------------------------------