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 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
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
25 changes: 24 additions & 1 deletion app/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1,24 @@
# place to init logging changes and monitoring integration
import logging

from app.config.settings import get_settings
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

settings = get_settings()
logger = logging.getLogger(__name__)

if settings.otel_service_name and settings.otel_exporter_otlp_endpoint:
resource = Resource(attributes={
SERVICE_NAME: settings.otel_service_name,
})
provider = TracerProvider(resource=resource, )
exporter = OTLPSpanExporter(endpoint=settings.otel_exporter_otlp_endpoint)
provider.add_span_processor(BatchSpanProcessor(exporter))
trace.set_tracer_provider(provider)

import opentelemetry.instrumentation.auto_instrumentation.sitecustomize as autotrace # NOQA
else:
logger.info("OpenTelemetry trace/span exports are disabled")
5 changes: 4 additions & 1 deletion app/config/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,11 @@ 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
otel_service_name: str = None
otel_exporter_otlp_endpoint: str = None

ALLOWED_CORS_ORIGINS: set = [
"localhost",
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
)
44 changes: 44 additions & 0 deletions docker-compose.local.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
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_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
Loading