Search in sources :

Example 1 with MetadataBuilder

use of org.eclipse.microprofile.metrics.MetadataBuilder in project Payara by payara.

the class MetricRegistryImplTest method registerByMetadataAcceptsInstancesWithSameNameButDifferentTags.

@Test
public void registerByMetadataAcceptsInstancesWithSameNameButDifferentTags() {
    EnumSet<MetricType> types = EnumSet.allOf(MetricType.class);
    // obviously
    types.remove(MetricType.INVALID);
    // gauges cannot be created
    types.remove(MetricType.GAUGE);
    for (MetricType type : types) {
        String name = nextName();
        for (int i = 0; i < 10; i++) {
            final int n = i;
            MetadataBuilder metadata = withNameAnd(name).withType(type);
            Runnable register = () -> registry.register(metadata.build(), null, new Tag("a", "b" + n));
            // first time should work
            register.run();
            // works again as all metrics are reusable
            register.run();
        }
    }
}
Also used : MetadataBuilder(org.eclipse.microprofile.metrics.MetadataBuilder) MetricType(org.eclipse.microprofile.metrics.MetricType) Tag(org.eclipse.microprofile.metrics.Tag) Test(org.junit.Test)

Example 2 with MetadataBuilder

use of org.eclipse.microprofile.metrics.MetadataBuilder in project Payara by payara.

the class MBeanMetadataHelper method loadSubAttribute.

private static List<MBeanMetadata> loadSubAttribute(ObjectName objName, MBeanExpression mBeanExpression, MBeanMetadata metadata, String key, String attribute, String instanceName, boolean isDynamicAttribute) {
    List<MBeanMetadata> metadataList = new ArrayList<>();
    String exp = objName.getCanonicalName();
    String subAttribute = mBeanExpression.getSubAttributeName();
    if (subAttribute != null) {
        if (SUB_ATTRIBUTE.equals(subAttribute)) {
            Object obj = mBeanExpression.querySubAttributes(objName, attribute);
            if (obj instanceof CompositeDataSupport) {
                CompositeDataSupport compositeData = (CompositeDataSupport) obj;
                MetadataBuilder newMetadataBuilder = Metadata.builder(metadata);
                for (String subAttrResolvedName : compositeData.getCompositeType().keySet()) {
                    subAttribute = subAttrResolvedName;
                    if ("description".equals(subAttribute) && compositeData.get(subAttribute) instanceof String && metadata.description().isPresent()) {
                        newMetadataBuilder = newMetadataBuilder.withDescription((String) compositeData.get(subAttribute));
                    } else if ("name".equals(subAttribute) && compositeData.get(subAttribute) instanceof String && metadata.getDisplayName() == null) {
                        newMetadataBuilder = newMetadataBuilder.withDisplayName((String) compositeData.get(subAttribute));
                    } else if ("unit".equals(subAttribute) && compositeData.get(subAttribute) instanceof String && MetricUnits.NONE.equals(metadata.unit().orElse("none"))) {
                        newMetadataBuilder = newMetadataBuilder.withUnit((String) compositeData.get(subAttribute));
                    }
                }
                MBeanMetadata newMbeanMetadata = new MBeanMetadata(newMetadataBuilder.build());
                newMbeanMetadata.addTags(metadata.getTags());
                metadataList.add(createMetadata(newMbeanMetadata, exp, key, attribute, subAttribute, instanceName));
            }
        } else if (isDynamicAttribute) {
            Object obj = mBeanExpression.querySubAttributes(objName, attribute);
            if (obj instanceof CompositeDataSupport) {
                CompositeDataSupport compositeData = (CompositeDataSupport) obj;
                if (compositeData.containsKey(subAttribute) && compositeData.get(subAttribute) instanceof Number) {
                    metadataList.add(createMetadata(metadata, exp, key, attribute, subAttribute, instanceName));
                }
            }
        } else {
            metadataList.add(createMetadata(metadata, exp, key, attribute, subAttribute, instanceName));
        }
    } else {
        metadataList.add(createMetadata(metadata, exp, key, attribute, subAttribute, instanceName));
    }
    return metadataList;
}
Also used : MetadataBuilder(org.eclipse.microprofile.metrics.MetadataBuilder) CompositeDataSupport(javax.management.openmbean.CompositeDataSupport) ArrayList(java.util.ArrayList)

Aggregations

MetadataBuilder (org.eclipse.microprofile.metrics.MetadataBuilder)2 ArrayList (java.util.ArrayList)1 CompositeDataSupport (javax.management.openmbean.CompositeDataSupport)1 MetricType (org.eclipse.microprofile.metrics.MetricType)1 Tag (org.eclipse.microprofile.metrics.Tag)1 Test (org.junit.Test)1