Skip to content

Instantly share code, notes, and snippets.

View jeffs's full-sized avatar

Jeff Schwab jeffs

View GitHub Profile
@jeffs
jeffs / README.md
Created April 9, 2024 18:28 — forked from nymous/README.md
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

@jeffs
jeffs / fibfib.rs
Last active February 18, 2024 23:57
Recursive Fibonacci sequence
/// Wraps an iterator of type I by creating it on the first call to .next(). Creates the wrapped
/// iterator by calling F.
enum Delay<I: Iterator, F: FnOnce() -> I> {
// New wraps an Option only so that we can take F out of it during the transition from New to
// Old. It feels like this shouldn't be necessary, but we're not allowed to move out of self in
// the body of Iterator::next, which (like any FnMut) merely borrows (rather than owning) self.
// See also StackOverflow:
// <https://stackoverflow.com/questions/36557412/how-can-i-change-enum-variant-while-moving-the-field-to-the-new-variant>
New(Option<F>),
Old(I),
@jeffs
jeffs / main.rs
Last active May 22, 2022 12:05
Rust File Mover Error
use std::{fs, io::Write};
fn main() {
println!("-Enter the account ID you wish to calculate for-");
let mut id = String::new();
std::io::stdin().read_line(&mut id).unwrap();
let id = id.trim();
println!("-Enter dummy data-");
let mut dd = String::new();
items.reduce(update, initialValue)
@jeffs
jeffs / nested-boring-reduce.py
Created April 9, 2021 05:42
Python's reduce function
from functools import reduce
result = reduce(update, items, initial_value)
@jeffs
jeffs / nested-boring-small-function.py
Last active April 9, 2021 06:34
A typical small function
def do_something(items):
result = initial_value
for item in items:
result = update(result, item)
return result
value = do_something(items)
@jeffs
jeffs / improve-support.md
Last active September 29, 2020 18:26
What could we do to improve your experience next time?

Firstly, avoid the need for users to contact support:

  1. Don't make breaking changes. (In the case at hand, the behavior of ⌘K was changed in a surprising and irritating way.)
  2. If you must make breaking changes:
  • Announce them loudly. (You did not.)
  • Provide an opt-out, or at least a carrot that's bigger than the stick. (You did neither.)

Secondly, make the "Contact us" easier to find: Don't make me jump through multiple links and support pages on the off chance that I'll find an existing answer to my question.

@jeffs
jeffs / dart.md
Created August 7, 2020 14:16 — forked from paulmillr/dart.md
Leaked internal google dart email

---------- Forwarded message ----------

From: Mark S. Miller <erights@google.com>
Date: Tue, Nov 16, 2010 at 3:44 PM
Subject: "Future of Javascript" doc from our internal "JavaScript Summit"
last week
To: javascript-standard@google.com
@jeffs
jeffs / death.sh
Created July 25, 2019 02:52
Launch a process in the background, then kill it after an hour.
# Dummy function. In your case, blast2 is a real program.
blast2() {
let 'i = 0'
while true; do
let 'i++'
echo $i
sleep 1
done
}
@jeffs
jeffs / bashrc
Created December 14, 2018 20:56
Touching files in new directories
touch() {
for f in "$@"; do
mkdir -p "$(dirname "$f")" && /usr/bin/touch "$f"
done
}