Skip to content

Commit

Permalink
Add prometheus monitoring to python app (open-telemetry#3004)
Browse files Browse the repository at this point in the history
* 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
nanfuka and ocelotl authored Feb 2, 2024
1 parent 1061d96 commit 43e1976
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 0 deletions.
23 changes: 23 additions & 0 deletions docs/examples/metrics/prometheus-grafana/prometheus-monitor.py
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())
2 changes: 2 additions & 0 deletions docs/examples/metrics/prometheus-grafana/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
opentelemetry-exporter-prometheus==1.12.0rc1
protobuf~=3.18.1

0 comments on commit 43e1976

Please sign in to comment.