Skip to content

Commit

Permalink
Fix k6 example (#3759)
Browse files Browse the repository at this point in the history
* Use latest version of k6 jslib

* Use latest Pyroscope k6 SDK

* Enable k6 integration for example pyroscope server

* Fix bug where middleware wasn't being applied correctly to handlers

* Update readme
  • Loading branch information
bryanhuhta authored Dec 11, 2024
1 parent cd9745d commit d071a31
Show file tree
Hide file tree
Showing 7 changed files with 18 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,6 @@ test against the Rideshare service.
```
k6 run load.js
```

Finally, navigate to http://localhost:3000 and open the Explore Profiles app. After a small delay, k6 test metadata should begin to appears as labels on the
"ride-sharing-app" application tile.
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,11 @@ services:
image: grafana/pyroscope:latest
ports:
- 4040:4040
volumes:
- ./grafana-provisioning/pyroscope:/etc/pyroscope
command:
- -config.file
- /etc/pyroscope/config.yml

load-generator:
build:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ require (
github.com/agoda-com/opentelemetry-logs-go v0.4.1
github.com/grafana/otel-profiling-go v0.5.1
github.com/grafana/pyroscope-go v1.1.1
github.com/grafana/pyroscope-go/x/k6 v0.0.0-20241003203156-a917cea171d3
github.com/grafana/pyroscope-go/x/k6 v0.0.0-20241129154546-3e89ad952d8f
go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.47.0
go.opentelemetry.io/otel v1.22.0
go.opentelemetry.io/otel/exporters/otlp/otlptrace v1.22.0
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ github.com/grafana/pyroscope-go v1.1.1 h1:PQoUU9oWtO3ve/fgIiklYuGilvsm8qaGhlY4Vw
github.com/grafana/pyroscope-go v1.1.1/go.mod h1:Mw26jU7jsL/KStNSGGuuVYdUq7Qghem5P8aXYXSXG88=
github.com/grafana/pyroscope-go/godeltaprof v0.1.8 h1:iwOtYXeeVSAeYefJNaxDytgjKtUuKQbJqgAIjlnicKg=
github.com/grafana/pyroscope-go/godeltaprof v0.1.8/go.mod h1:2+l7K7twW49Ct4wFluZD3tZ6e0SjanjcUUBPVD/UuGU=
github.com/grafana/pyroscope-go/x/k6 v0.0.0-20241003203156-a917cea171d3 h1:GtwQDlBz8aJHMy2Ko28UDRGgGzi7v4Vf20+ZyXaGy7M=
github.com/grafana/pyroscope-go/x/k6 v0.0.0-20241003203156-a917cea171d3/go.mod h1:nfbW6/4ke3ywlqLb+Zgr9t1z9Zv3m+2ImUp+vbkzHpc=
github.com/grafana/pyroscope-go/x/k6 v0.0.0-20241129154546-3e89ad952d8f h1:5v8jrCB7XCFPo4tXxqHyYbJfuRxTdCtkWYwGW54qpfM=
github.com/grafana/pyroscope-go/x/k6 v0.0.0-20241129154546-3e89ad952d8f/go.mod h1:nfbW6/4ke3ywlqLb+Zgr9t1z9Zv3m+2ImUp+vbkzHpc=
github.com/grpc-ecosystem/grpc-gateway/v2 v2.18.1 h1:6UKoz5ujsI55KNpsJH3UwCq3T8kKbZwNZBNPuTTje8U=
github.com/grpc-ecosystem/grpc-gateway/v2 v2.18.1/go.mod h1:YvJ2f6MplWDhfxiUC3KpyTy76kYUZA4W3pTv/wdKQ9Y=
github.com/klauspost/compress v1.17.8 h1:YcnTYrq7MikUT7k0Yb5eceMmALQPYBW/Xltxn0NAMnU=
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
self_profiling:
use_k6_middleware: true
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import http from 'k6/http';
import { check } from 'k6';
import pyroscope from 'https://jslib.k6.io/http-instrumentation-pyroscope/1.0.0/index.js';
import pyroscope from 'https://jslib.k6.io/http-instrumentation-pyroscope/1.0.1/index.js';

pyroscope.instrumentHTTP();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -127,17 +127,17 @@ func registerRoutes(mux *http.ServeMux, handlers []route) {
}

func applyOtelMiddleware(routes []route) []route {
for _, route := range routes {
route.Handler = otelhttp.NewHandler(route.Handler, route.Name)
for i, route := range routes {
routes[i].Handler = otelhttp.NewHandler(route.Handler, route.Name)
}
return routes
}

// applyK6Middleware adds the k6 instrumentation middleware to all routes. This
// enables the Pyroscope SDK to label the profiles with k6 test metadata.
func applyK6Middleware(routes []route) []route {
for _, route := range routes {
route.Handler = k6.LabelsFromBaggageHandler(route.Handler)
for i, route := range routes {
routes[i].Handler = k6.LabelsFromBaggageHandler(route.Handler)
}
return routes
}

0 comments on commit d071a31

Please sign in to comment.