Skip to content

Commit

Permalink
Set up opentelemetry using optional requirements
Browse files Browse the repository at this point in the history
  • Loading branch information
przemub committed Nov 24, 2024
1 parent ade8114 commit d0c289e
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 26 deletions.
10 changes: 8 additions & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,15 +1,20 @@
FROM python:3.12 AS build
WORKDIR /usr/src/app

ARG EXTRAS=""

COPY . .
RUN --mount=type=cache,target=/root/.cache/pip pip install --root-user-action ignore build

RUN --mount=type=cache,target=/root/.cache/pip python -m build -w
RUN --mount=type=cache,target=/root/.cache/pip pip install --root-user-action ignore dist/anime_quiz-*.whl
RUN --mount=type=cache,target=/root/.cache/pip \
pip install --root-user-action ignore dist/anime_quiz-0.1-py3-none-any.whl${EXTRAS}
RUN python manage.py collectstatic --no-input

FROM python:3.12 AS runtime

ARG EXTRAS=""

# Expose uwsgi socket and HTTP
EXPOSE 12345 8009
HEALTHCHECK --start-period=5m \
Expand All @@ -22,7 +27,8 @@ WORKDIR /usr/src/app
COPY uwsgi.cfg .
COPY --from=build /usr/src/app/dist dist
COPY --from=build /usr/src/app/static static
RUN --mount=type=cache,target=/root/.cache/pip pip install --root-user-action ignore dist/anime_quiz-*.whl
RUN --mount=type=cache,target=/root/.cache/pip \
pip install --root-user-action ignore dist/anime_quiz-0.1-py3-none-any.whl${EXTRAS}
RUN rm -r dist

USER app
22 changes: 0 additions & 22 deletions Dockerfile.opentelemetry.yml

This file was deleted.

14 changes: 13 additions & 1 deletion anime_quiz/wsgi.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
https://docs.djangoproject.com/en/3.1/howto/deployment/wsgi/
"""

# Copyright (c) 2021 Przemysław Buczkowski
# Copyright (c) 2021-24 Przemysław Buczkowski
#
# This file is part of Anime Quiz.
#
Expand All @@ -30,4 +30,16 @@

os.environ.setdefault("DJANGO_SETTINGS_MODULE", "anime_quiz.settings_prod")

try:
from uwsgidecorators import postfork
from opentelemetry.instrumentation.django import DjangoInstrumentor
from opentelemetry.instrumentation.redis import RedisInstrumentor
except ImportError:
pass
else:
@postfork
def init_tracing():
DjangoInstrumentor().instrument()
RedisInstrumentor().instrument()

application = get_wsgi_application()
25 changes: 25 additions & 0 deletions docker-compose.opentelemetry.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
x-environment: &environment
- OTEL_RESOURCE_ATTRIBUTES=service.name=Anime Themes Quiz,deployment.environment=generic:default
- OTEL_EXPORTER_OTLP_PROTOCOL=http/protobuf
- OTEL_EXPORTER_OTLP_TRACES_ENDPOINT=http://172.17.0.1:4318/v1/traces
#- OTEL_AWS_APPLICATION_SIGNALS_EXPORTER_ENDPOINT=http://172.17.0.1:4316/v1/metrics
- OTEL_METRICS_EXPORTER=none
- OTEL_LOGS_EXPORTER=none
- OTEL_TRACES_SAMPLER=xray
- OTEL_TRACES_SAMPLER_ARG=endpoint=http://172.17.0.1:2000
- OTEL_AWS_APPLICATION_SIGNALS_ENABLED=true

services:
anime_quiz:
build:
args:
- EXTRAS=[opentelemetry]
environment: *environment
command: opentelemetry-instrument uwsgi --ini uwsgi.cfg

tasks:
build:
args:
- EXTRAS=[opentelemetry]
environment: *environment
command: opentelemetry-instrument python -m quiz.tasks
2 changes: 1 addition & 1 deletion docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ services:
tasks:
build:
context: .
entrypoint: python -m quiz.tasks
command: python -m quiz.tasks
networks:
- anime_quiz
depends_on:
Expand Down
7 changes: 7 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,13 @@ gcp = [
"django-google-cloud-tasks~=2.17.0",
"google-cloud-logging"
]
opentelemetry = [
"aws-opentelemetry-distro",
"opentelemetry-instrumentation-django",
"opentelemetry-instrumentation-requests",
"opentelemetry-instrumentation-redis",
"uwsgidecorators==1.1.0"
]

[project.urls]
Homepage = "https://quiz.moe"
Expand Down

0 comments on commit d0c289e

Please sign in to comment.