Skip to content

Commit 78d2181

Browse files
committed
Merge remote-tracking branch 'origin/main'
2 parents d8ecdb5 + ad1fbe9 commit 78d2181

File tree

7 files changed

+223
-12
lines changed

7 files changed

+223
-12
lines changed

CONTRIBUTING.md

+1-11
Original file line numberDiff line numberDiff line change
@@ -45,15 +45,7 @@ during their normal contribution hours.
4545

4646
## Development
4747

48-
To quickly get up and running, you can use the `scripts/eachdist.py` tool that
49-
ships with this project. First create a virtualenv and activate it.
50-
Then run `python scripts/eachdist.py develop` to install all required packages
51-
as well as the project's packages themselves (in `--editable` mode).
52-
53-
You can then run `scripts/eachdist.py test` to test everything or
54-
`scripts/eachdist.py lint` to lint everything (fixing anything that is auto-fixable).
55-
56-
Additionally, this project uses [tox](https://tox.readthedocs.io) to automate
48+
This project uses [tox](https://tox.readthedocs.io) to automate
5749
some aspects of development, including testing against multiple Python versions.
5850
To install `tox`, run:
5951

@@ -104,8 +96,6 @@ The continuation integration overrides that environment variable with as per the
10496

10597
### Benchmarks
10698

107-
Performance progression of benchmarks for packages distributed by OpenTelemetry Python can be viewed as a [graph of throughput vs commit history](https://opentelemetry-python.readthedocs.io/en/latest/performance/benchmarks.html). From the linked page, you can download a JSON file with the performance results.
108-
10999
Running the `tox` tests also runs the performance tests if any are available. Benchmarking tests are done with `pytest-benchmark` and they output a table with results to the console.
110100

111101
To write benchmarks, simply use the [pytest benchmark fixture](https://pytest-benchmark.readthedocs.io/en/latest/usage.html#usage) like the following:

docs/examples/views/README.rst

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
View common scenarios
2+
=====================
3+
4+
These examples show how to customize the metrics that are output by the SDK using Views. There are multiple examples:
5+
6+
* change_aggregation: Shows how to configure to change the default aggregation for an instrument.
7+
* change_name: Shows how to change the name of a metric.
8+
* limit_num_of_attrs: Shows how to limit the number of attributes that are output for a metric.
9+
10+
The source files of these examples are available :scm_web:`here <docs/examples/views/>`.
11+
12+
13+
Installation
14+
------------
15+
16+
.. code-block:: sh
17+
18+
pip install -r requirements.txt
19+
20+
Run the Example
21+
---------------
22+
23+
.. code-block:: sh
24+
25+
python <example_name>.py
26+
27+
The output will be shown in the console.
28+
29+
Useful links
30+
------------
31+
32+
- OpenTelemetry_
33+
- :doc:`../../api/metrics`
34+
35+
.. _OpenTelemetry: https://github.com/open-telemetry/opentelemetry-python/
+53
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
# Copyright The OpenTelemetry Authors
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
import random
16+
import time
17+
18+
from opentelemetry.metrics import get_meter_provider, set_meter_provider
19+
from opentelemetry.sdk.metrics import MeterProvider
20+
from opentelemetry.sdk.metrics.export import (
21+
ConsoleMetricExporter,
22+
PeriodicExportingMetricReader,
23+
)
24+
from opentelemetry.sdk.metrics.view import SumAggregation, View
25+
26+
# Create a view matching the histogram instrument name `http.client.request.latency`
27+
# and configure the `SumAggregation` for the result metrics stream
28+
hist_to_sum_view = View(
29+
instrument_name="http.client.request.latency", aggregation=SumAggregation()
30+
)
31+
32+
# Use console exporter for the example
33+
exporter = ConsoleMetricExporter()
34+
35+
# Create a metric reader with stdout exporter
36+
reader = PeriodicExportingMetricReader(exporter, export_interval_millis=1_000)
37+
provider = MeterProvider(
38+
metric_readers=[
39+
reader,
40+
],
41+
views=[
42+
hist_to_sum_view,
43+
],
44+
)
45+
set_meter_provider(provider)
46+
47+
meter = get_meter_provider().get_meter("view-change-aggregation", "0.1.2")
48+
49+
histogram = meter.create_histogram("http.client.request.latency")
50+
51+
while 1:
52+
histogram.record(99.9)
53+
time.sleep(random.random())

docs/examples/views/change_name.py

+55
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
# Copyright The OpenTelemetry Authors
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
import random
16+
import time
17+
18+
from opentelemetry.metrics import get_meter_provider, set_meter_provider
19+
from opentelemetry.sdk.metrics import Counter, MeterProvider
20+
from opentelemetry.sdk.metrics.export import (
21+
ConsoleMetricExporter,
22+
PeriodicExportingMetricReader,
23+
)
24+
from opentelemetry.sdk.metrics.view import View
25+
26+
# Create a view matching the counter instrument `my.counter`
27+
# and configure the new name `my.counter.total` for the result metrics stream
28+
change_metric_name_view = View(
29+
instrument_type=Counter,
30+
instrument_name="my.counter",
31+
name="my.counter.total",
32+
)
33+
34+
# Use console exporter for the example
35+
exporter = ConsoleMetricExporter()
36+
37+
# Create a metric reader with stdout exporter
38+
reader = PeriodicExportingMetricReader(exporter, export_interval_millis=1_000)
39+
provider = MeterProvider(
40+
metric_readers=[
41+
reader,
42+
],
43+
views=[
44+
change_metric_name_view,
45+
],
46+
)
47+
set_meter_provider(provider)
48+
49+
meter = get_meter_provider().get_meter("view-name-change", "0.1.2")
50+
51+
my_counter = meter.create_counter("my.counter")
52+
53+
while 1:
54+
my_counter.add(random.randint(1, 10))
55+
time.sleep(random.random())
+71
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
# Copyright The OpenTelemetry Authors
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
import random
16+
import time
17+
from typing import Iterable
18+
19+
from opentelemetry.metrics import (
20+
CallbackOptions,
21+
Observation,
22+
get_meter_provider,
23+
set_meter_provider,
24+
)
25+
from opentelemetry.sdk.metrics import MeterProvider, ObservableGauge
26+
from opentelemetry.sdk.metrics.export import (
27+
ConsoleMetricExporter,
28+
PeriodicExportingMetricReader,
29+
)
30+
from opentelemetry.sdk.metrics.view import View
31+
32+
# Create a view matching the observable gauge instrument `observable_gauge`
33+
# and configure the attributes in the result metric stream
34+
# to contain only the attributes with keys with `k_3` and `k_5`
35+
view_with_attributes_limit = View(
36+
instrument_type=ObservableGauge,
37+
instrument_name="observable_gauge",
38+
attribute_keys={"k_3", "k_5"},
39+
)
40+
41+
exporter = ConsoleMetricExporter()
42+
43+
reader = PeriodicExportingMetricReader(exporter, export_interval_millis=1_000)
44+
provider = MeterProvider(
45+
metric_readers=[
46+
reader,
47+
],
48+
views=[
49+
view_with_attributes_limit,
50+
],
51+
)
52+
set_meter_provider(provider)
53+
54+
meter = get_meter_provider().get_meter("reduce-cardinality-with-view", "0.1.2")
55+
56+
57+
def observable_gauge_func(options: CallbackOptions) -> Iterable[Observation]:
58+
attrs = {}
59+
for i in range(random.randint(1, 100)):
60+
attrs[f"k_{i}"] = f"v_{i}"
61+
yield Observation(1, attrs)
62+
63+
64+
# Async gauge
65+
observable_gauge = meter.create_observable_gauge(
66+
"observable_gauge",
67+
[observable_gauge_func],
68+
)
69+
70+
while 1:
71+
time.sleep(1)

docs/examples/views/requirements.txt

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
Deprecated==1.2.13
2+
opentelemetry-api==1.12.0rc2
3+
opentelemetry-sdk==1.12.0rc2
4+
opentelemetry-semantic-conventions==0.32b0
5+
typing_extensions==4.3.0
6+
wrapt==1.14.1

opentelemetry-sdk/src/opentelemetry/sdk/resources/__init__.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -71,8 +71,9 @@
7171
OTEL_SERVICE_NAME,
7272
)
7373
from opentelemetry.semconv.resource import ResourceAttributes
74+
from opentelemetry.util.types import AttributeValue
7475

75-
LabelValue = typing.Union[str, bool, int, float]
76+
LabelValue = AttributeValue
7677
Attributes = typing.Dict[str, LabelValue]
7778
logger = logging.getLogger(__name__)
7879

0 commit comments

Comments
 (0)