forked from open-telemetry/opentelemetry-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fixes open-telemetry#1835
- Loading branch information
Showing
24 changed files
with
2,052 additions
and
6 deletions.
There are no files selected for viewing
This file contains 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 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 |
---|---|---|
|
@@ -9,4 +9,5 @@ OpenTelemetry Python API | |
baggage | ||
context | ||
trace | ||
metrics | ||
environment_variables |
This file contains 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,10 @@ | ||
Grocery | ||
======= | ||
|
||
This is the implementation of the grocery scenario. | ||
|
||
|
||
Useful links | ||
------------ | ||
|
||
.. _Grocery: https://github.com/open-telemetry/oteps/blob/main/text/metrics/0146-metrics-prototype-scenarios.md#scenario-1-grocery |
This file contains 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,94 @@ | ||
# Copyright The 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. | ||
|
||
from opentelemetry.metrics import get_meter_provider | ||
from opentelemetry.sdk.metrics.export import ConsoleExporter | ||
|
||
exporter = ConsoleExporter() | ||
|
||
get_meter_provider().start_pipeline_equivalent(exporter, 5) | ||
|
||
meter = get_meter_provider().get_meter() | ||
|
||
|
||
order_counter = meter.create_counter( | ||
name="orders", | ||
description="number of orders", | ||
unit="order", | ||
value_type=int, | ||
) | ||
|
||
amount_counter = meter.create_counter( | ||
name="amount", | ||
description="amount paid", | ||
unit="dollar", | ||
value_type=int, | ||
) | ||
|
||
sold_items_counter = meter.create_counter( | ||
name="sold items", | ||
description="number of sold items", | ||
unit="item", | ||
value_type=int, | ||
) | ||
|
||
customers_in_store = meter.create_up_down_counter( | ||
name="customers in store", | ||
description="amount of customers present in store", | ||
unit="customer", | ||
value_type=int, | ||
) | ||
|
||
|
||
class GroceryStore: | ||
def __init__(self, name): | ||
self._name = name | ||
|
||
def process_order(self, customer_name, potatoes=0, tomatoes=0): | ||
order_counter.add(1, {"store": self._name, "customer": customer_name}) | ||
|
||
amount_counter.add( | ||
(potatoes * 1) + (tomatoes * 3), | ||
{"store": self._name, "customer": customer_name}, | ||
) | ||
|
||
sold_items_counter.add( | ||
potatoes, | ||
{ | ||
"store": self._name, | ||
"customer": customer_name, | ||
"item": "potato", | ||
}, | ||
) | ||
|
||
sold_items_counter.add( | ||
tomatoes, | ||
{ | ||
"store": self._name, | ||
"customer": customer_name, | ||
"item": "tomato", | ||
}, | ||
) | ||
|
||
def enter_customer(self, customer_name, account_type): | ||
|
||
customers_in_store.add( | ||
1, {"store": self._name, "account_type": account_type} | ||
) | ||
|
||
def exit_customer(self, customer_name, account_type): | ||
|
||
customers_in_store.add( | ||
-1, {"store": self._name, "account_type": account_type} | ||
) |
This file contains 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 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 |
---|---|---|
|
@@ -8,5 +8,6 @@ OpenTelemetry Python SDK | |
|
||
resources | ||
trace | ||
metrics | ||
error_handler | ||
environment_variables |
This file contains 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 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
140 changes: 140 additions & 0 deletions
140
opentelemetry-api/src/opentelemetry/metrics/instrument.py
This file contains 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,140 @@ | ||
# Copyright The 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. | ||
|
||
# pylint: disable=abstract-class-instantiated | ||
# pylint: disable=too-many-ancestors | ||
# pylint: disable=useless-super-delegation | ||
|
||
|
||
from abc import ABC, abstractmethod | ||
from logging import getLogger | ||
from re import ASCII | ||
from re import compile as compile_ | ||
from typing import Callable | ||
|
||
_logger = getLogger(__name__) | ||
|
||
|
||
class Instrument(ABC): | ||
|
||
_name_regex = compile_(r"[a-zA-Z][-.\w]{0,62}", ASCII) | ||
|
||
@abstractmethod | ||
def __init__(self, name, unit="", description=""): | ||
|
||
if self._name_regex.fullmatch(name) is None: | ||
raise Exception("Invalid instrument name {}".format(name)) | ||
|
||
if len(unit) > 63 or any(ord(character) > 127 for character in unit): | ||
raise Exception("unit must be 63 characters or shorter") | ||
|
||
|
||
class Synchronous(Instrument): | ||
pass | ||
|
||
|
||
class Asynchronous(Instrument): | ||
@abstractmethod | ||
def __init__(self, name, callback, unit="", description=""): | ||
|
||
if not isinstance(callback, Callable): | ||
raise Exception("callback must be callable") | ||
|
||
super().__init__(name, unit=unit, description=description) | ||
|
||
|
||
class Adding(Instrument): | ||
pass | ||
|
||
|
||
class Grouping(Instrument): | ||
pass | ||
|
||
|
||
class Monotonic(Adding): | ||
pass | ||
|
||
|
||
class NonMonotonic(Adding): | ||
pass | ||
|
||
|
||
class Counter(Monotonic, Synchronous): | ||
@abstractmethod | ||
def add(self, amount, **attributes): | ||
if amount < 0: | ||
raise Exception("Amount must be non-negative") | ||
|
||
|
||
class DefaultCounter(Counter): | ||
def __init__(self, name, unit="", description=""): | ||
super().__init__(name, unit=unit, description=description) | ||
|
||
def add(self, amount, **attributes): | ||
return super().add(amount, **attributes) | ||
|
||
|
||
class UpDownCounter(NonMonotonic, Synchronous): | ||
@abstractmethod | ||
def add(self, amount, **attributes): | ||
pass | ||
|
||
|
||
class DefaultUpDownCounter(UpDownCounter): | ||
def __init__(self, name, unit="", description=""): | ||
super().__init__(name, unit=unit, description=description) | ||
|
||
def add(self, amount, **attributes): | ||
return super().add(amount, **attributes) | ||
|
||
|
||
class ObservableCounter(Monotonic, Asynchronous): | ||
pass | ||
|
||
|
||
class DefaultObservableCounter(ObservableCounter): | ||
def __init__(self, name, callback, unit="", description=""): | ||
super().__init__(name, callback, unit=unit, description=description) | ||
|
||
|
||
class ObservableUpDownCounter(NonMonotonic, Asynchronous): | ||
pass | ||
|
||
|
||
class DefaultObservableUpDownCounter(ObservableUpDownCounter): | ||
def __init__(self, name, callback, unit="", description=""): | ||
super().__init__(name, callback, unit=unit, description=description) | ||
|
||
|
||
class Histogram(Grouping, Synchronous): | ||
@abstractmethod | ||
def record(self, amount, **attributes): | ||
pass | ||
|
||
|
||
class DefaultHistogram(Histogram): | ||
def __init__(self, name, unit="", description=""): | ||
super().__init__(name, unit=unit, description=description) | ||
|
||
def record(self, amount, **attributes): | ||
return super().record(amount, **attributes) | ||
|
||
|
||
class ObservableGauge(Grouping, Asynchronous): | ||
pass | ||
|
||
|
||
class DefaultObservableGauge(ObservableGauge): | ||
def __init__(self, name, callback, unit="", description=""): | ||
super().__init__(name, callback, unit=unit, description=description) |
Oops, something went wrong.