-
Notifications
You must be signed in to change notification settings - Fork 18
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Feat: Auto-label profiles based on request baggage #99
Merged
Merged
Changes from 11 commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
5a3cb3a
Read set profiling labels based on request baggage
bryanhuhta a4b6b60
Merge branch 'main' of github.com:grafana/pyroscope-go into add-auto-…
bryanhuhta 4a7b4c8
Move feature to contrib package
bryanhuhta ba551aa
Don't allocate label set when baggage cannot be parsed
bryanhuhta 1b5b66c
Make filter and transform funcs take baggage.Member
bryanhuhta 8ad231e
Remove filter/transform options
bryanhuhta 2316cf9
Make contrib its own go module
bryanhuhta ba439f1
`make go/mod`
bryanhuhta 037c9e6
Merge branch 'main' of github.com:grafana/pyroscope-go into add-auto-…
bryanhuhta e71a332
Try fix build problems
bryanhuhta d091bb0
Update makefile
bryanhuhta cbda95a
Move k6 to its own module
bryanhuhta 4cfbe1f
Merge branch 'k6-module' into add-auto-label
bryanhuhta 68138ca
Move k6 package to x/k6 submodule
bryanhuhta 6daedb6
make go/mod
bryanhuhta 2ea7c88
Downgrade otel package
bryanhuhta 016a171
Use 1.18
bryanhuhta 5727817
Check if baggage key is `k6` before inspecting value
bryanhuhta 539601a
Inline `pyroscope.TagWrapper`
bryanhuhta File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 |
---|---|---|
@@ -0,0 +1,17 @@ | ||
module github.com/grafana/pyroscope-go/contrib | ||
|
||
go 1.22.3 | ||
|
||
require ( | ||
github.com/grafana/pyroscope-go v1.1.1 | ||
github.com/stretchr/testify v1.9.0 | ||
go.opentelemetry.io/otel v1.27.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,18 @@ | ||
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/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.27.0 h1:9BZoF3yMK/O1AafMiQTVu0YDj5Ea4hPhxCs7sGva+cg= | ||
go.opentelemetry.io/otel v1.27.0/go.mod h1:DMpAK8fzYRzs+bi3rS5REupisuqTheUlSZJ1WnZaPAQ= | ||
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= |
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,67 @@ | ||
package k6 | ||
|
||
import ( | ||
"context" | ||
"net/http" | ||
"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 | ||
} | ||
|
||
pyroscope.TagWrapper(r.Context(), *labels, func(ctx context.Context) { | ||
lh.innerHandler.ServeHTTP(w, r.WithContext(ctx)) | ||
}) | ||
bryanhuhta marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
// 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 m.Value() == "" { | ||
continue | ||
} | ||
|
||
if !strings.HasPrefix(m.Key(), "k6.") { | ||
continue | ||
} | ||
bryanhuhta marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
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 |
---|---|---|
@@ -1,7 +1,8 @@ | ||
go 1.19 | ||
go 1.22.3 | ||
|
||
use ( | ||
. | ||
contrib | ||
godeltaprof | ||
godeltaprof/compat | ||
) |
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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should we go further and put
k6
stuff into a submodule?contrib
would be just a container for integration sub-modules.Otherwise, once we add another integration, its dependencies would be added to the k6 package, and vice-versa
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yep! I agree with this move! I was attempting to do that last Friday but
go work sync
was fighting with me. It kept trying to update the go version of other modules.I'll continue to work on this though and get it addressed before merging.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Okay, I got this fixed in https://github.com/grafana/pyroscope-go/pull/99/files/cbda95a5bdb83f75d53d811f4b921385e53e6ed0..016a1717a57831f63723fc46f4298531c17ac3f5. A couple things to note:
k6
package as experimental by moving it to thex/k6
submodule because this API may change or be removed entirely at any time.1.27.0
to1.17.0
to make the submodule compatible with go versions <1.21.