io.opencensus.common.Functions Java Examples

The following examples show how to use io.opencensus.common.Functions. 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: DistributionTest.java    From opencensus-java with Apache License 2.0 6 votes vote down vote up
@Test
public void createAndGet_ExplicitBuckets() {
  List<Double> bucketBounds = Arrays.asList(1.0, 2.0, 3.0);

  BucketOptions bucketOptions = BucketOptions.explicitOptions(bucketBounds);
  final List<Double> actual = new ArrayList<Double>();
  bucketOptions.match(
      new Function<ExplicitOptions, Object>() {
        @Override
        public Object apply(ExplicitOptions arg) {
          actual.addAll(arg.getBucketBoundaries());
          return null;
        }
      },
      Functions.throwAssertionError());

  assertThat(actual).containsExactlyElementsIn(bucketBounds).inOrder();
}
 
Example #2
Source File: DistributionTest.java    From opencensus-java with Apache License 2.0 6 votes vote down vote up
@Test
public void createAndGet_ExplicitBucketsEmptyBounds() {
  List<Double> bucketBounds = new ArrayList<Double>();
  BucketOptions bucketOptions = BucketOptions.explicitOptions(bucketBounds);

  final List<Double> actual = new ArrayList<Double>();
  bucketOptions.match(
      new Function<ExplicitOptions, Object>() {
        @Override
        public Object apply(ExplicitOptions arg) {
          actual.addAll(arg.getBucketBoundaries());
          return null;
        }
      },
      Functions.throwAssertionError());

  assertThat(actual).isEmpty();
}
 
Example #3
Source File: NoopStats.java    From opencensus-java with Apache License 2.0 6 votes vote down vote up
@Override
@javax.annotation.Nullable
@SuppressWarnings("deprecation")
public ViewData getView(View.Name name) {
  Utils.checkNotNull(name, "name");
  synchronized (registeredViews) {
    View view = registeredViews.get(name);
    if (view == null) {
      return null;
    } else {
      return ViewData.create(
          view,
          Collections.<List</*@Nullable*/ TagValue>, AggregationData>emptyMap(),
          view.getWindow()
              .match(
                  Functions.<ViewData.AggregationWindowData>returnConstant(
                      ViewData.AggregationWindowData.CumulativeData.create(
                          ZERO_TIMESTAMP, ZERO_TIMESTAMP)),
                  Functions.<ViewData.AggregationWindowData>returnConstant(
                      ViewData.AggregationWindowData.IntervalData.create(ZERO_TIMESTAMP)),
                  Functions.<ViewData.AggregationWindowData>throwAssertionError()));
    }
  }
}
 
Example #4
Source File: AggregationTest.java    From opencensus-java with Apache License 2.0 6 votes vote down vote up
@Test
public void testMatch() {
  List<Aggregation> aggregations =
      Arrays.asList(
          Sum.create(),
          Count.create(),
          Mean.create(),
          Distribution.create(BucketBoundaries.create(Arrays.asList(-10.0, 1.0, 5.0))),
          LastValue.create());

  List<String> actual = new ArrayList<String>();
  for (Aggregation aggregation : aggregations) {
    actual.add(
        aggregation.match(
            Functions.returnConstant("SUM"),
            Functions.returnConstant("COUNT"),
            Functions.returnConstant("DISTRIBUTION"),
            Functions.returnConstant("LASTVALUE"),
            Functions.returnConstant("UNKNOWN")));
  }

  assertThat(actual)
      .isEqualTo(Arrays.asList("SUM", "COUNT", "UNKNOWN", "DISTRIBUTION", "LASTVALUE"));
}
 
