Skip to content

Updated opentelemetry and fixed imports issue. #98

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jul 7, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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"
Expand All @@ -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"
Expand All @@ -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",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 %}
Expand All @@ -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"
Expand All @@ -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:
"""
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -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" %}
Expand Down Expand Up @@ -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)

Expand All @@ -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 %}

Expand Down