Skip to content

Commit

Permalink
Introduce ability to have custom scopes
Browse files Browse the repository at this point in the history
- Remove MetricRegistry.Type and use String constants instead
- Change MetricRegistry.getType() to getScope()
- @RegistryType no longer qualifier, param type() changed to scope()
- Introduce scope annotation param to @counted, @timed, @Gauge and
@Metric
  • Loading branch information
Channyboy committed Aug 5, 2022
1 parent 1c6299d commit 1b9fb14
Show file tree
Hide file tree
Showing 11 changed files with 92 additions and 92 deletions.
Expand Up @@ -42,43 +42,11 @@
*/
public interface MetricRegistry {

/**
* An enumeration representing the scopes of the MetricRegistry
*/
enum Type {
/**
* The Application (default) scoped MetricRegistry. Any metric registered/accessed via CDI will use this
* MetricRegistry.
*/
APPLICATION("application"),

/**
* The Base scoped MetricRegistry. This MetricRegistry will contain required metrics specified in the
* MicroProfile Metrics specification.
*/
BASE("base"),

/**
* The Vendor scoped MetricRegistry. This MetricRegistry will contain vendor provided metrics which may vary
* between different vendors.
*/
VENDOR("vendor");

private final String name;

Type(String name) {
this.name = name;
}

/**
* Returns the name of the MetricRegistry scope.
*
* @return the scope
*/
public String getName() {
return name;
}
}
public static final String APPLICATION_SCOPE = "application";

public static final String VENDOR_SCOPE = "vendor";

public static final String BASE_SCOPE = "base";

/**
* Concatenates elements to form a dotted name, eliding any null values or empty strings.
Expand Down Expand Up @@ -755,6 +723,6 @@ static String name(Class<?> klass, String... names) {
*
* @return Type of this registry (VENDOR, BASE, APPLICATION)
*/
String getType();
String getScope();

}
Expand Up @@ -29,6 +29,7 @@
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

import org.eclipse.microprofile.metrics.MetricRegistry;
import org.eclipse.microprofile.metrics.MetricUnits;

import jakarta.enterprise.util.Nonbinding;
Expand Down Expand Up @@ -144,4 +145,13 @@
@Nonbinding
String unit() default MetricUnits.NONE;

/**
* The scope that this counter belongs to.
*
* @return The scope this counter belongs to. By default, the value is {@link MetricRegistry.APPLICATION_SCOPE}.
*
*/
@Nonbinding
String scope() default MetricRegistry.APPLICATION_SCOPE;

}
Expand Up @@ -27,6 +27,8 @@
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

import org.eclipse.microprofile.metrics.MetricRegistry;

import jakarta.enterprise.util.Nonbinding;
import jakarta.interceptor.InterceptorBinding;

Expand Down Expand Up @@ -115,4 +117,13 @@
@Nonbinding
String unit();

/**
* The scope that this gauge belongs to.
*
* @return The scope this gauge belongs to. By default, the value is {@link MetricRegistry.APPLICATION_SCOPE}.
*
*/
@Nonbinding
String scope() default MetricRegistry.APPLICATION_SCOPE;

}
Expand Up @@ -27,6 +27,7 @@
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

import org.eclipse.microprofile.metrics.MetricRegistry;
import org.eclipse.microprofile.metrics.MetricUnits;

import jakarta.enterprise.util.Nonbinding;
Expand Down Expand Up @@ -114,4 +115,12 @@
@Nonbinding
String unit() default MetricUnits.NONE;

/**
* The scope that this counter belongs to.
*
* @return The scope this counter belongs to. By default, the value is {@link MetricRegistry.APPLICATION_SCOPE}.
*
*/
@Nonbinding
String scope() default MetricRegistry.APPLICATION_SCOPE;
}
Expand Up @@ -29,38 +29,42 @@

import org.eclipse.microprofile.metrics.MetricRegistry;

import jakarta.inject.Qualifier;

/**
* Qualifies the type of Metric Registry to inject.
* The type of Metric Registry to inject.
* <p>
* This can be used to obtain the respective scoped {@link MetricRegistry}:
* </p>
*
* <pre>
* <code>
* {@literal @}Inject
* {@literal @}RegistryType(type=MetricRegistry.Type.BASE)
* MetricRegistry baseRegistry;
* {@literal @}RegistryType(type=MetricRegistry.APPLICATION_SCOPE)
* MetricRegistry appRegistry;
*
* {@literal @}Inject
* {@literal @}RegistryType(type="customScope")
* MetricRegistry appRegistry;
* </code>
* </pre>
*
* @see org.eclipse.microprofile.metrics.MetricRegistry.Type
* see {@link MetricRegistry.APPLICATION_SCOPE}, {@link MetricRegistry.BASE_SCOPE} and
* {@link MetricRegistry.VEDNOR_SCOPE}
*
* @author Raymond Lam
*
*/
@Qualifier
@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.METHOD, ElementType.FIELD, ElementType.PARAMETER, ElementType.ANNOTATION_TYPE})
public @interface RegistryType {
/**
* The scope of the MetricRegistry.
*
* @return Returns the scope of the MetricRegistry. The {@link MetricRegistry.Type} can be {@code APPLICATION},
* {@code BASE}, or {@code VENDOR}.
* @see org.eclipse.microprofile.metrics.MetricRegistry.Type
* @return Returns or creates the scope of the MetricRegistry. The MicroProfile runtimes provides
* {@code application}, {@code base} and {@code vendor} scopes as part of the runtime.
*
* see {@link MetricRegistry.APPLICATION_SCOPE}, {@link MetricRegistry.BASE_SCOPE} and
* {@link MetricRegistry.VEDNOR_SCOPE}
*/
MetricRegistry.Type type() default MetricRegistry.Type.APPLICATION;
String scope() default MetricRegistry.APPLICATION_SCOPE;
}
Expand Up @@ -29,6 +29,7 @@
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

