This repository was archived by the owner on Sep 17, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 246
Add Metric and supporting classes #347
Merged
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
6096660
Add TimeSeries
c24t fee87f4
Add ValueDistribution and related classes
c24t 8f94f5b
Add Metric and TS type check
c24t b805f72
Improve TimeSeries point class check and test
c24t a979a8f
Update for @mayurkale22's comments on #347
c24t f17f1f1
Change TS arg order, make start_timestamp nullable
c24t 7e4ea62
Merge branch 'master' into add-metric
c24t 4a47970
Move noqa to bottom of docstring
c24t 39d2dcb
Lift value type mapping out of check function
c24t 3656284
Merge branch 'add-metric' of github.com:c24t/opencensus-python into a…
c24t File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,91 @@ | ||
| # 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. | ||
|
|
||
| from opencensus.metrics.export import metric_descriptor | ||
| from opencensus.metrics.export import value | ||
|
|
||
|
|
||
| DESCRIPTOR_VALUE = { | ||
| metric_descriptor.MetricDescriptorType.GAUGE_INT64: | ||
| value.ValueLong, | ||
| metric_descriptor.MetricDescriptorType.CUMULATIVE_INT64: | ||
| value.ValueLong, | ||
| metric_descriptor.MetricDescriptorType.GAUGE_DOUBLE: | ||
| value.ValueDouble, | ||
| metric_descriptor.MetricDescriptorType.CUMULATIVE_DOUBLE: | ||
| value.ValueDouble, | ||
| metric_descriptor.MetricDescriptorType.GAUGE_DISTRIBUTION: | ||
| value.ValueDistribution, | ||
| metric_descriptor.MetricDescriptorType.CUMULATIVE_DISTRIBUTION: | ||
| value.ValueDistribution, | ||
| metric_descriptor.MetricDescriptorType.SUMMARY: | ||
| value.ValueSummary, | ||
| } | ||
|
|
||
|
|
||
| class Metric(object): | ||
| """A collection of time series data and label metadata. | ||
|
|
||
| This class implements the spec for v1 Metrics as of opencensus-proto | ||
| release v0.0.2. See opencensus-proto for details: | ||
|
|
||
| https://github.com/census-instrumentation/opencensus-proto/blob/24333298e36590ea0716598caacc8959fc393c48/src/opencensus/proto/metrics/v1/metrics.proto#33 | ||
|
|
||
| Defines a Metric which has one or more timeseries. | ||
|
|
||
| :type descriptor: class: '~opencensus.metrics.export.metric_descriptor.MetricDescriptor' | ||
| :param descriptor: The metric's descriptor. | ||
|
|
||
| :type timeseries: list(:class: '~opencensus.metrics.export.time_series.TimeSeries') | ||
| :param timeseries: One or more timeseries for a single metric, where each | ||
| timeseries has one or more points. | ||
| """ # noqa | ||
|
|
||
| def __init__(self, descriptor, time_series): | ||
| if not time_series: | ||
| raise ValueError("time_series must not be empty or null") | ||
| if descriptor is None: | ||
| raise ValueError("descriptor must not be null") | ||
| self._time_series = time_series | ||
| self._descriptor = descriptor | ||
| self._check_type() | ||
|
|
||
| @property | ||
| def time_series(self): | ||
| return self._time_series | ||
|
|
||
| @property | ||
| def descriptor(self): | ||
| return self._descriptor | ||
|
|
||
| def _check_type(self): | ||
| """Check that point value types match the descriptor type.""" | ||
| check_type = DESCRIPTOR_VALUE.get(self.descriptor.type) | ||
| if check_type is None: | ||
| raise ValueError("Unknown metric descriptor type") | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. OK, changed in 39d2dcb. |
||
| for ts in self.time_series: | ||
| if not ts.check_points_type(check_type): | ||
| raise ValueError("Invalid point value type") | ||
|
|
||
| def _check_start_timestamp(self): | ||
| """Check that starting timestamp exists for cumulative metrics.""" | ||
| if self.descriptor.type in ( | ||
| metric_descriptor.MetricDescriptorType.CUMULATIVE_INT64, | ||
| metric_descriptor.MetricDescriptorType.CUMULATIVE_DOUBLE, | ||
| metric_descriptor.MetricDescriptorType.CUMULATIVE_DISTRIBUTION, | ||
| ): | ||
| for ts in self.time_series: | ||
| if ts.start_timestamp is None: | ||
| raise ValueError("time_series.start_timestamp must exist " | ||
| "for cumulative metrics") | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,74 @@ | ||
| # 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. | ||
|
|
||
| from opencensus.metrics.export import metric_descriptor | ||
|
|
||
|
|
||
| class TimeSeries(object): | ||
| """Time series data for a given metric and time interval. | ||
|
|
||
| This class implements the spec for v1 TimeSeries structs as of | ||
| opencensus-proto release v0.0.2. See opencensus-proto for details: | ||
|
|
||
| https://github.com/census-instrumentation/opencensus-proto/blob/24333298e36590ea0716598caacc8959fc393c48/src/opencensus/proto/metrics/v1/metrics.proto#L112 | ||
|
|
||
| A TimeSeries is a collection of data points that describes the time-varying | ||
| values of a metric. | ||
|
|
||
| :type label_values: list(:class: | ||
| '~opencensus.metrics.label_value.LabelValue') | ||
| :param label_values: The set of label values that uniquely identify this | ||
| timeseries. | ||
|
|
||
| :type points: list(:class: '~opencensus.metrics.export.point.Point') | ||
| :param points: The data points of this timeseries. | ||
|
|
||
| :type start_timestamp: str | ||
| :param start_timestamp: The time when the cumulative value was reset to | ||
| zero, must be set for cumulative metrics. | ||
| """ # noqa | ||
|
|
||
| def __init__(self, label_values, points, start_timestamp): | ||
| if not label_values: | ||
| raise ValueError("label_values must not be null or empty") | ||
| if not points: | ||
| raise ValueError("points must not be null or empty") | ||
| self._label_values = label_values | ||
| self._points = points | ||
| self._start_timestamp = start_timestamp | ||
|
|
||
| @property | ||
| def start_timestamp(self): | ||
| return self._start_timestamp | ||
|
|
||
| @property | ||
| def label_values(self): | ||
| return self._label_values | ||
|
|
||
| @property | ||
| def points(self): | ||
| return self._points | ||
|
|
||
| def check_points_type(self, type_): | ||
| """Check that each point's value is an instance `type_`. | ||
|
|
||
| :type type_: type | ||
| :param type_: Type to check against. | ||
| """ | ||
| type_class = ( | ||
| metric_descriptor.MetricDescriptorType.to_type_class(type_)) | ||
| for point in self.points: | ||
| if not isinstance(point.value.value, type_class): | ||
| return False | ||
| return True |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.