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

chore: Add structured logging for DataDog integration (M2-8041) #1675

Merged
merged 24 commits into from
Dec 11, 2024
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 .dockerignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,5 @@ docker-compose.yaml
*.md
*.rst
venv*/
allure-results
.git
7 changes: 6 additions & 1 deletion .env.default
Original file line number Diff line number Diff line change
Expand Up @@ -94,4 +94,9 @@ OTEL_EXPORTER_OTLP_TRACES_ENDPOINT=
# OTEL_TRACES_EXPORTER
# OTEL_EXPORTER_OTLP_CERTIFICATE

MULTI_INFORMANT__TEMP_RELATION_EXPIRY_SECS=86400
MULTI_INFORMANT__TEMP_RELATION_EXPIRY_SECS=86400

# False for local dev
DD_TRACE_ENABLED=false
DD_LOGS_ENABLED=false
LOG_JSON_FORMAT=false
1 change: 1 addition & 0 deletions .github/workflows/pr-open.yml
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ jobs:
permissions:
issues: write
pull-requests: write
discussions: write
needs: [ run-unit-tests ]
steps:
- name: Post the link to the report
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/run_build_deploy.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
name: Build and Deploy
on:
push:
# branches:
# - develop
branches:
- develop
workflow_dispatch: {}


Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -62,3 +62,4 @@ venv/

# MacOS
.DS_Store

10 changes: 8 additions & 2 deletions Pipfile
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,14 @@ taskiq = { extras = ["reload"], version = "==0.11.7" }
taskiq-aio-pika = "==0.4.1"
taskiq-fastapi = "==0.3.2"
taskiq-redis = "==1.0.2"
typer = "==0.15.1"
uvicorn = { extras = ["standard"], version = "==0.32.1" }
typer = "==0.12.5"
uvicorn = { extras = ["standard"], version = "==0.32.0" }
pyjwt = "==2.9.0"
ddtrace = "==2.17.2"
bytecode = "==0.16.0"
structlog = "==24.4.0"
asgi-correlation-id = "==4.3.4"


[dev-packages]
allure-pytest = "==2.13.5"
Expand Down
270 changes: 187 additions & 83 deletions Pipfile.lock

Large diffs are not rendered by default.

5 changes: 5 additions & 0 deletions compose/fastapi/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,11 @@ RUN sed -i 's/\r$//g' /fastapi-entrypoint && chmod +x /fastapi-entrypoint
COPY --chown=code:code ./compose/fastapi/start /fastapi-start
RUN sed -i 's/\r$//g' /fastapi-start && chmod +x /fastapi-start

COPY --chown=code:code ./compose/fastapi/start-datadog /fastapi-start-datadog
RUN sed -i 's/\r$//g' /fastapi-start-datadog && chmod +x /fastapi-start-datadog



COPY --chown=code:code ./compose/fastapi/migrate /fastapi-migrate
RUN sed -i 's/\r$//g' /fastapi-migrate && chmod +x /fastapi-migrate

Expand Down
2 changes: 1 addition & 1 deletion compose/fastapi/ecs-start-feature
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@ set -eo pipefail
set -o nounset

ENV=testing /fastapi-migrate
/fastapi-start
/fastapi-start-datadog
9 changes: 6 additions & 3 deletions compose/fastapi/start
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ set -o nounset
export UVICORN_HOST="0.0.0.0"
export UVICORN_PORT=80

opentelemetry-instrument \
--logs_exporter otlp \
uvicorn src.main:app --reload --host ${UVICORN_HOST} --port ${UVICORN_PORT} --proxy-headers
#opentelemetry-instrument \
# --logs_exporter otlp \
# uvicorn src.main:app --reload --host ${UVICORN_HOST} --port ${UVICORN_PORT} --proxy-headers

rcmerlo marked this conversation as resolved.
Show resolved Hide resolved

uvicorn src.main:app --reload --host ${UVICORN_HOST} --port ${UVICORN_PORT} --proxy-headers
15 changes: 15 additions & 0 deletions compose/fastapi/start-datadog
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#!/bin/bash

set -o errexit
set -o pipefail
set -o nounset

# https://www.uvicorn.org/settings/
export UVICORN_HOST="0.0.0.0"
export UVICORN_PORT=80


LOG_JSON_FORMAT=true DD_TRACE_ENABLED=true uvicorn main:app \
--host ${UVICORN_HOST} --port ${UVICORN_PORT} \
--reload --proxy-headers \
--log-config uvicorn_disable_logging.json
7 changes: 7 additions & 0 deletions copilot/mindlogger-backend/addons/svc-role.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,13 @@ Resources:
- "arn:aws:s3:::cmiml-feature-answer/*"
- "arn:aws:s3:::cmiml-feature-operations/*"

- Sid: DataDogAgent
Effect: Allow
Action:
- "ecs:ListClusters"
- "ecs:ListContainerInstances"
- "ecs:DescribeContainerInstances"
Resource: ["*"]


