Skip to content

Commit

Permalink
Fast forward branch
Browse files Browse the repository at this point in the history
  • Loading branch information
andrevitalb committed Dec 19, 2024
2 parents 81d8639 + b6fffcf commit 9250ace
Show file tree
Hide file tree
Showing 12 changed files with 51 additions and 48 deletions.
5 changes: 3 additions & 2 deletions .github/workflows/pr-open.yml
Original file line number Diff line number Diff line change
Expand Up @@ -49,13 +49,13 @@ jobs:
permissions:
issues: write
pull-requests: write
discussions: write
statuses: write
needs: [ run-unit-tests ]
steps:
- name: Post the link to the report
uses: guibranco/github-status-action-v2@v1
with:
authToken: ${{secrets.PAT_TOKEN}}
authToken: ${{secrets.GITHUB_TOKEN}}
context: 'Unit Test report'
state: ${{ needs.run-unit-tests.result }}
sha: ${{ github.event.pull_request.head.sha }}
Expand Down Expand Up @@ -141,6 +141,7 @@ jobs:
permissions:
issues: write
pull-requests: write
statuses: write
needs: [run-e2e-tests]
steps:
- name: "Send Slack message on failure"
Expand Down
4 changes: 3 additions & 1 deletion .github/workflows/run_build_deploy.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -98,13 +98,15 @@ jobs:
echo "IMAGE_NAME=${ECR_REPO}:${SHORT_SHA}" >> "$GITHUB_OUTPUT"
- name: Build and push
uses: docker/build-push-action@v5
uses: docker/build-push-action@v6
with:
context: .
file: ./compose/fastapi/Dockerfile
push: true
build-args: |
PIPENV_EXTRA_ARGS=--dev
DD_GIT_REPOSITORY_URL=${{ github.repositoryUrl }}
DD_GIT_COMMIT_SHA=${{ github.sha }}
cache-to: mode=max,image-manifest=true,oci-mediatypes=true,type=registry,ref=${{ env.ECR_REPO }}:cache
cache-from: type=registry,ref=${{ env.ECR_REPO }}:cache
tags: |
Expand Down
6 changes: 6 additions & 0 deletions compose/fastapi/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,12 @@ RUN apt-get -y update && apt-get -y upgrade \
ARG USERNAME=code
ARG USER_UID=1000
ARG USER_GID=$USER_UID

ARG DD_GIT_REPOSITORY_URL
ARG DD_GIT_COMMIT_SHA
ENV DD_GIT_REPOSITORY_URL=${DD_GIT_REPOSITORY_URL}
ENV DD_GIT_COMMIT_SHA=${DD_GIT_COMMIT_SHA}

RUN groupadd --gid $USER_GID $USERNAME \
&& useradd --uid $USER_UID --gid $USER_GID -m $USERNAME -s /bin/bash

Expand Down
6 changes: 4 additions & 2 deletions compose/fastapi/start-datadog
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
#!/bin/bash

