Skip to content
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

Metrics export pipeline + metrics stdout exporter #341

Merged
merged 48 commits into from
Feb 11, 2020
Merged
Show file tree
Hide file tree
Changes from 22 commits
Commits
Show all changes
48 commits
Select commit Hold shift + click to select a range
bbfb6f6
metrics
lzchen Dec 13, 2019
da5a9f4
implementation
lzchen Dec 17, 2019
c783e48
comments
lzchen Dec 17, 2019
88bdc57
fix tests
lzchen Dec 18, 2019
ea77627
tests
lzchen Dec 18, 2019
07e5bb5
Merge branch 'master' of https://github.com/open-telemetry/openteleme…
lzchen Dec 19, 2019
b8e4aed
fix stateful logic
lzchen Dec 19, 2019
bb814ec
batcher tests
lzchen Dec 19, 2019
2f44e84
test aggregate
lzchen Dec 19, 2019
e7c8eaa
fix lint
lzchen Dec 26, 2019
248a793
fix lint
lzchen Dec 31, 2019
4ce4ae1
fix lint
lzchen Dec 31, 2019
9be693c
fix lint
lzchen Dec 31, 2019
f0f302e
fix lint
lzchen Dec 31, 2019
8351ef9
address comments
lzchen Jan 7, 2020
4408d94
fix tests
lzchen Jan 7, 2020
9db1540
fix lint
lzchen Jan 7, 2020
cc862b9
Add setters
lzchen Jan 7, 2020
7a5a14d
add examples
lzchen Jan 7, 2020
f9fbd6d
fix lint
lzchen Jan 8, 2020
ba41d38
fix lint
lzchen Jan 8, 2020
5c2b86e
fix lint
lzchen Jan 8, 2020
4e771d6
Address comments
lzchen Jan 15, 2020
3af96b7
Merge branch 'master' of https://github.com/open-telemetry/openteleme…
lzchen Jan 27, 2020
303fefe
LabelSet change
lzchen Jan 28, 2020
fcb46aa
Remove lock implementation from aggregate abc, fix comments, rename c…
lzchen Jan 28, 2020
0ba6611
fix lint
lzchen Jan 28, 2020
35697d4
Fix lint
lzchen Jan 28, 2020
6dfa2b2
Merge branch 'master' of https://github.com/open-telemetry/openteleme…
lzchen Feb 4, 2020
9578dba
Add no-op implementations
lzchen Feb 4, 2020
f4d82e7
Fix examples
lzchen Feb 4, 2020
a7a9c54
re-add default
lzchen Feb 5, 2020
fe44402
Build
lzchen Feb 5, 2020
d011b8e
Merge branch 'master' of https://github.com/open-telemetry/openteleme…
lzchen Feb 5, 2020
6588aee
address comments
lzchen Feb 7, 2020
d99a2a3
Shutdown exporter on exit
lzchen Feb 7, 2020
1c9d44d
fix tests
lzchen Feb 7, 2020
56f68e8
fix test
lzchen Feb 7, 2020
cb51341
black
lzchen Feb 7, 2020
b1bfa38
Address comments
lzchen Feb 8, 2020
ac1aff9
Merge branch 'master' of https://github.com/open-telemetry/openteleme…
lzchen Feb 8, 2020
f135cc8
Make default meter a DefaultMeter
c24t Feb 10, 2020
897a8ba
Merge pull request #7 from c24t/fix-meter-loader-types
lzchen Feb 10, 2020
f73da8d
Fix typing
lzchen Feb 10, 2020
1619575
add sleep
lzchen Feb 10, 2020
6136987
fix units
lzchen Feb 10, 2020
08a5357
Merge branch 'master' of https://github.com/open-telemetry/openteleme…
lzchen Feb 11, 2020
5f68832
Fix lint
lzchen Feb 11, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions docs/opentelemetry.sdk.metrics.export.aggregate.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
opentelemetry.sdk.metrics.export.aggregate
==========================================

.. automodule:: opentelemetry.sdk.metrics.export.aggregate
:members:
:undoc-members:
:show-inheritance:
11 changes: 11 additions & 0 deletions docs/opentelemetry.sdk.metrics.export.batcher.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
opentelemetry.sdk.metrics.export.batcher
==========================================

.. toctree::

opentelemetry.sdk.metrics.export

