Skip to content

Commit

Permalink
fix: python metrics instrumentation (#6)
Browse files Browse the repository at this point in the history
* fix: python metrics instrumentation

* fix: cleanup

* fix: set service name and version
  • Loading branch information
mati007thm authored Jan 12, 2024
1 parent 8a72670 commit 8092653
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 13 deletions.
14 changes: 3 additions & 11 deletions dapr-distributed-calendar/kubernetes/python-messages.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -27,23 +27,15 @@ 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
# value: "messages"
# - name: OTEL_TRACES_EXPORTER
# value: console,otlp
# - name: OTEL_METRICS_EXPORTER
# value: console,otlp
# - name: OTEL_EXPORTER_OTLP_TRACES_ENDPOINT
# value: http://otel-dapr-collector:4317
# - name: OTEL_EXPORTER_OTLP_METRICS_ENDPOINT
# value: http://otel-dapr-collector:4318
- name: "APP_PORT"
value: "5000"
- name: FLASK_RUN_PORT
value: "5000"
- name: OTEL_EXPORTER_OTLP_ENDPOINT
value: "http://otel-dapr-collector:4317"
ports:
- containerPort: 5000
imagePullPolicy: Always
28 changes: 27 additions & 1 deletion dapr-distributed-calendar/python/messages.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,35 @@

from dapr.clients import DaprClient

from opentelemetry.sdk.resources import SERVICE_NAME, SERVICE_VERSION, 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: "messages",
SERVICE_VERSION: '0.1.0'
})

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 +49,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 8092653

Please sign in to comment.