# Fargate does not have the metadata service, this will fail there but that's ok
TOKEN=$(curl -X PUT "http://169.254.169.254/latest/api/token" -H "X-aws-ec2-metadata-token-ttl-seconds: 21600") && \
export DD_AGENT_HOST=$(curl http://169.254.169.254/latest/meta-data/local-ipv4 -H "X-aws-ec2-metadata-token: $TOKEN")

set -o errexit
set -o pipefail
set -o nounset
Expand All @@ -8,8 +12,6 @@ set -o nounset
export UVICORN_HOST="0.0.0.0"
export UVICORN_PORT=80

TOKEN=$(curl -X PUT "http://169.254.169.254/latest/api/token" -H "X-aws-ec2-metadata-token-ttl-seconds: 21600")
export DD_AGENT_HOST=$(curl http://169.254.169.254/latest/meta-data/local-ipv4 -H "X-aws-ec2-metadata-token: $TOKEN")
export LOG_JSON_FORMAT=true
export DD_TRACE_ENABLED=true

Expand Down
10 changes: 5 additions & 5 deletions copilot/mindlogger-backend/manifest.yml
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,6 @@ storage:
variables:
# Python configurations
# ENV: testing

# PostgreSQL configurations
DATABASE__HOST: cmiml-feature-db.cjg844g2kgxp.us-east-1.rds.amazonaws.com
DATABASE__PORT: 5432
Expand Down Expand Up @@ -181,9 +180,9 @@ logging:
Name: "datadog"
Host: "http-intake.logs.datadoghq.com"
dd_service: "backend-api"
dd_source: "backend"
dd_source: "feature"
dd_message_key: "log"
dd_tags: "project:fluentbit"
dd_tags: "version:${COPILOT_ENVIRONMENT_NAME},env:feature"
TLS: "on"
provider: "ecs"
configFilePath: "/fluent-bit/configs/parse-json.conf"
Expand All @@ -198,8 +197,9 @@ sidecars:
image: public.ecr.aws/datadog/agent:7
variables:
ECS_FARGATE: true
DD_SITE: us5.datadoghq.com
DD_ENV: ${COPILOT_ENVIRONMENT_NAME}
DD_SITE: datadoghq.com
DD_ENV: feature
DD_VERSION: ${COPILOT_ENVIRONMENT_NAME}
DD_LOGS_ENABLED: true
DD_LOGS_CONFIG_CONTAINER_COLLECT_ALL: true
DD_CONTAINER_EXCLUDE: name:datadog-agent
Expand Down
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-datadog
command: /fastapi-start
env_file: .env
depends_on:
- postgres
Expand Down
4 changes: 2 additions & 2 deletions src/apps/mailing/static/templates/invitation_new_user_el.html
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@
</a>
</td>
</tr>
{% include 'blocks/team_info_en.html' %}
{% include 'blocks/team_info_el.html' %}
</table>
{% include 'footers/footer_info_en.html' %}
{% include 'footers/footer_info_el.html' %}
</body>

</html>
17 changes: 0 additions & 17 deletions src/infrastructure/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,21 +112,4 @@ 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
19 changes: 8 additions & 11 deletions src/infrastructure/datadog.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import logging
import sys
import time

import structlog
Expand All @@ -8,8 +7,6 @@
from fastapi import Request, Response
from starlette.middleware.base import BaseHTTPMiddleware
from structlog.types import EventDict, Processor
from uvicorn.protocols.utils import get_path_with_query_string


# Much of this is borrowed from: https://gist.github.com/Brymes/cd8f9f138e12845417a246822f64ca26

Expand Down Expand Up @@ -63,7 +60,7 @@ def setup_structured_logging(json_logs: bool = False, log_level: str = "INFO"):
structlog.stdlib.PositionalArgumentsFormatter(),
structlog.stdlib.ExtraAdder(),
drop_color_message_key,
tracer_injection,
# tracer_injection, # Not needed, patched in
timestamper,
structlog.processors.dict_tracebacks,
structlog.processors.StackInfoRenderer(),
Expand All @@ -79,10 +76,10 @@ def setup_structured_logging(json_logs: bool = False, log_level: str = "INFO"):

structlog.configure(
processors=shared_processors
+ [
# Prepare event dict for `ProcessorFormatter`.
structlog.stdlib.ProcessorFormatter.wrap_for_formatter,
],
+ [
# Prepare event dict for `ProcessorFormatter`.
structlog.stdlib.ProcessorFormatter.wrap_for_formatter,
],
logger_factory=structlog.stdlib.LoggerFactory(),
cache_logger_on_first_use=True,
)
Expand Down Expand Up @@ -164,9 +161,9 @@ async def dispatch(self, request: Request, call_next) -> Response:
access_logger = structlog.stdlib.get_logger("api.access")
process_time = time.perf_counter_ns() - start_time
status_code = response.status_code
url = get_path_with_query_string(request.scope)
client_host = request.client.host
client_port = request.client.port
url = request.url
client_host = request.client.host if request.client else None
client_port = request.client.port if request.client else None
real_host = request.headers.get("X-Forwarded-For", client_host)
http_method = request.method
http_version = request.scope["http_version"]
Expand Down
2 changes: 0 additions & 2 deletions src/infrastructure/http/execeptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,6 @@ def custom_base_errors_handler(_: Request, error: BaseError) -> JSONResponse:
]
)



return JSONResponse(
response.dict(by_alias=True),
status_code=error.status_code,
Expand Down
2 changes: 1 addition & 1 deletion src/infrastructure/logger.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,4 @@
LOG_JSON_FORMAT = parse_obj_as(bool, os.getenv("LOG_JSON_FORMAT", False))
LOG_LEVEL = os.getenv("LOG_LEVEL", "INFO")
setup_structured_logging(json_logs=LOG_JSON_FORMAT, log_level=LOG_LEVEL)
logger = structlog.stdlib.get_logger("api")
logger = structlog.stdlib.get_logger("api") # type: ignore
22 changes: 18 additions & 4 deletions src/main.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,30 @@
import os
import logging
import os

# Import DataDog tracer ASAP
if os.getenv("DD_TRACE_ENABLED", "false").lower() == "true":
logging.getLogger("main").setLevel(logging.INFO)
logging.getLogger("main").addHandler(logging.StreamHandler())
logging.getLogger("main").info("Enabling Datadog")
# import ddtrace.auto # noqa
from ddtrace import patch
from ddtrace import config, patch

# Manually patch. The auto patcher throws some errors in AMQP (which it doesn't support so why patch it??)
patch(sqlalchemy=True, fastapi=True, botocore=True, asyncpg=True, httpx=True, jinja2=True,
requests=True, starlette=True, structlog=True)
patch(
sqlalchemy=True,
fastapi=True,
botocore=True,
asyncpg=True,
httpx=True,
jinja2=True,
requests=True,
starlette=True,
structlog=True,
futures=True,
gevent=True,
redis=True,
)
config.botocore["distributed_tracing"] = True


from infrastructure.app import create_app
Expand Down

0 comments on commit 9250ace

Please sign in to comment.