Search in sources :

Example 1 with TimeSeries

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

the class LongGaugeImplTest 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);
    LongGaugeImpl longGauge = new LongGaugeImpl(METRIC_NAME, METRIC_DESCRIPTION, METRIC_UNIT, labelKeys, constantLabels);
    LongPoint longPoint = longGauge.getOrCreateTimeSeries(labelValues);
    longPoint.add(1);
    longPoint.add(2);
    LongPoint defaultPoint = longGauge.getDefaultTimeSeries();
    defaultPoint.set(100);
    List<LabelKey> allKeys = new ArrayList<>(labelKeys);
    allKeys.add(constantKey);
    MetricDescriptor expectedDescriptor = MetricDescriptor.create(METRIC_NAME, METRIC_DESCRIPTION, METRIC_UNIT, Type.GAUGE_INT64, allKeys);
    List<LabelValue> allValues = new ArrayList<>(labelValues);
    allValues.add(constantValue);
    List<TimeSeries> expectedTimeSeriesList = new ArrayList<TimeSeries>();
    TimeSeries defaultTimeSeries = TimeSeries.createWithOnePoint(Arrays.asList(UNSET_VALUE, UNSET_VALUE, constantValue), Point.create(Value.longValue(100), TEST_TIME), null);
    expectedTimeSeriesList.add(TimeSeries.createWithOnePoint(allValues, Point.create(Value.longValue(3), TEST_TIME), null));
    expectedTimeSeriesList.add(defaultTimeSeries);
    Metric metric = longGauge.getMetric(testClock);
    assertThat(metric).isNotNull();
    assertThat(metric.getMetricDescriptor()).isEqualTo(expectedDescriptor);
    assertThat(metric.getTimeSeriesList().size()).isEqualTo(2);
    assertThat(metric.getTimeSeriesList()).containsExactlyElementsIn(expectedTimeSeriesList);
    longGauge.removeTimeSeries(labelValues);
    Metric metric2 = longGauge.getMetric(testClock);
    assertThat(metric2).isNotNull();
    assertThat(metric2.getTimeSeriesList()).containsExactly(defaultTimeSeries);
}
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) LongPoint(io.opencensus.metrics.LongGauge.LongPoint) Test(org.junit.Test)

Example 2 with TimeSeries

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

the class DropWizardMetrics method collectCounter.

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

Example 3 with TimeSeries

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

the class DropWizardMetrics method collectMeter.

/**
 * Returns a {@code Metric} collected from {@link Meter}.
 *
 * @param dropwizardName the metric name.
 * @param meter the meter object to collect
 * @return a {@code Metric}.
 */
private Metric collectMeter(String dropwizardName, Meter meter) {
    String metricName = DropWizardUtils.generateFullMetricName(dropwizardName, "meter");
    String metricDescription = DropWizardUtils.generateFullMetricDescription(dropwizardName, meter);
    MetricDescriptor metricDescriptor = MetricDescriptor.create(metricName, metricDescription, DEFAULT_UNIT, Type.CUMULATIVE_INT64, Collections.<LabelKey>emptyList());
    TimeSeries timeSeries = TimeSeries.createWithOnePoint(Collections.<LabelValue>emptyList(), Point.create(Value.longValue(meter.getCount()), clock.now()), cumulativeStartTimestamp);
    return Metric.createWithOneTimeSeries(metricDescriptor, timeSeries);
}
Also used : MetricDescriptor(io.opencensus.metrics.export.MetricDescriptor) TimeSeries(io.opencensus.metrics.export.TimeSeries)

Example 4 with TimeSeries

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

the class DropWizardMetrics method collectSnapshotAndCount.

/**
 * Returns a {@code Metric} collected from {@link Snapshot}.
 *
 * @param metricName the metric name.
 * @param metricDescription the metric description.
 * @param labelKeys metric label keys
 * @param labelValues metric label values
 * @param codahaleSnapshot the snapshot object to collect
 * @param count the value or count
 * @return a {@code Metric}.
 */
private Metric collectSnapshotAndCount(String metricName, String metricDescription, List<LabelKey> labelKeys, List<LabelValue> labelValues, String unit, io.dropwizard.metrics5.Snapshot codahaleSnapshot, long count) {
    List<ValueAtPercentile> valueAtPercentiles = Arrays.asList(ValueAtPercentile.create(50.0, codahaleSnapshot.getMedian()), ValueAtPercentile.create(75.0, codahaleSnapshot.get75thPercentile()), ValueAtPercentile.create(98.0, codahaleSnapshot.get98thPercentile()), ValueAtPercentile.create(99.0, codahaleSnapshot.get99thPercentile()), ValueAtPercentile.create(99.9, codahaleSnapshot.get999thPercentile()));
    Snapshot snapshot = Snapshot.create((long) codahaleSnapshot.size(), 0.0, valueAtPercentiles);
    Point point = Point.create(Value.summaryValue(Summary.create(count, 0.0, snapshot)), clock.now());
    // TODO(mayurkale): OPTIMIZATION: Cache the MetricDescriptor objects.
    MetricDescriptor metricDescriptor = MetricDescriptor.create(metricName, metricDescription, unit, Type.SUMMARY, labelKeys);
    TimeSeries timeSeries = TimeSeries.createWithOnePoint(labelValues, point, cumulativeStartTimestamp);
    return Metric.createWithOneTimeSeries(metricDescriptor, timeSeries);
}
Also used : ValueAtPercentile(io.opencensus.metrics.export.Summary.Snapshot.ValueAtPercentile) Snapshot(io.opencensus.metrics.export.Summary.Snapshot) MetricDescriptor(io.opencensus.metrics.export.MetricDescriptor) TimeSeries(io.opencensus.metrics.export.TimeSeries) Point(io.opencensus.metrics.export.Point)

Example 5 with TimeSeries

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

Aggregations

TimeSeries (io.opencensus.metrics.export.TimeSeries)25 ArrayList (java.util.ArrayList)19 MetricDescriptor (io.opencensus.metrics.export.MetricDescriptor)17 Metric (io.opencensus.metrics.export.Metric)16 Test (org.junit.Test)16 LabelValue (io.opencensus.metrics.LabelValue)10 Timestamp (io.opencensus.common.Timestamp)8 LabelKey (io.opencensus.metrics.LabelKey)8 Point (io.opencensus.metrics.export.Point)3 AbstractMap (java.util.AbstractMap)3 List (java.util.List)3 DoublePoint (io.opencensus.metrics.DoubleCumulative.DoublePoint)2 DoublePoint (io.opencensus.metrics.DoubleGauge.DoublePoint)2 LongPoint (io.opencensus.metrics.LongCumulative.LongPoint)2 LongPoint (io.opencensus.metrics.LongGauge.LongPoint)2 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