import org.eclipse.microprofile.metrics.MetricRegistry;
import org.eclipse.microprofile.metrics.MetricUnits;

import jakarta.enterprise.util.Nonbinding;
Expand Down Expand Up @@ -138,4 +139,13 @@
@Nonbinding
String unit() default MetricUnits.NANOSECONDS;

/**
* The scope that this Timer belongs to.
*
* @return The scope this Timer belongs to. By default, the value is {@link MetricRegistry.APPLICATION_SCOPE}.
*
*/
@Nonbinding
String scope() default MetricRegistry.APPLICATION_SCOPE;

}
Expand Up @@ -79,7 +79,7 @@
* <pre>
* <code>
* {@literal @}Inject
* {@literal @}RegistryType(type=MetricRegistry.Type.BASE)
* {@literal @}RegistryType(type=MetricRegistry.BASE_SCOPE)
* MetricRegistry baseRegistry;
* </code>
* </pre>
Expand Down
6 changes: 3 additions & 3 deletions spec/src/main/asciidoc/app-programming-model.adoc
Expand Up @@ -493,7 +493,7 @@ MetricRegistry metricRegistry;
[source, java]
----
@Inject
@RegistryType(type=MetricRegistry.Type.APPLICATION)
@RegistryType(scope=MetricRegistry.APPLICATION_SCOPE)
MetricRegistry metricRegistry;
----

Expand All @@ -504,7 +504,7 @@ The implementation must produce the _base_ `MetricRegistry` when the `RegistryTy
[source, java]
----
@Inject
@RegistryType(type=MetricRegistry.Type.BASE)
@RegistryType(scope=MetricRegistry.BASE_SCOPE)
MetricRegistry baseRegistry;
----

Expand All @@ -515,7 +515,7 @@ The implementation must produce the _vendor_ `MetricRegistry` when the `Registry
[source, java]
----
@Inject
@RegistryType(type=MetricRegistry.Type.VENDOR)
@RegistryType(scope=MetricRegistry.VENDOR_SCOPE)
MetricRegistry vendorRegistry;
----

Expand Down
30 changes: 9 additions & 21 deletions spec/src/main/asciidoc/appendix.adoc
Expand Up @@ -89,29 +89,17 @@ This configuration can be backed into the runtime or be provided via an external
public class MetricRegistryFactory {
@Produces
public static MetricRegistry getDefaultRegistry() {
return getApplicationRegistry();
}
@Default
public MetricRegistry getApplicationRegistry(InjectionPoint ip) {
@Produces
@RegistryType(type = Type.APPLICATION)
public static MetricRegistry getApplicationRegistry() {
// Returns the static instance of the Application MetricRegistry
[...]
}
RegistryType registryTypeAnnotation = ip.getAnnotated().getAnnotation(RegistryType.class);
@Produces
@RegistryType(type = Type.BASE)
public static MetricRegistry getBaseRegistry() {
// Returns the static instance of the Base MetricRegistry
[...]
}
@Produces
@RegistryType(type = Type.VENDOR)
public static MetricRegistry getVendorRegistry() {
// Returns the static instance of the Vendor MetricRegistry
[...]
if (registryTypeAnnotation == null) {
return getOrCreate(MetricRegistry.APPLICATION_SCOPE);
} else {
String annoScope = registryTypeAnnotation.scope();
return getOrCreate(annoScope);
}
}
}
Expand Down
Expand Up @@ -55,11 +55,11 @@ public class MetricRegistryTest {
private MetricRegistry metrics;

@Inject
@RegistryType(type = MetricRegistry.Type.BASE)
@RegistryType(scope = MetricRegistry.BASE_SCOPE)
private MetricRegistry baseMetrics;

@Inject
@RegistryType(type = MetricRegistry.Type.VENDOR)
@RegistryType(scope = MetricRegistry.VENDOR_SCOPE)
private MetricRegistry vendorMetrics;

@Deployment
Expand Down Expand Up @@ -106,9 +106,9 @@ public void useExistingMetaDataTest() {
@Test
@InSequence(5)
public void testMetricRegistryType() {
Assert.assertEquals(MetricRegistry.Type.APPLICATION, metrics.getType());
Assert.assertEquals(MetricRegistry.Type.BASE, baseMetrics.getType());
Assert.assertEquals(MetricRegistry.Type.VENDOR, vendorMetrics.getType());
Assert.assertEquals(MetricRegistry.APPLICATION_SCOPE, metrics.getScope());
Assert.assertEquals(MetricRegistry.BASE_SCOPE, baseMetrics.getScope());
Assert.assertEquals(MetricRegistry.VENDOR_SCOPE, vendorMetrics.getScope());
}

private void assertExists(Class<? extends org.eclipse.microprofile.metrics.Metric> expected, MetricID metricID) {
Expand Down

0 comments on commit 1b9fb14

Please sign in to comment.