-
Notifications
You must be signed in to change notification settings - Fork 766
Implement observer instrument #425
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
Merged
toumorokoshi
merged 13 commits into
open-telemetry:master
from
kinvolk:mauricio/implement-observer
Mar 2, 2020
Merged
Changes from 10 commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
f4b38b3
sdk/metrics/batcher: fix aggregator_for
mauriciovasquezbernal 2b09671
sdk/metrics: implement MinMaxSumCount aggregator
mauriciovasquezbernal 7b8ff91
example/metrics: combine into a single file and add Measure example
mauriciovasquezbernal bc99f48
sdk/metrics: implement MinMaxSumCount aggregator
mauriciovasquezbernal be2f029
metrics: Remove Gauge instrument
mauriciovasquezbernal c6a5c3c
Add Observer metric instrument
mauriciovasquezbernal 2e8cf8d
Merge branch 'master' into mauricio/implement-observer
mauriciovasquezbernal bc26ac4
examples/metrics: miscellaneous updates
mauriciovasquezbernal 3f8265d
clarify units parameter
mauriciovasquezbernal 7436362
add unit descriptions for observers
mauriciovasquezbernal df01d09
use ABC for observer
mauriciovasquezbernal 7ca709d
Merge branch 'master' into mauricio/implement-observer
mauriciovasquezbernal f56e583
fix tests
mauriciovasquezbernal File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,72 @@ | ||
| # Copyright 2020, 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 example shows how the Observer metric instrument can be used to capture | ||
| asynchronous metrics data. | ||
| """ | ||
| import psutil | ||
|
|
||
| from opentelemetry import metrics | ||
| from opentelemetry.sdk.metrics import LabelSet, Meter | ||
| from opentelemetry.sdk.metrics.export import ConsoleMetricsExporter | ||
| from opentelemetry.sdk.metrics.export.batcher import UngroupedBatcher | ||
| from opentelemetry.sdk.metrics.export.controller import PushController | ||
|
|
||
| # Configure a stateful batcher | ||
| batcher = UngroupedBatcher(stateful=True) | ||
|
|
||
| metrics.set_preferred_meter_implementation(lambda _: Meter(batcher)) | ||
| meter = metrics.meter() | ||
|
|
||
| # Exporter to export metrics to the console | ||
| exporter = ConsoleMetricsExporter() | ||
|
|
||
| # Configure a push controller | ||
| controller = PushController(meter=meter, exporter=exporter, interval=2) | ||
|
|
||
|
|
||
| # Callback to gather cpu usage | ||
| def get_cpu_usage_callback(observer): | ||
| for (number, percent) in enumerate(psutil.cpu_percent(percpu=True)): | ||
| label_set = meter.get_label_set({"cpu_number": str(number)}) | ||
| observer.observe(percent, label_set) | ||
|
|
||
|
|
||
| meter.register_observer( | ||
| callback=get_cpu_usage_callback, | ||
| name="cpu_percent", | ||
| description="per-cpu usage", | ||
| unit="1", | ||
| value_type=float, | ||
| label_keys=("cpu_number",), | ||
| ) | ||
|
|
||
|
|
||
| # Callback to gather RAM memory usage | ||
| def get_ram_usage_callback(observer): | ||
| ram_percent = psutil.virtual_memory().percent | ||
| observer.observe(ram_percent, LabelSet()) | ||
|
|
||
|
|
||
| meter.register_observer( | ||
| 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") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.