-
Notifications
You must be signed in to change notification settings - Fork 710
Adds Aggregation and instruments as part of Metrics SDK #2234
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
49 commits
Select commit
Hold shift + click to select a range
082d553
Adds metrics API (#1887)
ocelotl 162acb9
Make measurement a concrete class (#2153)
aabmass 22b561d
Return proxy instruments from ProxyMeter (#2169)
aabmass 6e8b1a7
Merge main 4 (#2236)
ocelotl 4b448d8
Add MeterProvider and Meter to the SDK
ocelotl aa2b1f0
Add FIXMEs
ocelotl 881b04a
Fix docstring
ocelotl e3816eb
Add FIXME
ocelotl 611ef1f
Fix meter return
ocelotl c7f0dae
Log an error if a force flush fails
ocelotl 0ab82ba
Add FIXME
ocelotl a01198f
Fix lint
ocelotl 558c9ac
Remove SDK API module
ocelotl 3c119a2
Unregister
ocelotl 899c064
Fix API names
ocelotl bdce736
Return _DefaultMeter
ocelotl 8cff4ca
Remove properties
ocelotl f019207
Pass MeterProvider as a parameter to __init__
ocelotl 07fdeac
Add FIXMEs
ocelotl 32b67e8
Add FIXMEs
ocelotl cb3ed60
Fix lint
ocelotl 325e904
Add Aggregation to the metrics SDK
ocelotl ab5d753
lint fix wip
ocelotl 26e103d
Fix lint
ocelotl 49e82b6
Add proto to setup.cfg
ocelotl a58e1f9
Add timestamp for last value
ocelotl deb696f
Rename modules to be private
ocelotl 904c7d9
Fix paths
ocelotl 50e708a
Set value in concrete classes init
ocelotl b157bb7
Fix test
ocelotl 3d5a779
Fix lint
ocelotl f1b9529
Remove temporalities
ocelotl 46dad80
Use frozenset as key
ocelotl 2c8e893
Test instruments
ocelotl 2d9e0c3
Handle min, max and sum in explicit bucket histogram aggregator
ocelotl 2f084f1
Add test for negative values
ocelotl 641823c
Remove collect method from aggregations
ocelotl ed502c5
Add make_point_and_reset
ocelotl 5f50e74
Remove add implementation
ocelotl f51cb8e
Remove _Synchronous
ocelotl c4196f7
Update opentelemetry-sdk/src/opentelemetry/sdk/_metrics/aggregation.py
ocelotl 4c8454c
Requested fixes
ocelotl d8797bc
Remove NoneAggregation
ocelotl 90f0ef8
Add changelog entry
ocelotl dd685f5
Fix tests
ocelotl b7f05b3
Fix boundaries
ocelotl 8c1a8aa
More fixes
ocelotl 7136925
Merge branch 'main' into issue_2229
ocelotl ec591ed
Update CHANGELOG.md
ocelotl 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
137 changes: 137 additions & 0 deletions
137
opentelemetry-sdk/src/opentelemetry/sdk/_metrics/aggregation.py
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,137 @@ | ||
# Copyright The OpenTelemetry 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 abc import ABC, abstractmethod | ||
from collections import OrderedDict | ||
from logging import getLogger | ||
from math import inf | ||
|
||
from opentelemetry._metrics.instrument import _Monotonic | ||
from opentelemetry.util._time import _time_ns | ||
|
||
_logger = getLogger(__name__) | ||
|
||
|
||
class Aggregation(ABC): | ||
@property | ||
def value(self): | ||
return self._value # pylint: disable=no-member | ||
|
||
@abstractmethod | ||
def aggregate(self, value): | ||
pass | ||
|
||
@abstractmethod | ||
def make_point_and_reset(self): | ||
""" | ||
Atomically return a point for the current value of the metric and reset the internal state. | ||
""" | ||
|
||
|
||
class SumAggregation(Aggregation): | ||
""" | ||
This aggregation collects data for the SDK sum metric point. | ||
""" | ||
|
||
def __init__(self, instrument): | ||
self._value = 0 | ||
|
||
def aggregate(self, value): | ||
self._value = self._value + value | ||
|
||
def make_point_and_reset(self): | ||
pass | ||
|
||
|
||
class LastValueAggregation(Aggregation): | ||
|
||
""" | ||
This aggregation collects data for the SDK sum metric point. | ||
""" | ||
|
||
def __init__(self, instrument): | ||
self._value = None | ||
self._timestamp = _time_ns() | ||
|
||
def aggregate(self, value): | ||
self._value = value | ||
self._timestamp = _time_ns() | ||
|
||
def make_point_and_reset(self): | ||
pass | ||
|
||
|
||
class ExplicitBucketHistogramAggregation(Aggregation): | ||
|
||
""" | ||
This aggregation collects data for the SDK sum metric point. | ||
""" | ||
|
||
def __init__( | ||
self, | ||
instrument, | ||
*args, | ||
boundaries=(0, 5, 10, 25, 50, 75, 100, 250, 500, 1000), | ||
record_min_max=True, | ||
ocelotl marked this conversation as resolved.
Show resolved
Hide resolved
|
||
): | ||
super().__init__() | ||
ocelotl marked this conversation as resolved.
Show resolved
Hide resolved
|
||
self._value = OrderedDict([(key, 0) for key in (*boundaries, inf)]) | ||
self._min = inf | ||
self._max = -inf | ||
self._sum = 0 | ||
self._instrument = instrument | ||
self._record_min_max = record_min_max | ||
|
||
@property | ||
def min(self): | ||
if not self._record_min_max: | ||
_logger.warning("Min is not being recorded") | ||
|
||
return self._min | ||
|
||
@property | ||
def max(self): | ||
if not self._record_min_max: | ||
_logger.warning("Max is not being recorded") | ||
|
||
return self._max | ||
|
||
@property | ||
def sum(self): | ||
if isinstance(self._instrument, _Monotonic): | ||
return self._sum | ||
|
||
_logger.warning( | ||
"Sum is not filled out when the associated " | ||
"instrument is not monotonic" | ||
) | ||
return None | ||
|
||
def aggregate(self, value): | ||
if self._record_min_max: | ||
self._min = min(self._min, value) | ||
self._max = max(self._max, value) | ||
|
||
if isinstance(self._instrument, _Monotonic): | ||
self._sum += value | ||
|
||
for key in self._value.keys(): | ||
|
||
if value < key: | ||
self._value[key] = self._value[key] + value | ||
|
||
break | ||
|
||
def make_point_and_reset(self): | ||
pass |
160 changes: 160 additions & 0 deletions
160
opentelemetry-sdk/src/opentelemetry/sdk/_metrics/instrument.py
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,160 @@ | ||
# Copyright The OpenTelemetry 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. | ||
|
||
# pylint: disable=function-redefined | ||
# pylint: disable=dangerous-default-value | ||
# Classes in this module use dictionaries as default arguments. This is | ||
# considered dangerous by pylint because the default dictionary is shared by | ||
# all instances. Implementations of these classes must not make any change to | ||
# this default dictionary in __init__. | ||
|
||
from opentelemetry._metrics.instrument import ( | ||
Counter, | ||
Histogram, | ||
ObservableCounter, | ||
ObservableGauge, | ||
ObservableUpDownCounter, | ||
UpDownCounter, | ||
) | ||
from opentelemetry.sdk._metrics.aggregation import ( | ||
ExplicitBucketHistogramAggregation, | ||
LastValueAggregation, | ||
SumAggregation, | ||
) | ||
|
||
|
||
class _Instrument: | ||
def __init__( | ||
self, | ||
name, | ||
unit="", | ||
description="", | ||
aggregation=None, | ||
aggregation_config={}, | ||
): | ||
self._attributes_aggregations = {} | ||
self._aggregation = aggregation | ||
self._aggregation_config = aggregation_config | ||
aggregation(self, **aggregation_config) | ||
|
||
|
||
class Counter(_Instrument, Counter): | ||
def __init__( | ||
self, | ||
name, | ||
unit="", | ||
description="", | ||
aggregation=SumAggregation, | ||
aggregation_config={}, | ||
): | ||
super().__init__( | ||
name, | ||
unit=unit, | ||
description=description, | ||
aggregation=aggregation, | ||
aggregation_config=aggregation_config, | ||
) | ||
|
||
|
||
class UpDownCounter(_Instrument, UpDownCounter): | ||
def __init__( | ||
self, | ||
name, | ||
unit="", | ||
description="", | ||
aggregation=SumAggregation, | ||
aggregation_config={}, | ||
): | ||
super().__init__( | ||
name, | ||
unit=unit, | ||
description=description, | ||
aggregation=aggregation, | ||
aggregation_config=aggregation_config, | ||
) | ||
|
||
|
||
class ObservableCounter(_Instrument, ObservableCounter): | ||
def __init__( | ||
self, | ||
name, | ||
callback, | ||
unit="", | ||
description="", | ||
aggregation=SumAggregation, | ||
aggregation_config={}, | ||
): | ||
super().__init__( | ||
name, | ||
unit=unit, | ||
description=description, | ||
aggregation=aggregation, | ||
aggregation_config=aggregation_config, | ||
) | ||
|
||
|
||
class ObservableUpDownCounter(_Instrument, ObservableUpDownCounter): | ||
def __init__( | ||
self, | ||
name, | ||
callback, | ||
unit="", | ||
description="", | ||
aggregation=SumAggregation, | ||
aggregation_config={}, | ||
): | ||
super().__init__( | ||
name, | ||
unit=unit, | ||
description=description, | ||
aggregation=aggregation, | ||
aggregation_config=aggregation_config, | ||
) | ||
|
||
|
||
class Histogram(_Instrument, Histogram): | ||
def __init__( | ||
self, | ||
name, | ||
unit="", | ||
description="", | ||
aggregation=ExplicitBucketHistogramAggregation, | ||
aggregation_config={}, | ||
): | ||
super().__init__( | ||
name, | ||
unit=unit, | ||
description=description, | ||
aggregation=aggregation, | ||
aggregation_config=aggregation_config, | ||
) | ||
|
||
|
||
class ObservableGauge(_Instrument, ObservableGauge): | ||
def __init__( | ||
self, | ||
name, | ||
callback, | ||
unit="", | ||
description="", | ||
aggregation=LastValueAggregation, | ||
aggregation_config={}, | ||
): | ||
super().__init__( | ||
name, | ||
unit=unit, | ||
description=description, | ||
aggregation=aggregation, | ||
aggregation_config=aggregation_config, | ||
) |
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.