def to_point(self, timestamp):
        """Get a Point conversion of this aggregation.

        This method creates a :class: `opencensus.metrics.export.point.Point`
        with a :class: `opencensus.metrics.export.value.ValueDistribution`
        value, and creates buckets and exemplars for that distribution from the
        appropriate classes in the `metrics` package. If the distribution
        doesn't have a histogram (i.e. `bounds` is empty) the converted point's
        `buckets` attribute will be null.

        :type timestamp: :class: `datetime.datetime`
        :param timestamp: The time to report the point as having been recorded.

        :rtype: :class: `opencensus.metrics.export.point.Point`
        :return: a :class: `opencensus.metrics.export.value.ValueDistribution`
        -valued Point.
        """
        if self.bounds:
            bucket_options = value.BucketOptions(value.Explicit(self.bounds))
            buckets = [None] * len(self.counts_per_bucket)
            for ii, count in enumerate(self.counts_per_bucket):
                stat_ex = self.exemplars.get(ii) if self.exemplars else None
                if stat_ex is not None:
                    metric_ex = value.Exemplar(stat_ex.value,
                                               stat_ex.timestamp,
                                               copy.copy(stat_ex.attachments))
                    buckets[ii] = value.Bucket(count, metric_ex)
                else:
                    buckets[ii] = value.Bucket(count)

        else:
            bucket_options = value.BucketOptions()
            buckets = None
        return point.Point(
            value.ValueDistribution(
                count=self.count_data,
                sum_=self.sum,
                sum_of_squared_deviation=self.sum_of_sqd_deviations,
                bucket_options=bucket_options,
                buckets=buckets
            ),
            timestamp
        )
def create_metric():
    lv = label_value.LabelValue('val')
    val = value.ValueLong(value=123)
    dt = datetime(2019, 3, 20, 21, 34, 0, 537954)
    pp = point.Point(value=val, timestamp=dt)

    ts = [
        time_series.TimeSeries(label_values=[lv],
                               points=[pp],
                               start_timestamp=utils.to_iso_str(dt))
    ]

    desc = metric_descriptor.MetricDescriptor(
        name='name',
        description='description',
        unit='unit',
        type_=metric_descriptor.MetricDescriptorType.GAUGE_INT64,
        label_keys=[label_key.LabelKey('key', 'description')])

    return metric.Metric(descriptor=desc, time_series=ts)
Esempio n. 3
0
def get_timeseries_list(points, timestamp):
    """Convert a list of `GaugePoint`s into a list of `TimeSeries`.

    Get a :class:`opencensus.metrics.export.time_series.TimeSeries` for each
    measurement in `points`. Each series contains a single
    :class:`opencensus.metrics.export.point.Point` that represents the last
    recorded value of the measurement.

    :type points: list(:class:`GaugePoint`)
    :param points: The list of measurements to convert.

    :type timestamp: :class:`datetime.datetime`
    :param timestamp: Recording time to report, usually the current time.

    :rtype: list(:class:`opencensus.metrics.export.time_series.TimeSeries`)
    :return: A list of one `TimeSeries` for each point in `points`.
    """
    ts_list = []
    for lv, gp in points.items():
        point = point_module.Point(gp.to_point_value(), timestamp)
        ts_list.append(time_series.TimeSeries(lv, [point], timestamp))
    return ts_list
Esempio n. 4
0
    def get_metric(self, timestamp):
        """Get a metric including all current time series.

        Get a :class:`opencensus.metrics.export.metric.Metric` with one
        :class:`opencensus.metrics.export.time_series.TimeSeries` for each
        set of label values with a recorded measurement. Each `TimeSeries`
        has a single point that represents the last recorded value.

        :type timestamp: :class:`datetime.datetime`
        :param timestamp: Recording time to report, usually the current time.

        :rtype: :class:`opencensus.metrics.export.metric.Metric` or None
        :return: A converted metric for all current measurements.
        """
        if not self.points:
            return None

        ts_list = []
        with self._points_lock:
            for lv, gp in self.points.items():
                point = point_module.Point(
                    self.value_type(gp.value), timestamp)
                ts_list.append(time_series.TimeSeries(lv, [point], timestamp))
        return metric.Metric(self.descriptor, ts_list)
Esempio n. 5
0
# 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.

import unittest

from opencensus.metrics import label_value
from opencensus.metrics.export import point
from opencensus.metrics.export import time_series
from opencensus.metrics.export import value

START_TIMESTAMP = '2018-10-09T22:33:44.012345Z'
LABEL_VALUE1 = label_value.LabelValue('value one')
LABEL_VALUE2 = label_value.LabelValue('价值二')
LABEL_VALUES = (LABEL_VALUE1, LABEL_VALUE2)
POINTS = (point.Point(value.ValueLong(1), "2018-10-09T23:33:44.012345Z"),
          point.Point(value.ValueLong(2), "2018-10-10T00:33:44.012345Z"),
          point.Point(value.ValueLong(3), "2018-10-10T01:33:44.012345Z"),
          point.Point(value.ValueLong(4), "2018-10-10T02:33:44.012345Z"),
          point.Point(value.ValueLong(5), "2018-10-10T03:33:44.012345Z"))


class TestTimeSeries(unittest.TestCase):
    def test_init(self):
        ts = time_series.TimeSeries(LABEL_VALUES, POINTS, START_TIMESTAMP)

        self.assertEqual(ts.start_timestamp, START_TIMESTAMP)
        self.assertEqual(ts.label_values, LABEL_VALUES)
        self.assertEqual(ts.points, POINTS)

    def test_init_invalid(self):