Listen to this Post
Having proper observability of your applications running on AWS is crucial for identifying bottlenecks, debugging issues, and ensuring optimal performance. AWS provides tools like OpenTelemetry collectors, CloudWatch, and X-Ray to help you achieve this. In this article, we will explore how to set up X-Ray and OpenTelemetry for tracing Python Flask applications running on Fargate serverless compute within an Elastic Kubernetes Service (EKS) cluster.
You Should Know:
1. Setting Up AWS X-Ray:
- Install the AWS X-Ray SDK for Python:
pip install aws-xray-sdk
- Configure X-Ray in your Flask application:
from aws_xray_sdk.core import xray_recorder from aws_xray_sdk.ext.flask.middleware import XRayMiddleware</li> </ul> app = Flask(<strong>name</strong>) xray_recorder.configure(service='My Flask App') XRayMiddleware(app, xray_recorder)
2. Integrating OpenTelemetry:
- Install the OpenTelemetry SDK and AWS X-Ray exporter:
pip install opentelemetry-api opentelemetry-sdk opentelemetry-instrumentation-flask opentelemetry-exporter-otlp
- Configure OpenTelemetry in your Flask app:
from opentelemetry import trace from opentelemetry.instrumentation.flask import FlaskInstrumentor from opentelemetry.sdk.trace import TracerProvider from opentelemetry.sdk.trace.export import BatchSpanProcessor from opentelemetry.exporter.otlp.proto.grpc.trace_exporter import OTLPSpanExporter</li> </ul> trace.set_tracer_provider(TracerProvider()) otlp_exporter = OTLPSpanExporter(endpoint="http://localhost:4317") span_processor = BatchSpanProcessor(otlp_exporter) trace.get_tracer_provider().add_span_processor(span_processor) FlaskInstrumentor().instrument_app(app)
3. Deploying on EKS Fargate:
- Create an EKS cluster:
eksctl create cluster --name my-cluster --fargate
- Deploy your Flask application using a Kubernetes deployment YAML:
apiVersion: apps/v1 kind: Deployment metadata: name: flask-app spec: replicas: 3 selector: matchLabels: app: flask-app template: metadata: labels: app: flask-app spec: containers:</li> <li>name: flask-app image: my-flask-app:latest ports:</li> <li>containerPort: 5000
4. Viewing Traces in AWS X-Ray:
- Open the AWS X-Ray console to view the traces and analyze the performance of your Flask application.
What Undercode Say:
Observability is a critical aspect of modern cloud-native applications. By leveraging AWS X-Ray and OpenTelemetry, you can gain deep insights into your application’s performance and troubleshoot issues effectively. The combination of these tools with Flask applications running on EKS Fargate provides a robust solution for tracing and monitoring.
Expected Output:
- AWS X-Ray traces for your Flask application.
- OpenTelemetry metrics and traces exported to your preferred backend.
- A fully observable Flask application running on EKS Fargate.
URLs:
References:
Reported By: Darryl Ruggles – Hackers Feeds
Extra Hub: Undercode MoN
Basic Verification: Pass ✅Join Our Cyber World:
- Create an EKS cluster:
- Install the OpenTelemetry SDK and AWS X-Ray exporter:



