Home | History | Annotate | Download | only in stats
      1 /*
      2  * Copyright 2018, OpenCensus Authors
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *     http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 package io.opencensus.implcore.stats;
     18 
     19 import com.google.common.annotations.VisibleForTesting;
     20 import io.opencensus.common.Function;
     21 import io.opencensus.common.Functions;
     22 import io.opencensus.metrics.LabelKey;
     23 import io.opencensus.metrics.LabelValue;
     24 import io.opencensus.metrics.export.MetricDescriptor;
     25 import io.opencensus.metrics.export.MetricDescriptor.Type;
     26 import io.opencensus.stats.Aggregation;
     27 import io.opencensus.stats.Measure;
     28 import io.opencensus.stats.View;
     29 import io.opencensus.tags.TagKey;
     30 import io.opencensus.tags.TagValue;
     31 import java.util.ArrayList;
     32 import java.util.List;
     33 
     34 /*>>>
     35 import org.checkerframework.checker.nullness.qual.Nullable;
     36 */
     37 
     38 @SuppressWarnings("deprecation")
     39 // Utils to convert Stats data models to Metric data models.
     40 final class MetricUtils {
     41 
     42   @javax.annotation.Nullable
     43   static MetricDescriptor viewToMetricDescriptor(View view) {
     44     if (view.getWindow() instanceof View.AggregationWindow.Interval) {
     45       // Only creates Metric for cumulative stats.
     46       return null;
     47     }
     48     List<LabelKey> labelKeys = new ArrayList<LabelKey>();
     49     for (TagKey tagKey : view.getColumns()) {
     50       // TODO: add description
     51       labelKeys.add(LabelKey.create(tagKey.getName(), ""));
     52     }
     53     Measure measure = view.getMeasure();
     54     return MetricDescriptor.create(
     55         view.getName().asString(),
     56         view.getDescription(),
     57         measure.getUnit(),
     58         getType(measure, view.getAggregation()),
     59         labelKeys);
     60   }
     61 
     62   @VisibleForTesting
     63   static Type getType(Measure measure, Aggregation aggregation) {
     64     return aggregation.match(
     65         Functions.returnConstant(
     66             measure.match(
     67                 TYPE_CUMULATIVE_DOUBLE_FUNCTION, // Sum Double
     68                 TYPE_CUMULATIVE_INT64_FUNCTION, // Sum Int64
     69                 TYPE_UNRECOGNIZED_FUNCTION)),
     70         TYPE_CUMULATIVE_INT64_FUNCTION, // Count
     71         TYPE_CUMULATIVE_DISTRIBUTION_FUNCTION, // Distribution
     72         Functions.returnConstant(
     73             measure.match(
     74                 TYPE_GAUGE_DOUBLE_FUNCTION, // LastValue Double
     75                 TYPE_GAUGE_INT64_FUNCTION, // LastValue Long
     76                 TYPE_UNRECOGNIZED_FUNCTION)),
     77         AGGREGATION_TYPE_DEFAULT_FUNCTION);
     78   }
     79 
     80   static List<LabelValue> tagValuesToLabelValues(List</*@Nullable*/ TagValue> tagValues) {
     81     List<LabelValue> labelValues = new ArrayList<LabelValue>();
     82     for (/*@Nullable*/ TagValue tagValue : tagValues) {
     83       labelValues.add(LabelValue.create(tagValue == null ? null : tagValue.asString()));
     84     }
     85     return labelValues;
     86   }
     87 
     88   private static final Function<Object, Type> TYPE_CUMULATIVE_DOUBLE_FUNCTION =
     89       Functions.returnConstant(Type.CUMULATIVE_DOUBLE);
     90 
     91   private static final Function<Object, Type> TYPE_CUMULATIVE_INT64_FUNCTION =
     92       Functions.returnConstant(Type.CUMULATIVE_INT64);
     93 
     94   private static final Function<Object, Type> TYPE_CUMULATIVE_DISTRIBUTION_FUNCTION =
     95       Functions.returnConstant(Type.CUMULATIVE_DISTRIBUTION);
     96 
     97   private static final Function<Object, Type> TYPE_GAUGE_DOUBLE_FUNCTION =
     98       Functions.returnConstant(Type.GAUGE_DOUBLE);
     99 
    100   private static final Function<Object, Type> TYPE_GAUGE_INT64_FUNCTION =
    101       Functions.returnConstant(Type.GAUGE_INT64);
    102 
    103   private static final Function<Object, Type> TYPE_UNRECOGNIZED_FUNCTION =
    104       Functions.<Type>throwAssertionError();
    105 
    106   private static final Function<Aggregation, Type> AGGREGATION_TYPE_DEFAULT_FUNCTION =
    107       new Function<Aggregation, Type>() {
    108         @Override
    109         public Type apply(Aggregation arg) {
    110           if (arg instanceof Aggregation.Mean) {
    111             return Type.CUMULATIVE_DOUBLE; // Mean
    112           }
    113           throw new AssertionError();
    114         }
    115       };
    116 
    117   private MetricUtils() {}
    118 }
    119