.. automodule:: opentelemetry.sdk.metrics.export.batcher
:members:
:undoc-members:
:show-inheritance:
7 changes: 7 additions & 0 deletions docs/opentelemetry.sdk.metrics.export.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
opentelemetry.sdk.metrics.export
==========================================

.. automodule:: opentelemetry.sdk.metrics.export
:members:
:undoc-members:
:show-inheritance:
8 changes: 8 additions & 0 deletions docs/opentelemetry.sdk.metrics.rst
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
opentelemetry.sdk.metrics package
==========================================

Submodules
----------

.. toctree::

opentelemetry.sdk.metrics.export.aggregate
opentelemetry.sdk.metrics.export.batcher

.. automodule:: opentelemetry.sdk.metrics
:members:
:undoc-members:
Expand Down
59 changes: 59 additions & 0 deletions examples/metrics/non_stateful.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
# Copyright 2019, OpenTelemetry Authors
lzchen marked this conversation as resolved.
Show resolved Hide resolved
lzchen marked this conversation as resolved.
Show resolved Hide resolved
#
# 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.
#
"""
This module serves as an example for a simple application using metrics
Examples show how to recording affects the collection of metrics to be exported
"""
import time

from opentelemetry import metrics
from opentelemetry.sdk.metrics import Counter, Meter
from opentelemetry.sdk.metrics.export import ConsoleMetricsExporter
from opentelemetry.sdk.metrics.export.batcher import UngroupedBatcher
from opentelemetry.sdk.metrics.export.controller import PushController

# Batcher used to collect all created metrics from meter ready for exporting
# Pass in false for non-stateful batcher. Indicates the batcher computes
# checkpoints which describe the updates of a single collection period (deltas)
batcher = UngroupedBatcher(False)
# Meter is responsible for creating and recording metrics
meter = Meter(batcher)
metrics.set_preferred_meter_implementation(meter)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FYI this example is broken now, possibly as a result of #311.

Traceback (most recent call last):
  File "examples/opentelemetry-example-app/src/opentelemetry_example_app/metrics_example.py", line 40, in <module>
    counter.add(25, label_set)
AttributeError: 'DefaultMetric' object has no attribute 'add'

loader.load_impl expects api_type to be the type of object factory produces (i.e. Meter) and a the concrete type of object to create (i.e. DefaultMeter) if the factory is or returns null.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually it's metrics_example.py that's broken for this reason. This example has some other problems:

set_preferred_meter_implementation takes a factory, not a meter instance. This only doesn't cause an error here because this example doesn't call metrics.meter(), which should fail with a TypeError.

This example also creates a Meter directly instead of using metrics.meter(), which users shouldn't generally do.

Copy link
Contributor Author

@lzchen lzchen Feb 4, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added no-op implementations of the metric and handle functions to the default classes so user's who don't have the meter loaded would not crash.

I agree the usage should be set_preferred_metered_implementation(lambda _: Meter()). However this breaks because api_type is DefaultMeter here but fails the type check in loader because Meter is not of type DefaultMeter.

I don't want to include too many loader changes in this PR. As of now, the examples would not work because the loader doesn't seem to be addressing use case.

# exporter to export metrics to the console
exporter = ConsoleMetricsExporter()
# controller collects metrics created from meter and exports it via the
# exporter every interval
controller = PushController(meter, exporter, 5)
mauriciovasquezbernal marked this conversation as resolved.
Show resolved Hide resolved

counter = meter.create_metric(
"available memory",
lzchen marked this conversation as resolved.
Show resolved Hide resolved
"available memory",
"bytes",
int,
Counter,
("environment",),
)

label_set = meter.get_label_set({"environment": "staging"})

counter.add(25, label_set)
# We sleep for 5 seconds, exported value should be 25
time.sleep(5)

counter.add(50, label_set)
# exported value should be 50 due to non-stateful batcher
time.sleep(5)
lzchen marked this conversation as resolved.
Show resolved Hide resolved

# Following exported values would be 0
68 changes: 68 additions & 0 deletions examples/metrics/record.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
# Copyright 2019, 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.
#
"""
This module serves as an example for a simple application using metrics.
It demonstrates the different ways you can record metrics via the meter.
"""

from opentelemetry import metrics
from opentelemetry.sdk.metrics import Counter, Meter
from opentelemetry.sdk.metrics.export import ConsoleMetricsExporter
from opentelemetry.sdk.metrics.export.controller import PushController

