Skip to content
This repository was archived by the owner on Jan 17, 2024. It is now read-only.

Commit b9b9877

Browse files
authored
Merge pull request #16 from grafana/feat/module-v2
Migration to the k6's modules v2 API
2 parents c90d657 + 50171b7 commit b9b9877

File tree

1,776 files changed

+138
-948270
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

1,776 files changed

+138
-948270
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
.idea
22
examples/k6
33
k6
4-
k6-distributed-tracing
4+
k6-distributed-tracing
5+
vendor

README.md

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
77
</div>
88

9-
This extension adds distributed tracing support to [k6](https://github.com/k6io/k6)! That means, that if you're testing a system that is instrumented, you can use this extension to start the traces on k6.
9+
This extension adds distributed tracing support to [k6](https://github.com/grafana/k6)! That means, that if you're testing a system that is instrumented, you can use this extension to start the traces on k6.
1010

1111
It is implemented using the [xk6](https://github.com/grafana/xk6) extension system.
1212

@@ -37,14 +37,16 @@ To build a `k6` binary with this extension, first ensure you have the prerequisi
3737
Then:
3838

3939
1. Download `xk6`:
40-
```bash
41-
$ go install go.k6.io/xk6/cmd/xk6@latest
42-
```
40+
41+
```bash
42+
$ go install go.k6.io/xk6/cmd/xk6@latest
43+
```
4344

4445
2. Build the binary:
45-
```bash
46-
$ xk6 build --with github.com/k6io/xk6-distributed-tracing@latest
47-
```
46+
47+
```bash
48+
$ xk6 build --with github.com/k6io/xk6-distributed-tracing@latest
49+
```
4850

4951
## Example
5052

client/client.go

Lines changed: 25 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@ import (
66
"net/http/httptrace"
77

88
"github.com/dop251/goja"
9-
jsHTTP "go.k6.io/k6/js/modules/k6/http"
9+
"go.k6.io/k6/js/modules"
10+
k6HTTP "go.k6.io/k6/js/modules/k6/http"
1011
"go.opentelemetry.io/contrib/instrumentation/net/http/httptrace/otelhttptrace"
1112
"go.opentelemetry.io/otel"
1213
"go.opentelemetry.io/otel/attribute"
@@ -15,52 +16,54 @@ import (
1516
)
1617

1718
type TracingClient struct {
18-
http *jsHTTP.HTTP
19+
vu modules.VU
20+
http *k6HTTP.HTTP
1921
}
2022

2123
type HTTPResponse struct {
22-
*jsHTTP.Response
24+
*k6HTTP.Response
2325
TraceID string
2426
}
2527

26-
type HttpFunc func(ctx context.Context, url goja.Value, args ...goja.Value) (*jsHTTP.Response, error)
28+
type HttpFunc func(ctx context.Context, url goja.Value, args ...goja.Value) (*k6HTTP.Response, error)
2729

28-
func New() *TracingClient {
30+
func New(vu modules.VU) *TracingClient {
2931
return &TracingClient{
30-
http: &jsHTTP.HTTP{},
32+
http: &k6HTTP.HTTP{},
33+
vu: vu,
3134
}
3235
}
3336

34-
func (c *TracingClient) Get(ctx context.Context, url goja.Value, args ...goja.Value) (*HTTPResponse, error) {
35-
return c.WithTrace(c.http.Get, "HTTP GET", ctx, url, args...)
37+
func (c *TracingClient) Get(url goja.Value, args ...goja.Value) (*HTTPResponse, error) {
38+
return c.WithTrace(c.http.Get, "HTTP GET", url, args...)
3639
}
3740

38-
func (c *TracingClient) Post(ctx context.Context, url goja.Value, args ...goja.Value) (*HTTPResponse, error) {
39-
return c.WithTrace(c.http.Post, "HTTP POST", ctx, url, args...)
41+
func (c *TracingClient) Post(url goja.Value, args ...goja.Value) (*HTTPResponse, error) {
42+
return c.WithTrace(c.http.Post, "HTTP POST", url, args...)
4043
}
4144

42-
func (c *TracingClient) Put(ctx context.Context, url goja.Value, args ...goja.Value) (*HTTPResponse, error) {
43-
return c.WithTrace(c.http.Put, "HTTP PUT", ctx, url, args...)
45+
func (c *TracingClient) Put(url goja.Value, args ...goja.Value) (*HTTPResponse, error) {
46+
return c.WithTrace(c.http.Put, "HTTP PUT", url, args...)
4447
}
4548

46-
func (c *TracingClient) Del(ctx context.Context, url goja.Value, args ...goja.Value) (*HTTPResponse, error) {
47-
return c.WithTrace(c.http.Del, "HTTP DEL", ctx, url, args...)
49+
func (c *TracingClient) Del(url goja.Value, args ...goja.Value) (*HTTPResponse, error) {
50+
return c.WithTrace(c.http.Del, "HTTP DEL", url, args...)
4851
}
4952

50-
func (c *TracingClient) Head(ctx context.Context, url goja.Value, args ...goja.Value) (*HTTPResponse, error) {
51-
return c.WithTrace(c.http.Head, "HTTP HEAD", ctx, url, args...)
53+
func (c *TracingClient) Head(url goja.Value, args ...goja.Value) (*HTTPResponse, error) {
54+
return c.WithTrace(c.http.Head, "HTTP HEAD", url, args...)
5255
}
5356

54-
func (c *TracingClient) Patch(ctx context.Context, url goja.Value, args ...goja.Value) (*HTTPResponse, error) {
55-
return c.WithTrace(c.http.Patch, "HTTP PATCH", ctx, url, args...)
57+
func (c *TracingClient) Patch(url goja.Value, args ...goja.Value) (*HTTPResponse, error) {
58+
return c.WithTrace(c.http.Patch, "HTTP PATCH", url, args...)
5659
}
5760

58-
func (c *TracingClient) Options(ctx context.Context, url goja.Value, args ...goja.Value) (*HTTPResponse, error) {
59-
return c.WithTrace(c.http.Options, "HTTP OPTIONS", ctx, url, args...)
61+
func (c *TracingClient) Options(url goja.Value, args ...goja.Value) (*HTTPResponse, error) {
62+
return c.WithTrace(c.http.Options, "HTTP OPTIONS", url, args...)
6063
}
6164

62-
func (c *TracingClient) WithTrace(fn HttpFunc, spanName string, ctx context.Context, url goja.Value, args ...goja.Value) (*HTTPResponse, error) {
63-
ctx, _, span := startTraceAndSpan(ctx, spanName)
65+
func (c *TracingClient) WithTrace(fn HttpFunc, spanName string, url goja.Value, args ...goja.Value) (*HTTPResponse, error) {
66+
ctx, _, span := startTraceAndSpan(c.vu.Context(), spanName)
6467
defer span.End()
6568

6669
id := span.SpanContext().TraceID().String()

go.mod

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@ module github.com/k6io/xk6-distributed-tracing
33
go 1.15
44

55
require (
6-
github.com/dop251/goja v0.0.0-20210427212725-462d53687b0d
6+
github.com/dop251/goja v0.0.0-20211022113120-dc8c55024d06
77
github.com/sirupsen/logrus v1.8.1
8-
go.k6.io/k6 v0.32.0
8+
go.k6.io/k6 v0.35.0
99
go.opentelemetry.io/contrib/instrumentation/net/http/httptrace/otelhttptrace v0.20.0
1010
go.opentelemetry.io/contrib/propagators v0.20.0
1111
go.opentelemetry.io/otel v0.20.0

0 commit comments

Comments
 (0)