Java Code Examples for org.eclipse.microprofile.metrics.Metadata#getTypeRaw()

The following examples show how to use org.eclipse.microprofile.metrics.Metadata#getTypeRaw() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: MetricsRegistryImpl.java    From smallrye-metrics with Apache License 2.0 5 votes vote down vote up
private Metadata sanitizeMetadata(Metadata metadata, Class<?> metricClass) {
    if (metadata.getTypeRaw() == null || metadata.getTypeRaw() == MetricType.INVALID) {
        MetricType inferredMetricType = inferMetricType(metricClass);
        return Metadata.builder(metadata).withType(inferredMetricType).build();
    } else {
        return metadata;
    }
}
 
Example 2
Source File: MetricsRegistryImpl.java    From smallrye-metrics with Apache License 2.0 5 votes vote down vote up
private Metadata sanitizeMetadata(Metadata metadata, MetricType metricType) {
    // if the metadata does not specify a type, we add it here
    // if the metadata specifies a type, we check that it's the correct one
    // (for example, someone might have called registry.counter(metadata) where metadata.type="gauge")
    if (metadata.getTypeRaw() == null || metadata.getTypeRaw() == MetricType.INVALID) {
        return Metadata.builder(metadata).withType(metricType).build();
    } else {
        if (metadata.getTypeRaw() != metricType) {
            throw SmallRyeMetricsMessages.msg.typeMismatch(metricType, metadata.getTypeRaw());
        } else {
            return metadata;
        }
    }
}
 
Example 3
Source File: OpenMetricsExporter.java    From smallrye-metrics with Apache License 2.0 5 votes vote down vote up
private void writeValueLine(StringBuilder sb,
        MetricRegistry.Type scope,
        String suffix,
        double valueRaw,
        Metadata md,
        Map<String, String> tags,
        boolean performScaling) {
    String name = md.getName();
    name = getOpenMetricsMetricName(name);
    fillBaseName(sb, scope, name, suffix, md);

    // add tags

    if (tags != null) {
        addTags(sb, tags, scope, md);
    }

    sb.append(SPACE);

    Double value;
    if (performScaling) {
        String scaleFrom = "nanoseconds";
        if (md.getTypeRaw() == MetricType.HISTOGRAM)
            // for histograms, internally the data is stored using the metric's unit
            scaleFrom = md.unit().orElse(NONE);
        value = OpenMetricsUnit.scaleToBase(scaleFrom, valueRaw);
    } else {
        value = valueRaw;
    }
    sb.append(value).append(LF);

}
 
Example 4
Source File: MetricBuildItem.java    From quarkus with Apache License 2.0 5 votes vote down vote up
/**
 * Create a metric build item from the specified metadata and tags.
 * Such metric will be picked up by the Metrics extension and registered in the VENDOR registry.
 * This constructor is applicable to all metric types except gauges.
 *
 * @param metadata The metadata that should be applied to the registered metric
 * @param enabled Whether this metric is enabled
 * @param tags The tags that will be applied to this metric
 * @param configRootName the name of the root configuration of the extension as defined by the <code>@ConfigRoot</code>
 *        annotation.
 */
public MetricBuildItem(Metadata metadata, boolean enabled, String configRootName, Tag... tags) {
    if (metadata.getTypeRaw() == MetricType.GAUGE) {
        throw new IllegalArgumentException("Gauges require a non-null implementation object");
    }
    this.metadata = metadata;
    this.tags = tags;
    this.implementor = null;
    this.enabled = enabled;
    this.configRootName = configRootName;
    this.registryType = MetricRegistry.Type.VENDOR;
}
 
Example 5
Source File: MetadataHolder.java    From quarkus with Apache License 2.0 5 votes vote down vote up
public static MetadataHolder from(Metadata metadata) {
    MetadataHolder result = new MetadataHolder();
    result.name = metadata.getName();
    result.metricType = metadata.getTypeRaw();
    result.description = metadata.getDescription().orElse(null);
    result.displayName = metadata.getDisplayName();
    result.unit = metadata.getUnit().orElse(MetricUnits.NONE);
    return result;
}
 
Example 6
Source File: MetricsRegistryImpl.java    From smallrye-metrics with Apache License 2.0 4 votes vote down vote up
private synchronized <T extends Metric> T get(MetricID metricID, Metadata metadata, T implementor) {
    String name = metadata.getName();
    MetricType type = metadata.getTypeRaw();
    if (name == null || name.isEmpty()) {
        throw SmallRyeMetricsMessages.msg.metricNameMustNotBeNullOrEmpty();
    }

    Metadata previousMetadata = metadataMap.get(name);
    Metric previousMetric = metricMap.get(metricID);

    if (previousMetric == null) {
        Metric m;
        switch (type) {

            case COUNTER:
                m = new CounterImpl();
                break;
            case GAUGE:
                m = implementor;
                break;
            case METERED:
                m = new MeterImpl();
                break;
            case HISTOGRAM:
                m = new HistogramImpl(new ExponentiallyDecayingReservoir());
                break;
            case TIMER:
                m = new TimerImpl(new ExponentiallyDecayingReservoir());
                break;
            case CONCURRENT_GAUGE:
                m = new ConcurrentGaugeImpl();
                break;
            case SIMPLE_TIMER:
                m = new SimpleTimerImpl();
                break;
            case INVALID:
            default:
                throw new IllegalStateException("Must not happen");
        }
        if (metadata instanceof OriginAndMetadata) {
            SmallRyeMetricsLogging.log.registerMetric(metricID, type,
                    ((OriginAndMetadata) metadata).getOrigin());
        } else {
            SmallRyeMetricsLogging.log.registerMetric(metricID, type);
        }

        register(metadata, m, metricID.getTagsAsList().toArray(new Tag[] {}));
    } else if (!previousMetadata.getTypeRaw().equals(metadata.getTypeRaw())) {
        throw SmallRyeMetricsMessages.msg.metricExistsUnderDifferentType(name, previousMetadata.getType());
    } else if (metadata instanceof OriginAndMetadata &&
            originMap.get(metricID) != null &&
            areCompatibleOrigins(originMap.get(metricID), ((OriginAndMetadata) metadata).getOrigin())) {
        // stop caring, same thing.
    } else {
        verifyMetadataEquality(metadata, previousMetadata);
    }

    return (T) metricMap.get(metricID);
}
 
