Skip to content

Commit 40931b6

Browse files
authored
Tracing: Enable traces to profiles (grafana#88896)
1 parent c9271ed commit 40931b6

File tree

6 files changed

+47
-6
lines changed

6 files changed

+47
-6
lines changed

devenv/datasources.yaml

+10
Original file line numberDiff line numberDiff line change
@@ -311,3 +311,13 @@ datasources:
311311
spanEndTimeShift: '-5m'
312312
customQuery: true
313313
query: '{filename="/var/log/grafana/grafana.log"} |="$${__span.traceId}"'
314+
tracesToProfiles:
315+
datasourceUid: gdev-pyroscope
316+
profileTypeId: "process_cpu:cpu:nanoseconds:cpu:nanoseconds"
317+
318+
- name: gdev-pyroscope
319+
type: grafana-pyroscope-datasource
320+
uid: gdev-pyroscope
321+
access: proxy
322+
url: http://localhost:4040
323+
editable: false

devenv/docker/blocks/self-instrumentation/docker-compose.yaml

+2
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@
3636
image: "grafana/pyroscope:latest"
3737
ports:
3838
- "4040:4040"
39+
extra_hosts:
40+
- "host.docker.internal:host-gateway"
3941
agent:
4042
image: grafana/agent:v0.37.0
4143
environment:

go.mod

+4-6
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@ require (
106106
// For local development grafana/grafana will always use the local files
107107
// Check go.work file for details
108108
github.com/grafana/grafana/pkg/promlib v0.0.6 // @grafana/observability-metrics
109+
github.com/grafana/otel-profiling-go v0.5.1 // @grafana/grafana-backend-group
109110
github.com/grafana/pyroscope-go/godeltaprof v0.1.6 // @grafana/observability-traces-and-profiling
110111
github.com/grafana/pyroscope/api v0.3.0 // @grafana/observability-traces-and-profiling
111112
github.com/grafana/tempo v1.5.1-0.20230524121406-1dc1bfe7085b // @grafana/observability-traces-and-profiling
@@ -242,6 +243,7 @@ require (
242243
github.com/asaskevich/govalidator v0.0.0-20230301143203-a9d515a09cc2 // indirect
243244
github.com/axiomhq/hyperloglog v0.0.0-20191112132149-a4c4c47bc57f // indirect
244245
github.com/bahlo/generic-list-go v0.2.0 // indirect
246+
github.com/bboreham/go-loser v0.0.0-20230920113527-fcc2c21820a3 // indirect
245247
github.com/beorn7/perks v1.0.1 // indirect
246248
github.com/bits-and-blooms/bitset v1.2.0 // indirect
247249
github.com/blevesearch/go-porterstemmer v1.0.3 // indirect
@@ -282,6 +284,7 @@ require (
282284
github.com/emicklei/go-restful/v3 v3.11.0 // indirect
283285
github.com/emicklei/proto v1.10.0 // indirect
284286
github.com/evanphx/json-patch v5.6.0+incompatible // indirect
287+
github.com/facette/natsort v0.0.0-20181210072756-2cd4dd1e2dcb // indirect
285288
github.com/felixge/httpsnoop v1.0.4 // indirect
286289
github.com/fsnotify/fsnotify v1.7.0 // indirect
287290
github.com/ghodss/yaml v1.0.1-0.20190212211648-25d852aebe32 // indirect
@@ -343,6 +346,7 @@ require (
343346
github.com/jonboulle/clockwork v0.4.0 // indirect
344347
github.com/josharian/intern v1.0.0 // indirect
345348
github.com/jpillora/backoff v1.0.0 // indirect
349+
github.com/jszwedko/go-datemath v0.1.1-0.20230526204004-640a500621d6 // indirect
346350
github.com/kballard/go-shellquote v0.0.0-20180428030007-95032a82bc51 // indirect
347351
github.com/klauspost/asmfmt v1.3.2 // indirect
348352
github.com/klauspost/compress v1.17.8 // indirect
@@ -457,12 +461,6 @@ require (
457461
sigs.k8s.io/yaml v1.3.0 // indirect; @grafana-app-platform-squad
458462
)
459463

460-
require (
461-
github.com/bboreham/go-loser v0.0.0-20230920113527-fcc2c21820a3 // indirect
462-
github.com/facette/natsort v0.0.0-20181210072756-2cd4dd1e2dcb // indirect
463-
github.com/jszwedko/go-datemath v0.1.1-0.20230526204004-640a500621d6 // indirect
464-
)
465-
466464
// Use fork of crewjam/saml with fixes for some issues until changes get merged into upstream
467465
replace github.com/crewjam/saml => github.com/grafana/saml v0.4.15-0.20240523142256-cc370b98af7c
468466

go.sum

+2
Original file line numberDiff line numberDiff line change
@@ -2344,6 +2344,8 @@ github.com/grafana/grafana/pkg/promlib v0.0.6 h1:FuRyHMIgVVXkLuJnCflNfk3gqJflmyi
23442344
github.com/grafana/grafana/pkg/promlib v0.0.6/go.mod h1:shFkrG1fQ/PPNRGhxAPNMLp0SAeG/jhqaLoG6n2191M=
23452345
github.com/grafana/grafana/pkg/util/xorm v0.0.1 h1:72QZjxWIWpSeOF8ob4aMV058kfgZyeetkAB8dmeti2o=
23462346
github.com/grafana/grafana/pkg/util/xorm v0.0.1/go.mod h1:eNfbB9f2jM8o9RfwqwjY8SYm5tvowJ8Ly+iE4P9rXII=
2347+
github.com/grafana/otel-profiling-go v0.5.1 h1:stVPKAFZSa7eGiqbYuG25VcqYksR6iWvF3YH66t4qL8=
2348+
github.com/grafana/otel-profiling-go v0.5.1/go.mod h1:ftN/t5A/4gQI19/8MoWurBEtC6gFw8Dns1sJZ9W4Tls=
23472349
github.com/grafana/prometheus-alertmanager v0.25.1-0.20240422145632-c33c6b5b6e6b h1:HCbWyVL6vi7gxyO76gQksSPH203oBJ1MJ3JcG1OQlsg=
23482350
github.com/grafana/prometheus-alertmanager v0.25.1-0.20240422145632-c33c6b5b6e6b/go.mod h1:01sXtHoRwI8W324IPAzuxDFOmALqYLCOhvSC2fUHWXc=
23492351
github.com/grafana/pyroscope-go/godeltaprof v0.1.6 h1:nEdZ8louGAplSvIJi1HVp7kWvFvdiiYg3COLlTwJiFo=

pkg/infra/tracing/tracing.go

+2
Original file line numberDiff line numberDiff line change
@@ -272,6 +272,8 @@ func (ots *TracingService) initOpentelemetryTracer() error {
272272
}
273273
}
274274

275+
tp = NewProfilingTracerProvider(tp)
276+
275277
// Register our TracerProvider as the global so any imported
276278
// instrumentation in the future will default to using it
277279
// only if tracing is enabled
+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package tracing
2+
3+
import (
4+
"context"
5+
6+
otelpyroscope "github.com/grafana/otel-profiling-go"
7+
trace "go.opentelemetry.io/otel/trace"
8+
)
9+
10+
type profilingTracerProvider struct {
11+
trace.TracerProvider
12+
wrappedTp tracerProvider
13+
}
14+
15+
// NewProfilingTracerProvider creates a new tracer provider that annotates pprof
16+
// samples with span_id label. This allows to establish a relationship
17+
// between pprof profiles and reported tracing spans.
18+
func NewProfilingTracerProvider(tp tracerProvider) tracerProvider {
19+
return &profilingTracerProvider{
20+
TracerProvider: otelpyroscope.NewTracerProvider(tp),
21+
wrappedTp: tp,
22+
}
23+
}
24+
25+
func (tp *profilingTracerProvider) Shutdown(ctx context.Context) error {
26+
return tp.wrappedTp.Shutdown(ctx)
27+
}

0 commit comments

Comments
 (0)