Aller au contenu

Commandes Pyroscope

Grafana Pyroscope est une plateforme de profilage continu qui vous aide à trouver les goulots d’étranglement de performance dans vos applications. Elle supporte plusieurs langages, s’intègre nativement avec Grafana et offre les modes push et pull pour la collecte de profils.

Installation

Linux/Ubuntu

# Download Pyroscope binary
curl -fL https://github.com/grafana/pyroscope/releases/latest/download/pyroscope_Linux_x86_64.tar.gz | tar xz
sudo mv pyroscope /usr/local/bin/

# Verify
pyroscope --version

# Docker
docker run -d --name pyroscope \
  -p 4040:4040 \
  grafana/pyroscope:latest

Helm (Kubernetes)

helm repo add grafana https://grafana.github.io/helm-charts
helm repo update
helm install pyroscope grafana/pyroscope -n pyroscope --create-namespace

Configuration du serveur

# pyroscope-config.yaml
# Démarrer avec : pyroscope -config.file=pyroscope-config.yaml

storage:
  backend: filesystem
  filesystem:
    dir: /var/lib/pyroscope/data

server:
  http_listen_port: 4040

limits:
  max_query_lookback: 720h
  max_query_length: 6h

scrape_configs:
- job_name: "my-go-service"
  scrape_interval: "15s"
  static_configs:
  - targets: ["my-service:6060"]
  profiling_config:
    pprof_config:
      process_cpu:
        enabled: true
      memory:
        enabled: true
# Démarrer le serveur Pyroscope
pyroscope -config.file=pyroscope-config.yaml

# Démarrer avec la configuration par défaut
pyroscope

# Accéder à l'interface web
# http://localhost:4040

Mode Push — SDK Go

// Install: go get github.com/grafana/pyroscope-go

// Add to your Go application main():
// import "github.com/grafana/pyroscope-go"
//
// pyroscope.Start(pyroscope.Config{
//     ApplicationName: "my-go-service",
//     ServerAddress:   "http://pyroscope:4040",
//     ProfileTypes: []pyroscope.ProfileType{
//         pyroscope.ProfileCPU,
//         pyroscope.ProfileAllocObjects,
//         pyroscope.ProfileAllocSpace,
//         pyroscope.ProfileInuseObjects,
//         pyroscope.ProfileInuseSpace,
//         pyroscope.ProfileGoroutines,
//         pyroscope.ProfileMutexCount,
//         pyroscope.ProfileMutexDuration,
//         pyroscope.ProfileBlockCount,
//         pyroscope.ProfileBlockDuration,
//     },
// })

Mode Push — SDK Python

# Installer le SDK Python
pip install pyroscope-io
# Ajouter à votre application Python
import pyroscope

pyroscope.configure(
    application_name="my-python-service",
    server_address="http://pyroscope:4040",
    tags={
        "region": "us-east-1",
        "env": "production",
    },
)

# Utiliser les tags pour l'étiquetage dynamique
with pyroscope.tag_wrapper({"endpoint": "/api/users"}):
    handle_users_request()

Mode Push — SDK Java

# Ajouter aux arguments JVM
# Télécharger le JAR de l'agent Java depuis les releases GitHub
java -javaagent:pyroscope.jar \
  -Dpyroscope.application.name=my-java-service \
  -Dpyroscope.server.address=http://pyroscope:4040 \
  -Dpyroscope.format=jfr \
  -jar my-app.jar

Mode Push — SDK Ruby

# Installer la gem Ruby
gem install pyroscope
# Ajouter à votre application Ruby
require 'pyroscope'

Pyroscope.configure do |config|
  config.application_name = "my-ruby-service"
  config.server_address = "http://pyroscope:4040"
  config.tags = {
    "region" => "us-east-1",
    "env" => "production",
  }
end

Mode Push — SDK Node.js

# Installer le package Node.js
npm install @pyroscope/nodejs
// Ajouter à votre application Node.js
const Pyroscope = require('@pyroscope/nodejs');

Pyroscope.init({
  serverAddress: 'http://pyroscope:4040',
  appName: 'my-node-service',
  tags: {
    region: 'us-east-1',
    env: 'production',
  },
});

Pyroscope.start();

Mode Push — SDK .NET

# Installer le package NuGet
dotnet add package Pyroscope
// Ajouter à votre application .NET Program.cs
// Pyroscope.Profiler.Instance.SetTag("region", "us-east-1");
// Utiliser les variables d'environnement PYROSCOPE_APPLICATION_NAME et PYROSCOPE_SERVER_ADDRESS
// pour configurer

