Learning Notes

O11y

<https://www.youtube.com/watch?v=ddZjhv66o_o>

log, metric and trace loguru josn parse

we have 2 way we can log. either we can do console log which is ok for developer when working another is structured log which is mostly in json and this is what enables us to parse the logs to different tools

import sys

from loguru import logger

logger.remove()
logger.add(sys.stdout, level="INFO", serialize=True, backtrace=True)

sentry for catching errors

import sentry_sdk
from sentry_sdk.integrations.fastapi import FastApiIntegration

sentry_sdk.init(
    dsn="YOUR_SENTRY_DSN", #get from sentry
    integrations=[FastApiIntegration()],
    traces_sample_rate=0.1,
    environment="production",
)
import os
import sys
from loguru import logger

# 1. THE CONFIGURATION (The "Filter" Setup)

# We look at the computer's environment to see if we are in production.
# If ENV is 'production', we set the filter to 'INFO'. Otherwise, it's 'DEBUG'.
if os.getenv("ENV") == "production":
    LOG_LIMIT = "INFO"
else:
    LOG_LIMIT = "DEBUG"

# Apply our filter limit to Loguru
logger.remove()  # Remove default setup
logger.add(sys.stderr, level=LOG_LIMIT)  # Set our new limit

# 2. YOUR APPLICATION CODE
def process_payment(amount):
    # This is noise for developers. We leave it in the code forever.
    logger.debug(f"Starting payment function. Amount passed: {amount}")
    # Connect to the bank
    logger.debug("Opening secure connection socket to payment gateway...")
    # The actual business event
    logger.info(f"Successfully charged customer ${amount}!")
# Run the function to see what prints
print(f"--- RUNNING APP WITH LOG_LIMIT = {LOG_LIMIT} ---")
process_payment(50)


pyroscope

import pyroscope

pyroscope.configure(
    application_name="your.python.app",      # Name of your application in Pyroscope
    server_address="http://localhost:4040",   # Your Pyroscope server or Grafana Cloud URL
    basic_auth_username='<Username>',         # Required if sending to Grafana Cloud
    basic_auth_password='<Password>',         # API key or password for Grafana Cloud
    tags={
        "env": "production",
        "region": "us-east-1"
    },
    detect_subprocesses=True,                 # Profile child processes
    oncpu=True,                               # Enable CPU profiling
    gil_only=True                             # (Python specific) Profile only the Global Interpreter Lock
)

Prometheus metrics Grafana dashboard

3 important pillers: logs, metric and traces