Example #5
Source File: MetricUtils.java    From opencensus-java with Apache License 2.0 6 votes vote down vote up
@VisibleForTesting
static Type getType(Measure measure, Aggregation aggregation) {
  return aggregation.match(
      Functions.returnConstant(
          measure.match(
              TYPE_CUMULATIVE_DOUBLE_FUNCTION, // Sum Double
              TYPE_CUMULATIVE_INT64_FUNCTION, // Sum Int64
              TYPE_UNRECOGNIZED_FUNCTION)),
      TYPE_CUMULATIVE_INT64_FUNCTION, // Count
      TYPE_CUMULATIVE_DISTRIBUTION_FUNCTION, // Distribution
      Functions.returnConstant(
          measure.match(
              TYPE_GAUGE_DOUBLE_FUNCTION, // LastValue Double
              TYPE_GAUGE_INT64_FUNCTION, // LastValue Long
              TYPE_UNRECOGNIZED_FUNCTION)),
      AGGREGATION_TYPE_DEFAULT_FUNCTION);
}
 
Example #6
Source File: MetricsProtoUtils.java    From opencensus-java with Apache License 2.0 6 votes vote down vote up
private static DistributionValue.BucketOptions toBucketOptionsProto(
    Distribution.BucketOptions bucketOptions) {
  final DistributionValue.BucketOptions.Builder builder =
      DistributionValue.BucketOptions.newBuilder();
  bucketOptions.match(
      new Function<Distribution.BucketOptions.ExplicitOptions, Void>() {
        @Override
        public Void apply(Distribution.BucketOptions.ExplicitOptions arg) {
          builder.setExplicit(
              DistributionValue.BucketOptions.Explicit.newBuilder()
                  .addAllBounds(arg.getBucketBoundaries())
                  .build());
          return null;
        }
      },
      Functions.<Void>throwAssertionError());
  return builder.build();
}
 
Example #7
Source File: HttpRequestTracingTest.java    From google-http-java-client with Apache License 2.0 5 votes vote down vote up
void assertAttributeEquals(SpanData span, String attributeName, String expectedValue) {
  Object attributeValue = span.getAttributes().getAttributeMap().get(attributeName);
  assertNotNull("expected span to contain attribute: " + attributeName, attributeValue);
  assertTrue(attributeValue instanceof AttributeValue);
  String value =
      ((AttributeValue) attributeValue)
          .match(
              Functions.returnToString(),
              Functions.returnToString(),
              Functions.returnToString(),
              Functions.returnToString(),
              Functions.</*@Nullable*/ String>returnNull());
  assertEquals(expectedValue, value);
}
 
Example #8
Source File: DatadogExporterHandler.java    From opencensus-java with Apache License 2.0 5 votes vote down vote up
private static String attributeValueToString(AttributeValue attributeValue) {
  return attributeValue.match(
      Functions.returnToString(),
      Functions.returnToString(),
      Functions.returnToString(),
      Functions.returnToString(),
      Functions.throwIllegalArgumentException());
}
 
Example #9
Source File: ZipkinExporterHandler.java    From opencensus-java with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("nullness")
private static String attributeValueToString(AttributeValue attributeValue) {
  return attributeValue.match(
      returnToString,
      returnToString,
      returnToString,
      returnToString,
      Functions.<String>returnConstant(""));
}
 
Example #10
Source File: JsonConversionUtils.java    From opencensus-java with Apache License 2.0 5 votes vote down vote up
@Nullable
@SuppressWarnings("nullness")
private static String attributeValueToString(AttributeValue attributeValue) {
  return attributeValue.match(
      Functions.returnToString(),
      Functions.returnToString(),
      Functions.returnToString(),
      Functions.returnToString(),
      Functions.returnConstant(""));
}
 
Example #11
Source File: InstanaExporterHandler.java    From opencensus-java with Apache License 2.0 5 votes vote down vote up
@javax.annotation.Nullable
private static String attributeValueToString(AttributeValue attributeValue) {
  return attributeValue.match(
      returnToString,
      returnToString,
      returnToString,
      returnToString,
      Functions.</*@Nullable*/ String>returnNull());
}
 
