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.
Add prometheus monitoring to python app (open-telemetry#3004)
* Use OTEL exporter * Fix errors * Add requirements file in prometheus example directory --------- Co-authored-by: Diego Hurtado <ocelotl@users.noreply.github.com>
- Loading branch information
Showing
2 changed files
with
25 additions
and
0 deletions.
There are no files selected for viewing
23 changes: 23 additions & 0 deletions
23
docs/examples/metrics/prometheus-grafana/prometheus-monitor.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,23 @@ | ||
import random | ||
import time | ||
|
||
from prometheus_client import start_http_server | ||
|
||
from opentelemetry.exporter.prometheus import PrometheusMetricReader | ||
from opentelemetry.metrics import get_meter_provider, set_meter_provider | ||
from opentelemetry.sdk.metrics import MeterProvider | ||
|
||
# Start Prometheus client | ||
start_http_server(port=8000, addr="localhost") | ||
# Exporter to export metrics to Prometheus | ||
prefix = "MyAppPrefix" | ||
reader = PrometheusMetricReader(prefix) | ||
# Meter is responsible for creating and recording metrics | ||
set_meter_provider(MeterProvider(metric_readers=[reader])) | ||
meter = get_meter_provider().get_meter("view-name-change", "0.1.2") | ||
|
||
my_counter = meter.create_counter("my.counter") | ||
|
||
while 1: | ||
my_counter.add(random.randint(1, 10)) | ||
time.sleep(random.random()) |
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,2 @@ | ||
opentelemetry-exporter-prometheus==1.12.0rc1 | ||
protobuf~=3.18.1 |