Skip to content

Commit 0eeaa77

Browse files
committed
Enable profiling in pull mode. Enable profiling in k8s and docker microservice setups
1 parent 6421f04 commit 0eeaa77

File tree

6 files changed

+37
-21
lines changed

6 files changed

+37
-21
lines changed

alloy/cloud-microservices.alloy

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ grafana_cloud.stack "receivers" {
1111
token = env("GRAFANA_CLOUD_TOKEN")
1212
}
1313

14+
// discover containers running QuickPizza
1415
discovery.docker "application_containers" {
1516
host = "unix:///var/run/docker.sock"
1617
filter {
@@ -19,6 +20,7 @@ discovery.docker "application_containers" {
1920
}
2021
}
2122

23+
// set expected labels
2224
discovery.relabel "application_containers" {
2325
rule {
2426
target_label = "job"
@@ -40,6 +42,11 @@ discovery.relabel "application_containers" {
4042
target_label = "service_namespace"
4143
replacement = "quickpizza"
4244
}
45+
// the `namespace` label is for visualizing Profiles in Application Observability
46+
rule {
47+
target_label = "namespace"
48+
replacement = "quickpizza"
49+
}
4350
rule {
4451
target_label = "service_name"
4552
source_labels = [
@@ -50,11 +57,12 @@ discovery.relabel "application_containers" {
5057
}
5158
targets = discovery.docker.application_containers.targets
5259
}
60+
5361
// Metrics
5462
prometheus.scrape "application_containers" {
5563
scrape_interval = "10s"
56-
forward_to = [grafana_cloud.stack.receivers.metrics]
5764
targets = discovery.relabel.application_containers.output
65+
forward_to = [grafana_cloud.stack.receivers.metrics]
5866
}
5967
// Logs
6068
loki.source.docker "application_containers" {
@@ -63,13 +71,12 @@ loki.source.docker "application_containers" {
6371
forward_to = [grafana_cloud.stack.receivers.logs]
6472
}
6573

66-
// Profiling
67-
pyroscope.receive_http "default" {
68-
http {
69-
listen_address = "0.0.0.0"
70-
listen_port = 9999
71-
}
72-
forward_to = [grafana_cloud.stack.receivers.profiles]
74+
// Profiling Pull Mode
75+
pyroscope.scrape "application_containers" {
76+
// https://grafana.com/docs/pyroscope/latest/configure-client/grafana-alloy/go_pull/
77+
scrape_interval = "30s"
78+
targets = discovery.relabel.application_containers.output
79+
forward_to = [grafana_cloud.stack.receivers.profiles]
7380
}
7481

7582
// Traces

cmd/main.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,11 @@ func main() {
2727
Level: logging.GetLogLevel(),
2828
})))
2929

30-
// Enable profiling middleware if Pyroscope is enabled
30+
// Profiling in pull mode is enabled by default.
31+
// If QUICKPIZZA_PYROSCOPE_ENDPOINT is set, profiling in push mode will be enabled.
3132
profilingConfig, profilingEnabled := envPyroscopeConfig()
3233
if profilingEnabled {
33-
slog.Debug("enabling Pyroscope profiling")
34+
slog.Debug("enabling Pyroscope profiling in push mode")
3435

3536
runtime.SetMutexProfileFraction(5)
3637
runtime.SetBlockProfileRate(5)

docker-compose-cloud.microservices.yaml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
x-quickpizza-env-common: &quickpizza-env-common
22
QUICKPIZZA_OTLP_ENDPOINT: http://alloy:4318
3-
QUICKPIZZA_PYROSCOPE_ENDPOINT: http://alloy:9999
43
QUICKPIZZA_TRUST_CLIENT_TRACEID: true
54
QUICKPIZZA_ALL_SERVICES: 0
65
QUICKPIZZA_CATALOG_ENDPOINT: http://catalog:3333

kubernetes/cloud-dev/config/config.alloy

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,14 @@ discovery.relabel "application_pods" {
4545
"__meta_kubernetes_pod_label_app_k8s_io_name",
4646
]
4747
}
48+
// the `namespace` label is for visualizing Profiles in Application Observability
49+
rule {
50+
target_label = "namespace"
51+
source_labels = [
52+
// quickpizza
53+
"__meta_kubernetes_pod_label_app_k8s_io_name",
54+
]
55+
}
4856
rule {
4957
target_label = "service_name"
5058
source_labels = [
@@ -67,14 +75,13 @@ loki.source.kubernetes "application_pods" {
6775
targets = discovery.relabel.application_pods.output
6876
}
6977

70-
// TODO: not working
71-
// pyroscope.receive_http "default" {
72-
// forward_to = [grafana_cloud.stack.receivers.profiles]
73-
// http {
74-
// listen_address = "127.0.0.1"
75-
// listen_port = 9999
76-
// }
77-
// }
78+
// Profiling Pull Mode
79+
pyroscope.scrape "application_pods" {
80+
// https://grafana.com/docs/pyroscope/latest/configure-client/grafana-alloy/go_pull/
81+
scrape_interval = "30s"
82+
targets = discovery.relabel.application_pods.output
83+
forward_to = [grafana_cloud.stack.receivers.profiles]
84+
}
7885

7986
// Traces
8087
otelcol.receiver.otlp "default" {

kubernetes/cloud-dev/kustomization.yaml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,6 @@ configMapGenerator:
2828
literals:
2929
# Enable tracing
3030
- QUICKPIZZA_OTLP_ENDPOINT=http://grafana-alloy:4318
31-
# TODO: pyroscope not working
32-
- QUICKPIZZA_PYROSCOPE_ENDPOINT=http://grafana-alloy:9999
3331
- name: alloy-config
3432
files:
3533
- ./config/config.alloy

pkg/http/http.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import (
1515
"math/rand"
1616
"net/http"
1717
"net/http/httputil"
18+
_ "net/http/pprof"
1819
"net/url"
1920
"os"
2021
"slices"
@@ -259,6 +260,9 @@ func NewServer(profiling bool, traceInstaller *TraceInstaller) *Server {
259260
}).Handler,
260261
)
261262

263+
// Enable Profiling Pull Mode
264+
router.Mount("/debug/pprof/", http.DefaultServeMux)
265+
262266
if profiling {
263267
router.Use(k6.LabelsFromBaggageHandler)
264268
}

0 commit comments

Comments
 (0)