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

Commit

Permalink
Migration to the k6's modules v2 API
Browse files Browse the repository at this point in the history
  • Loading branch information
olegbespalov committed Jan 19, 2022
1 parent a518be4 commit 50171b7
Show file tree
Hide file tree
Showing 5 changed files with 136 additions and 149 deletions.
16 changes: 9 additions & 7 deletions README.md
Expand Up @@ -6,7 +6,7 @@
</div>

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.
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.

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

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

1. Download `xk6`:
```bash
$ go install go.k6.io/xk6/cmd/xk6@latest
```

```bash
$ go install go.k6.io/xk6/cmd/xk6@latest
```

2. Build the binary:
```bash
$ xk6 build --with github.com/k6io/xk6-distributed-tracing@latest
```

```bash
$ xk6 build --with github.com/k6io/xk6-distributed-tracing@latest
```

## Example

Expand Down
47 changes: 25 additions & 22 deletions client/client.go
Expand Up @@ -6,7 +6,8 @@ import (
"net/http/httptrace"

"github.com/dop251/goja"
jsHTTP "go.k6.io/k6/js/modules/k6/http"
"go.k6.io/k6/js/modules"
k6HTTP "go.k6.io/k6/js/modules/k6/http"
"go.opentelemetry.io/contrib/instrumentation/net/http/httptrace/otelhttptrace"
"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/attribute"
Expand All @@ -15,52 +16,54 @@ import (
)

type TracingClient struct {
http *jsHTTP.HTTP
vu modules.VU
http *k6HTTP.HTTP
}

type HTTPResponse struct {
*jsHTTP.Response
*k6HTTP.Response
TraceID string
}

type HttpFunc func(ctx context.Context, url goja.Value, args ...goja.Value) (*jsHTTP.Response, error)
type HttpFunc func(ctx context.Context, url goja.Value, args ...goja.Value) (*k6HTTP.Response, error)

func New() *TracingClient {
func New(vu modules.VU) *TracingClient {
return &TracingClient{
http: &jsHTTP.HTTP{},
http: &k6HTTP.HTTP{},
vu: vu,
}
}

func (c *TracingClient) Get(ctx context.Context, url goja.Value, args ...goja.Value) (*HTTPResponse, error) {
return c.WithTrace(c.http.Get, "HTTP GET", ctx, url, args...)
func (c *TracingClient) Get(url goja.Value, args ...goja.Value) (*HTTPResponse, error) {
return c.WithTrace(c.http.Get, "HTTP GET", url, args...)
}

func (c *TracingClient) Post(ctx context.Context, url goja.Value, args ...goja.Value) (*HTTPResponse, error) {
return c.WithTrace(c.http.Post, "HTTP POST", ctx, url, args...)
func (c *TracingClient) Post(url goja.Value, args ...goja.Value) (*HTTPResponse, error) {
return c.WithTrace(c.http.Post, "HTTP POST", url, args...)
}

func (c *TracingClient) Put(ctx context.Context, url goja.Value, args ...goja.Value) (*HTTPResponse, error) {
return c.WithTrace(c.http.Put, "HTTP PUT", ctx, url, args...)
func (c *TracingClient) Put(url goja.Value, args ...goja.Value) (*HTTPResponse, error) {
return c.WithTrace(c.http.Put, "HTTP PUT", url, args...)
}

func (c *TracingClient) Del(ctx context.Context, url goja.Value, args ...goja.Value) (*HTTPResponse, error) {
return c.WithTrace(c.http.Del, "HTTP DEL", ctx, url, args...)
func (c *TracingClient) Del(url goja.Value, args ...goja.Value) (*HTTPResponse, error) {
return c.WithTrace(c.http.Del, "HTTP DEL", url, args...)
}

func (c *TracingClient) Head(ctx context.Context, url goja.Value, args ...goja.Value) (*HTTPResponse, error) {
return c.WithTrace(c.http.Head, "HTTP HEAD", ctx, url, args...)
func (c *TracingClient) Head(url goja.Value, args ...goja.Value) (*HTTPResponse, error) {
return c.WithTrace(c.http.Head, "HTTP HEAD", url, args...)
}

func (c *TracingClient) Patch(ctx context.Context, url goja.Value, args ...goja.Value) (*HTTPResponse, error) {
return c.WithTrace(c.http.Patch, "HTTP PATCH", ctx, url, args...)
func (c *TracingClient) Patch(url goja.Value, args ...goja.Value) (*HTTPResponse, error) {
return c.WithTrace(c.http.Patch, "HTTP PATCH", url, args...)
}

func (c *TracingClient) Options(ctx context.Context, url goja.Value, args ...goja.Value) (*HTTPResponse, error) {
return c.WithTrace(c.http.Options, "HTTP OPTIONS", ctx, url, args...)
func (c *TracingClient) Options(url goja.Value, args ...goja.Value) (*HTTPResponse, error) {
return c.WithTrace(c.http.Options, "HTTP OPTIONS", url, args...)
}

func (c *TracingClient) WithTrace(fn HttpFunc, spanName string, ctx context.Context, url goja.Value, args ...goja.Value) (*HTTPResponse, error) {
ctx, _, span := startTraceAndSpan(ctx, spanName)
func (c *TracingClient) WithTrace(fn HttpFunc, spanName string, url goja.Value, args ...goja.Value) (*HTTPResponse, error) {
ctx, _, span := startTraceAndSpan(c.vu.Context(), spanName)
defer span.End()

id := span.SpanContext().TraceID().String()
Expand Down
4 changes: 2 additions & 2 deletions go.mod
Expand Up @@ -3,9 +3,9 @@ module github.com/k6io/xk6-distributed-tracing
go 1.15

require (
github.com/dop251/goja v0.0.0-20210427212725-462d53687b0d
github.com/dop251/goja v0.0.0-20211022113120-dc8c55024d06
github.com/sirupsen/logrus v1.8.1
go.k6.io/k6 v0.32.0
go.k6.io/k6 v0.35.0
go.opentelemetry.io/contrib/instrumentation/net/http/httptrace/otelhttptrace v0.20.0
go.opentelemetry.io/contrib/propagators v0.20.0
go.opentelemetry.io/otel v0.20.0
Expand Down

0 comments on commit 50171b7

Please sign in to comment.