{"id":22423006,"url":"https://github.com/adityawdubey/instrumenting-fastapi-microservices-using-opentelemetry","last_synced_at":"2025-08-04T17:42:03.040Z","repository":{"id":256185402,"uuid":"854542394","full_name":"adityawdubey/Instrumenting-FastAPI-microservices-using-OpenTelemetry","owner":"adityawdubey","description":"This repository provides a guide and code examples for instrumenting FastAPI microservices with OpenTelemetry for distributed tracing and observability.","archived":false,"fork":false,"pushed_at":"2025-01-15T17:35:30.000Z","size":41,"stargazers_count":2,"open_issues_count":0,"forks_count":3,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-08-01T07:56:53.472Z","etag":null,"topics":["fastapi","grafana","observability","opentelemetry","python3"],"latest_commit_sha":null,"homepage":"","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/adityawdubey.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null}},"created_at":"2024-09-09T11:12:25.000Z","updated_at":"2025-05-09T21:14:57.000Z","dependencies_parsed_at":null,"dependency_job_id":"cc452e06-db09-4ca9-8851-54de8eaca886","html_url":"https://github.com/adityawdubey/Instrumenting-FastAPI-microservices-using-OpenTelemetry","commit_stats":null,"previous_names":["adityawdubey/instrumenting-fastapi-using-opentelemetry","adityawdubey/instrumenting-fastapi-microservices-using-opentelemetry"],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/adityawdubey/Instrumenting-FastAPI-microservices-using-OpenTelemetry","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/adityawdubey%2FInstrumenting-FastAPI-microservices-using-OpenTelemetry","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/adityawdubey%2FInstrumenting-FastAPI-microservices-using-OpenTelemetry/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/adityawdubey%2FInstrumenting-FastAPI-microservices-using-OpenTelemetry/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/adityawdubey%2FInstrumenting-FastAPI-microservices-using-OpenTelemetry/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/adityawdubey","download_url":"https://codeload.github.com/adityawdubey/Instrumenting-FastAPI-microservices-using-OpenTelemetry/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/adityawdubey%2FInstrumenting-FastAPI-microservices-using-OpenTelemetry/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":268736468,"owners_count":24299290,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","status":"online","status_checked_at":"2025-08-04T02:00:09.867Z","response_time":79,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["fastapi","grafana","observability","opentelemetry","python3"],"created_at":"2024-12-05T18:08:40.650Z","updated_at":"2025-08-04T17:42:02.957Z","avatar_url":"https://github.com/adityawdubey.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Instrumenting-FastAPI-microservices-using-OpenTelemetry\n\nThis project demonstrates how to instrument FastAPI applications using OpenTelemetry and visualize the traces using **Grafana** and **Tempo**. This setup helps you monitor, trace, and analyze the performance of FastAPI-based microservices and pinpoint bottlenecks or issues across distributed systems.\n\nFor a detailed walkthrough and additional insights, check out my blog post: [Instrumenting FastAPI Services with OpenTelemetry, Grafana, and Tempo.](https://adityadubey.tech/instrumenting-fastapi-services-with-opentelemetry-grafana-and-tempo)\n\n![Blank diagram (7) copy](https://github.com/user-attachments/assets/3edd12cb-c763-4d98-ba3a-f3e53d22a569)\n\n# Table of Contents\n\n1. [Overview](#overview)\n2. [Technologies Used](#️technologies-used)\n3. [Getting Started](#getting-started)\n   - [Clone the repository](#1-clone-the-repository)\n   - [Deploy Services](#2-deploy-services)\n     - [Build Docker Images for Your FastAPI Services](#-build-docker-images-for-your-fastapi-services)\n     - [Deploy FastAPI Services on k8s](#-deploy-fastapi-services-on-k8s)\n     - [Install Grafana and Tempo](#-install-grafana-and-tempo)\n     - [Port forward to access services](#-port-forward-to-access-services)\n     - [Add Tempo data source](#-add-tempo-data-source)\n4. [Usage](#usage)\n   - [Open `service_a` docs and send req to `service b`](#⁠⁠open-service_a-docs-and-send-req-to-service-b)\n   - [Check Tempo explore](#check-tempo-explore)\n5. [Contributing](#contributing) \n\n# Overview\n\nThis repository contains a sample FastAPI project instrumented with OpenTelemetry. It showcases how you can trace API requests, monitor service performance, and use Grafana and Tempo for visualizing these traces.\n\nThis project is ideal if you want to:\n\n- Gain insights into your FastAPI application’s performance.\n- Set up distributed tracing for better observability in a microservices architecture.\n- Use open-source observability tools like Grafana, Tempo, and Prometheus.\n\n# ️Technologies Used\n\n- **FastAPI**: A high-performance, Python web framework for building APIs.\n- **OpenTelemetry**: An open-source framework for collecting telemetry data (traces, metrics, logs) across various programming languages and systems.\n- **Docker**: For containerizing applications.\n- **Kubernetes**: For deploying applications in a containerized environment.\n- **Grafana**: For visualizing traces.\n- **Tempo**: For storing and querying traces.\n\n\n# Getting Started\n\n\n## 1. Clone the repository:\n\n```\ngit clone https://github.com/your-username fastapi-opentelemetry-demo.git\n```\n\n## 2. Deploy Services\n\n\nIf you plan to deploy using Docker or Kubernetes, follow the specific instructions within the codebase for building Docker images and deploying services.\n\n\n### ❏ Build Docker Images for Your FastAPI Services\n\nContainerize your FastAPI applications so that they can be deployed in a consistent and reproducible environment, whether locally or in Kubernetes. To do this, navigate to each service’s directory and run the following Docker build commands:\n\n```\ndocker build -t service_a -f service_a/Dockerfile   \ndocker tag service_a aniketdubey/service_a:latest   \ndocker push adityadubey/service_a:latest\n```\n\n```\ndocker build -t service_b -f service_b/Dockerfile\ndocker tag service_b adityadubey/service_b:latest\ndocker push aniketdubey/service_b:latest\n```\n\nThese commands create Docker images for both `service_a` and `service_b`. The `-t` flag assigns a tag (name) to each image, which is essential for identifying and deploying the containers later on.\n\n### ❏ Deploy FastAPI Services on k8s\n\nCreate a dedicated namespace for the FastAPI microservices to organize resources more effectively:\n\n```\nkubectl create namespace fastapi\n```\n\nOnce the namespace is created, you can deploy `service_a` and `service_b` by applying their respective YAML files (which should define the necessary Kubernetes configurations like deployments, services, etc.)\n\n```\nkubectl apply -f service_a.yaml  \nkubectl apply -f service_b.yaml\n```\n\n### ❏ Install Grafana and Tempo\n\nTo monitor and trace the behavior of your microservices, Install Grafana (for visualization) and Tempo (for storing and querying traces).\n\n```\nhelm repo add grafana https://grafana.github.io/helm-charts\nhelm install grafana grafana/grafana -n fastapi   \nhelm install tempo grafana/tempo -n fastapi\n```\n\nThese commands will set up Grafana and Tempo in your Kubernetes environment, preparing them to receive and visualize traces from your FastAPI services.\n\nFor more details on LGTM Stack: https://github.com/adityawdubey/LGTM-Monitoring-Stack\n\n### ❏ Port forward to access services\n\nOnce your services and observability stack are deployed, you'll need to access them. In a typical Kubernetes setup, you can use port forwarding to expose your services locally.\n\n``` \nkubectl port-forward svc/grafana 3000:80\n```\n\nYou can now access Grafana by opening your browser and navigating to [http://localhost:3000](http://localhost:3000/)\n\nSimilarly,\n\n```\nkubectl port-forward svc/service-a-service 8000:8000\nkubectl port-forward svc/service-b-service 8001:8001\n```\n\n### ❏ Add temp data source\n\nWith Grafana running, it’s time to integrate Tempo to visualize the traces collected from your FastAPI services. To do this:\n\n1. Get grafana password  \n    `kubectl get secret --namespace fastapi grafana -o jsonpath=\"{.data.admin-password}\" | base64 --decode ; echo`\n\n2. Open Grafana in your browser ([`http://localhost:3000`](http://localhost:3000)) and log in (default credentials are usually `admin`/`admin`).\n    \n3. Navigate to **Configuration \u0026gt; Data Sources**.\n    \n4. Click on **Add Data Source** and select **Tempo** from the list.\n    \n5. Enter the necessary details for Tempo (usually the default settings should work if Tempo is installed correctly).\n    \n6. Save the configuration.\n\n    \u003cimg width=\"1439\" alt=\"Screenshot 2024-10-02 at 12 24 42 AM (1)\" src=\"https://github.com/user-attachments/assets/8528f9e1-727b-406c-a2b8-cef6fbb79ea9\"\u003e\n\n## Usage\n\n### ⁠⁠Open `service_a` docs and send req to `service b`\n\n![Screenshot 2024-10-02 at 12 56 47 PM](https://github.com/user-attachments/assets/7139b5d7-444d-486a-a670-2f6df4c3925f)\n\n### Check tempo explore\n\nOnce Tempo is integrated, you can explore your trace data in Grafana using the **Explore** tab. Traces from **Service A** to **Service B** will be displayed.\n\n\u003cimg width=\"1440\" alt=\"Screenshot 2024-10-02 at 12 24 22 AM\" src=\"https://github.com/user-attachments/assets/cd385394-2368-4eab-a304-4860e4305ffe\"\u003e\n\n## Contributing\nContributions are welcome! Feel free to submit a pull request or open an issue to improve the project.\n\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fadityawdubey%2Finstrumenting-fastapi-microservices-using-opentelemetry","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fadityawdubey%2Finstrumenting-fastapi-microservices-using-opentelemetry","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fadityawdubey%2Finstrumenting-fastapi-microservices-using-opentelemetry/lists"}