Mode Pull — Collecte des endpoints pprof

# Configurer Pyroscope pour collecter les endpoints pprof (dans pyroscope-config.yaml)
scrape_configs:
- job_name: "go-services"
  scrape_interval: "15s"
  static_configs:
  - targets: ["app1:6060", "app2:6060"]
  profiling_config:
    pprof_config:
      process_cpu:
        enabled: true
        path: "/debug/pprof/profile"
        delta: true
      memory:
        enabled: true
        path: "/debug/pprof/heap"
        delta: true
      goroutine:
        enabled: true
        path: "/debug/pprof/goroutine"
      block:
        enabled: true
        path: "/debug/pprof/block"
        delta: true
      mutex:
        enabled: true
        path: "/debug/pprof/mutex"
        delta: true

- job_name: "kubernetes-pods"
  scrape_interval: "15s"
  kubernetes_sd_configs:
  - role: pod
  relabel_configs:
  - source_labels: [__meta_kubernetes_pod_annotation_pyroscope_io_scrape]
    action: keep
    regex: true
  - source_labels: [__meta_kubernetes_pod_annotation_pyroscope_io_port]
    action: replace
    target_label: __address__
    regex: (.+)
    replacement: ${1}

Intégration Grafana

# Ajouter Pyroscope comme source de données dans Grafana
# 1. Naviguer vers Configuration > Data Sources > Add data source
# 2. Rechercher "Pyroscope" (ou "Grafana Pyroscope")
# 3. Définir l'URL sur http://pyroscope:4040
# 4. Cliquer sur "Save & Test"

# Grafana Explore :
# 1. Sélectionner la source de données Pyroscope
# 2. Choisir le type de profil (cpu, mémoire, goroutine, etc.)
# 3. Filtrer par étiquettes (service_name, env, region)
# 4. Voir le flame graph, le tableau, ou les deux

Requêtes de profils

# Requêter les profils via l'API HTTP
# Lister les noms d'étiquettes disponibles
curl -s http://localhost:4040/pyroscope/api/v1/labels | python3 -m json.tool

# Lister les valeurs d'étiquettes
curl -s "http://localhost:4040/pyroscope/api/v1/label-values?label=service_name" | python3 -m json.tool

# Rendre un profil (retourne le format pprof)
curl -s "http://localhost:4040/pyroscope/api/v1/profiles/render?query=process_cpu:cpu:nanoseconds:cpu:nanoseconds{service_name=\"my-service\"}&from=now-1h&until=now" -o profile.pb.gz

# Analyser avec go tool pprof
go tool pprof -http=:8080 profile.pb.gz

Vue différentielle (Comparaison de profils)

# Dans l'interface web Pyroscope :
# 1. Sélectionner le mode "Comparison" dans la barre supérieure
# 2. Définir la plage temporelle de référence à gauche
# 3. Définir la plage temporelle de comparaison à droite
# 4. Voir le flame graph différentiel
# Rouge = temps augmenté, Vert = temps diminué

# Via API — comparer deux plages temporelles
curl -s "http://localhost:4040/pyroscope/api/v1/profiles/render-diff?leftQuery=process_cpu:cpu:nanoseconds{service_name=\"my-service\"}&leftFrom=now-2h&leftUntil=now-1h&rightQuery=process_cpu:cpu:nanoseconds{service_name=\"my-service\"}&rightFrom=now-1h&rightUntil=now"

Types de profils

Type de profilDescription
process_cpuTemps CPU consommé par le processus
memoryAllocations mémoire du tas
goroutineNombre de goroutines actives (Go)
mutexContention de mutex (Go)
blockOpérations bloquantes (Go)
wallTemps horloge murale
alloc_objectsNombre d’allocations
alloc_spaceOctets alloués
inuse_objectsObjets actifs en mémoire
inuse_spaceOctets actifs en mémoire

Référence rapide

ComposantPort par défautFonction
Pyroscope Server4040Stockage, requêtes, interface web
Grafana3000Visualisation et tableaux de bord
pprof endpoints6060Profilage d’applications Go
FonctionnalitéDescription
Mode pushLes SDKs envoient les profils à Pyroscope
Mode pullPyroscope collecte les endpoints pprof
Flame graphsVisualisation interactive CPU/mémoire
Vue différentielleComparer les profils entre périodes
Filtrage par étiquettesFiltrer par service, env, région, etc.
Natif GrafanaSource de données Pyroscope intégrée