Skip to content

Instantly share code, notes, and snippets.

@nymous
nymous / README.md
Last active June 19, 2024 23:42
Logging setup for FastAPI, Uvicorn and Structlog (with Datadog integration)

Logging setup for FastAPI

This logging setup configures Structlog to output pretty logs in development, and JSON log lines in production.

Then, you can use Structlog loggers or standard logging loggers, and they both will be processed by the Structlog pipeline (see the hello() endpoint for reference). That way any log generated by your dependencies will also be processed and enriched, even if they know nothing about Structlog!

Requests are assigned a correlation ID with the asgi-correlation-id middleware (either captured from incoming request or generated on the fly). All logs are linked to the correlation ID, and to the Datadog trace/span if instrumented. This data "global to the request" is stored in context vars, and automatically added to all logs produced during the request thanks to Structlog. You can add to these "global local variables" at any point in an endpoint with `structlog.contextvars.bind_contextvars(custom

@banditkings
banditkings / CustomCalendar.py
Last active March 21, 2023 23:45
helper function to create 5253WW calendar lookup
"""
Make a dataframe for a 52/53-workweek accounting period calendar given the last day of the previous year.
Since many companies use this 52/53 workweek for their fiscal periods, this is a useful function to quickly
build custom lookup tables so you can join between datasets!
"""
import pandas as pd
from datetime import datetime
import numpy as np
@Vigrond
Vigrond / celery_django_redis_ratelimit.py
Last active February 12, 2024 05:42
Celery / Django / Redis Rate Limits done "as expected" - Simple SMTP Example
# Rate limiting with Celery + Django + Redis
# Multiple Fixed Windows Algorithm inspired by Figma https://www.figma.com/blog/an-alternative-approach-to-rate-limiting/
# and Celery's sometimes ambiguous, vague, and one-paragraph documentation
#
# Celery's Task is subclassed and the is_rate_okay function is added
# celery.py or however your App is implemented in Django
import os
import math
@michelmilezzi
michelmilezzi / aws_glue_avoiding_duplicates.py
Last active August 29, 2023 22:08
AWS Glue script showing how to avoid duplicates during a job execution.
import sys
import pydevd
from awsglue.transforms import *
from awsglue.utils import getResolvedOptions
from pyspark.context import SparkContext
from awsglue.context import GlueContext
from awsglue.dynamicframe import DynamicFrame
from pyspark.sql.functions import col
from awsglue.job import Job
@caroso1222
caroso1222 / dom.service.ts
Last active August 18, 2023 13:34
Service to dynamically append Angular components to the body
import {
Injectable,
Injector,
ComponentFactoryResolver,
EmbeddedViewRef,
ApplicationRef
} from '@angular/core';
@Injectable()
export class DomService {
@nitrag
nitrag / commands.config
Last active May 27, 2020 20:54
How to setup GeoDjango on AWS Elastic Beanstalk (EB) with a Custom AMI
container_commands:
001installs:
command: "sudo yum-config-manager --enable epel"
setup_gdal:
command: cd /home/ec2-user/gdal-1.11.2/swig/python && /opt/python/run/venv/bin/python setup.py install