# Meter is responsible for creating and recording metrics
meter = Meter()
metrics.set_preferred_meter_implementation(meter)
# exporter to export metrics to the console
exporter = ConsoleMetricsExporter()
# controller collects metrics created from meter and exports it via the
# exporter every interval
controller = PushController(meter, exporter, 5)

# Example to show how to record using the meter
counter = meter.create_metric(
"available memory",
"available memory",
"bytes",
int,
Counter,
("environment",),
)

# Labelsets are used to identify key-values that are associated with a specific
lzchen marked this conversation as resolved.
Show resolved Hide resolved
# metric that you want to record. These are useful for pre-aggregation and can
# be used to store custom dimensions pertaining to a metric

# The meter takes a dictionary of key value pairs
label_set = meter.get_label_set({"environment": "staging"})

# Handle usage
# You can record metrics with metric handles. Handles are created by passing in
# a labelset. A handle is essentially metric data that corresponds to a specific
# set of labels. Therefore, getting a handle using the same set of labels will
# yield the same metric handle.
counter_handle = counter.get_handle(label_set)
counter_handle.add(100)

# Direct metric usage
# You can record metrics directly using the metric instrument. You pass in a
# labelset that you would like to record for.
counter.add(25, label_set)

# Record batch usage
# You can record metrics in a batch by passing in a labelset and a sequence of
# (metric, value) pairs. The value would be recorded for each metric using the
# specified labelset for each.
meter.record_batch(label_set, [(counter, 50)])
lzchen marked this conversation as resolved.
Show resolved Hide resolved
79 changes: 79 additions & 0 deletions examples/metrics/stateful.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
# Copyright 2019, 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.
#
"""
This module serves as an example for a simple application using metrics
Examples show how to recording affects the collection of metrics to be exported
"""
import time

from opentelemetry import metrics
from opentelemetry.sdk.metrics import Counter, Meter
from opentelemetry.sdk.metrics.export import ConsoleMetricsExporter
from opentelemetry.sdk.metrics.export.batcher import UngroupedBatcher
from opentelemetry.sdk.metrics.export.controller import PushController

# Batcher used to collect all created metrics from meter ready for exporting
# Pass in true/false to indicate whether the batcher is stateful. True
# indicates the batcher computes checkpoints from over the process lifetime.
# False indicates the batcher computes checkpoints which describe the updates
# of a single collection period (deltas)
batcher = UngroupedBatcher(True)
# If a batcher is not provded, a default batcher is used
# Meter is responsible for creating and recording metrics
meter = Meter(batcher)
metrics.set_preferred_meter_implementation(meter)
# exporter to export metrics to the console
exporter = ConsoleMetricsExporter()
# controller collects metrics created from meter and exports it via the
# exporter every interval
controller = PushController(meter, exporter, 5)

counter = meter.create_metric(
"available memory",
"available memory",
"bytes",
int,
Counter,
("environment",),
)

counter2 = meter.create_metric(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is the "stateful" example different from the "non-stateful" one in ways other than batcher state?

If the goal is to compare stateful and non-stateful batchers, what do you think about combining these examples to show only that?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Combining the two examples will require metrics to load a different implementation of Meter since a Meter instance is initialized with a certain Batcher. The second example can be run after the first sequentially but the example file would get huge and quite confusing to read. I'd prefer to separate the two examples.

"available memory2",
"available memory2",
"bytes2",
int,
Counter,
("environment",),
)

label_set = meter.get_label_set({"environment": "staging"})
label_set2 = meter.get_label_set({"environment": "testing"})

counter.add(25, label_set)
# We sleep for 5 seconds, exported value should be 25
time.sleep(5)

counter.add(50, label_set)
# exported value should be 75
time.sleep(5)

counter.add(35, label_set2)
# should be two exported values 75 and 35, one for each labelset
time.sleep(5)

counter2.add(5, label_set)
# should be three exported values, labelsets can be reused for different
# metrics but will be recorded seperately, 75, 35 and 5
time.sleep(5)
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,12 @@

from opentelemetry import metrics
from opentelemetry.sdk.metrics import Counter, Meter
from opentelemetry.sdk.metrics.export import ConsoleMetricsExporter
from opentelemetry.sdk.metrics.export.batcher import UngroupedBatcher
from opentelemetry.sdk.metrics.export.controller import PushController

