Skip to content

Instantly share code, notes, and snippets.

@leogregianin
leogregianin / prom-k8s-request-limits.md
Created March 19, 2025 11:17 — forked from max-rocket-internet/prom-k8s-request-limits.md
How to display Kubernetes request and limit in Grafana / Prometheus properly

CPU: percentage of limit

A lot of people land when trying to find out how to calculate CPU usage metric correctly in prometheus, myself included! So I'll post what I eventually ended up using as I think it's still a little difficult trying to tie together all the snippets of info here and elsewhere.

This is specific to k8s and containers that have CPU limits set.

To show CPU usage as a percentage of the limit given to the container, this is the Prometheus query we used to create nice graphs in Grafana:

sum(rate(container_cpu_usage_seconds_total{name!~".*prometheus.*", image!="", container_name!="POD"}[5m])) by (pod_name, container_name) /
@leogregianin
leogregianin / custos.py
Last active March 19, 2025 13:40
planilha de custos com pandas
import pandas as pd
# Caminho do arquivo
file_path = "/mnt/data/controle.xlsx"
# Criando um arquivo Excel com múltiplas abas usando xlsxwriter
with pd.ExcelWriter(file_path, engine="xlsxwriter") as writer:
workbook = writer.book
# Criando dados para a aba "Lançamentos"
@leogregianin
leogregianin / benchmark_excel.py
Created January 16, 2024 16:33
benchmarking read excel in python with pandas, polars and calamine
import time
from typing import IO, Iterator
import python_calamine
def f_calamine(file: IO[bytes]) -> Iterator[dict[str, object]]:
workbook = python_calamine.CalamineWorkbook.from_filelike(file)
yield from iter(workbook.get_sheet_by_index(0).to_python())
@leogregianin
leogregianin / xmlbench.py
Created August 28, 2022 20:30 — forked from hhatto/xmlbench.py
benchmark of Python's XML parsing packages
from xml.etree import ElementTree
from lxml.etree import XML
import xmltodict
import untangle
from benchmarker import Benchmarker
N = 1000 * 1
N = 1000 * 100
xml_string = """<?xml version="1.0"?>
@leogregianin
leogregianin / 0001_migrate_to_encrypted.py
Created May 27, 2022 14:59 — forked from thismatters/0001_migrate_to_encrypted.py
Migrating existing columns to use django-cryptography
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from django.db import migrations, models
from django_cryptography.fields import encrypt
app_with_model = 'account'
model_with_column = 'User'
column_to_encrypt = 'email_address'
column_field_class = models.CharField
@leogregianin
leogregianin / match_turtle.py
Created May 25, 2022 20:37 — forked from rochacbruno/match_turtle.py
Turtle e Pattern Match
"""Turtle module http://cursodepython.com.br"""
import turtle
turtle.bgcolor("black")
turtle = turtle.Turtle()
turtle.shape("turtle")
turtle.speed(3)
turtle.width(10)
turtle.color("blue", "yellow")
turtle.pencolor("red")
@leogregianin
leogregianin / brazil_bank.py
Created April 21, 2022 18:49
Brazilian bank list
# Repo: https://github.com/leogregianin/BancosBrasileiros/blob/master/schemas/python.py
import json
import httpx
req = httpx.get('https://raw.githubusercontent.com/guibranco/BancosBrasileiros/master/data/bancos.json')
bank_json = req.text.replace("\n", "")
with open(bank_json) as json_string:
result = bank_from_dict(json.load(json_string))
@leogregianin
leogregianin / perf.py
Last active January 27, 2022 15:25
String filter performance
import timeit
def so_numeros_filter_lambda(texto):
return ''.join(filter(lambda c: ord(c) in range(48, 58), texto))
def so_numeros_filter(texto):
return ''.join(filter(str.isdigit, str(texto)))
def so_numeros_listcomp(texto):
@leogregianin
leogregianin / sys.path.sh
Created December 27, 2021 18:47
Medium article examples
$ python -c "import sys; print(sys.path)"
[
'',
'/usr/local/lib/python3.7',
'~/.local/lib/python3.7/site-packages',
'/usr/local/lib/python3.7/site-packages'
]