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 / 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 / models.py
Created April 20, 2022 18:36 — forked from jacobian/models.py
An example of using many-to-many "through" to augment m2m relationships. See http://www.quora.com/How-do-you-query-with-a-condition-on-a-ManyToMany-model-in-Django for context.
from django.db import models
class Person(models.Model):
name = models.CharField(max_length=200)
groups = models.ManyToManyField('Group', through='GroupMember', related_name='people')
class Meta:
ordering = ['name']
def __unicode__(self):
@leogregianin
leogregianin / settings.py
Created December 15, 2020 14:46 — forked from palewire/settings.py
My current default Django LOGGING configuration
LOGGING = {
'version': 1,
'disable_existing_loggers': False,
'handlers': {
'mail_admins': {
'level': 'ERROR',
'class': 'django.utils.log.AdminEmailHandler'
},
'null': {
'level':'DEBUG',
Install Python 3.7.0 on CentOS/RHEL 7
1.Requirements:
yum install gcc openssl-devel bzip2-devel
2.Download Python 3.7:
cd /usr/src
wget https://www.python.org/ftp/python/3.7.0/Python-3.7.0.tgz
sudo apt-get update
sudo apt-get install python3-pip python3-dev libpq-dev postgresql postgresql-contrib nginx
sudo -u postgres psql
- paste this code in postgres console:
CREATE DATABASE django_project;
CREATE USER username WITH PASSWORD 'pass1234';
ALTER ROLE username SET client_encoding TO 'utf8';
ALTER ROLE username SET default_transaction_isolation TO 'read committed';
@leogregianin
leogregianin / Calisthenics.md
Created February 11, 2019 18:43 — forked from bobuss/Calisthenics.md
The 9 Rules of Object Calisthenics

Object Calisthenics outlines 9 basic rules to apply when performing the exercise:

  • One level of indentation per method.
  • Don't use the ELSE keyword.
  • Wrap all primitives and Strings in classes.
  • First class collections.
  • One dot per line.
  • Don't abbreviate.
  • Keep all classes less than 50 lines.
  • No classes with more than two instance variables.