metrics.set_preferred_meter_implementation(lambda _: Meter())
batcher = UngroupedBatcher(True)
metrics.set_preferred_meter_implementation(lambda _: Meter(batcher))
meter = metrics.meter()
counter = meter.create_metric(
"available memory",
Expand All @@ -33,14 +37,14 @@
label_set = meter.get_label_set({"environment": "staging"})

# Direct metric usage
counter.add(label_set, 25)
counter.add(25, label_set)

# Handle usage
counter_handle = counter.get_handle(label_set)
counter_handle.add(100)

# Record batch usage
meter.record_batch(label_set, [(counter, 50)])
print(counter_handle.data)

# TODO: exporters
exporter = ConsoleMetricsExporter()
lzchen marked this conversation as resolved.
Show resolved Hide resolved
controller = PushController(meter, exporter, 5)
19 changes: 11 additions & 8 deletions opentelemetry-api/src/opentelemetry/metrics/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -128,12 +128,12 @@ def get_handle(self, label_set: LabelSet) -> "CounterHandle":
"""Gets a `CounterHandle`."""
return CounterHandle()

def add(self, label_set: LabelSet, value: ValueT) -> None:
def add(self, value: ValueT, label_set: LabelSet) -> None:
"""Increases the value of the counter by ``value``.

Args:
label_set: `LabelSet` to associate with the returned handle.
value: The value to add to the counter metric.
label_set: `LabelSet` to associate with the returned handle.
"""


Expand All @@ -150,12 +150,12 @@ def get_handle(self, label_set: LabelSet) -> "GaugeHandle":
"""Gets a `GaugeHandle`."""
return GaugeHandle()

def set(self, label_set: LabelSet, value: ValueT) -> None:
def set(self, value: ValueT, label_set: LabelSet) -> None:
"""Sets the value of the gauge to ``value``.

Args:
label_set: `LabelSet` to associate with the returned handle.
value: The value to set the gauge metric to.
label_set: `LabelSet` to associate with the returned handle.
"""


Expand All @@ -171,12 +171,12 @@ def get_handle(self, label_set: LabelSet) -> "MeasureHandle":
"""Gets a `MeasureHandle` with a float value."""
return MeasureHandle()

def record(self, label_set: LabelSet, value: ValueT) -> None:
def record(self, value: ValueT, label_set: LabelSet) -> None:
"""Records the ``value`` to the measure.

Args:
label_set: `LabelSet` to associate with the returned handle.
value: The value to record to this measure metric.
label_set: `LabelSet` to associate with the returned handle.
"""


Expand Down Expand Up @@ -221,6 +221,7 @@ def create_metric(
label_keys: Sequence[str] = (),
enabled: bool = True,
monotonic: bool = False,
absolute: bool = True,
) -> "Metric":
"""Creates a ``metric_kind`` metric with type ``value_type``.

Expand All @@ -232,8 +233,10 @@ def create_metric(
metric_type: The type of metric being created.
label_keys: The keys for the labels with dynamic values.
enabled: Whether to report the metric by default.
monotonic: Whether to only allow non-negative values.

monotonic: Configure a counter or gauge that accepts only
monotonic/non-monotonic updates.
absolute: Configure a measure that does or does not accept negative
updates.
Returns: A new ``metric_type`` metric with values of ``value_type``.
"""
# pylint: disable=no-self-use
Expand Down
6 changes: 3 additions & 3 deletions opentelemetry-api/tests/metrics/test_metrics.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ def test_counter(self):
def test_counter_add(self):
counter = metrics.Counter()
label_set = metrics.LabelSet()
counter.add(label_set, 1)
counter.add(1, label_set)

def test_gauge(self):
gauge = metrics.Gauge()
Expand All @@ -63,7 +63,7 @@ def test_gauge(self):
def test_gauge_set(self):
gauge = metrics.Gauge()
label_set = metrics.LabelSet()
gauge.set(label_set, 1)
gauge.set(1, label_set)

def test_measure(self):
measure = metrics.Measure()
Expand All @@ -74,7 +74,7 @@ def test_measure(self):
def test_measure_record(self):
measure = metrics.Measure()
label_set = metrics.LabelSet()
measure.record(label_set, 1)
measure.record(1, label_set)

def test_default_handle(self):
metrics.DefaultMetricHandle()
Expand Down
Loading