From 71f19100f70af2f566acab126b255df01e792714 Mon Sep 17 00:00:00 2001 From: Bar <51476543+bardabun@users.noreply.github.com> Date: Wed, 20 Nov 2024 11:33:47 +0200 Subject: [PATCH] Add Documentation for ECS Setup with OpenTelemetry for Java, Python, and Node.js (#704) * Add ECS OpenTelemetry instrumentation docs for Java, Python, and Node.js --- docs/shipping/Code/java.md | 295 +++++++++++++++++++++++++++++++++- docs/shipping/Code/node-js.md | 278 +++++++++++++++++++++++++++++++- docs/shipping/Code/python.md | 259 ++++++++++++++++++++++++++++- 3 files changed, 820 insertions(+), 12 deletions(-) diff --git a/docs/shipping/Code/java.md b/docs/shipping/Code/java.md index 3fb039d6..1182c952 100644 --- a/docs/shipping/Code/java.md +++ b/docs/shipping/Code/java.md @@ -503,7 +503,7 @@ If the log appender does not ship logs, add `true This integration uses the OpenTelemetry logging exporter to send logs to Logz.io via the OpenTelemetry Protocol (OTLP) listener. -### Prerequisites +#### Prerequisites - Java 8+ @@ -1152,4 +1152,295 @@ Supported values for `otel.traces.sampler` are ### Viewing Traces in Logz.io -Give your traces time to process, after which they'll be available in your [Tracing](https://app.logz.io/#/dashboard/jaeger) dashboard. \ No newline at end of file +Give your traces time to process, after which they'll be available in your [Tracing](https://app.logz.io/#/dashboard/jaeger) dashboard. + +## Java Application Setup for ECS Service with OpenTelemetry + +This guide provides an overview of deploying your Java application on Amazon ECS, using OpenTelemetry to collect and send tracing data to Logz.io. It offers a step-by-step process for setting up OpenTelemetry instrumentation and deploying both the application and OpenTelemetry Collector sidecar in an ECS environment. + +#### Prerequisites + +Before you begin, ensure you have the following prerequisites in place: + +- AWS CLI configured with access to your AWS account. +- Docker installed for building images. +- AWS IAM role with sufficient permissions to create and manage ECS resources. +- Amazon ECR repository for storing the Docker images. +- Java JDK 11+ installed locally for development and testing. +- Maven or Gradle for building the Java project. + +:::note +For a complete example, refer to [this repo](https://github.com/logzio/opentelemetry-examples/tree/main/java/traces/ecs-service). +::: + +#### Architecture Overview + +The deployment will involve two main components: + +1. Java Application Container + + A container running your Java application, instrumented with OpenTelemetry to capture traces. + +2. OpenTelemetry Collector Sidecar + A sidecar container that receives telemetry data from the application, processes it, and exports it to Logz.io. + +The architecture is structured as follows: + +``` +project-root/ +├── java-app/ +│ ├── src/ # Java application source code +│ ├── pom.xml # Maven build configuration +│ ├── Dockerfile # Dockerfile for Java application +│ └── opentelemetry-javaagent.jar # OpenTelemetry Java agent for instrumentation +├── ecs/ +│ └── task-definition.json # ECS task definition file +└── otel-collector + ├── collector-config.yaml # OpenTelemetry Collector configuration + └── Dockerfile # Dockerfile for the Collector +``` + +#### Steps to Deploy the Application + +1. Project Structure Setup + + Ensure your project structure follows the architecture outline. You should have a directory for your Java application and a separate directory for the OpenTelemetry Collector. + +2. Set Up OpenTelemetry Instrumentation + + Add OpenTelemetry instrumentation to your Java application by using the OpenTelemetry Java agent. This agent provides automatic instrumentation for common frameworks and libraries. + +Ensure you download the latest version of the `opentelemetry-javaagent.jar` and place it in your project directory. + +Add the following dependencies in your `pom.xml` (or equivalent Gradle build file) to support OpenTelemetry instrumentation: + +```xml + + + + org.springframework.boot + spring-boot-starter-web + + + + + org.springframework.boot + spring-boot-starter-actuator + + +``` + +#### Integrating OpenTelemetry Java Agent + +Include the Java agent when running your application to enable tracing. +see [here](https://opentelemetry.io/docs/zero-code/java/agent/getting-started/) for more details + +1. Download the OpenTelemetry Java Agent + + Get the latest version of `opentelemetry-javaagent.jar` from the [OpenTelemetry Java Agent GitHub releases](https://github.com/open-telemetry/opentelemetry-java-instrumentation/releases). + +2. Add the Agent to Your Application + + Place the `opentelemetry-javaagent.jar` in your project as mentioned in the atchitectre structure above. + +3. Modify the Application's Startup Command + + Include the `-javaagent` flag when starting your Java application to load the OpenTelemetry agent: + + ```shell + java -javaagent:/path/to/opentelemetry-javaagent.jar -jar your-app.jar + ``` + +4. Set Environment Variables for OpenTelemetry + + Use environment variables to configure the agent, such as the OTLP endpoint and resource attributes: + + ```shell + export OTEL_TRACES_SAMPLER=always_on + export OTEL_EXPORTER_OTLP_ENDPOINT=http://localhost:4318 + export OTEL_RESOURCE_ATTRIBUTES="service.name=java-app" + ``` + +#### Dockerize Your Application + +Create a Dockerfile to build a Docker image of your Java application. Below is the essential Dockerfile to get started: + +#### Dockerfile + +```dockerfile +# Use a Maven image to build the application +FROM maven:3.8.6-openjdk-11-slim AS builder + +# Set the working directory +WORKDIR /app + +# Copy the project files +COPY pom.xml . +COPY src ./src + +# Package the application +RUN mvn clean package -DskipTests + +# Use a lightweight OpenJDK image for the runtime +FROM openjdk:11-jre-slim + +# Set the working directory +WORKDIR /app + +# Copy the packaged application and the OpenTelemetry agent +COPY --from=builder /app/target/java-app-0.0.1-SNAPSHOT.jar app.jar +COPY opentelemetry-javaagent.jar opentelemetry-javaagent.jar + +# Expose the application port +EXPOSE 8080 + +# Set environment variables for OpenTelemetry +ENV OTEL_TRACES_SAMPLER=always_on +ENV OTEL_EXPORTER_OTLP_ENDPOINT="http://localhost:4318" +ENV OTEL_RESOURCE_ATTRIBUTES="service.name=java-app" + +# Start the application with the OpenTelemetry Java agent +ENTRYPOINT ["java", "-javaagent:/app/opentelemetry-javaagent.jar", "-jar", "app.jar"] + +``` + +#### Configure the OpenTelemetry Collector + +The OpenTelemetry Collector receives traces from the application and exports them to Logz.io. Create a `collector-config.yaml` file to define how the Collector should handle traces. + +collector-config.yaml + +{@include: ../../_include/tracing-shipping/collector-config.md} + +#### Build Docker Images + +Build Docker images for both the Java application and the OpenTelemetry Collector: + +```shell +# Build Java application image +cd java-app/ +docker build --platform linux/amd64 -t java-app:latest . + +# Build OpenTelemetry Collector image +cd ../otel-collector/ +docker build --platform linux/amd64 -t otel-collector:latest . +``` +#### Push Docker Images to Amazon ECR + +Push both images to your Amazon ECR repository: + +```shell +# Authenticate Docker to Amazon ECR +aws ecr get-login-password --region | docker login --username AWS --password-stdin .dkr.ecr..amazonaws.com + +# Tag and push images +docker tag java-app:latest .dkr.ecr..amazonaws.com/java-app:latest +docker push .dkr.ecr..amazonaws.com/java-app:latest + +docker tag otel-collector:latest .dkr.ecr..amazonaws.com/otel-collector:latest +docker push .dkr.ecr..amazonaws.com/otel-collector:latest +``` + +##### Log Group Creation: + +Create log groups for your Java application and OpenTelemetry Collector in CloudWatch. + +```shell +aws logs create-log-group --log-group-name /ecs/java-app +aws logs create-log-group --log-group-name /ecs/otel-collector +``` + +#### Define ECS Task + +Create a task definition (`task-definition.json`) for ECS that defines both the Java application container and the OpenTelemetry Collector container. + +#### task-definition.json + +```json +{ + "family": "java-app-task", + "networkMode": "awsvpc", + "requiresCompatibilities": ["FARGATE"], + "cpu": "256", + "memory": "512", + "executionRoleArn": "arn:aws:iam:::role/ecsTaskExecutionRole", + "containerDefinitions": [ + { + "name": "java-app", + "image": ".dkr.ecr..amazonaws.com/java-app:latest", + "cpu": 128, + "portMappings": [ + { + "containerPort": 8080, + "protocol": "tcp" + } + ], + "essential": true, + "environment": [], + "logConfiguration": { + "logDriver": "awslogs", + "options": { + "awslogs-group": "/ecs/java-app", + "awslogs-region": "", + "awslogs-stream-prefix": "ecs" + } + } + }, + { + "name": "otel-collector", + "image": ".dkr.ecr..amazonaws.com/otel-collector:latest", + "cpu": 128, + "essential": false, + "command": ["--config=/etc/collector-config.yaml"], + "environment": [ + { + "name": "LOGZIO_TRACING_TOKEN", + "value": "" + }, + { + "name": "LOGZIO_REGION", + "value": "" + } + ], + "logConfiguration": { + "logDriver": "awslogs", + "options": { + "awslogs-group": "/ecs/otel-collector", + "awslogs-region": "", + "awslogs-stream-prefix": "ecs" + } + } + } + ] +} +``` + +#### Deploy to ECS + +Create an ECS Cluster: Create a cluster to deploy your containers: + +```shell +aws ecs create-cluster --cluster-name your-app-cluster --region +``` + +Register the Task Definition: + +```shell +aws ecs register-task-definition --cli-input-json file://ecs/task-definition.json +``` + +Create ECS Service: Deploy the task definition using a service: + +```shell +aws ecs create-service \ + --cluster your-app-cluster \ + --service-name your-java-app-service \ + --task-definition java-app-task \ + --desired-count 1 \ + --launch-type FARGATE \ + --network-configuration "awsvpcConfiguration={subnets=[\"YOUR_SUBNET_ID\"],securityGroups=[\"YOUR_SECURITY_GROUP_ID\"],assignPublicIp=ENABLED}" +``` + +#### Verify Application and Tracing + +After deploying, run your application to generate activity that will create tracing data. Wait a few minutes, then check the Logz.io dashboard to confirm that traces are being sent correctly. \ No newline at end of file diff --git a/docs/shipping/Code/node-js.md b/docs/shipping/Code/node-js.md index 1852830d..9aee2b5c 100644 --- a/docs/shipping/Code/node-js.md +++ b/docs/shipping/Code/node-js.md @@ -356,7 +356,7 @@ This integration uses the OpenTelemetry logging exporter to send logs to Logz.io - An active account with Logz.io :::note -If you need an example aplication to test this integration, please refer to our [NodeJS OpenTelemetry repository](https://github.com/logzio/opentelemetry-examples/tree/main/nodjs/logs). +If you need an example aplication to test this integration, please refer to our [NodeJS OpenTelemetry repository](https://github.com/logzio/opentelemetry-examples/tree/main/nodejs/logs). ::: ### Configure the instrumentation @@ -805,7 +805,7 @@ To find your cluster domain name, run the following command: ```shell kubectl run -it --image=k8s.gcr.io/e2e-test-images/jessie-dnsutils:1.3 --restart=Never shell -- \ -sh -c 'nslookup kubernetes.default | grep Name | sed "s/Name:\skubernetes.default//"' +shell -c 'nslookup kubernetes.default | grep Name | sed "s/Name:\skubernetes.default//"' ``` This command deploys a temporary pod to extract your cluster domain name. You can remove the pod after retrieving the domain name. @@ -928,6 +928,280 @@ helm uninstall logzio-k8s-telemetry ``` + + + +This guide provides an overview of deploying your Node.js application on Amazon ECS, using OpenTelemetry to collect and send tracing data to Logz.io. It offers a step-by-step process for setting up OpenTelemetry instrumentation and deploying both the application and OpenTelemetry Collector sidecar in an ECS environment. + +##### Prerequisites + +Before you begin, ensure you have the following prerequisites in place: + +- AWS CLI configured with access to your AWS account. +- Docker installed for building images. +- AWS IAM role with sufficient permissions to create and manage ECS resources. +- Amazon ECR repository for storing the Docker images. +- Node.js and npm installed locally for development and testing. + +:::note +For a complete example, refer to [this repo](https://github.com/logzio/opentelemetry-examples/tree/main/nodejs/traces/ecs-service). +::: + +##### Architecture Overview + +The deployment will involve two main components: + +1. Node.js Application Container: A container running your Node.js application, instrumented with OpenTelemetry to capture traces. + +2. OpenTelemetry Collector Sidecar: A sidecar container that receives telemetry data from the application, processes it, and exports it to Logz.io. + +``` +project-root/ +├── nodejs-app/ # Your Node.js application directory +│ ├── app.js # Node.js application entry point +│ ├── tracing.js # OpenTelemetry tracing setup +│ ├── Dockerfile # Dockerfile to build Node.js application image +│ └── package.json # Node.js dependencies, includes OpenTelemetry +├── ecs/ +│ └── task-definition.json # ECS task definition file +└── otel-collector + ├── collector-config.yaml # OpenTelemetry Collector configuration + └── Dockerfile # Dockerfile for the Collector + +``` + +#### Steps to Deploy the Application + +1. Project Structure Setup + + Ensure your project structure follows the architecture outline. You should have a directory for your Node.js application and a separate directory for the OpenTelemetry Collector. + +2. Set Up OpenTelemetry Instrumentation + + Add OpenTelemetry instrumentation to your Node.js application by including a tracing setup `tracing.js`. This file will initialize OpenTelemetry and configure the trace exporter to send traces to the OpenTelemetry Collector. + +##### tracing.js + +```javascript +"use strict"; + +const { NodeSDK } = require("@opentelemetry/sdk-node"); +const { + OTLPTraceExporter, +} = require("@opentelemetry/exporter-trace-otlp-grpc"); +const { diag, DiagConsoleLogger, DiagLogLevel } = require("@opentelemetry/api"); +const { Resource } = require("@opentelemetry/resources"); + +// Optional: Enable diagnostic logging for debugging +diag.setLogger(new DiagConsoleLogger(), DiagLogLevel.INFO); + +function startTracing() { + const traceExporter = new OTLPTraceExporter({ + url: process.env.OTEL_EXPORTER_OTLP_ENDPOINT || "grpc://localhost:4317", + }); + + const sdk = new NodeSDK({ + traceExporter, + resource: new Resource({ + "service.name": "nodejs-app", + }), + }); + + sdk + .start() + .then(() => console.log("Tracing initialized")) + .catch((error) => console.log("Error initializing tracing", error)); + + // Optional: Gracefully shutdown tracing on process exit + process.on("SIGTERM", () => { + sdk + .shutdown() + .then(() => console.log("Tracing terminated")) + .catch((error) => console.log("Error terminating tracing", error)) + .finally(() => process.exit(0)); + }); +} + +module.exports = { startTracing }; +``` + +The `tracing.js` file initializes OpenTelemetry tracing, configuring the OTLP exporter to send trace data to the OpenTelemetry Collector. + +Include the tracing setup at the entry point of your application `app.js`, ensuring tracing starts before any other logic. + +##### Dockerfile + +```dockerfile +# Use Node.js LTS version +FROM node:16-alpine + +# Set the working directory +WORKDIR /app + +# Copy and install dependencies +COPY package*.json ./ +RUN npm install --production + +# Copy the application code +COPY . . + +# Expose the application port +EXPOSE 3000 + +# Set environment variables for OpenTelemetry +ENV OTEL_TRACES_SAMPLER=always_on +ENV OTEL_EXPORTER_OTLP_ENDPOINT="http://localhost:4317" +ENV OTEL_RESOURCE_ATTRIBUTES="service.name=nodejs-app" + +# Start the application +CMD ["npm", "start"] +``` + +The Dockerfile installs the necessary dependencies, sets the environment variables required for OpenTelemetry configuration, and starts the application. + +##### Configure the OpenTelemetry Collector + +The OpenTelemetry Collector receives traces from the application and exports them to Logz.io. Create a `collector-config.yaml` file to define how the Collector should handle traces. + +##### collector-config.yaml + +{@include: ../../_include/tracing-shipping/collector-config.md} + +##### Build Docker Images + +Build Docker images for both the Node.js application and the OpenTelemetry Collector: + +```shell +# Build Node.js application image +cd nodejs-app/ +docker build --platform linux/amd64 -t your-nodejs-app:latest . + +# Build OpenTelemetry Collector image +cd ../otel-collector/ +docker build --platform linux/amd64 -t otel-collector:latest . +``` + +##### Push Docker Images to Amazon ECR + +Push both images to your Amazon ECR repository: + +```shell +# Authenticate Docker to Amazon ECR +aws ecr get-login-password --region | docker login --username AWS --password-stdin .dkr.ecr..amazonaws.com + +# Tag and push images +docker tag your-nodejs-app:latest .dkr.ecr..amazonaws.com/your-nodejs-app:latest +docker push .dkr.ecr..amazonaws.com/your-nodejs-app:latest + +docker tag otel-collector:latest .dkr.ecr..amazonaws.com/otel-collector:latest +docker push .dkr.ecr..amazonaws.com/otel-collector:latest +``` + +##### Log Group Creation: + +Create log groups for your Nodejs application and OpenTelemetry Collector in CloudWatch. + +```shell +aws logs create-log-group --log-group-name /ecs/nodejs-app +aws logs create-log-group --log-group-name /ecs/otel-collector +``` + +##### Define ECS Task + +Create a task definition (task-definition.json) for ECS that defines both the Node.js application container and the OpenTelemetry Collector container. + +##### task-definition.json + +```json +{ + "family": "your-nodejs-app-task", + "networkMode": "awsvpc", + "requiresCompatibilities": ["FARGATE"], + "cpu": "256", + "memory": "512", + "executionRoleArn": "arn:aws:iam:::role/ecsTaskExecutionRole", + "containerDefinitions": [ + { + "name": "your-nodejs-app", + "image": ".dkr.ecr..amazonaws.com/your-nodejs-app:latest", + "cpu": 128, + "portMappings": [ + { + "containerPort": 3000, + "protocol": "tcp" + } + ], + "essential": true, + "environment": [], + "logConfiguration": { + "logDriver": "awslogs", + "options": { + "awslogs-group": "/ecs/your-nodejs-app", + "awslogs-region": "", + "awslogs-stream-prefix": "ecs" + } + } + }, + { + "name": "otel-collector", + "image": ".dkr.ecr..amazonaws.com/otel-collector:latest", + "cpu": 128, + "essential": false, + "command": ["--config=/etc/collector-config.yaml"], + "environment": [ + { + "name": "LOGZIO_TRACING_TOKEN", + "value": "" + }, + { + "name": "LOGZIO_REGION", + "value": "" + } + ], + "logConfiguration": { + "logDriver": "awslogs", + "options": { + "awslogs-group": "/ecs/otel-collector", + "awslogs-region": "", + "awslogs-stream-prefix": "ecs" + } + } + } + ] +} +``` + +##### Deploy to ECS + +- Create an ECS Cluster: Create a cluster to deploy your containers: + + ```shell + aws ecs create-cluster --cluster-name your-app-cluster --region + ``` + +- Register the Task Definition: + + ```shell + aws ecs register-task-definition --cli-input-json file://ecs/task-definition.json + ``` + +- Create ECS Service: Deploy the task definition using a service: + + ```shell + aws ecs create-service \ + --cluster your-app-cluster \ + --service-name your-nodejs-app-service \ + --task-definition your-nodejs-app-task \ + --desired-count 1 \ + --launch-type FARGATE \ + --network-configuration "awsvpcConfiguration={subnets=[\"YOUR_SUBNET_ID\"],securityGroups=[\"YOUR_SECURITY_GROUP_ID\"],assignPublicIp=ENABLED}" \ + --region \ + ``` + +##### Verify Application and Tracing + +After deploying, run your application to generate activity that will create tracing data. Wait a few minutes, then check the Logz.io dashboard to confirm that traces are being sent correctly. + diff --git a/docs/shipping/Code/python.md b/docs/shipping/Code/python.md index 27182427..fb1c52d1 100644 --- a/docs/shipping/Code/python.md +++ b/docs/shipping/Code/python.md @@ -1025,6 +1025,8 @@ This integration includes: * Running your Python application in conjunction with the OpenTelemetry instrumentation On deployment, the Python instrumentation automatically captures spans from your application and forwards them to the collector, which exports the data to your Logz.io account. + + ### Local host Python application auto instrumentation @@ -1041,7 +1043,7 @@ This integration uses OpenTelemetry Collector Contrib, not the OpenTelemetry Col ::: -### Install OpenTelemetry components for Python +#### Install OpenTelemetry components for Python ```shell @@ -1051,7 +1053,7 @@ opentelemetry-bootstrap --action=install pip3 install opentelemetry-exporter-otlp ``` -### Set environment variables +#### Set environment variables After installation, configure the exporter with this command: @@ -1060,7 +1062,7 @@ export OTEL_TRACES_EXPORTER=otlp export OTEL_RESOURCE_ATTRIBUTES="service.name=<>" ``` -### Download and configure OpenTelemetry collector +#### Download and configure OpenTelemetry collector Create a directory on your Python application and download the relevant [OpenTelemetry collector](https://github.com/open-telemetry/opentelemetry-collector-contrib/releases/tag/v0.111.0). Create a `config.yaml` with the following parameters: @@ -1071,7 +1073,7 @@ Create a directory on your Python application and download the relevant [OpenTel {@include: ../../_include/tracing-shipping/tail-sampling.md} -### Start the collector +#### Start the collector Run: @@ -1081,7 +1083,7 @@ Run: * Replace `` with the collector's directory. * Replace `` with the version name, e.g. `otelcontribcol_darwin_amd64`. -### Run OpenTelemetry with your Python application +#### Run OpenTelemetry with your Python application Run this code from the directory of your Python application script: @@ -1091,12 +1093,13 @@ opentelemetry-instrument python3 .py Replace `` with your Python application script name. -### Viewing Traces in Logz.io +#### Viewing Traces in Logz.io Give your traces time to process, after which they'll be available in your [Tracing](https://app.logz.io/#/dashboard/jaeger) dashboard. - + + ### Docker Python application auto instrumentation @@ -1342,10 +1345,248 @@ Replace `<>` with your Python application script name. #### Viewing Traces in Logz.io Give your traces time to process, after which they'll be available in your [Tracing](https://app.logz.io/#/dashboard/jaeger) dashboard. + + + +## Python Application Setup for ECS Service with OpenTelemetry + +This guide provides an overview of deploying your Python application on Amazon ECS, using OpenTelemetry to collect and send tracing data to Logz.io. It offers a step-by-step process for setting up OpenTelemetry instrumentation and deploying both the application and OpenTelemetry Collector sidecar in an ECS environment. + +#### Prerequisites + +Before you begin, ensure you have the following prerequisites in place: + +- AWS CLI configured with access to your AWS account. +- Docker installed for building images. +- AWS IAM role with sufficient permissions to create and manage ECS resources. +- Amazon ECR repository for storing the Docker images. +- Python 3.x and pip installed locally for development and testing. + +:::note +For a complete example, refer to [this repo](https://github.com/logzio/opentelemetry-examples/tree/main/python/traces/ecs-service). +::: + +#### Architecture Overview + +The deployment will involve two main components: + +1. Python Application Container + + A container running your Python application, instrumented with OpenTelemetry to capture traces. + +2. OpenTelemetry Collector Sidecar + + A sidecar container that receives telemetry data from the application, processes it, and exports it to Logz.io. + +The architecture is structured as follows: + +``` +project-root/ +├── python-app/ # Your Python application directory +│ ├── app.py # Python application entry point +│ ├── Dockerfile # Dockerfile to build Python application image +│ └── requirements.txt # Python dependencies, includes OpenTelemetry +├── ecs/ +│ └── task-definition.json # ECS task definition file +└── otel-collector + ├── collector-config.yaml # OpenTelemetry Collector configuration + └── Dockerfile # Dockerfile for the Collector +``` + +#### Steps to Deploy the Application + +1. Project Structure Setup + + Ensure your project structure follows the architecture outline. You should have a directory for your Python application and a separate directory for the OpenTelemetry Collector. + +2. Set Up OpenTelemetry Instrumentation + + Add OpenTelemetry instrumentation to your Python application by including the necessary OpenTelemetry packages and configuring the tracing setup. This can be done by installing the `opentelemetry-distro` and `opentelemetry-instrumentation-flask` packages and using them to instrument your Flask app. + +Install dependencies: + +#### requirements.txt + +``` +flask +opentelemetry-distro +opentelemetry-exporter-otlp +opentelemetry-instrumentation-flask +``` + +The package `opentelemetry-instrumentation-flask` handles the automatic instrumentation of Flask at runtime. + +#### Dockerize Your Application + +Create a Dockerfile to build a Docker image of your Python application. Below is the Dockerfile to get started: + +#### Dockerfile + +```dockerfile +FROM python:3.8-slim + +WORKDIR /app + +COPY . . + +RUN pip install --no-cache-dir -r requirements.txt + +# Set environment variables for OpenTelemetry configuration +ENV OTEL_TRACES_SAMPLER=always_on +ENV OTEL_EXPORTER_OTLP_ENDPOINT="http://localhost:4317" +ENV OTEL_RESOURCE_ATTRIBUTES="service.name=python-app" + +EXPOSE 5000 + +CMD ["opentelemetry-instrument", "python", "app.py"] +``` + +#### Configure the OpenTelemetry Collector +The OpenTelemetry Collector receives traces from the application and exports them to Logz.io. Create a `collector-config.yaml` file to define how the Collector should handle traces. +#### collector-config.yaml +{@include: ../../_include/tracing-shipping/collector-config.md} + +#### Build Docker Images + +Build Docker images for both the Node.js application and the OpenTelemetry Collector: + +```shell +# Build Node.js application image +cd python-app/ +docker build --platform linux/amd64 -t your-python-app:latest . + +# Build OpenTelemetry Collector image +cd ../otel-collector/ +docker build --platform linux/amd64 -t otel-collector:latest . +``` + +#### Push Docker Images to Amazon ECR + +Push both images to your Amazon ECR repository: + +```shell +# Authenticate Docker to Amazon ECR +aws ecr get-login-password --region | docker login --username AWS --password-stdin .dkr.ecr..amazonaws.com + +# Tag and push images +docker tag your-python-app:latest .dkr.ecr..amazonaws.com/your-python-app:latest +docker push .dkr.ecr..amazonaws.com/your-python-app:latest +docker tag otel-collector:latest .dkr.ecr..amazonaws.com/otel-collector:latest +docker push .dkr.ecr..amazonaws.com/otel-collector:latest +``` + +##### Log Group Creation: + +Create log groups for your Python application and OpenTelemetry Collector in CloudWatch. + +```shell +aws logs create-log-group --log-group-name /ecs/python-app +aws logs create-log-group --log-group-name /ecs/otel-collector +``` + +#### Define ECS Task + +Create a task definition (task-definition.json) for ECS that defines both the Node.js application container and the OpenTelemetry Collector container. + +#### task-definition.json + +```json +{ + "family": "your-python-app-task", + "networkMode": "awsvpc", + "requiresCompatibilities": ["FARGATE"], + "cpu": "256", + "memory": "512", + "executionRoleArn": "arn:aws:iam:::role/ecsTaskExecutionRole", + "containerDefinitions": [ + { + "name": "your-python-app", + "image": ".dkr.ecr..amazonaws.com/your-python-app:latest", + "cpu": 128, + "portMappings": [ + { + "containerPort": 5000, + "protocol": "tcp" + } + ], + "essential": true, + "environment": [], + "logConfiguration": { + "logDriver": "awslogs", + "options": { + "awslogs-group": "/ecs/your-python-app", + "awslogs-region": "", + "awslogs-stream-prefix": "ecs" + } + } + }, + { + "name": "otel-collector", + "image": ".dkr.ecr..amazonaws.com/otel-collector:latest", + "cpu": 128, + "essential": false, + "command": ["--config=/etc/collector-config.yaml"], + "environment": [ + { + "name": "LOGZIO_TRACING_TOKEN", + "value": "" + }, + { + "name": "LOGZIO_REGION", + "value": "" + } + ], + "logConfiguration": { + "logDriver": "awslogs", + "options": { + "awslogs-group": "/ecs/otel-collector", + "awslogs-region": "", + "awslogs-stream-prefix": "ecs" + } + } + } + ] +} +``` + +#### Deploy to ECS + +- Create an ECS Cluster: Create a cluster to deploy your containers: + + ```shell + aws ecs create-cluster --cluster-name your-app-cluster --region + ``` + +- Register the Task Definition: + + ```shell + aws ecs register-task-definition --cli-input-json file://ecs/task-definition.json + ``` + +- Create ECS Service: Deploy the task definition using a service: + + ```shell + aws ecs create-service \ + --cluster your-app-cluster \ + --service-name your-python-app-service \ + --task-definition your-python-app-task \ + --desired-count 1 \ + --launch-type FARGATE \ + --network-configuration "awsvpcConfiguration={subnets=[\"YOUR_SUBNET_ID\"],securityGroups=[\"YOUR_SECURITY_GROUP_ID\"],assignPublicIp=ENABLED}" \ + --region \ + ``` + +#### Verify Application and Tracing + +After deploying, run your application to generate activity that will create tracing data. Wait a few minutes, then check the Logz.io dashboard to confirm that traces are being sent correctly. + +--- + + ### Kuberenetes Python application auto insturmentation @@ -1538,7 +1779,9 @@ To uninstall the `logzio-monitoring` deployment, run: helm uninstall logzio-monitoring ``` - + + + ## Troubleshooting