diff --git a/fastapi_template/template/{{cookiecutter.project_name}}/pyproject.toml b/fastapi_template/template/{{cookiecutter.project_name}}/pyproject.toml index 96d33c0..79d04db 100644 --- a/fastapi_template/template/{{cookiecutter.project_name}}/pyproject.toml +++ b/fastapi_template/template/{{cookiecutter.project_name}}/pyproject.toml @@ -82,7 +82,7 @@ httptools = "^0.3.0" strawberry-graphql = { version = "^0.114.2", extras = ["fastapi"] } {%- endif %} {%- if cookiecutter.enable_rmq == "True" %} -aio-pika = "^8.0.3" +aio-pika = "^7.2.0" {%- endif %} {%- if cookiecutter.prometheus_enabled == "True" %} prometheus-client = "^0.14.1" @@ -92,28 +92,30 @@ prometheus-fastapi-instrumentator = "5.8.1" sentry-sdk = "^1.5.12" {%- endif %} {%- if cookiecutter.otlp_enabled == "True" %} -protobuf = "~3.20.0" -opentelemetry-api = {version = "^1.12.0rc1", allow-prereleases = true} -opentelemetry-sdk = {version = "^1.12.0rc1", allow-prereleases = true} -opentelemetry-exporter-otlp = {version = "^1.12.0rc1", allow-prereleases = true} -opentelemetry-instrumentation = "^0.31b0" -opentelemetry-instrumentation-logging = "^0.31b0" -opentelemetry-instrumentation-fastapi = "^0.31b0" +opentelemetry-api = {version = "^1.12.0rc2", allow-prereleases = true} +opentelemetry-sdk = {version = "^1.12.0rc2", allow-prereleases = true} +opentelemetry-exporter-otlp = {version = "^1.12.0rc2", allow-prereleases = true} +opentelemetry-instrumentation = "^0.32b0" +opentelemetry-instrumentation-logging = "^0.32b0" +opentelemetry-instrumentation-fastapi = "^0.32b0" {%- if cookiecutter.enable_redis == "True" %} -opentelemetry-instrumentation-redis = "^0.31b0" +opentelemetry-instrumentation-redis = "^0.32b0" {%- endif %} {%- if cookiecutter.db_info.name == "postgresql" and cookiecutter.orm in ["ormar", "tortoise"] %} -opentelemetry-instrumentation-asyncpg = "^0.31b0" +opentelemetry-instrumentation-asyncpg = "^0.32b0" {%- endif %} {%- if cookiecutter.orm == "sqlalchemy" %} -opentelemetry-instrumentation-sqlalchemy = "^0.31b0" +opentelemetry-instrumentation-sqlalchemy = "^0.32b0" +{%- endif %} +{%- if cookiecutter.enable_rmq == "True" %} +opentelemetry-instrumentation-aio-pika = "^0.32b0" {%- endif %} {%- endif %} [tool.poetry.dev-dependencies] pytest = "^7.0" flake8 = "^4.0.1" -mypy = "^0.910" +mypy = "^0.961" isort = "^5.10.1" yesqa = "^1.3.0" pre-commit = "^2.19.0" @@ -134,10 +136,6 @@ asynctest = "^0.13.0" nest-asyncio = "^1.5.5" {%- endif %} httpx = "^0.22.0" -{%- if cookiecutter.enable_redis == "True" %} -types-redis = "^4.2.6" -{%- endif %} - [tool.isort] profile = "black" @@ -155,10 +153,23 @@ implicit_reexport = true allow_untyped_decorators = true warn_unused_ignores = false warn_return_any = false +namespace_packages = true {%- if cookiecutter.orm == "sqlalchemy" %} plugins = ["sqlalchemy.ext.mypy.plugin"] {%- endif %} +{%- if cookiecutter.enable_redis == "True" %} + +# Remove this and add `types-redis` +# when the issue https://github.com/python/typeshed/issues/8242 is resolved. +[[tool.mypy.overrides]] +module = [ + 'redis.asyncio' +] +ignore_missing_imports = true + +{%- endif %} + [tool.pytest.ini_options] filterwarnings = [ "error", diff --git a/fastapi_template/template/{{cookiecutter.project_name}}/{{cookiecutter.project_name}}/settings.py b/fastapi_template/template/{{cookiecutter.project_name}}/{{cookiecutter.project_name}}/settings.py index ca06549..70cf086 100644 --- a/fastapi_template/template/{{cookiecutter.project_name}}/{{cookiecutter.project_name}}/settings.py +++ b/fastapi_template/template/{{cookiecutter.project_name}}/{{cookiecutter.project_name}}/settings.py @@ -26,8 +26,9 @@ class Settings(BaseSettings): # Current environment environment: str = "dev" + {% if cookiecutter.db_info.name != "none" -%} - {%- if cookiecutter.db_info.name != "none" %} + # Variables for the database {%- if cookiecutter.db_info.name == "sqlite" %} db_file: Path = TEMP_DIR / "db.sqlite3" {%- else %} @@ -38,19 +39,25 @@ class Settings(BaseSettings): db_base: str = "{{cookiecutter.project_name}}" {%- endif %} db_echo: bool = False + {%- endif %} {%- if cookiecutter.enable_redis == "True" %} + + # Variables for Redis redis_host: str = "{{cookiecutter.project_name}}-redis" redis_port: int = 6379 redis_user: Optional[str] = None redis_pass: Optional[str] = None redis_base: Optional[int] = None + {%- endif %} {%- if cookiecutter.enable_rmq == "True" %} + + # Variables for RabbitMQ rabbit_host: str = "{{cookiecutter.project_name}}-rmq" rabbit_port: int = 5672 rabbit_user: str = "guest" @@ -59,26 +66,40 @@ class Settings(BaseSettings): rabbit_pool_size: int = 2 rabbit_channel_pool_size: int = 10 + {%- endif %} {%- if cookiecutter.prometheus_enabled == "True" %} + + # This variable is used to define + # multiproc_dir. It's required for [uvi|guni]corn projects. prometheus_dir: Path = TEMP_DIR / "prom" + {%- endif %} {%- if cookiecutter.sentry_enabled == "True" %} + + # Sentry's configuration. sentry_dsn: Optional[str] = None sentry_sample_rate: float = 1.0 + {%- endif %} {%- if cookiecutter.otlp_enabled == "True" %} + + # Grpc endpoint for opentelemetry. + # E.G. http://localhost:4317 opentelemetry_endpoint: Optional[str] = None + {%- endif %} {%- if cookiecutter.db_info.name != "none" %} + + @property def db_url(self) -> URL: """ diff --git a/fastapi_template/template/{{cookiecutter.project_name}}/{{cookiecutter.project_name}}/web/gql/dummy/query.py b/fastapi_template/template/{{cookiecutter.project_name}}/{{cookiecutter.project_name}}/web/gql/dummy/query.py index 7b74cb7..2bd7215 100644 --- a/fastapi_template/template/{{cookiecutter.project_name}}/{{cookiecutter.project_name}}/web/gql/dummy/query.py +++ b/fastapi_template/template/{{cookiecutter.project_name}}/{{cookiecutter.project_name}}/web/gql/dummy/query.py @@ -36,4 +36,4 @@ async def get_dummy_models( {%- else %} dao = DummyDAO() {%- endif %} - return await dao.get_all_dummies(limit=limit, offset=offset) + return await dao.get_all_dummies(limit=limit, offset=offset) # type: ignore diff --git a/fastapi_template/template/{{cookiecutter.project_name}}/{{cookiecutter.project_name}}/web/lifetime.py b/fastapi_template/template/{{cookiecutter.project_name}}/{{cookiecutter.project_name}}/web/lifetime.py index 7c90c15..0504375 100644 --- a/fastapi_template/template/{{cookiecutter.project_name}}/{{cookiecutter.project_name}}/web/lifetime.py +++ b/fastapi_template/template/{{cookiecutter.project_name}}/{{cookiecutter.project_name}}/web/lifetime.py @@ -27,31 +27,33 @@ {%- endif %} {%- if cookiecutter.otlp_enabled == "True" %} -from opentelemetry.exporter.otlp.proto.grpc.trace_exporter import ( # type: ignore +from opentelemetry.exporter.otlp.proto.grpc.trace_exporter import ( OTLPSpanExporter, ) -from opentelemetry.instrumentation.fastapi import FastAPIInstrumentor # type: ignore -from opentelemetry.sdk.resources import ( # type: ignore +from opentelemetry.instrumentation.fastapi import FastAPIInstrumentor +from opentelemetry.sdk.resources import ( SERVICE_NAME, TELEMETRY_SDK_LANGUAGE, DEPLOYMENT_ENVIRONMENT, Resource, ) -from opentelemetry.sdk.trace import TracerProvider # type: ignore -from opentelemetry.sdk.trace.export import BatchSpanProcessor # type: ignore -from opentelemetry.trace import set_tracer_provider # type: ignore +from opentelemetry.sdk.trace import TracerProvider +from opentelemetry.sdk.trace.export import BatchSpanProcessor +from opentelemetry.trace import set_tracer_provider {%- if cookiecutter.enable_redis == "True" %} -from opentelemetry.instrumentation.redis import RedisInstrumentor # type: ignore +from opentelemetry.instrumentation.redis import RedisInstrumentor {%- endif %} {%- if cookiecutter.db_info.name == "postgresql" and cookiecutter.orm in ["ormar", "tortoise"] %} -from opentelemetry.instrumentation.asyncpg import AsyncPGInstrumentor # type: ignore +from opentelemetry.instrumentation.asyncpg import AsyncPGInstrumentor {%- endif %} {%- if cookiecutter.orm == "sqlalchemy" %} -from opentelemetry.instrumentation.sqlalchemy import ( # type: ignore +from opentelemetry.instrumentation.sqlalchemy import ( SQLAlchemyInstrumentor, ) {%- endif %} - +{%- if cookiecutter.enable_rmq == "True" %} +from opentelemetry.instrumentation.aio_pika import AioPikaInstrumentor +{%- endif %} {%- endif %} {%- if cookiecutter.orm == "psycopg" %} @@ -185,6 +187,11 @@ def setup_opentelemetry(app: FastAPI) -> None: engine=app.state.db_engine.sync_engine, ) {%- endif %} + {%- if cookiecutter.enable_rmq == "True" %} + AioPikaInstrumentor().instrument( + tracer_provider=tracer_provider, + ) + {%- endif %} set_tracer_provider(tracer_provider=tracer_provider) @@ -208,6 +215,9 @@ def stop_opentelemetry(app: FastAPI) -> None: {%- if cookiecutter.orm == "sqlalchemy" %} SQLAlchemyInstrumentor().uninstrument() {%- endif %} + {%- if cookiecutter.enable_rmq == "True" %} + AioPikaInstrumentor().uninstrument() + {%- endif %} {%- endif %}