Skip to content

Commit

Permalink
Apply Chris' changes
Browse files Browse the repository at this point in the history
  • Loading branch information
lzchen committed Nov 28, 2019
1 parent 30e5e5a commit f375128
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 75 deletions.
54 changes: 8 additions & 46 deletions opentelemetry-sdk/src/opentelemetry/sdk/metrics/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,8 @@
class LabelSet(metrics_api.LabelSet):
"""See `opentelemetry.metrics.LabelSet."""

def __init__(
self,
meter: "Meter" = None,
labels: Dict[str, str] = None,
encoded: str = "",
):
self.meter = meter
def __init__(self, labels: Dict[str, str] = None):
self.labels = labels
self.encoded = encoded


class BaseHandle:
Expand Down Expand Up @@ -110,7 +103,6 @@ def __init__(
description: str,
unit: str,
value_type: Type[metrics_api.ValueT],
meter: "Meter",
label_keys: Sequence[str] = None,
enabled: bool = True,
monotonic: bool = False,
Expand All @@ -119,39 +111,21 @@ def __init__(
self.description = description
self.unit = unit
self.value_type = value_type
self.meter = meter
self.label_keys = label_keys
self.enabled = enabled
self.monotonic = monotonic
self.handles = {}

def get_handle(self, label_set: LabelSet) -> BaseHandle:
"""See `opentelemetry.metrics.Metric.get_handle`."""
lable_set_for = self._label_set_for(label_set)
handle = self.handles.get(lable_set_for.encoded)
handle = self.handles.get(label_set)
if not handle:
handle = self.HANDLE_TYPE(
self.value_type, self.enabled, self.monotonic
)
self.handles[lable_set_for.encoded] = handle
self.handles[label_set] = handle
return handle

def _label_set_for(self, label_set: LabelSet) -> LabelSet:
"""Returns an appropriate `LabelSet` based off this metric's `meter`
If this metric's `meter` is the same as label_set's meter, that means
label_set was created from this metric's `meter` instance and the
proper handle can be found. If not, label_set was created using a
different `meter` implementation, in which the metric cannot interpret.
In this case, return the `EMPTY_LABEL_SET`.
Args:
label_set: The `LabelSet` to check for the same `Meter` implentation.
"""
if label_set.meter and label_set.meter is self.meter:
return label_set
return EMPTY_LABEL_SET

def __repr__(self):
return '{}(name="{}", description={})'.format(
type(self).__name__, self.name, self.description
Expand All @@ -176,7 +150,6 @@ def __init__(
description: str,
unit: str,
value_type: Type[metrics_api.ValueT],
meter: "Meter",
label_keys: Sequence[str] = None,
enabled: bool = True,
monotonic: bool = True,
Expand All @@ -186,7 +159,6 @@ def __init__(
description,
unit,
value_type,
meter,
label_keys=label_keys,
enabled=enabled,
monotonic=monotonic,
Expand Down Expand Up @@ -214,7 +186,6 @@ def __init__(
description: str,
unit: str,
value_type: Type[metrics_api.ValueT],
meter: "Meter",
label_keys: Sequence[str] = None,
enabled: bool = True,
monotonic: bool = False,
Expand All @@ -224,7 +195,6 @@ def __init__(
description,
unit,
value_type,
meter,
label_keys=label_keys,
enabled=enabled,
monotonic=monotonic,
Expand Down Expand Up @@ -252,7 +222,6 @@ def __init__(
description: str,
unit: str,
value_type: Type[metrics_api.ValueT],
meter: "Meter",
label_keys: Sequence[str] = None,
enabled: bool = False,
monotonic: bool = False,
Expand All @@ -262,7 +231,6 @@ def __init__(
description,
unit,
value_type,
meter,
label_keys=label_keys,
enabled=enabled,
monotonic=monotonic,
Expand All @@ -275,7 +243,7 @@ def record(self, label_set: LabelSet, value: metrics_api.ValueT) -> None:
UPDATE_FUNCTION = record


# Singleton of meter.get_label_set() with zero arguments
# Used when getting a LabelSet with no key/values
EMPTY_LABEL_SET = LabelSet()


Expand Down Expand Up @@ -312,7 +280,6 @@ def create_metric(
description,
unit,
value_type,
self,
label_keys=label_keys,
enabled=enabled,
monotonic=monotonic,
Expand All @@ -328,16 +295,11 @@ def get_label_set(self, labels: Dict[str, str]):
"""
if len(labels) == 0:
return EMPTY_LABEL_SET
sorted_labels = OrderedDict(sorted(labels.items()))
# Uses statsd encoding for labels
encoded = "," + ",".join(
"%s=%s" % (key, value) for (key, value) in sorted_labels.items()
)
# Use simple encoding for now until encoding API is implemented
encoded = tuple(sorted(labels.items()))
# If LabelSet exists for this meter in memory, use existing one
if not self.labels.get(encoded):
self.labels[encoded] = LabelSet(
self, labels=sorted_labels, encoded=encoded
)
if not encoded in self.labels:
self.labels[encoded] = LabelSet(labels=labels)
return self.labels[encoded]


Expand Down
39 changes: 10 additions & 29 deletions opentelemetry-sdk/tests/metrics/test_metrics.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ def test_record_batch_exists(self):
def test_create_metric(self):
meter = metrics.Meter()
counter = meter.create_metric(
"name", "desc", "unit", int, metrics.Counter, meter, ()
"name", "desc", "unit", int, metrics.Counter, ()
)
self.assertTrue(isinstance(counter, metrics.Counter))
self.assertEqual(counter.value_type, int)
Expand All @@ -81,7 +81,7 @@ def test_create_metric(self):
def test_create_gauge(self):
meter = metrics.Meter()
gauge = meter.create_metric(
"name", "desc", "unit", float, metrics.Gauge, meter, ()
"name", "desc", "unit", float, metrics.Gauge, ()
)
self.assertTrue(isinstance(gauge, metrics.Gauge))
self.assertEqual(gauge.value_type, float)
Expand All @@ -90,7 +90,7 @@ def test_create_gauge(self):
def test_create_measure(self):
meter = metrics.Meter()
measure = meter.create_metric(
"name", "desc", "unit", float, metrics.Measure, meter, ()
"name", "desc", "unit", float, metrics.Measure, ()
)
self.assertTrue(isinstance(measure, metrics.Measure))
self.assertEqual(measure.value_type, float)
Expand All @@ -100,8 +100,8 @@ def test_get_label_set(self):
meter = metrics.Meter()
kvp = {"environment": "staging", "a": "z"}
label_set = meter.get_label_set(kvp)
encoding = ",a=z,environment=staging"
self.assertEqual(label_set.encoded, encoding)
encoded = tuple(sorted(kvp.items()))
self.assertIs(meter.labels[encoded], label_set)

def test_get_label_set_empty(self):
meter = metrics.Meter()
Expand All @@ -114,7 +114,7 @@ def test_get_label_set_exists(self):
kvp = {"environment": "staging", "a": "z"}
label_set = meter.get_label_set(kvp)
label_set2 = meter.get_label_set(kvp)
self.assertEqual(label_set, label_set2)
self.assertIs(label_set, label_set2)


class TestMetric(unittest.TestCase):
Expand All @@ -126,32 +126,13 @@ def test_get_handle(self):
kvp = {"key": "value"}
label_set = meter.get_label_set(kvp)
handle = metric.get_handle(label_set)
self.assertEqual(metric.handles.get(label_set.encoded), handle)

def test_label_set_for(self):
meter = metrics.Meter()
metric = metrics.Counter("name", "desc", "unit", int, meter, ("key",))
kvp = {"environment": "staging", "a": "z"}
label_set = meter.get_label_set(kvp)
# pylint: disable=protected-access
self.assertEqual(metric._label_set_for(label_set), label_set)

def test_label_set_for_different(self):
meter = metrics.Meter()
meter2 = metrics.Meter()
metric = metrics.Counter("name", "desc", "unit", int, meter, ("key",))
kvp = {"environment": "staging", "a": "z"}
label_set = meter2.get_label_set(kvp)
# pylint: disable=protected-access
self.assertEqual(
metric._label_set_for(label_set), metrics.EMPTY_LABEL_SET
)
self.assertEqual(metric.handles.get(label_set), handle)


class TestCounter(unittest.TestCase):
def test_add(self):
meter = metrics.Meter()
metric = metrics.Counter("name", "desc", "unit", int, meter, ("key",))
metric = metrics.Counter("name", "desc", "unit", int, ("key",))
kvp = {"key": "value"}
label_set = meter.get_label_set(kvp)
handle = metric.get_handle(label_set)
Expand All @@ -163,7 +144,7 @@ def test_add(self):
class TestGauge(unittest.TestCase):
def test_set(self):
meter = metrics.Meter()
metric = metrics.Gauge("name", "desc", "unit", int, meter, ("key",))
metric = metrics.Gauge("name", "desc", "unit", int, ("key",))
kvp = {"key": "value"}
label_set = meter.get_label_set(kvp)
handle = metric.get_handle(label_set)
Expand All @@ -176,7 +157,7 @@ def test_set(self):
class TestMeasure(unittest.TestCase):
def test_record(self):
meter = metrics.Meter()
metric = metrics.Measure("name", "desc", "unit", int, meter, ("key",))
metric = metrics.Measure("name", "desc", "unit", int, ("key",))
kvp = {"key": "value"}
label_set = meter.get_label_set(kvp)
handle = metric.get_handle(label_set)
Expand Down

0 comments on commit f375128

Please sign in to comment.