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

The following examples show how to use org.eclipse.microprofile.metrics.Metadata#getName() . 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: 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 2
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 3
Source File: MetricsRegistryImpl.java    From smallrye-metrics with Apache License 2.0 4 votes vote down vote up
@Override
public synchronized <T extends Metric> T register(Metadata metadata, T metric, Tag... tags) {
    String name = metadata.getName();
    if (name == null) {
        throw SmallRyeMetricsMessages.msg.metricNameMustNotBeNullOrEmpty();
    }
    MetricID metricID = new MetricID(name, tags);
    Metadata existingMetadata = metadataMap.get(name);

    if (metricMap.containsKey(metricID) && metadata.getTypeRaw().equals(MetricType.GAUGE)) {
        throw SmallRyeMetricsMessages.msg.gaugeWithIdAlreadyExists(metricID);
    }

    /*
     * if metadata for this name already exists:
     * - if no metadata was specified for this registration, check that this metric has the same type, then reuse the
     * existing metadata instance
     * - if metadata was specified for this registration, verify that it's the same as the existing one
     * if no metadata for this name exists:
     * - if no metadata was specified for this registration, create a reasonable default
     * - if metadata was specified for this registration, use it
     */
    if (existingMetadata != null) {
        if (metadata instanceof UnspecifiedMetadata) {
            if (!metadata.getType().equals(existingMetadata.getType())) {
                throw SmallRyeMetricsMessages.msg.metricExistsUnderDifferentType(name, existingMetadata.getType());
            }
            metricMap.put(metricID, metric);
        } else {
            verifyMetadataEquality(metadata, existingMetadata);
            metricMap.put(metricID, metric);
            if (metadata instanceof OriginAndMetadata) {
                originMap.put(metricID, ((OriginAndMetadata) metadata).getOrigin());
            }
        }
    } else {
        if (metadata instanceof UnspecifiedMetadata) {
            Metadata realMetadata = ((UnspecifiedMetadata) metadata).convertToRealMetadata();
            metadataMap.put(name, realMetadata);
            metricMap.put(metricID, metric);
        } else {
            if (metadata instanceof OriginAndMetadata) {
                originMap.put(metricID, ((OriginAndMetadata) metadata).getOrigin());
                metadataMap.put(name, ((OriginAndMetadata) metadata).getMetadata());
            } else {
                metadataMap.put(name, sanitizeMetadata(metadata, metric.getClass()));
            }
            metricMap.put(metricID, metric);
        }
    }
    return metric;
}
 
Example 4
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 5
Source File: OpenMetricsExporter.java    From smallrye-metrics with Apache License 2.0 4 votes vote down vote up
private void writeTypeAndValue(StringBuilder sb, MetricRegistry.Type scope, String suffix, double valueRaw, String type,
        Metadata md, boolean performScaling, Map<String, String> tags) {
    String key = md.getName();
    writeTypeLine(sb, scope, key, md, suffix, type);
    writeValueLine(sb, scope, suffix, valueRaw, md, tags, performScaling);
}