Skip to content

Commit

Permalink
renaming usages of meter to accumulator in metrics sdk context
Browse files Browse the repository at this point in the history
  • Loading branch information
Azfaar Qureshi committed Nov 10, 2020
1 parent baa6b10 commit 2edb265
Show file tree
Hide file tree
Showing 13 changed files with 186 additions and 167 deletions.
8 changes: 4 additions & 4 deletions docs/examples/basic_meter/basic_metrics.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,24 +42,24 @@

# The Meter is responsible for creating and recording metrics. Each meter has a
# unique name, which we set as the module's name here.
meter = metrics.get_meter(__name__)
accumulator = metrics.get_meter(__name__)

# Exporter to export metrics to the console
exporter = ConsoleMetricsExporter()

# start_pipeline will notify the MeterProvider to begin collecting/exporting
# metrics with the given meter, exporter and interval in seconds
metrics.get_meter_provider().start_pipeline(meter, exporter, 5)
metrics.get_meter_provider().start_pipeline(accumulator, exporter, 5)

# Metric instruments allow to capture measurements
requests_counter = meter.create_counter(
requests_counter = accumulator.create_counter(
name="requests",
description="number of requests",
unit="1",
value_type=int,
)

requests_size = meter.create_valuerecorder(
requests_size = accumulator.create_valuerecorder(
name="requests_size",
description="size of requests",
unit="1",
Expand Down
14 changes: 9 additions & 5 deletions docs/examples/basic_meter/calling_conventions.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,17 +24,19 @@

# Use the meter type provided by the SDK package
metrics.set_meter_provider(MeterProvider())
meter = metrics.get_meter(__name__)
metrics.get_meter_provider().start_pipeline(meter, ConsoleMetricsExporter(), 5)
accumulator = metrics.get_meter(__name__)
metrics.get_meter_provider().start_pipeline(
accumulator, ConsoleMetricsExporter(), 5
)

requests_counter = meter.create_counter(
requests_counter = accumulator.create_counter(
name="requests",
description="number of requests",
unit="1",
value_type=int,
)

clicks_counter = meter.create_counter(
clicks_counter = accumulator.create_counter(
name="clicks", description="number of clicks", unit="1", value_type=int,
)

Expand All @@ -60,7 +62,9 @@
# You can record metrics in a batch by passing in labels and a sequence of
# (metric, value) pairs. The value would be recorded for each metric using the
# specified labels for each.
meter.record_batch(labels, ((requests_counter, 50), (clicks_counter, 70)))
accumulator.record_batch(
labels, ((requests_counter, 50), (clicks_counter, 70))
)
time.sleep(5)

input("...\n")
10 changes: 6 additions & 4 deletions docs/examples/basic_meter/observer.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,10 @@
from opentelemetry.sdk.metrics.export import ConsoleMetricsExporter

metrics.set_meter_provider(MeterProvider())
meter = metrics.get_meter(__name__)
metrics.get_meter_provider().start_pipeline(meter, ConsoleMetricsExporter(), 5)
accumulator = metrics.get_meter(__name__)
metrics.get_meter_provider().start_pipeline(
accumulator, ConsoleMetricsExporter(), 5
)


# Callback to gather cpu usage
Expand All @@ -34,7 +36,7 @@ def get_cpu_usage_callback(observer):
observer.observe(percent, labels)


meter.register_valueobserver(
accumulator.register_valueobserver(
callback=get_cpu_usage_callback,
name="cpu_percent",
description="per-cpu usage",
Expand All @@ -49,7 +51,7 @@ def get_ram_usage_callback(observer):
observer.observe(ram_percent, {})


meter.register_valueobserver(
accumulator.register_valueobserver(
callback=get_ram_usage_callback,
name="ram_percent",
description="RAM memory usage",
Expand Down
18 changes: 10 additions & 8 deletions docs/examples/basic_meter/view.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,17 +29,19 @@

# Use the meter type provided by the SDK package
metrics.set_meter_provider(MeterProvider())
meter = metrics.get_meter(__name__)
metrics.get_meter_provider().start_pipeline(meter, ConsoleMetricsExporter(), 5)
accumulator = metrics.get_meter(__name__)
metrics.get_meter_provider().start_pipeline(
accumulator, ConsoleMetricsExporter(), 5
)

requests_counter = meter.create_counter(
requests_counter = accumulator.create_counter(
name="requests",
description="number of requests",
unit="1",
value_type=int,
)

requests_size = meter.create_valuerecorder(
requests_size = accumulator.create_valuerecorder(
name="requests_size",
description="size of requests",
unit="1",
Expand Down Expand Up @@ -85,10 +87,10 @@
# Register the views to the view manager to use the views. Views MUST be
# registered before recording metrics. Metrics that are recorded without
# views defined for them will use a default for that type of metric
meter.register_view(counter_view1)
meter.register_view(counter_view2)
meter.register_view(counter_view3)
meter.register_view(size_view)
accumulator.register_view(counter_view1)
accumulator.register_view(counter_view2)
accumulator.register_view(counter_view3)
accumulator.register_view(size_view)

# The views will evaluate the labels passed into the record and aggregate upon
# the unique labels that are generated
Expand Down
6 changes: 3 additions & 3 deletions docs/examples/opencensus-exporter-metrics/collector.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,10 @@
)

metrics.set_meter_provider(MeterProvider())
meter = metrics.get_meter(__name__)
metrics.get_meter_provider().start_pipeline(meter, exporter, 5)
accumulator = metrics.get_meter(__name__)
metrics.get_meter_provider().start_pipeline(accumulator, exporter, 5)

requests_counter = meter.create_counter(
requests_counter = accumulator.create_counter(
name="requests",
description="number of requests",
unit="1",
Expand Down
6 changes: 3 additions & 3 deletions docs/getting_started/metrics_example.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,13 @@
from opentelemetry.sdk.metrics.export.controller import PushController

metrics.set_meter_provider(MeterProvider())
meter = metrics.get_meter(__name__, True)
accumulator = metrics.get_meter(__name__, True)
exporter = ConsoleMetricsExporter()
controller = PushController(meter, exporter, 5)
controller = PushController(accumulator, exporter, 5)

staging_labels = {"environment": "staging"}

requests_counter = meter.create_counter(
requests_counter = accumulator.create_counter(
name="requests",
description="number of requests",
unit="1",
Expand Down
8 changes: 4 additions & 4 deletions docs/getting_started/otlpcollector_example.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,20 +41,20 @@
# headers=(("metadata", "metadata")),
)

# Meter is responsible for creating and recording metrics
# Accumulator is responsible for creating and recording metrics
metrics.set_meter_provider(MeterProvider())
meter = metrics.get_meter(__name__)
accumulator = metrics.get_meter(__name__)
# controller collects metrics created from meter and exports it via the
# exporter every interval
controller = PushController(meter, metric_exporter, 5)
controller = PushController(accumulator, metric_exporter, 5)

# Configure the tracer to use the collector exporter
tracer = trace.get_tracer_provider().get_tracer(__name__)

with tracer.start_as_current_span("foo"):
print("Hello world!")

requests_counter = meter.create_counter(
requests_counter = accumulator.create_counter(
name="requests",
description="number of requests",
unit="1",
Expand Down
6 changes: 3 additions & 3 deletions docs/getting_started/prometheus_example.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,13 @@

processor_mode = "stateful"
metrics.set_meter_provider(MeterProvider())
meter = metrics.get_meter(__name__, processor_mode == "stateful")
accumulator = metrics.get_meter(__name__, processor_mode == "stateful")
exporter = PrometheusMetricsExporter("MyAppPrefix")
controller = PushController(meter, exporter, 5)
controller = PushController(accumulator, exporter, 5)

staging_labels = {"environment": "staging"}

requests_counter = meter.create_counter(
requests_counter = accumulator.create_counter(
name="requests",
description="number of requests",
unit="1",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@
class TestPrometheusMetricExporter(unittest.TestCase):
def setUp(self):
set_meter_provider(metrics.MeterProvider())
self._meter = get_meter_provider().get_meter(__name__)
self._test_metric = self._meter.create_counter(
self.accumulator = get_meter_provider().get_meter(__name__)
self._test_metric = self.accumulator.create_counter(
"testname", "testdesc", "unit", int,
)
labels = {"environment": "staging"}
Expand Down Expand Up @@ -79,8 +79,8 @@ def test_export(self):
self.assertIs(result, MetricsExportResult.SUCCESS)

def test_min_max_sum_aggregator_to_prometheus(self):
meter = get_meter_provider().get_meter(__name__)
metric = meter.create_valuerecorder(
accumulator = get_meter_provider().get_meter(__name__)
metric = accumulator.create_valuerecorder(
"test@name", "testdesc", "unit", int, []
)
labels = {}
Expand All @@ -100,8 +100,10 @@ def test_min_max_sum_aggregator_to_prometheus(self):
self.assertIn("testprefix_test_name_sum 579.0", result)

def test_counter_to_prometheus(self):
meter = get_meter_provider().get_meter(__name__)
metric = meter.create_counter("test@name", "testdesc", "unit", int,)
accumulator = get_meter_provider().get_meter(__name__)
metric = accumulator.create_counter(
"test@name", "testdesc", "unit", int,
)
labels = {"environment@": "staging", "os": "Windows"}
key_labels = get_dict_as_key(labels)
aggregator = SumAggregator()
Expand Down Expand Up @@ -131,8 +133,8 @@ def test_counter_to_prometheus(self):
# TODO: Add unit test once Measure Aggregators are available

def test_invalid_metric(self):
meter = get_meter_provider().get_meter(__name__)
metric = StubMetric("tesname", "testdesc", "unit", int, meter)
accumulator = get_meter_provider().get_meter(__name__)
metric = StubMetric("tesname", "testdesc", "unit", int, accumulator)
labels = {"environment": "staging"}
key_labels = get_dict_as_key(labels)
record = MetricRecord(
Expand Down Expand Up @@ -161,9 +163,9 @@ def __init__(
description: str,
unit: str,
value_type,
meter,
accumulator,
enabled: bool = True,
):
super().__init__(
name, description, unit, value_type, meter, enabled=enabled,
name, description, unit, value_type, accumulator, enabled=enabled,
)
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,12 @@ def run(self):

def shutdown(self):
self.finished.set()
# Run one more collection pass to flush metrics batched in the meter
# Run one more collection pass to flush metrics batched in the
# accumulator
self.tick()

def tick(self):
# Collect all of the meter's metrics to be exported
# Collect all of the accumulator's metrics to be exported
self.accumulator.collect()
# Export the collected metrics
token = attach(set_value("suppress_instrumentation", True))
Expand Down
32 changes: 16 additions & 16 deletions opentelemetry-sdk/tests/metrics/export/test_export.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,14 @@ class TestConsoleMetricsExporter(unittest.TestCase):
# pylint: disable=no-self-use
def test_export(self):
meter_provider = metrics.MeterProvider()
meter = meter_provider.get_meter(__name__)
accumulator = meter_provider.get_meter(__name__)
exporter = ConsoleMetricsExporter()
metric = metrics.Counter(
"available memory",
"available memory",
"bytes",
int,
meter,
accumulator,
("environment",),
)
labels = {"environment": "staging"}
Expand Down Expand Up @@ -94,11 +94,11 @@ def test_checkpoint_set_empty(self):

def test_finished_collection_stateless(self):
meter_provider = metrics.MeterProvider()
meter = meter_provider.get_meter(__name__)
accumulator = meter_provider.get_meter(__name__)
processor = Processor(False, meter_provider.resource)
aggregator = SumAggregator()
metric = metrics.Counter(
"available memory", "available memory", "bytes", int, meter
"available memory", "available memory", "bytes", int, accumulator
)
aggregator.update(1.0)
labels = ()
Expand All @@ -110,11 +110,11 @@ def test_finished_collection_stateless(self):

def test_finished_collection_stateful(self):
meter_provider = metrics.MeterProvider()
meter = meter_provider.get_meter(__name__)
accumulator = meter_provider.get_meter(__name__)
processor = Processor(True, meter_provider.resource)
aggregator = SumAggregator()
metric = metrics.Counter(
"available memory", "available memory", "bytes", int, meter
"available memory", "available memory", "bytes", int, accumulator
)
aggregator.update(1.0)
labels = ()
Expand All @@ -126,12 +126,12 @@ def test_finished_collection_stateful(self):

def test_processor_process_exists(self):
meter_provider = metrics.MeterProvider()
meter = meter_provider.get_meter(__name__)
accumulator = meter_provider.get_meter(__name__)
processor = Processor(True, meter_provider.resource)
aggregator = SumAggregator()
aggregator2 = SumAggregator()
metric = metrics.Counter(
"available memory", "available memory", "bytes", int, meter
"available memory", "available memory", "bytes", int, accumulator
)
labels = ()
_batch_map = {}
Expand All @@ -148,11 +148,11 @@ def test_processor_process_exists(self):

def test_processor_process_not_exists(self):
meter_provider = metrics.MeterProvider()
meter = meter_provider.get_meter(__name__)
accumulator = meter_provider.get_meter(__name__)
processor = Processor(True, meter_provider.resource)
aggregator = SumAggregator()
metric = metrics.Counter(
"available memory", "available memory", "bytes", int, meter
"available memory", "available memory", "bytes", int, accumulator
)
labels = ()
_batch_map = {}
Expand Down Expand Up @@ -688,29 +688,29 @@ def test_merge_with_empty(self):

class TestController(unittest.TestCase):
def test_push_controller(self):
meter = mock.Mock()
accumulator = mock.Mock()
exporter = mock.Mock()
controller = PushController(meter, exporter, 5.0)
meter.collect.assert_not_called()
controller = PushController(accumulator, exporter, 5.0)
accumulator.collect.assert_not_called()
exporter.export.assert_not_called()

controller.shutdown()
self.assertTrue(controller.finished.isSet())

# shutdown should flush the meter
self.assertEqual(meter.collect.call_count, 1)
self.assertEqual(accumulator.collect.call_count, 1)
self.assertEqual(exporter.export.call_count, 1)

def test_push_controller_suppress_instrumentation(self):
meter = mock.Mock()
accumulator = mock.Mock()
exporter = mock.Mock()
exporter.export = lambda x: self.assertIsNotNone(
get_value("suppress_instrumentation")
)
with mock.patch(
"opentelemetry.context._RUNTIME_CONTEXT"
) as context_patch:
controller = PushController(meter, exporter, 30.0)
controller = PushController(accumulator, exporter, 30.0)
controller.tick()
self.assertEqual(context_patch.attach.called, True)
self.assertEqual(context_patch.detach.called, True)
Expand Down
Loading

0 comments on commit 2edb265

Please sign in to comment.