Example 7
Source File: SmallRyeMetricsRecorder.java    From quarkus with Apache License 2.0 4 votes vote down vote up
public void registerMetric(MetricRegistry.Type scope,
        MetadataHolder metadataHolder,
        TagHolder[] tagHolders,
        Object implementor) {
    Metadata metadata = metadataHolder.toMetadata();
    Tag[] tags = Arrays.stream(tagHolders).map(TagHolder::toTag).toArray(Tag[]::new);
    MetricRegistry registry = MetricRegistries.get(scope);

    switch (metadata.getTypeRaw()) {
        case GAUGE:
            registry.register(metadata, (Gauge) implementor, tags);
            break;
        case TIMER:
            if (implementor == null) {
                registry.timer(metadata, tags);
            } else {
                registry.register(metadata, (Timer) implementor);
            }
            break;
        case COUNTER:
            if (implementor == null) {
                registry.counter(metadata, tags);
            } else {
                registry.register(metadata, (Counter) implementor, tags);
            }
            break;
        case HISTOGRAM:
            if (implementor == null) {
                registry.histogram(metadata, tags);
            } else {
                registry.register(metadata, (Histogram) implementor, tags);
            }
            break;
        case CONCURRENT_GAUGE:
            if (implementor == null) {
                registry.concurrentGauge(metadata, tags);
            } else {
                registry.register(metadata, (ConcurrentGauge) implementor, tags);
            }
            break;
        case METERED:
            if (implementor == null) {
                registry.meter(metadata, tags);
            } else {
                registry.register(metadata, (Metered) implementor, tags);
            }
            break;
        case INVALID:
            break;
        default:
            break;
    }
}
 
Example 8
Source File: MetricBuildItem.java    From quarkus with Apache License 2.0 3 votes vote down vote up
/**
 * Create a metric build item from the specified metadata, tags, and a callable.
 * Such metric will be picked up by the Metrics extension and registered in the VENDOR registry.
 *
 * @param metadata The metadata that should be applied to the registered metric
 * @param implementor The object that implements the metric. It must be an instance of the appropriate
 *        metric class (from the {@link org.eclipse.microprofile.metrics} package).
 *        This is required for gauges and optional for all other metric types.
 * @param enabled Whether this metric is enabled
 * @param tags The tags that will be applied to this metric
 * @param configRootName the name of the root configuration of the extension as defined by the <code>@ConfigRoot</code>
 *        annotation.
 *
 */
public MetricBuildItem(Metadata metadata, Object implementor, boolean enabled, String configRootName, Tag... tags) {
    if (implementor == null && metadata.getTypeRaw() == MetricType.GAUGE) {
        throw new IllegalArgumentException("Gauges require a non-null implementation object");
    }
    this.metadata = metadata;
    this.tags = tags;
    this.implementor = implementor;
    this.enabled = enabled;
    this.configRootName = configRootName;
    this.registryType = MetricRegistry.Type.VENDOR;
}
 
Example 9
Source File: MetricBuildItem.java    From quarkus with Apache License 2.0 3 votes vote down vote up
/**
 * Create a metric build item from the specified metadata, tags, a callable, and scope.
 * Such metric will be picked up by the Metrics extension and registered in the registry for the desired scope.
 *
 * @param metadata The metadata that should be applied to the registered metric
 * @param implementor The object that implements the metric. It must be an instance of the appropriate
 *        metric class (from the {@link org.eclipse.microprofile.metrics} package).
 *        This is required for gauges and optional for all other metric types.
 * @param enabled Whether this metric is enabled
 * @param tags The tags that will be applied to this metric
 * @param configRootName the name of the root configuration of the extension as defined by the <code>@ConfigRoot</code>
 *        annotation.
 * @param registryType Registry where the metric should be placed
 *
 */
public MetricBuildItem(Metadata metadata, Object implementor, boolean enabled, String configRootName,
        MetricRegistry.Type registryType, Tag... tags) {
    if (implementor == null && metadata.getTypeRaw() == MetricType.GAUGE) {
        throw new IllegalArgumentException("Gauges require a non-null implementation object");
    }
    this.metadata = metadata;
    this.tags = tags;
    this.implementor = implementor;
    this.enabled = enabled;
    this.configRootName = configRootName;
    this.registryType = registryType;
}