Example #12
Source File: TraceProtoUtils.java    From opencensus-java with Apache License 2.0 5 votes vote down vote up
@javax.annotation.Nullable
private static AttributeValue toAttributeValueProto(
    io.opencensus.trace.AttributeValue attributeValue) {
  return attributeValue.match(
      stringAttributeValueFunction,
      booleanAttributeValueFunction,
      longAttributeValueFunction,
      doubleAttributeValueFunction,
      Functions.</*@Nullable*/ AttributeValue>returnNull());
}
 
Example #13
Source File: StackdriverV2ExporterHandler.java    From opencensus-java with Apache License 2.0 5 votes vote down vote up
@javax.annotation.Nullable
private static AttributeValue toAttributeValueProto(
    io.opencensus.trace.AttributeValue attributeValue) {
  return attributeValue.match(
      stringAttributeValueFunction,
      booleanAttributeValueFunction,
      longAttributeValueFunction,
      doubleAttributeValueFunction,
      Functions.</*@Nullable*/ AttributeValue>returnNull());
}
 
Example #14
Source File: SignalFxSessionAdaptor.java    From opencensus-java with Apache License 2.0 5 votes vote down vote up
@VisibleForTesting
static Datum createDatum(Value value) {
  return value.match(
      datumDoubleFunction,
      datumLongFunction,
      datumDistributionFunction,
      datumSummaryFunction,
      Functions.<Datum>throwIllegalArgumentException());
}
 
Example #15
Source File: StackdriverExportUtils.java    From opencensus-java with Apache License 2.0 5 votes vote down vote up
@VisibleForTesting
static BucketOptions createBucketOptions(
    @javax.annotation.Nullable
        io.opencensus.metrics.export.Distribution.BucketOptions bucketOptions) {
  final BucketOptions.Builder builder = BucketOptions.newBuilder();
  if (bucketOptions == null) {
    return builder.build();
  }

  return bucketOptions.match(
      bucketOptionsExplicitFunction, Functions.<BucketOptions>throwIllegalArgumentException());
}
 
Example #16
Source File: StackdriverExportUtils.java    From opencensus-java with Apache License 2.0 5 votes vote down vote up
@VisibleForTesting
static TypedValue createTypedValue(Value value) {
  return value.match(
      typedValueDoubleFunction,
      typedValueLongFunction,
      typedValueDistributionFunction,
      typedValueSummaryFunction,
      Functions.<TypedValue>throwIllegalArgumentException());
}
 
Example #17
Source File: StatszZPageHandlerTest.java    From opencensus-java with Apache License 2.0 5 votes vote down vote up
private static void assertContainsMeasure(OutputStream output, Measure measure) {
  assertThat(output.toString()).contains(measure.getName());
  assertThat(output.toString()).contains(measure.getDescription());
  assertThat(output.toString()).contains(measure.getUnit());
  String type =
      measure.match(
          Functions.returnConstant("Double"),
          Functions.returnConstant("Long"),
          Functions.throwAssertionError());
  assertThat(output.toString()).contains(type);
}
 
Example #18
Source File: StatszZPageHandler.java    From opencensus-java with Apache License 2.0 5 votes vote down vote up
private static void emitMeasureTableRow(Measure measure, PrintWriter out, Formatter formatter) {
  out.write("<tr>");
  formatter.format("<td><b>%s</b></td>", measure.getName());
  formatter.format("<td class=\"borderLL\">%s&nbsp;</td>", measure.getDescription());
  formatter.format("<td class=\"borderLL\">%s&nbsp;</td>", measure.getUnit());
  String measureType =
      measure.match(
          Functions.returnConstant("Double"),
          Functions.returnConstant("Long"),
          Functions.throwAssertionError());
  formatter.format("<td class=\"borderLL\">%s&nbsp;</td>", measureType);
  out.write("</tr>");
}
 
