Search in sources :

Example 6 with MetricDescriptor

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

the class DropWizardMetrics method collectCounter.

/**
 * Returns a {@code Metric} collected from {@link Counter}.
 *
 * @param dropwizardMetric the metric name.
 * @param counter the counter object to collect.
 * @return a {@code Metric}.
 */
private Metric collectCounter(MetricName dropwizardMetric, Counter counter) {
    String metricName = DropWizardUtils.generateFullMetricName(dropwizardMetric.getKey(), "counter");
    String metricDescription = DropWizardUtils.generateFullMetricDescription(dropwizardMetric.getKey(), counter);
    AbstractMap.SimpleImmutableEntry<List<LabelKey>, List<LabelValue>> labels = DropWizardUtils.generateLabels(dropwizardMetric);
    MetricDescriptor metricDescriptor = MetricDescriptor.create(metricName, metricDescription, DEFAULT_UNIT, Type.GAUGE_INT64, labels.getKey());
    TimeSeries timeSeries = TimeSeries.createWithOnePoint(labels.getValue(), Point.create(Value.longValue(counter.getCount()), clock.now()), null);
    return Metric.createWithOneTimeSeries(metricDescriptor, timeSeries);
}
Also used : AbstractMap(java.util.AbstractMap) MetricDescriptor(io.opencensus.metrics.export.MetricDescriptor) TimeSeries(io.opencensus.metrics.export.TimeSeries) ArrayList(java.util.ArrayList) List(java.util.List)

Example 7 with MetricDescriptor

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

the class SignalFxSessionAdaptor method adapt.

/**
 * Converts the given Metric into datapoints that can be sent to SignalFx.
 *
 * @param metric The {@link Metric} containing the timeseries of each combination of label values.
 * @return A list of datapoints for the corresponding metric timeseries of this metric.
 */
static List<DataPoint> adapt(Metric metric) {
    MetricDescriptor metricDescriptor = metric.getMetricDescriptor();
    MetricType metricType = getType(metricDescriptor.getType());
    if (metricType == null) {
        return Collections.emptyList();
    }
    DataPoint.Builder shared = DataPoint.newBuilder();
    shared.setMetric(metricDescriptor.getName());
    shared.setMetricType(metricType);
    ArrayList<DataPoint> datapoints = Lists.newArrayList();
    for (TimeSeries timeSeries : metric.getTimeSeriesList()) {
        DataPoint.Builder builder = shared.clone();
        builder.addAllDimensions(createDimensions(metricDescriptor.getLabelKeys(), timeSeries.getLabelValues()));
        List<Point> points = timeSeries.getPoints();
        datapoints.ensureCapacity(datapoints.size() + points.size());
        for (Point point : points) {
            datapoints.add(builder.setValue(createDatum(point.getValue())).build());
        }
    }
    return datapoints;
}
Also used : MetricDescriptor(io.opencensus.metrics.export.MetricDescriptor) TimeSeries(io.opencensus.metrics.export.TimeSeries) DataPoint(com.signalfx.metrics.protobuf.SignalFxProtocolBuffers.DataPoint) MetricType(com.signalfx.metrics.protobuf.SignalFxProtocolBuffers.MetricType) Point(io.opencensus.metrics.export.Point) DataPoint(com.signalfx.metrics.protobuf.SignalFxProtocolBuffers.DataPoint)

Example 8 with MetricDescriptor

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

the class DerivedDoubleCumulativeImplTest method withConstantLabels.

@Test
public void withConstantLabels() {
    List<LabelKey> labelKeys = Arrays.asList(LabelKey.create("key1", "desc"), LabelKey.create("key2", "desc"));
    List<LabelValue> labelValues = Arrays.asList(LabelValue.create("value1"), LabelValue.create("value2"));
    LabelKey constantKey = LabelKey.create("constant_key", "desc");
    LabelValue constantValue = LabelValue.create("constant_value");
    Map<LabelKey, LabelValue> constantLabels = Collections.<LabelKey, LabelValue>singletonMap(constantKey, constantValue);
    DerivedDoubleCumulativeImpl derivedDoubleCumulative2 = new DerivedDoubleCumulativeImpl(METRIC_NAME, METRIC_DESCRIPTION, METRIC_UNIT, labelKeys, constantLabels, START_TIME);
    derivedDoubleCumulative2.createTimeSeries(labelValues, new QueueManager(), queueManagerFunction);
    testClock.advanceTime(ONE_MINUTE);
    Timestamp endTime = testClock.now();
    List<LabelKey> allKeys = new ArrayList<>(labelKeys);
    allKeys.add(constantKey);
    MetricDescriptor expectedDescriptor = MetricDescriptor.create(METRIC_NAME, METRIC_DESCRIPTION, METRIC_UNIT, Type.CUMULATIVE_DOUBLE, allKeys);
    List<LabelValue> allValues = new ArrayList<>(labelValues);
    allValues.add(constantValue);
    TimeSeries expectedTimeSeries = TimeSeries.createWithOnePoint(allValues, Point.create(Value.doubleValue(2.5), endTime), START_TIME);
    Metric metric = derivedDoubleCumulative2.getMetric(testClock);
    assertThat(metric).isNotNull();
    assertThat(metric.getMetricDescriptor()).isEqualTo(expectedDescriptor);
    assertThat(metric.getTimeSeriesList()).containsExactly(expectedTimeSeries);
    derivedDoubleCumulative2.removeTimeSeries(labelValues);
    Metric metric2 = derivedDoubleCumulative2.getMetric(testClock);
    assertThat(metric2).isNull();
}
Also used : MetricDescriptor(io.opencensus.metrics.export.MetricDescriptor) TimeSeries(io.opencensus.metrics.export.TimeSeries) LabelValue(io.opencensus.metrics.LabelValue) ArrayList(java.util.ArrayList) LabelKey(io.opencensus.metrics.LabelKey) Metric(io.opencensus.metrics.export.Metric) Timestamp(io.opencensus.common.Timestamp) Test(org.junit.Test)

