From fd37de1359fce7145cc0dbf9220df7476fedba1f Mon Sep 17 00:00:00 2001 From: Rafael Soares Date: Tue, 14 Nov 2023 18:34:47 -0300 Subject: [PATCH 1/4] add sentry integration --- app/config.py | 1 + app/handlers/products.py | 5 ---- app/main.py | 6 +++++ poetry.lock | 50 ++++++++++++++++++++++++++++++++++++++-- pyproject.toml | 1 + 5 files changed, 56 insertions(+), 7 deletions(-) diff --git a/app/config.py b/app/config.py index 67b68f5..47f1c7c 100644 --- a/app/config.py +++ b/app/config.py @@ -24,3 +24,4 @@ def __init__(self): "HUGGINGFACE_API_TOKEN", "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" ), } + self.sentry_dsn = os.environ.get("SENTRY_DSN", "") diff --git a/app/handlers/products.py b/app/handlers/products.py index db57766..366ac17 100644 --- a/app/handlers/products.py +++ b/app/handlers/products.py @@ -81,7 +81,6 @@ def index(self, request: ProductIndexRequest): docs = self.product_indexer.index(request.catalog_id, request.product) return ProductIndexResponse(catalog_id=request.catalog_id, documents=docs) except Exception as e: - logger.error(msg=str(e)) raise HTTPException(status_code=500, detail=[{"msg": str(e)}]) def batch_index(self, request: ProductBatchIndexRequest): @@ -91,7 +90,6 @@ def batch_index(self, request: ProductBatchIndexRequest): ) return ProductIndexResponse(catalog_id=request.catalog_id, documents=docs) except Exception as e: - logger.error(msg=str(e)) raise HTTPException(status_code=500, detail=[{"msg": str(e)}]) def search(self, request: ProductSearchRequest): @@ -101,7 +99,6 @@ def search(self, request: ProductSearchRequest): ) return ProductSearchResponse(products=matched_products) except Exception as e: - logger.error(msg=str(e)) raise HTTPException(status_code=500, detail=[{"msg": str(e)}]) def delete( @@ -117,7 +114,6 @@ def delete( docs = self.product_indexer.delete(catalog_id, product_retailer_id) return ProductIndexResponse(catalog_id=catalog_id, documents=docs) except Exception as e: - logger.error(msg=str(e)) raise HTTPException(status_code=500, detail=[{"msg": str(e)}]) def delete_batch(self, request: ProductDeleteRequest): @@ -127,5 +123,4 @@ def delete_batch(self, request: ProductDeleteRequest): ) return ProductIndexResponse(catalog_id=request.catalog_id, documents=docs) except Exception as e: - logger.error(msg=str(e)) raise HTTPException(status_code=500, detail=[{"msg": str(e)}]) diff --git a/app/main.py b/app/main.py index 5558f05..c9aaa6c 100644 --- a/app/main.py +++ b/app/main.py @@ -1,3 +1,4 @@ +import sentry_sdk from fastapi import FastAPI from langchain.embeddings import SagemakerEndpointEmbeddings, HuggingFaceHubEmbeddings from langchain.embeddings.base import Embeddings @@ -38,6 +39,11 @@ def __init__(self, config: AppConfig): content_handler=content_handler, ) + if config.sentry_dsn != "": + sentry_sdk.init( + dsn=config.sentry_dsn, + ) + self.api = FastAPI() self.vectorstore = ElasticVectorSearch( elasticsearch_url=config.es_url, diff --git a/poetry.lock b/poetry.lock index 1aaf9bd..8c439f9 100644 --- a/poetry.lock +++ b/poetry.lock @@ -1,4 +1,4 @@ -# This file is automatically @generated by Poetry 1.6.1 and should not be changed by hand. +# This file is automatically @generated by Poetry 1.7.0 and should not be changed by hand. [[package]] name = "aiohttp" @@ -1442,6 +1442,52 @@ botocore = ">=1.12.36,<2.0a.0" [package.extras] crt = ["botocore[crt] (>=1.20.29,<2.0a.0)"] +[[package]] +name = "sentry-sdk" +version = "1.35.0" +description = "Python client for Sentry (https://sentry.io)" +optional = false +python-versions = "*" +files = [ + {file = "sentry-sdk-1.35.0.tar.gz", hash = "sha256:04e392db9a0d59bd49a51b9e3a92410ac5867556820465057c2ef89a38e953e9"}, + {file = "sentry_sdk-1.35.0-py2.py3-none-any.whl", hash = "sha256:a7865952701e46d38b41315c16c075367675c48d049b90a4cc2e41991ebc7efa"}, +] + +[package.dependencies] +certifi = "*" +fastapi = {version = ">=0.79.0", optional = true, markers = "extra == \"fastapi\""} +urllib3 = {version = ">=1.26.11", markers = "python_version >= \"3.6\""} + +[package.extras] +aiohttp = ["aiohttp (>=3.5)"] +arq = ["arq (>=0.23)"] +asyncpg = ["asyncpg (>=0.23)"] +beam = ["apache-beam (>=2.12)"] +bottle = ["bottle (>=0.12.13)"] +celery = ["celery (>=3)"] +chalice = ["chalice (>=1.16.0)"] +clickhouse-driver = ["clickhouse-driver (>=0.2.0)"] +django = ["django (>=1.8)"] +falcon = ["falcon (>=1.4)"] +fastapi = ["fastapi (>=0.79.0)"] +flask = ["blinker (>=1.1)", "flask (>=0.11)", "markupsafe"] +grpcio = ["grpcio (>=1.21.1)"] +httpx = ["httpx (>=0.16.0)"] +huey = ["huey (>=2)"] +loguru = ["loguru (>=0.5)"] +opentelemetry = ["opentelemetry-distro (>=0.35b0)"] +opentelemetry-experimental = ["opentelemetry-distro (>=0.40b0,<1.0)", "opentelemetry-instrumentation-aiohttp-client (>=0.40b0,<1.0)", "opentelemetry-instrumentation-django (>=0.40b0,<1.0)", "opentelemetry-instrumentation-fastapi (>=0.40b0,<1.0)", "opentelemetry-instrumentation-flask (>=0.40b0,<1.0)", "opentelemetry-instrumentation-requests (>=0.40b0,<1.0)", "opentelemetry-instrumentation-sqlite3 (>=0.40b0,<1.0)", "opentelemetry-instrumentation-urllib (>=0.40b0,<1.0)"] +pure-eval = ["asttokens", "executing", "pure-eval"] +pymongo = ["pymongo (>=3.1)"] +pyspark = ["pyspark (>=2.4.4)"] +quart = ["blinker (>=1.1)", "quart (>=0.16.1)"] +rq = ["rq (>=0.6)"] +sanic = ["sanic (>=0.8)"] +sqlalchemy = ["sqlalchemy (>=1.2)"] +starlette = ["starlette (>=0.19.1)"] +starlite = ["starlite (>=1.48)"] +tornado = ["tornado (>=5)"] + [[package]] name = "six" version = "1.16.0" @@ -1800,4 +1846,4 @@ multidict = ">=4.0" [metadata] lock-version = "2.0" python-versions = "^3.10" -content-hash = "c1782e5264a0a0a66f044c4babc8b5ed9b17ba0961f5e2bb81feae5df29bda49" +content-hash = "755f831e9f5a261fd52d2e80fc59a8f164e62926001328c95a04476a99bc7c4d" diff --git a/pyproject.toml b/pyproject.toml index 2c3df6b..3780238 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -14,6 +14,7 @@ boto3 = "^1.28.41" fastapi = "^0.103.1" uvicorn = "^0.23.2" huggingface-hub = "^0.16.4" +sentry-sdk = {extras = ["fastapi"], version = "^1.35.0"} [tool.poetry.group.dev.dependencies] From 9fa0ad93340a350a78ce0dab202b5a450dfbfef7 Mon Sep 17 00:00:00 2001 From: Rafael Soares Date: Tue, 14 Nov 2023 18:38:48 -0300 Subject: [PATCH 2/4] add elasticsearch timeout config --- app/config.py | 1 + app/main.py | 4 ++++ 2 files changed, 5 insertions(+) diff --git a/app/config.py b/app/config.py index 67b68f5..6d5e394 100644 --- a/app/config.py +++ b/app/config.py @@ -24,3 +24,4 @@ def __init__(self): "HUGGINGFACE_API_TOKEN", "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" ), } + self.es_timeout = os.environ.get("ELASTICSEARCH_TIMEOUT", "30") diff --git a/app/main.py b/app/main.py index 5558f05..8cb57cb 100644 --- a/app/main.py +++ b/app/main.py @@ -1,3 +1,4 @@ +from elasticsearch import Elasticsearch from fastapi import FastAPI from langchain.embeddings import SagemakerEndpointEmbeddings, HuggingFaceHubEmbeddings from langchain.embeddings.base import Embeddings @@ -44,6 +45,9 @@ def __init__(self, config: AppConfig): index_name=config.product_index_name, embedding=self.embeddings, ) + self.vectorstore.client = Elasticsearch( + hosts=config.es_url, timeout=int(config.es_timeout) + ) self.elasticStore = ElasticsearchVectorStoreIndex(self.vectorstore) self.products_indexer = ProductsIndexer(self.elasticStore) self.products_handler = ProductsHandler(self.products_indexer) From 7699742e48f5d85f01b2be0d277433cfc88cab1b Mon Sep 17 00:00:00 2001 From: Alisson Date: Fri, 29 Dec 2023 10:48:22 -0300 Subject: [PATCH 3/4] return 200 at / --- app/main.py | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/app/main.py b/app/main.py index 5558f05..54cfdf1 100644 --- a/app/main.py +++ b/app/main.py @@ -52,3 +52,8 @@ def __init__(self, config: AppConfig): config = AppConfig() main_app = App(config) + + +@main_app.api.get('/', status_code=200) +def home(): + return {} From 2c911357417ac5ca96f2202a410e96ee92020e89 Mon Sep 17 00:00:00 2001 From: Rafael Soares Date: Fri, 5 Jan 2024 11:42:36 -0300 Subject: [PATCH 4/4] update changelog for v0.1.1 --- CHANGELOG.md | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 07513c9..27bba74 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,9 @@ +v0.1.1 +---------- + * Sentry integration + * Elasticsearch timeout configuration + * Route for / with status 200 + v0.1.0 ---------- * Index, Delete and Search products