Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Specify handling of CDI stereotypes with metrics #541

Merged
merged 2 commits into from Mar 4, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 4 additions & 0 deletions spec/src/main/asciidoc/app-programming-model.adoc
Expand Up @@ -571,6 +571,10 @@ public void init(@Metric(name="instances") Counter instances) {
}
----

=== Usage of CDI stereotypes
If a metric annotation is applied to a bean through a CDI stereotype, the implementation must handle it the same way as if the metric annotation
was applied on the target bean directly. Metric names are computed relative to the name and package of the bean itself, not of the stereotype.

=== Registering metrics dynamically
In addition to declaring metrics via annotations, it is possible to dynamically (un)register metrics by
calling methods of a `MetricRegistry` object. While using annotations is generally the preferred approach,
Expand Down
3 changes: 3 additions & 0 deletions spec/src/main/asciidoc/changelog.adoc
Expand Up @@ -30,6 +30,9 @@ Changes marked with icon:bolt[role="red"] are breaking changes relative to previ
=== API/SPI Changes
** Added the `MetricRegistry.getType()` method

=== Specification Changes
** Clarified how the implementation must handle metrics applied via CDI stereotypes

[[release_notes_2_3]]
== Changes in 2.3

Expand Down
@@ -0,0 +1,34 @@
/*
**********************************************************************
* Copyright (c) 2020 Contributors to the Eclipse Foundation
*
* See the NOTICES file(s) distributed with this work for additional
* information regarding copyright ownership.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* You may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* SPDX-License-Identifier: Apache-2.0
**********************************************************************/

package org.eclipse.microprofile.metrics.tck.cdi.stereotype;

import org.eclipse.microprofile.metrics.tck.cdi.stereotype.stereotypes.CountMe;

@CountMe
public class StereotypeCountedClassBean {

public void foo() {

}

}
@@ -0,0 +1,94 @@
/*
**********************************************************************
* Copyright (c) 2020 Contributors to the Eclipse Foundation
*
* See the NOTICES file(s) distributed with this work for additional
* information regarding copyright ownership.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* You may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* SPDX-License-Identifier: Apache-2.0
**********************************************************************/
package org.eclipse.microprofile.metrics.tck.cdi.stereotype;

import org.eclipse.microprofile.metrics.Metadata;
import org.eclipse.microprofile.metrics.MetricID;
import org.eclipse.microprofile.metrics.MetricRegistry;
import org.eclipse.microprofile.metrics.tck.cdi.stereotype.stereotypes.CountMe;
import org.eclipse.microprofile.metrics.tck.cdi.stereotype.stereotypes.CountMeWithSpecifiedMetadata;
import org.jboss.arquillian.container.test.api.Deployment;
import org.jboss.arquillian.junit.Arquillian;
import org.jboss.shrinkwrap.api.ShrinkWrap;
import org.jboss.shrinkwrap.api.asset.EmptyAsset;
import org.jboss.shrinkwrap.api.spec.WebArchive;
import org.junit.Test;
import org.junit.runner.RunWith;

import javax.inject.Inject;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;

@RunWith(Arquillian.class)
public class StereotypeCountedClassBeanTest {

@Deployment
public static WebArchive createTestArchive() {
return ShrinkWrap.create(WebArchive.class)
.addAsWebInfResource(EmptyAsset.INSTANCE, "beans.xml")
.addClasses(StereotypeCountedClassBean.class,
StereotypeCountedClassBeanWithSpecifiedMetadata.class,
CountMe.class,
CountMeWithSpecifiedMetadata.class);
}

@Inject
private MetricRegistry metricRegistry;

@Inject
private StereotypeCountedClassBean bean;

@Inject
private StereotypeCountedClassBeanWithSpecifiedMetadata beanWithSpecifiedMetadata;

@Test
public void testPlainAnnotation() {
MetricID constructorMetricId = new MetricID(StereotypeCountedClassBean.class.getName() + ".StereotypeCountedClassBean");
assertTrue(metricRegistry.getCounters().containsKey(constructorMetricId));
MetricID methodMetricId = new MetricID(StereotypeCountedClassBean.class.getName() + ".foo");
assertTrue(metricRegistry.getCounters().containsKey(methodMetricId));
bean.foo();
assertEquals(1, metricRegistry.getCounters().get(methodMetricId).getCount());
}

@Test
public void testWithMetadata() {
String constructorMetricName = "org.eclipse.microprofile.metrics.tck.cdi.stereotype.bloop.StereotypeCountedClassBeanWithSpecifiedMetadata";
MetricID constructorMetricId = new MetricID(constructorMetricName);
assertTrue(metricRegistry.getCounters().containsKey(constructorMetricId));
Metadata constructorMetadata = metricRegistry.getMetadata().get(constructorMetricName);
assertEquals("description", constructorMetadata.getDescription().orElse(null));
assertEquals("displayName", constructorMetadata.getDisplayName());

String methodMetricName = "org.eclipse.microprofile.metrics.tck.cdi.stereotype.bloop.foo";
MetricID methodMetricId = new MetricID(methodMetricName);
assertTrue(metricRegistry.getCounters().containsKey(methodMetricId));
Metadata methodMetadata = metricRegistry.getMetadata().get(methodMetricName);
assertEquals("description", methodMetadata.getDescription().orElse(null));
assertEquals("displayName", methodMetadata.getDisplayName());

beanWithSpecifiedMetadata.foo();
assertEquals(1, metricRegistry.getCounters().get(methodMetricId).getCount());
}

}
@@ -0,0 +1,34 @@
/*
**********************************************************************
* Copyright (c) 2020 Contributors to the Eclipse Foundation
*
* See the NOTICES file(s) distributed with this work for additional
* information regarding copyright ownership.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* You may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* SPDX-License-Identifier: Apache-2.0
**********************************************************************/

package org.eclipse.microprofile.metrics.tck.cdi.stereotype;

import org.eclipse.microprofile.metrics.tck.cdi.stereotype.stereotypes.CountMeWithSpecifiedMetadata;

@CountMeWithSpecifiedMetadata
public class StereotypeCountedClassBeanWithSpecifiedMetadata {

public void foo() {

}

}
@@ -0,0 +1,39 @@
/*
**********************************************************************
* Copyright (c) 2020 Contributors to the Eclipse Foundation
*
* See the NOTICES file(s) distributed with this work for additional
* information regarding copyright ownership.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* You may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* SPDX-License-Identifier: Apache-2.0
**********************************************************************/

package org.eclipse.microprofile.metrics.tck.cdi.stereotype.stereotypes;

import org.eclipse.microprofile.metrics.annotation.Counted;

import javax.enterprise.inject.Stereotype;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Stereotype
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.TYPE})
@Counted
public @interface CountMe {

}
@@ -0,0 +1,39 @@
/*
**********************************************************************
* Copyright (c) 2020 Contributors to the Eclipse Foundation
*
* See the NOTICES file(s) distributed with this work for additional
* information regarding copyright ownership.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* You may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* SPDX-License-Identifier: Apache-2.0
**********************************************************************/

package org.eclipse.microprofile.metrics.tck.cdi.stereotype.stereotypes;

import org.eclipse.microprofile.metrics.annotation.Counted;

import javax.enterprise.inject.Stereotype;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Stereotype
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.TYPE})
@Counted(name = "bloop", description = "description", displayName = "displayName")
public @interface CountMeWithSpecifiedMetadata {

}