-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Feat: Auto-label profiles based on request baggage (#99)
- Loading branch information
1 parent
ed0d159
commit f1a626f
Showing
9 changed files
with
211 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,4 +4,5 @@ use ( | |
. | ||
godeltaprof | ||
godeltaprof/compat | ||
x/k6 | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
package k6 | ||
|
||
import ( | ||
"net/http" | ||
"runtime/pprof" | ||
"strings" | ||
|
||
"github.com/grafana/pyroscope-go" | ||
"go.opentelemetry.io/otel/baggage" | ||
) | ||
|
||
// LabelsFromBaggageHandler is a middleware that will extract key-value pairs | ||
// from the request baggage and make them profiling labels. | ||
func LabelsFromBaggageHandler(handler http.Handler) http.Handler { | ||
lh := &labelHandler{ | ||
innerHandler: handler, | ||
} | ||
|
||
return lh | ||
} | ||
|
||
type labelHandler struct { | ||
innerHandler http.Handler | ||
} | ||
|
||
func (lh *labelHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { | ||
labels := getBaggageLabels(r) | ||
if labels == nil { | ||
lh.innerHandler.ServeHTTP(w, r) | ||
return | ||
} | ||
|
||
// Inlined version of pryoscope.TagWrapper and pprof.Do to reduce noise in | ||
// the stack trace. | ||
ctx := r.Context() | ||
defer pprof.SetGoroutineLabels(ctx) | ||
ctx = pprof.WithLabels(ctx, *labels) | ||
pprof.SetGoroutineLabels(ctx) | ||
|
||
lh.innerHandler.ServeHTTP(w, r.WithContext(ctx)) | ||
} | ||
|
||
// getBaggageLabels applies filters and transformations to request baggage and | ||
// returns the resulting LabelSet. | ||
func getBaggageLabels(r *http.Request) *pyroscope.LabelSet { | ||
b, err := baggage.Parse(r.Header.Get("Baggage")) | ||
if err != nil { | ||
return nil | ||
} | ||
|
||
labels := baggageToLabels(b) | ||
return &labels | ||
} | ||
|
||
// baggageToLabels converts request baggage to a LabelSet. | ||
func baggageToLabels(b baggage.Baggage) pyroscope.LabelSet { | ||
labelPairs := make([]string, 0, len(b.Members())*2) | ||
for _, m := range b.Members() { | ||
if !strings.HasPrefix(m.Key(), "k6.") { | ||
continue | ||
} | ||
|
||
if m.Value() == "" { | ||
continue | ||
} | ||
|
||
key := strings.ReplaceAll(m.Key(), ".", "_") | ||
labelPairs = append(labelPairs, key, m.Value()) | ||
} | ||
|
||
return pyroscope.Labels(labelPairs...) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
package k6 | ||
|
||
import ( | ||
"context" | ||
"net/http" | ||
"net/http/httptest" | ||
"runtime/pprof" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
"go.opentelemetry.io/otel/baggage" | ||
) | ||
|
||
func Test_getBaggageLabels(t *testing.T) { | ||
t.Run("empty values are skipped", func(t *testing.T) { | ||
req := httptest.NewRequest("GET", "http://example.com", nil) | ||
req = testRequestWithBaggage(t, req, map[string]string{ | ||
"blank": "", | ||
}) | ||
|
||
labelSet := getBaggageLabels(req) | ||
gotLabels := testPprofLabelsToMap(t, *labelSet) | ||
|
||
expectedLabels := map[string]string{} | ||
require.Equal(t, expectedLabels, gotLabels) | ||
}) | ||
|
||
t.Run("with K6Options", func(t *testing.T) { | ||
req := httptest.NewRequest("GET", "http://example.com", nil) | ||
req = testRequestWithBaggage(t, req, map[string]string{ | ||
"k6.test_run_id": "123", | ||
"not_k6.some_other_key": "value", | ||
}) | ||
|
||
labelSet := getBaggageLabels(req) | ||
gotLabels := testPprofLabelsToMap(t, *labelSet) | ||
|
||
expectedLabels := map[string]string{ | ||
"k6_test_run_id": "123", | ||
} | ||
require.Equal(t, expectedLabels, gotLabels) | ||
}) | ||
|
||
t.Run("does not allocate with failure to parse baggage", func(t *testing.T) { | ||
req := httptest.NewRequest("GET", "http://example.com", nil) | ||
req.Header.Add("Baggage", "invalid") | ||
|
||
labelSet := getBaggageLabels(req) | ||
require.Nil(t, labelSet) | ||
}) | ||
} | ||
|
||
func testRequestWithBaggage(t *testing.T, req *http.Request, bag map[string]string) *http.Request { | ||
t.Helper() | ||
|
||
members := []baggage.Member{} | ||
for k, v := range bag { | ||
member, err := baggage.NewMember(k, v) | ||
require.NoError(t, err) | ||
|
||
members = append(members, member) | ||
} | ||
|
||
b, err := baggage.New(members...) | ||
require.NoError(t, err) | ||
|
||
req.Header.Add("Baggage", b.String()) | ||
return req | ||
} | ||
|
||
func testPprofLabelsToMap(t *testing.T, labelSet pprof.LabelSet) map[string]string { | ||
t.Helper() | ||
|
||
gotLabels := map[string]string{} | ||
ctx := pprof.WithLabels(context.Background(), labelSet) | ||
pprof.ForLabels(ctx, func(key, value string) bool { | ||
gotLabels[key] = value | ||
return true | ||
}) | ||
|
||
return gotLabels | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
module github.com/grafana/pyroscope-go/x/k6 | ||
|
||
go 1.18 | ||
|
||
require ( | ||
github.com/grafana/pyroscope-go v1.1.1 | ||
github.com/stretchr/testify v1.9.0 | ||
go.opentelemetry.io/otel v1.17.0 | ||
) | ||
|
||
require ( | ||
github.com/davecgh/go-spew v1.1.1 // indirect | ||
github.com/grafana/pyroscope-go/godeltaprof v0.1.6 // indirect | ||
github.com/klauspost/compress v1.17.8 // indirect | ||
github.com/pmezard/go-difflib v1.0.0 // indirect | ||
gopkg.in/yaml.v3 v3.0.1 // indirect | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= | ||
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= | ||
github.com/grafana/pyroscope-go v1.1.1 h1:PQoUU9oWtO3ve/fgIiklYuGilvsm8qaGhlY4Vw6MAcQ= | ||
github.com/grafana/pyroscope-go v1.1.1/go.mod h1:Mw26jU7jsL/KStNSGGuuVYdUq7Qghem5P8aXYXSXG88= | ||
github.com/grafana/pyroscope-go/godeltaprof v0.1.6 h1:nEdZ8louGAplSvIJi1HVp7kWvFvdiiYg3COLlTwJiFo= | ||
github.com/grafana/pyroscope-go/godeltaprof v0.1.6/go.mod h1:Tk376Nbldo4Cha9RgiU7ik8WKFkNpfds98aUzS8omLE= | ||
github.com/klauspost/compress v1.17.3/go.mod h1:/dCuZOvVtNoHsyb+cuJD3itjs3NbnF6KH9zAO4BDxPM= | ||
github.com/klauspost/compress v1.17.8 h1:YcnTYrq7MikUT7k0Yb5eceMmALQPYBW/Xltxn0NAMnU= | ||
github.com/klauspost/compress v1.17.8/go.mod h1:Di0epgTjJY877eYKx5yC51cX2A2Vl2ibi7bDH9ttBbw= | ||
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= | ||
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= | ||
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg= | ||
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY= | ||
go.opentelemetry.io/otel v1.17.0 h1:MW+phZ6WZ5/uk2nd93ANk/6yJ+dVrvNWUjGhnnFU5jM= | ||
go.opentelemetry.io/otel v1.17.0/go.mod h1:I2vmBGtFaODIVMBSTPVDlJSzBDNf93k60E6Ft0nyjo0= | ||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= | ||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= | ||
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= | ||
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= |