Example #19
Source File: TracezZPageHandler.java    From opencensus-java with Apache License 2.0 5 votes vote down vote up
@javax.annotation.Nullable
private static String attributeValueToString(AttributeValue attributeValue) {
  return attributeValue.match(
      returnToString,
      returnToString,
      returnToString,
      returnToString,
      Functions.</*@Nullable*/ String>returnNull());
}
 
Example #20
Source File: StatsTestUtil.java    From opencensus-java with Apache License 2.0 5 votes vote down vote up
static ViewData createEmptyViewData(View view) {
  return ViewData.create(
      view,
      Collections.<List<TagValue>, AggregationData>emptyMap(),
      view.getWindow()
          .match(
              Functions.<AggregationWindowData>returnConstant(
                  CumulativeData.create(ZERO_TIMESTAMP, ZERO_TIMESTAMP)),
              Functions.<AggregationWindowData>returnConstant(
                  IntervalData.create(ZERO_TIMESTAMP)),
              Functions.<AggregationWindowData>throwAssertionError()));
}
 
Example #21
Source File: MutableViewData.java    From opencensus-java with Apache License 2.0 5 votes vote down vote up
/**
 * Constructs a new {@link MutableViewData}.
 *
 * @param view the {@code View} linked with this {@code MutableViewData}.
 * @param start the start {@code Timestamp}.
 * @return a {@code MutableViewData}.
 */
static MutableViewData create(final View view, final Timestamp start) {
  return view.getWindow()
      .match(
          new CreateCumulative(view, start),
          new CreateInterval(view, start),
          Functions.<MutableViewData>throwAssertionError());
}
 
Example #22
Source File: AttributeValueTest.java    From opencensus-java with Apache License 2.0 5 votes vote down vote up
@Test
public void doubleAttributeValue() {
  AttributeValue attribute = AttributeValue.doubleAttributeValue(1.23456);
  attribute.match(
      new Function<String, Object>() {
        @Override
        @Nullable
        public Object apply(String stringValue) {
          fail("Expected a Double");
          return null;
        }
      },
      new Function<Boolean, Object>() {
        @Override
        @Nullable
        public Object apply(Boolean booleanValue) {
          fail("Expected a Double");
          return null;
        }
      },
      new Function<Long, Object>() {
        @Override
        @Nullable
        public Object apply(Long longValue) {
          fail("Expected a Double");
          return null;
        }
      },
      new Function<Double, Object>() {
        @Override
        @Nullable
        public Object apply(Double doubleValue) {
          assertThat(doubleValue).isEqualTo(1.23456);
          return null;
        }
      },
      Functions.throwIllegalArgumentException());
}
 
Example #23
Source File: AttributeValueTest.java    From opencensus-java with Apache License 2.0 5 votes vote down vote up
@Test
public void longAttributeValue() {
  AttributeValue attribute = AttributeValue.longAttributeValue(123456L);
  attribute.match(
      new Function<String, Object>() {
        @Override
        @Nullable
        public Object apply(String stringValue) {
          fail("Expected a Long");
          return null;
        }
      },
      new Function<Boolean, Object>() {
        @Override
        @Nullable
        public Object apply(Boolean booleanValue) {
          fail("Expected a Long");
          return null;
        }
      },
      new Function<Long, Object>() {
        @Override
        @Nullable
        public Object apply(Long longValue) {
          assertThat(longValue).isEqualTo(123456L);
          return null;
        }
      },
      Functions.throwIllegalArgumentException());
}
 
Example #24
Source File: AttributeValueTest.java    From opencensus-java with Apache License 2.0 5 votes vote down vote up
@Test
public void booleanAttributeValue() {
  AttributeValue attribute = AttributeValue.booleanAttributeValue(true);
  attribute.match(
      new Function<String, Object>() {
        @Override
        @Nullable
        public Object apply(String stringValue) {
          fail("Expected a Boolean");
          return null;
        }
      },
      new Function<Boolean, Object>() {
        @Override
        @Nullable
        public Object apply(Boolean booleanValue) {
          assertThat(booleanValue).isTrue();
          return null;
        }
      },
      new Function<Long, Object>() {
        @Override
        @Nullable
        public Object apply(Long longValue) {
          fail("Expected a Boolean");
          return null;
        }
      },
      Functions.throwIllegalArgumentException());
}
 
