Skip to content

Commit

Permalink
Fix tracing module sampling option should default to 1.0 when not set
Browse files Browse the repository at this point in the history
This commit ensures that if the `sampling` option of the tracing
module's client is not provided by user, the Client defaults to
sample 100% of the request.
  • Loading branch information
oleiade committed Jul 27, 2023
1 parent 6c31075 commit bb608a2
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 1 deletion.
52 changes: 52 additions & 0 deletions cmd/tests/tracing_module_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,58 @@ func TestTracingModuleClient_HundredPercentSampling(t *testing.T) {
assertHasTraceIDMetadata(t, jsonResults, 100, tb.Replacer.Replace("HTTPBIN_IP_URL/tracing"))
}

func TestTracingModuleClient_NoSamplingSetShouldAlwaysSample(t *testing.T) {
t.Parallel()
tb := httpmultibin.NewHTTPMultiBin(t)

var gotRequests int64
var gotSampleFlags int64

tb.Mux.HandleFunc("/tracing", func(w http.ResponseWriter, r *http.Request) {
atomic.AddInt64(&gotRequests, 1)

traceparent := r.Header.Get("traceparent")
require.NotEmpty(t, traceparent)
require.Len(t, traceparent, 55)

if traceparent[54] == '1' {
atomic.AddInt64(&gotSampleFlags, 1)
}
})

script := tb.Replacer.Replace(`
import http from "k6/http";
import { check } from "k6";
import tracing from "k6/experimental/tracing";
export const options = {
// 100 iterations to make sure we get 100% sampling
iterations: 100,
}
// We do not set the sampling option, thus the default
// behavior should be to always sample.
const instrumentedHTTP = new tracing.Client({
propagator: "w3c",
})
export default function () {
instrumentedHTTP.get("HTTPBIN_IP_URL/tracing");
};
`)

ts := getSingleFileTestState(t, script, []string{"--out", "json=results.json"}, 0)
cmd.ExecuteWithGlobalState(ts.GlobalState)

assert.Equal(t, int64(100), atomic.LoadInt64(&gotSampleFlags))
assert.Equal(t, int64(100), atomic.LoadInt64(&gotRequests))

jsonResults, err := fsext.ReadFile(ts.FS, "results.json")
require.NoError(t, err)

assertHasTraceIDMetadata(t, jsonResults, 100, tb.Replacer.Replace("HTTPBIN_IP_URL/tracing"))
}

func TestTracingModuleClient_ZeroPercentSampling(t *testing.T) {
t.Parallel()
tb := httpmultibin.NewHTTPMultiBin(t)
Expand Down
2 changes: 1 addition & 1 deletion js/modules/k6/experimental/tracing/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ func (c *Client) Configure(opts options) error {
}

var sampler Sampler = NewAlwaysOnSampler()
if opts.Sampling != 1.0 {
if opts.Sampling > 0.0 && opts.Sampling != 1.0 {
sampler = NewProbabilisticSampler(opts.Sampling)
}

Expand Down

0 comments on commit bb608a2

Please sign in to comment.