Skip to content

Commit 0b3fbe0

Browse files
committed
Revert "Remove metrics from main branch (#1568)"
This reverts commit 24edd3d.
1 parent 24edd3d commit 0b3fbe0

File tree

95 files changed

+7158
-34
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

95 files changed

+7158
-34
lines changed

.github/workflows/test.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ env:
1010
# Otherwise, set variable to the commit of your branch on
1111
# opentelemetry-python-contrib which is compatible with these Core repo
1212
# changes.
13-
CONTRIB_REPO_SHA: 43df76e5ed69f45d993c98ea68daea3c4622ea2d
13+
CONTRIB_REPO_SHA: b404de2f393aaaeca73694c37fe58fecf423a707
1414

1515
jobs:
1616
build:

CHANGELOG.md

-2
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,6 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
2323
### Removed
2424
- Remove Configuration
2525
([#1523](https://github.com/open-telemetry/opentelemetry-python/pull/1523))
26-
- Remove Metrics as part of stable, marked as experimental
27-
([#1568](https://github.com/open-telemetry/opentelemetry-python/pull/1568))
2826

2927
## [0.17b0](https://github.com/open-telemetry/opentelemetry-python/releases/tag/v0.17b0) - 2021-01-20
3028

CONTRIBUTING.md

-4
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,6 @@ Please take a look at this list first, your contributions may belong in one of t
2323
programmatic instrumentations that are now in the `ext` directory in the main OpenTelemetry repo. Please ask in the Gitter
2424
channel (see below) for guidance if you want to contribute with these instrumentations.
2525

26-
# Find the right branch
27-
28-
The default branch for this repo is `main`. Changes that pertain to components marked as `stable` in the [specifications](https://github.com/open-telemetry/opentelemetry-specification) go into this branch, which currently does not include `metrics`. Changes that pertain to `metrics` go into the `metrics` branch.
29-
3026
## Find a Buddy and get Started Quickly!
3127

3228
If you are looking for someone to help you find a starting point and be a resource for your first contribution, join our

docs/api/api.rst

+1
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,6 @@ OpenTelemetry Python API
88

99
baggage
1010
context
11+
metrics
1112
trace
1213
environment_variables

docs/api/metrics.rst

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
opentelemetry.metrics package
2+
=============================
3+
4+
Module contents
5+
---------------
6+
7+
.. automodule:: opentelemetry.metrics

docs/examples/auto-instrumentation/README.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ and run the following command instead:
151151

152152
.. code:: sh
153153
154-
$ opentelemetry-instrument -e console_span python server_uninstrumented.py
154+
$ opentelemetry-instrument -e console_span,console_metrics python server_uninstrumented.py
155155
156156
In the console where you previously executed ``client.py``, run the following
157157
command again:

docs/examples/basic_meter/README.rst

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
Basic Meter
2+
===========
3+
4+
These examples show how to use OpenTelemetry to capture and report metrics.
5+
6+
There are three different examples:
7+
8+
* basic_metrics: Shows how to create a metric instrument, how to configure an
9+
exporter and a controller and also how to capture data by using the direct
10+
calling convention.
11+
12+
* calling_conventions: Shows how to use the direct, bound and batch calling conventions.
13+
14+
* observer: Shows how to use the observer instrument.
15+
16+
The source files of these examples are available :scm_web:`here <docs/examples/basic_meter/>`.
17+
18+
Installation
19+
------------
20+
21+
.. code-block:: sh
22+
23+
pip install opentelemetry-api
24+
pip install opentelemetry-sdk
25+
pip install psutil # needed to get ram and cpu usage in the observer example
26+
27+
Run the Example
28+
---------------
29+
30+
.. code-block:: sh
31+
32+
python <example_name>.py
33+
34+
The output will be shown in the console after few seconds.
35+
36+
Useful links
37+
------------
38+
39+
- OpenTelemetry_
40+
- :doc:`../../api/metrics`
41+
42+
.. _OpenTelemetry: https://github.com/open-telemetry/opentelemetry-python/
+87
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
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+
"""
16+
This module serves as an example for a simple application using metrics.
17+
18+
It shows:
19+
- How to configure a meter passing a stateful or stateless.
20+
- How to configure an exporter and how to create a controller.
21+
- How to create some metrics instruments and how to capture data with them.
22+
- How to use views to specify aggregation types for each metric instrument.
23+
"""
24+
import sys
25+
import time
26+
27+
from opentelemetry import metrics
28+
from opentelemetry.sdk.metrics import MeterProvider
29+
from opentelemetry.sdk.metrics.export import ConsoleMetricsExporter
30+
31+
print(
32+
"Starting example, values will be printed to the console every 5 seconds."
33+
)
34+
35+
# Stateful determines whether how metrics are collected: if true, metrics
36+
# accumulate over the process lifetime. If false, metrics are reset at the
37+
# beginning of each collection interval.
38+
stateful = True
39+
40+
# Sets the global MeterProvider instance
41+
metrics.set_meter_provider(MeterProvider())
42+
43+
# The Meter is responsible for creating and recording metrics. Each meter has a
44+
# unique name, which we set as the module's name here.
45+
meter = metrics.get_meter(__name__)
46+
47+
# Exporter to export metrics to the console
48+
exporter = ConsoleMetricsExporter()
49+
50+
# start_pipeline will notify the MeterProvider to begin collecting/exporting
51+
# metrics with the given meter, exporter and interval in seconds
52+
metrics.get_meter_provider().start_pipeline(meter, exporter, 5)
53+
54+
# Metric instruments allow to capture measurements
55+
requests_counter = meter.create_counter(
56+
name="requests",
57+
description="number of requests",
58+
unit="1",
59+
value_type=int,
60+
)
61+
62+
requests_size = meter.create_valuerecorder(
63+
name="requests_size",
64+
description="size of requests",
65+
unit="1",
66+
value_type=int,
67+
)
68+
69+
# Labels are used to identify key-values that are associated with a specific
70+
# metric that you want to record. These are useful for pre-aggregation and can
71+
# be used to store custom dimensions pertaining to a metric
72+
staging_labels = {"environment": "staging"}
73+
testing_labels = {"environment": "testing"}
74+
75+
# Update the metric instruments using the direct calling convention
76+
requests_counter.add(25, staging_labels)
77+
requests_size.record(100, staging_labels)
78+
time.sleep(10)
79+
80+
requests_counter.add(50, staging_labels)
81+
requests_size.record(5000, staging_labels)
82+
time.sleep(5)
83+
84+
requests_counter.add(35, testing_labels)
85+
requests_size.record(2, testing_labels)
86+
87+
input("...\n")
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
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+
"""
16+
This example shows how to use the different modes to capture metrics.
17+
It shows the usage of the direct, bound and batch calling conventions.
18+
"""
19+
import time
20+
21+
from opentelemetry import metrics
22+
from opentelemetry.sdk.metrics import MeterProvider
23+
from opentelemetry.sdk.metrics.export import ConsoleMetricsExporter
24+
25+
# Use the meter type provided by the SDK package
26+
metrics.set_meter_provider(MeterProvider())
27+
meter = metrics.get_meter(__name__)
28+
metrics.get_meter_provider().start_pipeline(meter, ConsoleMetricsExporter(), 5)
29+
30+
requests_counter = meter.create_counter(
31+
name="requests",
32+
description="number of requests",
33+
unit="1",
34+
value_type=int,
35+
)
36+
37+
clicks_counter = meter.create_counter(
38+
name="clicks", description="number of clicks", unit="1", value_type=int,
39+
)
40+
41+
labels = {"environment": "staging"}
42+
43+
print("Updating using direct calling convention...")
44+
# You can record metrics directly using the metric instrument. You pass in
45+
# labels that you would like to record for.
46+
requests_counter.add(25, labels)
47+
time.sleep(10)
48+
49+
print("Updating using a bound instrument...")
50+
# You can record metrics with bound metric instruments. Bound metric
51+
# instruments are created by passing in labels. A bound metric instrument
52+
# is essentially metric data that corresponds to a specific set of labels.
53+
# Therefore, getting a bound metric instrument using the same set of labels
54+
# will yield the same bound metric instrument.
55+
bound_requests_counter = requests_counter.bind(labels)
56+
bound_requests_counter.add(100)
57+
time.sleep(5)
58+
59+
print("Updating using batch calling convention...")
60+
# You can record metrics in a batch by passing in labels and a sequence of
61+
# (metric, value) pairs. The value would be recorded for each metric using the
62+
# specified labels for each.
63+
meter.record_batch(labels, ((requests_counter, 50), (clicks_counter, 70)))
64+
time.sleep(5)
65+
66+
input("...\n")
+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
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+
"""
16+
This module shows how you can enable collection and exporting of http metrics
17+
related to instrumentations.
18+
"""
19+
import requests
20+
21+
from opentelemetry import metrics
22+
from opentelemetry.instrumentation.requests import RequestsInstrumentor
23+
from opentelemetry.sdk.metrics import MeterProvider
24+
from opentelemetry.sdk.metrics.export import ConsoleMetricsExporter
25+
26+
# Sets the global MeterProvider instance
27+
metrics.set_meter_provider(MeterProvider())
28+
29+
# Exporter to export metrics to the console
30+
exporter = ConsoleMetricsExporter()
31+
32+
# Instrument the requests library
33+
RequestsInstrumentor().instrument()
34+
35+
# Indicate to start collecting and exporting requests related metrics
36+
metrics.get_meter_provider().start_pipeline(
37+
RequestsInstrumentor().meter, exporter, 5
38+
)
39+
40+
response = requests.get("http://example.com")
41+
42+
input("...\n")

docs/examples/basic_meter/observer.py

+60
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
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+
"""
16+
This example shows how the Observer metric instrument can be used to capture
17+
asynchronous metrics data.
18+
"""
19+
import psutil
20+
21+
from opentelemetry import metrics
22+
from opentelemetry.sdk.metrics import MeterProvider, ValueObserver
23+
from opentelemetry.sdk.metrics.export import ConsoleMetricsExporter
24+
25+
metrics.set_meter_provider(MeterProvider())
26+
meter = metrics.get_meter(__name__)
27+
metrics.get_meter_provider().start_pipeline(meter, ConsoleMetricsExporter(), 5)
28+
29+
30+
# Callback to gather cpu usage
31+
def get_cpu_usage_callback(observer):
32+
for (number, percent) in enumerate(psutil.cpu_percent(percpu=True)):
33+
labels = {"cpu_number": str(number)}
34+
observer.observe(percent, labels)
35+
36+
37+
meter.register_valueobserver(
38+
callback=get_cpu_usage_callback,
39+
name="cpu_percent",
40+
description="per-cpu usage",
41+
unit="1",
42+
value_type=float,
43+
)
44+
45+
46+
# Callback to gather RAM memory usage
47+
def get_ram_usage_callback(observer):
48+
ram_percent = psutil.virtual_memory().percent
49+
observer.observe(ram_percent, {})
50+
51+
52+
meter.register_valueobserver(
53+
callback=get_ram_usage_callback,
54+
name="ram_percent",
55+
description="RAM memory usage",
56+
unit="1",
57+
value_type=float,
58+
)
59+
60+
input("Metrics will be printed soon. Press a key to finish...\n")

0 commit comments

Comments
 (0)