-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Expose the tracing client publicly as k6/experimental/tracing.Client
This commit exposes the Client constructor publicly as part of the k6/experimental/tracing module. From this point forward users will be able to instantiate the Client, and perform instrumented HTTP requests using it. This commit also adds a bunch of integration tests covering the expected behavior of the module's API.
- Loading branch information
Showing
4 changed files
with
216 additions
and
1 deletion.
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
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,35 @@ | ||
package tracing | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
|
||
"github.com/dop251/goja" | ||
"go.k6.io/k6/js/common" | ||
"go.k6.io/k6/js/modules" | ||
) | ||
|
||
// Tracing is the JS module instance that will be created for each VU. | ||
type Tracing struct { | ||
vu modules.VU | ||
} | ||
|
||
// NewClient is the JS constructor for the tracing.Client | ||
// | ||
// It expects a single configuration object as argument, which | ||
// will be used to instantiate an `Object` instance internally, | ||
// and will be used by the client to configure itself. | ||
func (t *Tracing) NewClient(cc goja.ConstructorCall) *goja.Object { | ||
rt := t.vu.Runtime() | ||
|
||
if len(cc.Arguments) < 1 { | ||
common.Throw(rt, errors.New("Client constructor expects a single configuration object as argument; none given")) | ||
} | ||
|
||
var opts options | ||
if err := rt.ExportTo(cc.Arguments[0], &opts); err != nil { | ||
common.Throw(rt, fmt.Errorf("unable to parse options object; reason: %w", err)) | ||
} | ||
|
||
return rt.ToValue(NewClient(t.vu, opts)).ToObject(rt) | ||
} |
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,46 @@ | ||
import http from "k6/http"; | ||
import { check } from "k6"; | ||
import tracing from "k6/experimental/tracing"; | ||
|
||
// Explicitly instantiating a tracing client allows to distringuish | ||
// instrumented from non-instrumented HTTP calls, by keeping APIs separate. | ||
// It also allows for finer-grained configuration control, by letting | ||
// users changing the tracing configuration on the fly during their | ||
// script's execution. | ||
let instrumentedHTTP = new tracing.Client({ | ||
propagator: "w3c", | ||
}); | ||
|
||
const testData = { name: "Bert" }; | ||
|
||
export default () => { | ||
// Using the tracing client instance, HTTP calls will have | ||
// their trace context headers set. | ||
let res = instrumentedHTTP.request("GET", "http://httpbin.org/get", null, { | ||
headers: { | ||
"X-Example-Header": "instrumented/request", | ||
}, | ||
}); | ||
check(res, { | ||
"status is 200": (r) => r.status === 200, | ||
}); | ||
|
||
// The tracing client offers more flexibility over | ||
// the `instrumentHTTP` function, as it leaves the | ||
// imported standard http module untouched. Thus, | ||
// one can still perform non-instrumented HTTP calls | ||
// using it. | ||
res = http.post("http://httpbin.org/post", JSON.stringify(testData), { | ||
headers: { "X-Example-Header": "noninstrumented/post" }, | ||
}); | ||
check(res, { | ||
"status is 200": (r) => r.status === 200, | ||
}); | ||
|
||
res = instrumentedHTTP.del("http://httpbin.org/delete", null, { | ||
headers: { "X-Example-Header": "instrumented/delete" }, | ||
}); | ||
check(res, { | ||
"status is 200": (r) => r.status === 200, | ||
}); | ||
}; |