Example 9 with MetricDescriptor

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

the class DerivedDoubleGaugeImplTest method withConstantLabels.

@Test
public void withConstantLabels() {
    List<LabelKey> labelKeys = Arrays.asList(LabelKey.create("key1", "desc"), LabelKey.create("key2", "desc"));
    List<LabelValue> labelValues = Arrays.asList(LabelValue.create("value1"), LabelValue.create("value2"));
    LabelKey constantKey = LabelKey.create("constant_key", "desc");
    LabelValue constantValue = LabelValue.create("constant_value");
    Map<LabelKey, LabelValue> constantLabels = Collections.<LabelKey, LabelValue>singletonMap(constantKey, constantValue);
    DerivedDoubleGaugeImpl derivedDoubleGauge2 = new DerivedDoubleGaugeImpl(METRIC_NAME, METRIC_DESCRIPTION, METRIC_UNIT, labelKeys, constantLabels);
    derivedDoubleGauge2.createTimeSeries(labelValues, new QueueManager(), queueManagerFunction);
    List<LabelKey> allKeys = new ArrayList<>(labelKeys);
    allKeys.add(constantKey);
    MetricDescriptor expectedDescriptor = MetricDescriptor.create(METRIC_NAME, METRIC_DESCRIPTION, METRIC_UNIT, Type.GAUGE_DOUBLE, allKeys);
    List<LabelValue> allValues = new ArrayList<>(labelValues);
    allValues.add(constantValue);
    TimeSeries expectedTimeSeries = TimeSeries.createWithOnePoint(allValues, Point.create(Value.doubleValue(2.5), TEST_TIME), null);
    Metric metric = derivedDoubleGauge2.getMetric(testClock);
    assertThat(metric).isNotNull();
    assertThat(metric.getMetricDescriptor()).isEqualTo(expectedDescriptor);
    assertThat(metric.getTimeSeriesList()).containsExactly(expectedTimeSeries);
    derivedDoubleGauge2.removeTimeSeries(labelValues);
    Metric metric2 = derivedDoubleGauge2.getMetric(testClock);
    assertThat(metric2).isNull();
}
Also used : MetricDescriptor(io.opencensus.metrics.export.MetricDescriptor) TimeSeries(io.opencensus.metrics.export.TimeSeries) LabelValue(io.opencensus.metrics.LabelValue) ArrayList(java.util.ArrayList) LabelKey(io.opencensus.metrics.LabelKey) Metric(io.opencensus.metrics.export.Metric) Test(org.junit.Test)

Example 10 with MetricDescriptor

use of io.opencensus.metrics.export.MetricDescriptor 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)

Aggregations

MetricDescriptor (io.opencensus.metrics.export.MetricDescriptor)21 TimeSeries (io.opencensus.metrics.export.TimeSeries)17 ArrayList (java.util.ArrayList)11 Test (org.junit.Test)11 LabelValue (io.opencensus.metrics.LabelValue)10 LabelKey (io.opencensus.metrics.LabelKey)8 Metric (io.opencensus.metrics.export.Metric)8 Timestamp (io.opencensus.common.Timestamp)4 Point (io.opencensus.metrics.export.Point)3 AbstractMap (java.util.AbstractMap)3 List (java.util.List)3 Type (io.opencensus.metrics.export.MetricDescriptor.Type)2 Snapshot (io.opencensus.metrics.export.Summary.Snapshot)2 ValueAtPercentile (io.opencensus.metrics.export.Summary.Snapshot.ValueAtPercentile)2 Value (io.opencensus.metrics.export.Value)2 Nullable (javax.annotation.Nullable)2 DataPoint (com.signalfx.metrics.protobuf.SignalFxProtocolBuffers.DataPoint)1 MetricType (com.signalfx.metrics.protobuf.SignalFxProtocolBuffers.MetricType)1 DoublePoint (io.opencensus.metrics.DoubleCumulative.DoublePoint)1 DoublePoint (io.opencensus.metrics.DoubleGauge.DoublePoint)1