Skip to content
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

add jaeger tracing #3

Merged
merged 7 commits into from
Jun 5, 2023
Merged
Show file tree
Hide file tree
Changes from 6 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
2 changes: 2 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ ENV PYTHONUNBUFFERED=1 \
POETRY_VERSION=1.4.2

ENV PATH="$POETRY_HOME/bin:$VENV_PATH/bin:$PATH"
ENV PYTHONPATH="${PYTHONPATH}:${PYSETUP_PATH}"
RUN apk add bash libstdc++

# create user
Expand Down Expand Up @@ -67,6 +68,7 @@ FROM python-base as release
COPY --from=python-base --chown=appuser $VENV_PATH $VENV_PATH

USER appuser

WORKDIR /home/appuser

COPY --chown=appuser . .
Expand Down
7 changes: 7 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
startd:
docker-compose -f docker-compose.local.yml up -d --build \
&& docker-compose run --rm fast-api-docker-poetry poetry run alembic upgrade head

stopd:
docker-compose -f docker-compose.local.yml down

startslimd:
docker-compose up -d --build \
&& docker-compose run --rm fast-api-docker-poetry poetry run alembic upgrade head

Expand Down
12 changes: 8 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -73,19 +73,23 @@ fast-api-docker-poetry

## Quick Start

1. Start with docker
1. Start with jaeger tracing using docker
```
make startd
```
2. Test with docker
2. Start with no tracing using docker
```
make startslimd
```
3. Test with docker
```
make testd
```
3. Start with poetry
4. Start with poetry
```
make startp
```
4. Test with poetry
5. Test with poetry
```
make testp
```
Expand Down
5 changes: 4 additions & 1 deletion app/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1,4 @@
# place to init logging changes and monitoring integration
import os

if os.environ.get("OTEL_SERVICE_NAME"):
import opentelemetry.instrumentation.auto_instrumentation.sitecustomize as autotrace
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice, this certainly works. You can also do the pre-setup too like you had and just call this at the end. Example:

from opentelemetry import trace
from opentelemetry.exporter.otlp.proto.grpc.trace_exporter import OTLPSpanExporter
from opentelemetry.sdk.resources import SERVICE_NAME, Resource
from opentelemetry.sdk.trace import TracerProvider
from opentelemetry.sdk.trace.export import BatchSpanProcessor

resource = Resource(attributes={
    SERVICE_NAME: "fastApiDockerPoetry-2"
})
provider = TracerProvider(resource=resource)
provider.add_span_processor(BatchSpanProcessor(OTLPSpanExporter()))
trace.set_tracer_provider(provider)

import opentelemetry.instrumentation.auto_instrumentation.sitecustomize as autotrace

The real power of doing that is that you can do different things based on your BaseSettings bindings instead of only using environment variables.

For example, you might want to disable exports like in a local environment (relevant if you're using something like Datadog or don't wan to run Jaeger):

settings = get_settings()

if settings.otel_export_enabled:
    trace_provider.add_span_processor(
        BatchSpanProcessor(
            OTLPSpanExporter(endpoint=f"http://{settings.otel_agent}:4317", insecure=True, timeout=5)
        )
    )
else:
    logger.info("OpenTelemetry trace/span exports are disabled")

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

youre right I'm creating an inconsistent pattern. Ill add these to BaseSettings and merge

      OTEL_SERVICE_NAME: fastApiDockerPoetry
      OTEL_METRICS_EXPORTER: none
      OTEL_TRACES_EXPORTER: "otlp"
      OTEL_EXPORTER_OTLP_ENDPOINT: http://jaeger:4317

3 changes: 2 additions & 1 deletion app/config/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ class Settings(BaseSettings):
service: str = "fast-api-docker-poetry"
port: int = int("8009")
host: str = "0.0.0.0"
log_level: str = "info"
log_level: str = "debug"
app_reload: bool = False
db_retry_window_seconds: int = 60

ALLOWED_CORS_ORIGINS: set = [
Expand Down
2 changes: 1 addition & 1 deletion app/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,5 +69,5 @@ async def shutdown():
port=settings.port,
log_level=settings.log_level,
access_log=True,
reload=settings.is_local_dev,
reload=settings.app_reload, # has to be false for tracing to work
)
46 changes: 46 additions & 0 deletions docker-compose.local.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
version: "3.8"

services:
fast-api-docker-poetry:
image: local/fast-api-docker-poetry:${BUILD_TAG:-local-dev}
ports:
- "8009:8009"
build:
context: .
dockerfile: Dockerfile
target: ${TARGET:-development}
environment:
POSTGRES_DB_HOST: postgresql
OTEL_SERVICE_NAME: fastApiDockerPoetry
OTEL_METRICS_EXPORTER: none
OTEL_TRACES_EXPORTER: "otlp"
OTEL_EXPORTER_OTLP_ENDPOINT: http://jaeger:4317
volumes:
- ./app:/home/appuser/app
- ./tests:/home/appuser/tests
depends_on:
- fast-api-postgres
- jaeger
links:
- fast-api-postgres:postgresql
- jaeger:jaeger

fast-api-postgres:
image: postgres:15-alpine
ports:
- "5432:5432"
# volumes:
# - .pgdata/:/var/lib/postgresql/data
environment:
POSTGRES_DB: fastapi_db
POSTGRES_USER: postgres
POSTGRES_PASSWORD: postgres

jaeger:
image: jaegertracing/all-in-one:latest
ports:
- "16686:16686" # Jaeger UI
- "4317:4317" # OTLP gRPC
environment:
COLLECTOR_OTLP_ENABLED: "true"
LOG_LEVEL: "debug"
12 changes: 0 additions & 12 deletions docker-compose.override.yml

This file was deleted.

7 changes: 7 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,15 @@ version: "3.8"
services:
fast-api-docker-poetry:
image: local/fast-api-docker-poetry:${BUILD_TAG:-local-dev}
ports:
- "8009:8009"
build:
context: .
dockerfile: Dockerfile
target: ${TARGET:-development}
environment:
POSTGRES_DB_HOST: postgresql
APP_RELOAD: "true"
volumes:
- ./app:/home/appuser/app
- ./tests:/home/appuser/tests
Expand All @@ -19,6 +22,10 @@ services:

fast-api-postgres:
image: postgres:15-alpine
ports:
- "5432:5432"
# volumes:
# - .pgdata/:/var/lib/postgresql/data
environment:
POSTGRES_DB: fastapi_db
POSTGRES_USER: postgres
Expand Down
1,825 changes: 1,688 additions & 137 deletions poetry.lock

Large diffs are not rendered by default.

3 changes: 3 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ sqlalchemy = "^2.0.12"
httpx = "^0.24.0"
asyncpg = "^0.27.0"
psycopg2-binary = "^2.9.6"
opentelemetry-distro = "^0.39b0"
opentelemetry-exporter-otlp = "^1.18.0"
opentelemetry-contrib-instrumentations = "^0.39b0"

[tool.poetry.dev-dependencies]
black = "^22.6.0"
Expand Down