Outputs:
Expand Down
72 changes: 70 additions & 2 deletions copilot/mindlogger-backend/manifest.yml
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,19 @@ storage:
efs: false
path: /app/uploads
read_only: false

# For Datadog
# sock:
# efs: false
# path: '/var/run/docker.sock'
# read_only: true
# proc:
# efs: false
# path: '/proc/'
# read_only: true
# cgroup:
# efs: false
# path: '/sys/fs/cgroup/'
# read_only: true

variables:
# Python configurations
Expand Down Expand Up @@ -119,6 +131,10 @@ variables:
# CDN__LEGACY_ACCESS_KEY:
CDN__TTL_SIGNED_URLS: 3600

DD_ENV: feature
DD_SERVICE: backend-api
DD_VERSION: ${COPILOT_ENVIRONMENT_NAME}

# jsonld converter
JSONLD_CONVERTER__PROTOCOL_PASSWORD:

Expand Down Expand Up @@ -158,4 +174,56 @@ taskdef_overrides:
value:
Name: "nofile"
SoftLimit: 1048576
HardLimit: 1048576
HardLimit: 1048576

logging:
destination:
Name: "datadog"
Host: "http-intake.logs.datadoghq.com"
dd_service: "backend-api"
dd_source: "backend"
dd_message_key: "log"
dd_tags: "project:fluentbit"
TLS: "on"
provider: "ecs"
configFilePath: "/fluent-bit/configs/parse-json.conf"
enableMetadata: true
secretOptions:
apikey:
secretsmanager: "cmiml-feature-${COPILOT_ENVIRONMENT_NAME}:DD_API_KEY::"


sidecars:
datadog:
image: public.ecr.aws/datadog/agent:7
variables:
ECS_FARGATE: true
DD_SITE: us5.datadoghq.com
DD_ENV: ${COPILOT_ENVIRONMENT_NAME}
DD_LOGS_ENABLED: true
DD_LOGS_CONFIG_CONTAINER_COLLECT_ALL: true
DD_CONTAINER_EXCLUDE: name:datadog-agent
DD_APM_ENABLED: true
secrets:
DD_API_KEY:
secretsmanager: "cmiml-feature-${COPILOT_ENVIRONMENT_NAME}:DD_API_KEY::"

#/copilot/${COPILOT_APPLICATION_NAME}/${COPILOT_ENVIRONMENT_NAME}/secrets/DATADOG_API_KEY
# mount_points:
# - source_volume: sock
# path: '/var/run/docker.sock'
# - source_volume: proc
# path: '/proc/'
# - source_volume: cgroup
# path: '/sys/fs/cgroup/'

# firelens:
# essential: true
# image: amazon/aws-for-fluent-bit:stable
# name: log_router
# firelensConfiguration:
# type: fluentbit
# options:
# enable-ecs-log-metadata: 'true'
# config-file-type: file
# config-file-value: "/fluent-bit/configs/parse-json.conf"
2 changes: 1 addition & 1 deletion docker-compose.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ services:
args:
- PIPENV_EXTRA_ARGS=--dev
entrypoint: /fastapi-entrypoint
command: /fastapi-start
command: /fastapi-start-datadog
env_file: .env
depends_on:
- postgres
Expand Down
21 changes: 21 additions & 0 deletions src/infrastructure/app.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from typing import Iterable, Type

import sentry_sdk
from asgi_correlation_id import CorrelationIdMiddleware
from fastapi import FastAPI
from fastapi.exceptions import RequestValidationError
from fastapi.routing import APIRouter
Expand Down Expand Up @@ -28,6 +29,7 @@
import middlewares as middlewares_
from apps.shared.exception import BaseError
from config import settings
from infrastructure.datadog import StructuredLoggingMiddleware
from infrastructure.http.execeptions import (
custom_base_errors_handler,
pydantic_validation_errors_handler,
Expand Down Expand Up @@ -77,6 +79,8 @@
),
(middlewares_.InternalizationMiddleware, {}),
(middlewares_.CORSMiddleware, middlewares_.cors_options),
(StructuredLoggingMiddleware, {}),
(CorrelationIdMiddleware, {}),
)


Expand Down Expand Up @@ -108,4 +112,21 @@ def create_app():
# https://github.com/Tufin/oasdiff/issues/52
app.openapi_version = "3.0.3"

# UGLY HACK
# Datadog's `TraceMiddleware` is applied as the very first middleware
# in the list, by patching `FastAPI` constructor.
# Unfortunately that means that it is the innermost middleware, so the trace/span are
# created last in the middleware
# chain. Because we want to add the trace_id/span_id in the access log,
# we need to extract it from the middleware list,
# put it back as the outermost middleware, and rebuild the middleware stack.
# tracing_middleware = next(
# (m for m in app.user_middleware if m.cls == TraceMiddleware), None
# )
# if tracing_middleware is not None:
# app.user_middleware = [m for m in app.user_middleware if m.cls != TraceMiddleware]
#
# app.user_middleware.insert(0, tracing_middleware)
# app.middleware_stack = app.build_middleware_stack()

return app
Loading
Loading