Example #25
Source File: AttributeValueTest.java    From opencensus-java with Apache License 2.0 5 votes vote down vote up
@Test
public void stringAttributeValue() {
  AttributeValue attribute = AttributeValue.stringAttributeValue("MyStringAttributeValue");
  attribute.match(
      new Function<String, Object>() {
        @Override
        @Nullable
        public Object apply(String stringValue) {
          assertThat(stringValue).isEqualTo("MyStringAttributeValue");
          return null;
        }
      },
      new Function<Boolean, Object>() {
        @Override
        @Nullable
        public Object apply(Boolean booleanValue) {
          fail("Expected a String");
          return null;
        }
      },
      new Function<Long, Object>() {
        @Override
        @Nullable
        public Object apply(Long longValue) {
          fail("Expected a String");
          return null;
        }
      },
      Functions.throwIllegalArgumentException());
}
 
Example #26
Source File: DistributionTest.java    From opencensus-java with Apache License 2.0 5 votes vote down vote up
@Test
public void createAndGet_Distribution() {
  Exemplar exemplar = Exemplar.create(15.0, TIMESTAMP, ATTACHMENTS);
  List<Double> bucketBounds = Arrays.asList(1.0, 2.0, 5.0);
  BucketOptions bucketOptions = BucketOptions.explicitOptions(bucketBounds);
  List<Bucket> buckets =
      Arrays.asList(
          Bucket.create(3), Bucket.create(1), Bucket.create(2), Bucket.create(4, exemplar));
  Distribution distribution = Distribution.create(10, 6.6, 678.54, bucketOptions, buckets);
  assertThat(distribution.getCount()).isEqualTo(10);
  assertThat(distribution.getSum()).isWithin(TOLERANCE).of(6.6);
  assertThat(distribution.getSumOfSquaredDeviations()).isWithin(TOLERANCE).of(678.54);

  final List<Double> actual = new ArrayList<Double>();
  distribution
      .getBucketOptions()
      .match(
          new Function<ExplicitOptions, Object>() {
            @Override
            public Object apply(ExplicitOptions arg) {
              actual.addAll(arg.getBucketBoundaries());
              return null;
            }
          },
          Functions.throwAssertionError());

  assertThat(actual).containsExactlyElementsIn(bucketBounds).inOrder();

  assertThat(distribution.getBuckets()).containsExactlyElementsIn(buckets).inOrder();
}
 
