Skip to content

Commit

Permalink
fix: python metrics instrumentation
Browse files Browse the repository at this point in the history
  • Loading branch information
mati007thm committed Jan 11, 2024
1 parent bf47a62 commit 07c09c4
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 3 deletions.
4 changes: 3 additions & 1 deletion dapr-distributed-calendar/kubernetes/python-messages.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
27 changes: 26 additions & 1 deletion dapr-distributed-calendar/python/messages.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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'}

Expand Down
5 changes: 4 additions & 1 deletion dapr-distributed-calendar/python/requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
yarl==1.9.4
opentelemetry-api==1.22.0
opentelemetry-sdk==1.22.0
opentelemetry-exporter-otlp-proto-grpc==1.22.0

0 comments on commit 07c09c4

Please sign in to comment.