Skip to content

Instantly share code, notes, and snippets.

@nymous
nymous / README.md
Last active November 19, 2024 08:17
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

@flushentitypacket
flushentitypacket / DragScrollProvider.tsx
Last active April 4, 2024 22:29
Typescript React component to provide click-and-drag scrolling
import * as React from 'react'
type DragScrollProvisions = {
onMouseDown: React.MouseEventHandler<HTMLElement>,
ref: React.Ref<HTMLElement>,
}
export type Props = {
children: (provisions: DragScrollProvisions) => React.ReactNode,
}

Motivation

  • expression-oriented programming one of the great advances of FP
  • expressions plug together like legos, making more malleable programming experience in-the-small

Examples

Write in an expression-oriented style, scoping variables as locally as possible:

@kourge
kourge / unit.ts
Created January 19, 2017 02:45
Type-safe units in TypeScript
namespace TypeBranding {
// This incurs no runtime cost. It is a purely compile-time construct.
type Meters = number & { __metersBrand: any };
type Miles = number & { __milesBrand: any };
function Meters(i: number): Meters { return i as Meters; }
function Miles(i: number): Miles { return i as Miles; }
let a: Meters;
@scottopell
scottopell / fix_exfat_drive.md
Last active November 16, 2024 14:29
Fix corrupted exFAT disk macOS/OSX

exFAT support on macOS seems to have some bugs because my external drives with exFAT formatting will randomly get corrupted.

Disk Utility is unable to repair this at first, but the fix is this:

  1. Use diskutil list to find the right drive id.
  2. You want the id under the IDENTIFIER column, it should look like disk1s1
  3. Run sudo fsck_exfat -d <id from above>. eg sudo fsck_exfat -d disk1s3
  4. -d is debug so you'll see all your files output as they're processed.
  5. Answer YES if it gives you the prompt Main boot region needs to be updated. Yes/No?
@mpneuried
mpneuried / Makefile
Last active August 22, 2024 10:36
Simple Makefile to build, run, tag and publish a docker containier to AWS-ECR
# import config.
# You can change the default config with `make cnf="config_special.env" build`
cnf ?= config.env
include $(cnf)
export $(shell sed 's/=.*//' $(cnf))
# import deploy config
# You can change the default deploy config with `make cnf="deploy_special.env" release`
dpl ?= deploy.env
include $(dpl)
@rdarder
rdarder / optional.ts
Created December 28, 2015 19:51
Optional<T> a la Java in typescript
///<reference path="iterator.ts"/>
///<reference path="checks.ts"/>
import AbstractIterator = Iterators.AbstractIterator;
import Iterator = Iterators.Iterator;
import checkNotNull = Preconditions.checkNotNull;
abstract class Optional<T> {
static absent<T>():Optional<T> {
@heyimalex
heyimalex / WebStorage.js
Last active May 1, 2020 09:17
localStorage sync with redux
export default class WebStorage {
constructor(key, storageArea = window.localStorage) {
this.key = key;
this.storageArea = storageArea;
}
load(defaultValue) {
const serialized = this.storageArea.getItem(this.key);
return serialized === null ? defaultValue : this.deserialize(serialized);
}
@MohamedAlaa
MohamedAlaa / tmux-cheatsheet.markdown
Last active November 19, 2024 10:49
tmux shortcuts & cheatsheet

tmux shortcuts & cheatsheet

start new:

tmux

start new with session name:

tmux new -s myname