Search in sources :

Example 1 with Value

use of io.opencensus.metrics.export.Value in project instrumentation-java by census-instrumentation.

the class DropWizardMetrics method collectGauge.

/**
 * Returns a {@code Metric} collected from {@link Gauge}.
 *
 * @param dropwizardName the metric name.
 * @param gauge the gauge object to collect.
 * @return a {@code Metric}.
 */
@SuppressWarnings("rawtypes")
@Nullable
private Metric collectGauge(String dropwizardName, Gauge gauge) {
    String metricName = DropWizardUtils.generateFullMetricName(dropwizardName, "gauge");
    String metricDescription = DropWizardUtils.generateFullMetricDescription(dropwizardName, gauge);
    // Figure out which gauge instance and call the right method to get value
    Type type;
    Value value;
    Object obj = gauge.getValue();
    if (obj instanceof Number) {
        type = Type.GAUGE_DOUBLE;
        value = Value.doubleValue(((Number) obj).doubleValue());
    } else if (obj instanceof Boolean) {
        type = Type.GAUGE_INT64;
        value = Value.longValue(((Boolean) obj) ? 1 : 0);
    } else {
        // Ignoring Gauge (gauge.getKey()) with unhandled type.
        return null;
    }
    MetricDescriptor metricDescriptor = MetricDescriptor.create(metricName, metricDescription, DEFAULT_UNIT, type, Collections.<LabelKey>emptyList());
    TimeSeries timeSeries = TimeSeries.createWithOnePoint(Collections.<LabelValue>emptyList(), Point.create(value, clock.now()), null);
    return Metric.createWithOneTimeSeries(metricDescriptor, timeSeries);
}
Also used : Type(io.opencensus.metrics.export.MetricDescriptor.Type) MetricDescriptor(io.opencensus.metrics.export.MetricDescriptor) TimeSeries(io.opencensus.metrics.export.TimeSeries) LabelValue(io.opencensus.metrics.LabelValue) Value(io.opencensus.metrics.export.Value) Nullable(javax.annotation.Nullable)

Example 2 with Value

use of io.opencensus.metrics.export.Value in project instrumentation-java by census-instrumentation.

the class DropWizardMetrics method collectGauge.

/**
 * Returns a {@code Metric} collected from {@link Gauge}.
 *
 * @param dropwizardMetric the metric name.
 * @param gauge the gauge object to collect.
 * @return a {@code Metric}.
 */
@SuppressWarnings("rawtypes")
@Nullable
private Metric collectGauge(MetricName dropwizardMetric, Gauge gauge) {
    // TODO cache dropwizard MetricName -> OC MetricDescriptor, Labels conversion
    String metricName = DropWizardUtils.generateFullMetricName(dropwizardMetric.getKey(), "gauge");
    String metricDescription = DropWizardUtils.generateFullMetricDescription(dropwizardMetric.getKey(), gauge);
    AbstractMap.SimpleImmutableEntry<List<LabelKey>, List<LabelValue>> labels = DropWizardUtils.generateLabels(dropwizardMetric);
    // Figure out which gauge instance and call the right method to get value
    Type type;
    Value value;
    Object obj = gauge.getValue();
    if (obj instanceof Number) {
        type = Type.GAUGE_DOUBLE;
        value = Value.doubleValue(((Number) obj).doubleValue());
    } else if (obj instanceof Boolean) {
        type = Type.GAUGE_INT64;
        value = Value.longValue(((Boolean) obj) ? 1 : 0);
    } else {
        // Ignoring Gauge (gauge.getKey()) with unhandled type.
        return null;
    }
    MetricDescriptor metricDescriptor = MetricDescriptor.create(metricName, metricDescription, DEFAULT_UNIT, type, labels.getKey());
    TimeSeries timeSeries = TimeSeries.createWithOnePoint(labels.getValue(), Point.create(value, clock.now()), null);
    return Metric.createWithOneTimeSeries(metricDescriptor, timeSeries);
}
Also used : AbstractMap(java.util.AbstractMap) Type(io.opencensus.metrics.export.MetricDescriptor.Type) MetricDescriptor(io.opencensus.metrics.export.MetricDescriptor) TimeSeries(io.opencensus.metrics.export.TimeSeries) LabelValue(io.opencensus.metrics.LabelValue) Value(io.opencensus.metrics.export.Value) ArrayList(java.util.ArrayList) List(java.util.List) Nullable(javax.annotation.Nullable)

Aggregations

LabelValue (io.opencensus.metrics.LabelValue)2 MetricDescriptor (io.opencensus.metrics.export.MetricDescriptor)2 Type (io.opencensus.metrics.export.MetricDescriptor.Type)2 TimeSeries (io.opencensus.metrics.export.TimeSeries)2 Value (io.opencensus.metrics.export.Value)2 Nullable (javax.annotation.Nullable)2 AbstractMap (java.util.AbstractMap)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1