aboutsummaryrefslogtreecommitdiff
path: root/impl_core/src/main/java/io/opencensus/implcore/metrics/Gauge.java
blob: e6498066072c68c59d0e6f940c5930974b96a8e9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
/*
 * Copyright 2018, OpenCensus Authors
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package io.opencensus.implcore.metrics;

import static com.google.common.base.Preconditions.checkNotNull;

import io.opencensus.common.Clock;
import io.opencensus.common.ToDoubleFunction;
import io.opencensus.common.ToLongFunction;
import io.opencensus.metrics.LabelKey;
import io.opencensus.metrics.LabelValue;
import io.opencensus.metrics.Metric;
import io.opencensus.metrics.MetricDescriptor;
import io.opencensus.metrics.MetricDescriptor.Type;
import io.opencensus.metrics.Point;
import io.opencensus.metrics.TimeSeries;
import io.opencensus.metrics.Value;
import java.util.Collections;
import java.util.List;

abstract class Gauge {
  private final MetricDescriptor metricDescriptor;
  private final List<LabelValue> labelValues;

  final Metric getMetric(Clock clock) {
    return Metric.create(metricDescriptor, Collections.singletonList(getTimeSeries(clock)));
  }

  abstract TimeSeries getTimeSeries(Clock clock);

  static final class DoubleGauge<T> extends Gauge {
    private final T obj;
    private final ToDoubleFunction<T> function;

    DoubleGauge(
        String name,
        String description,
        String unit,
        List<LabelKey> labelKeys,
        List<LabelValue> labelValues,
        T obj,
        ToDoubleFunction<T> function) {
      super(
          MetricDescriptor.create(name, description, unit, Type.GAUGE_DOUBLE, labelKeys),
          labelValues);
      this.obj = obj;
      this.function = function;
    }

    @Override
    TimeSeries getTimeSeries(Clock clock) {
      return TimeSeries.create(
          getLabelValues(),
          Collections.singletonList(
              Point.create(Value.doubleValue(function.applyAsDouble(obj)), clock.now())),
          null);
    }
  }

  static final class LongGauge<T> extends Gauge {
    private final T obj;
    private final ToLongFunction<T> function;

    LongGauge(
        String name,
        String description,
        String unit,
        List<LabelKey> labelKeys,
        List<LabelValue> labelValues,
        T obj,
        ToLongFunction<T> function) {
      super(
          MetricDescriptor.create(name, description, unit, Type.GAUGE_INT64, labelKeys),
          labelValues);
      this.obj = obj;
      this.function = function;
    }

    @Override
    TimeSeries getTimeSeries(Clock clock) {
      return TimeSeries.create(
          getLabelValues(),
          Collections.singletonList(
              Point.create(Value.longValue(function.applyAsLong(obj)), clock.now())),
          null);
    }
  }

  List<LabelValue> getLabelValues() {
    return labelValues;
  }

  Gauge(MetricDescriptor metricDescriptor, List<LabelValue> labelValues) {
    this.metricDescriptor = checkNotNull(metricDescriptor, "metricDescriptor");
    this.labelValues = Collections.unmodifiableList(labelValues);
  }
}