From 07c09c405b53592aa759e8a094f2c64c9cfa937f Mon Sep 17 00:00:00 2001 From: Matthias Theuermann Date: Thu, 11 Jan 2024 21:31:50 +0100 Subject: [PATCH] fix: python metrics instrumentation --- .../kubernetes/python-messages.yaml | 4 ++- dapr-distributed-calendar/python/messages.py | 27 ++++++++++++++++++- .../python/requirements.txt | 5 +++- 3 files changed, 33 insertions(+), 3 deletions(-) diff --git a/dapr-distributed-calendar/kubernetes/python-messages.yaml b/dapr-distributed-calendar/kubernetes/python-messages.yaml index 7649076..1cdb383 100644 --- a/dapr-distributed-calendar/kubernetes/python-messages.yaml +++ b/dapr-distributed-calendar/kubernetes/python-messages.yaml @@ -27,7 +27,7 @@ spec: spec: containers: - name: messages - image: mati007thm/dapr-distributed-calendar_messages:latest + image: mati007thm/dapr-distributed-calendar_messages:manual # image: mati007thm/dapr-distributed-calendar_messages:woOtel env: # - name: OTEL_SERVICE_NAME @@ -44,6 +44,8 @@ spec: value: "5000" - name: FLASK_RUN_PORT value: "5000" + - name: OTEL_EXPORTER_OTLP_ENDPOINT + value: "http://otel-dapr-collector:4317" ports: - containerPort: 5000 imagePullPolicy: Always diff --git a/dapr-distributed-calendar/python/messages.py b/dapr-distributed-calendar/python/messages.py index 5539fd4..94116fe 100644 --- a/dapr-distributed-calendar/python/messages.py +++ b/dapr-distributed-calendar/python/messages.py @@ -7,10 +7,34 @@ from dapr.clients import DaprClient +from opentelemetry.sdk.resources import SERVICE_NAME, Resource + +from opentelemetry import metrics +from opentelemetry.exporter.otlp.proto.grpc.metric_exporter import OTLPMetricExporter +from opentelemetry.sdk.metrics import MeterProvider +from opentelemetry.sdk.metrics.export import PeriodicExportingMetricReader + +# Service name is required for most backends +resource = Resource(attributes={ + SERVICE_NAME: "your-service-name" +}) + +reader = PeriodicExportingMetricReader( + OTLPMetricExporter() +) +meterProvider = MeterProvider(resource=resource, metric_readers=[reader]) +metrics.set_meter_provider(meterProvider) + +meter = metrics.get_meter("messages.meter") + +subscription_counter = meter.create_counter( + "subscription.counter", unit="1", description="Counts the times the subscribe function is called" +) + app = flask.Flask(__name__) CORS(app) -dapr_port = os.getenv("DAPR_HTTP_PORT", 5000) +dapr_port = os.getenv("FLASK_RUN_PORT", 5000) # dapr calls this endpoint to register the subscriber configuration # an alternative way would to be declare this inside a config yaml file @@ -24,6 +48,7 @@ def subscribe(): # subscriber acts as a listener for the topic events-topic @app.route('/getmsg', methods=['POST']) def subscriber(): + subscription_counter.add(1) print(request.json, flush=True) return json.dumps({'success':True}), 200, {'ContentType':'application/json'} diff --git a/dapr-distributed-calendar/python/requirements.txt b/dapr-distributed-calendar/python/requirements.txt index 4a063e8..5e9731e 100644 --- a/dapr-distributed-calendar/python/requirements.txt +++ b/dapr-distributed-calendar/python/requirements.txt @@ -19,4 +19,7 @@ python-dotenv==1.0.0 six==1.16.0 typing_extensions==4.9.0 Werkzeug==3.0.1 -yarl==1.9.4 \ No newline at end of file +yarl==1.9.4 +opentelemetry-api==1.22.0 +opentelemetry-sdk==1.22.0 +opentelemetry-exporter-otlp-proto-grpc==1.22.0 \ No newline at end of file