Skip to content

Commit

Permalink
examples/metrics: miscellaneous updates
Browse files Browse the repository at this point in the history
- use explicit parameter names to make it more clear
- fix comments (actually remove them) in simple_example
  • Loading branch information
mauriciovasquezbernal committed Feb 19, 2020
1 parent 2e8cf8d commit bc26ac4
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 22 deletions.
23 changes: 14 additions & 9 deletions examples/metrics/observer_example.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
from opentelemetry.sdk.metrics.export.controller import PushController

# Configure a stateful batcher
batcher = UngroupedBatcher(True)
batcher = UngroupedBatcher(stateful=True)

metrics.set_preferred_meter_implementation(lambda _: Meter(batcher))
meter = metrics.meter()
Expand All @@ -34,7 +34,7 @@
exporter = ConsoleMetricsExporter()

# Configure a push controller
controller = PushController(meter, exporter, 2)
controller = PushController(meter=meter, exporter=exporter, interval=2)


# Callback to gather cpu usage
Expand All @@ -45,12 +45,12 @@ def get_cpu_usage_callback(observer):


meter.register_observer(
get_cpu_usage_callback,
"cpu_percent",
"per-cpu usage",
1,
float,
("cpu_number",),
callback=get_cpu_usage_callback,
name="cpu_percent",
description="per-cpu usage",
unit="1",
value_type=float,
label_keys=("cpu_number",),
)


Expand All @@ -61,7 +61,12 @@ def get_ram_usage_callback(observer):


meter.register_observer(
get_ram_usage_callback, "ram_percent", "RAM memory usage", 1, float, (),
callback=get_ram_usage_callback,
name="ram_percent",
description="RAM memory usage",
unit="1",
value_type=float,
label_keys=(),
)

input("Press a key to finish...\n")
16 changes: 13 additions & 3 deletions examples/metrics/record.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,25 @@
exporter = ConsoleMetricsExporter()
# controller collects metrics created from meter and exports it via the
# exporter every interval
controller = PushController(meter, exporter, 5)
controller = PushController(meter=meter, exporter=exporter, interval=5)

# Example to show how to record using the meter
counter = meter.create_metric(
"requests", "number of requests", 1, int, Counter, ("environment",)
name="requests",
description="number of requests",
unit="1",
value_type=int,
metric_type=Counter,
label_keys=("environment",),
)

counter2 = meter.create_metric(
"clicks", "number of clicks", 1, int, Counter, ("environment",)
name="clicks",
description="number of clicks",
unit="1",
value_type=int,
metric_type=Counter,
label_keys=("environment",),
)

# Labelsets are used to identify key-values that are associated with a specific
Expand Down
31 changes: 21 additions & 10 deletions examples/metrics/simple_example.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
from opentelemetry.sdk.metrics.export.controller import PushController

batcher_mode = "stateful"
stateful_bacher = False


def usage(argv):
Expand All @@ -50,7 +51,8 @@ def usage(argv):
# lifetime.
# False indicates the batcher computes checkpoints which describe the updates
# of a single collection period (deltas)
batcher = UngroupedBatcher(batcher_mode == "stateful")
stateful_bacher = batcher_mode == "stateful"
batcher = UngroupedBatcher(stateful=stateful_bacher)

# If a batcher is not provided, a default batcher is used
# Meter is responsible for creating and recording metrics
Expand All @@ -66,15 +68,30 @@ def usage(argv):

# Metric instruments allow to capture measurements
requests_counter = meter.create_metric(
"requests", "number of requests", 1, int, Counter, ("environment",)
name="requests",
description="number of requests",
unit="1",
value_type=int,
metric_type=Counter,
label_keys=("environment",),
)

clicks_counter = meter.create_metric(
"clicks", "number of clicks", 1, int, Counter, ("environment",)
name="clicks",
description="number of clicks",
unit="1",
value_type=int,
metric_type=Counter,
label_keys=("environment",),
)

requests_size = meter.create_metric(
"requests_size", "size of requests", 1, int, Measure, ("environment",)
name="requests_size",
description="size of requests",
unit="1",
value_type=int,
metric_type=Measure,
label_keys=("environment",),
)

# Labelsets are used to identify key-values that are associated with a specific
Expand All @@ -86,21 +103,15 @@ def usage(argv):
# Update the metric instruments using the direct calling convention
requests_size.record(100, staging_label_set)
requests_counter.add(25, staging_label_set)
# Sleep for 5 seconds, exported value should be 25
time.sleep(5)

requests_size.record(5000, staging_label_set)
requests_counter.add(50, staging_label_set)
# Exported value should be 75
time.sleep(5)

requests_size.record(2, testing_label_set)
requests_counter.add(35, testing_label_set)
# There should be two exported values 75 and 35, one for each labelset
time.sleep(5)

clicks_counter.add(5, staging_label_set)
# There should be three exported values, labelsets can be reused for different
# metrics but will be recorded seperately, 75, 35 and 5

time.sleep(5)

0 comments on commit bc26ac4

Please sign in to comment.