From 75891428f12e6cce28a52289079f2b4d973f8284 Mon Sep 17 00:00:00 2001 From: lvliang-intel Date: Fri, 21 Jun 2024 10:17:21 +0800 Subject: [PATCH] Support prometheus metrics for opea microservices (#225) * add preload env in run.sh Signed-off-by: lvliang-intel * Support prometheus metrics for all opea microservices Signed-off-by: lvliang-intel * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --------- Signed-off-by: lvliang-intel Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> --- comps/cores/mega/http_service.py | 2 ++ requirements.txt | 1 + tests/cores/mega/test_microservice.py | 2 ++ 3 files changed, 5 insertions(+) diff --git a/comps/cores/mega/http_service.py b/comps/cores/mega/http_service.py index 470059b82..86c5ba8d5 100644 --- a/comps/cores/mega/http_service.py +++ b/comps/cores/mega/http_service.py @@ -4,6 +4,7 @@ from typing import Optional from fastapi import FastAPI +from prometheus_fastapi_instrumentator import Instrumentator from uvicorn import Config, Server from .base_service import BaseService @@ -32,6 +33,7 @@ def __init__( self.uvicorn_kwargs = uvicorn_kwargs or {} self.cors = cors self._app = self._create_app() + Instrumentator().instrument(self._app).expose(self._app) @property def app(self): diff --git a/requirements.txt b/requirements.txt index 12e0cdd9c..6a453d50f 100644 --- a/requirements.txt +++ b/requirements.txt @@ -5,6 +5,7 @@ httpx opentelemetry-api opentelemetry-exporter-otlp opentelemetry-sdk +prometheus-fastapi-instrumentator pyyaml requests shortuuid diff --git a/tests/cores/mega/test_microservice.py b/tests/cores/mega/test_microservice.py index 126fb03ca..dbaff9a76 100644 --- a/tests/cores/mega/test_microservice.py +++ b/tests/cores/mega/test_microservice.py @@ -31,6 +31,8 @@ def test_add_route(self): response = self.client.post("/v1/add", json={"text": "Hello, "}) self.assertEqual(response.status_code, 200) self.assertEqual(response.json()["text"], "Hello, OPEA Project!") + response = self.client.get("/metrics") + self.assertEqual(response.status_code, 200) if __name__ == "__main__":