可观测性之profile

2026-02-09  本文已影响0人  wwq2020

背景

使用pyroscope可以对应用程序进行profile,从而了解应用程序的性能问题。

操作步骤

安装pyroscope和grafana

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

部署测试服务

创建demo.go内容如下

package main

import (
    "io"
    "net/http"
    _ "net/http/pprof"
)

func main() {
    http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
        io.WriteString(w, "hello world")
    })
    if err := http.ListenAndServe(":80", nil); err != nil {
        panic(err)
    }
}

创建Dockerfile内容如下

from golang:1.25 as builder
workdir /app
copy demo.go .
copy go.mod .
run CGO_ENABLED=0 go build -o demo .

from alpine
workdir /app
copy --from=builder /app/demo .
cmd ["/app/demo"]

构建测试镜像

docker build -t demo:v0.1 .

部署测试,yaml如下

apiVersion: apps/v1
kind: Deployment
metadata:
  name: demo
  namespace: pyroscope-test
spec:
  replicas: 1
  selector:
    matchLabels:
      app.kubernetes.io/name: demo
  template:
    metadata:
      annotations:
        profiles.grafana.com/cpu.port_name: http
        profiles.grafana.com/cpu.scrape: "true"
      labels:
        app.kubernetes.io/name: demo
        name: demo
    spec:
      containers:
      - image: demo:v0.1
        imagePullPolicy: IfNotPresent
        name: demo
        ports:
        - containerPort: 80
          name: http
          protocol: TCP
      restartPolicy: Always

查看profile

端口映射出来

kubectl port-forward svc/grafana 3000:3000

打开

http://localhost:3000

选择Drilldown,选择Profiles,选择profile type为cpu,如下


image.png

选择flamegraph,得到如下


image.png
image.png
上一篇 下一篇

猜你喜欢

热点阅读