Skip to content

Commit

Permalink
Add kwargs to derived gauge (#1135)
Browse files Browse the repository at this point in the history
  • Loading branch information
lzchen authored Jun 30, 2022
1 parent 7783196 commit 8e30d7d
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 10 deletions.
5 changes: 3 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
# Changelog

## Unreleased
- Allow specifying metrics (custom_measurements) for Azure custom events
([#1117](https://github.com/census-instrumentation/opencensus-python/pull/1117))

- Add kwargs to derived gauge
([#1135](https://github.com/census-instrumentation/opencensus-python/pull/1135))

# 0.9.0
Released 2022-04-20
Expand Down
2 changes: 2 additions & 0 deletions contrib/opencensus-ext-azure/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

## Unreleased

- Allow specifying metrics (custom_measurements) for Azure custom events
([#1117](https://github.com/census-instrumentation/opencensus-python/pull/1117))
- Shutdown Statsbeat when hitting error/exception threshold
([#1127](https://github.com/census-instrumentation/opencensus-python/pull/1127))
- Fix failure counting statsbeat - refactor status code logic in transport
Expand Down
18 changes: 10 additions & 8 deletions opencensus/metrics/export/gauge.py
Original file line number Diff line number Diff line change
Expand Up @@ -194,15 +194,17 @@ class DerivedGaugePoint(GaugePoint):
:class:`opencensus.metrics.export.cumulative.CumulativePointDouble`
:param gauge_point: The underlying `GaugePoint`.
"""
def __init__(self, func, gauge_point):
def __init__(self, func, gauge_point, **kwargs):
self.gauge_point = gauge_point
self.func = utils.get_weakref(func)
self._kwargs = kwargs

def __repr__(self):
return ("{}({})"
return ("{}({})({})"
.format(
type(self).__name__,
self.func()
self.func(),
self._kwargs
))

def get_value(self):
Expand All @@ -216,7 +218,7 @@ def get_value(self):
longer exists.
"""
try:
val = self.func()()
val = self.func()(**self._kwargs)
except TypeError: # The underlying function has been GC'd
return None

Expand Down Expand Up @@ -406,13 +408,13 @@ class DerivedGauge(BaseGauge):
instead of using this class directly.
"""

def _create_time_series(self, label_values, func):
def _create_time_series(self, label_values, func, **kwargs):
with self._points_lock:
return self.points.setdefault(
tuple(label_values),
DerivedGaugePoint(func, self.point_type()))
DerivedGaugePoint(func, self.point_type(), **kwargs))

def create_time_series(self, label_values, func):
def create_time_series(self, label_values, func, **kwargs):
"""Create a derived measurement to trac `func`.
:type label_values: list(:class:`LabelValue`)
Expand All @@ -432,7 +434,7 @@ def create_time_series(self, label_values, func):
raise ValueError
if func is None:
raise ValueError
return self._create_time_series(label_values, func)
return self._create_time_series(label_values, func, **kwargs)

def create_default_time_series(self, func):
"""Create the default derived measurement for this gauge.
Expand Down
9 changes: 9 additions & 0 deletions tests/unit/metrics/export/test_gauge.py
Original file line number Diff line number Diff line change
Expand Up @@ -393,6 +393,15 @@ def test_create_time_series(self):
unused_mock_fn.assert_not_called()
self.assertEqual(len(derived_gauge.points.keys()), 1)

# with kwargs
def fn_with_args(value=None):
if value:
return value
return 0
label_values2 = [1, 2]
point3 = derived_gauge.create_time_series(label_values2, fn_with_args, value=5) # noqa: E501
self.assertEqual(point3.get_value(), 5)

def test_create_default_time_series(self):
derived_gauge = gauge.DerivedLongGauge(
Mock(), Mock(), Mock(), [Mock(), Mock])
Expand Down

0 comments on commit 8e30d7d

Please sign in to comment.