Example #27
Source File: StackdriverExportUtils.java    From opencensus-java with Apache License 2.0 4 votes vote down vote up
@VisibleForTesting
static List<io.opencensus.metrics.export.Metric> convertSummaryMetric(
    io.opencensus.metrics.export.Metric summaryMetric) {
  List<io.opencensus.metrics.export.Metric> metricsList = Lists.newArrayList();
  final List<io.opencensus.metrics.export.TimeSeries> percentileTimeSeries = new ArrayList<>();
  final List<io.opencensus.metrics.export.TimeSeries> summaryCountTimeSeries = new ArrayList<>();
  final List<io.opencensus.metrics.export.TimeSeries> summarySumTimeSeries = new ArrayList<>();
  for (final io.opencensus.metrics.export.TimeSeries timeSeries :
      summaryMetric.getTimeSeriesList()) {
    final List<LabelValue> labelValuesWithPercentile =
        new ArrayList<>(timeSeries.getLabelValues());
    final io.opencensus.common.Timestamp timeSeriesTimestamp = timeSeries.getStartTimestamp();
    for (io.opencensus.metrics.export.Point point : timeSeries.getPoints()) {
      final io.opencensus.common.Timestamp pointTimestamp = point.getTimestamp();
      point
          .getValue()
          .match(
              Functions.<Void>returnNull(),
              Functions.<Void>returnNull(),
              Functions.<Void>returnNull(),
              new Function<Summary, Void>() {
                @Override
                public Void apply(Summary summary) {
                  Long count = summary.getCount();
                  if (count != null) {
                    createTimeSeries(
                        timeSeries.getLabelValues(),
                        Value.longValue(count),
                        pointTimestamp,
                        timeSeriesTimestamp,
                        summaryCountTimeSeries);
                  }
                  Double sum = summary.getSum();
                  if (sum != null) {
                    createTimeSeries(
                        timeSeries.getLabelValues(),
                        Value.doubleValue(sum),
                        pointTimestamp,
                        timeSeriesTimestamp,
                        summarySumTimeSeries);
                  }
                  Snapshot snapshot = summary.getSnapshot();
                  for (ValueAtPercentile valueAtPercentile : snapshot.getValueAtPercentiles()) {
                    labelValuesWithPercentile.add(
                        LabelValue.create(valueAtPercentile.getPercentile() + ""));
                    createTimeSeries(
                        labelValuesWithPercentile,
                        Value.doubleValue(valueAtPercentile.getValue()),
                        pointTimestamp,
                        null,
                        percentileTimeSeries);
                    labelValuesWithPercentile.remove(labelValuesWithPercentile.size() - 1);
                  }
                  return null;
                }
              },
              Functions.<Void>returnNull());
    }
  }

  // Metric for summary->count.
  if (summaryCountTimeSeries.size() > 0) {
    addMetric(
        metricsList,
        io.opencensus.metrics.export.MetricDescriptor.create(
            summaryMetric.getMetricDescriptor().getName() + SUMMARY_SUFFIX_COUNT,
            summaryMetric.getMetricDescriptor().getDescription(),
            "1",
            Type.CUMULATIVE_INT64,
            summaryMetric.getMetricDescriptor().getLabelKeys()),
        summaryCountTimeSeries);
  }

  // Metric for summary->sum.
  if (summarySumTimeSeries.size() > 0) {
    addMetric(
        metricsList,
        io.opencensus.metrics.export.MetricDescriptor.create(
            summaryMetric.getMetricDescriptor().getName() + SUMMARY_SUFFIX_SUM,
            summaryMetric.getMetricDescriptor().getDescription(),
            summaryMetric.getMetricDescriptor().getUnit(),
            Type.CUMULATIVE_DOUBLE,
            summaryMetric.getMetricDescriptor().getLabelKeys()),
        summarySumTimeSeries);
  }

  // Metric for summary->snapshot->percentiles.
  List<LabelKey> labelKeys = new ArrayList<>(summaryMetric.getMetricDescriptor().getLabelKeys());
  labelKeys.add(PERCENTILE_LABEL_KEY);
  addMetric(
      metricsList,
      io.opencensus.metrics.export.MetricDescriptor.create(
          summaryMetric.getMetricDescriptor().getName() + SNAPSHOT_SUFFIX_PERCENTILE,
          summaryMetric.getMetricDescriptor().getDescription(),
          summaryMetric.getMetricDescriptor().getUnit(),
          Type.GAUGE_DOUBLE,
          labelKeys),
      percentileTimeSeries);
  return metricsList;
}
 
Example #28
Source File: RecordUtils.java    From opencensus-java with Apache License 2.0 4 votes vote down vote up
static double getDoubleValueFromMeasurement(Measurement measurement) {
  return measurement.match(
      GET_VALUE_FROM_MEASUREMENT_DOUBLE,
      GET_VALUE_FROM_MEASUREMENT_LONG,
      Functions.<Double>throwAssertionError());
}