Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix http-instrumentation-pyroscope not using name tag #134

Merged
merged 2 commits into from
Jul 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
92 changes: 92 additions & 0 deletions lib/http-instrumentation-pyroscope/1.0.1/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
// This code specifically uses commonJS as to be faster loading in k6
// this can be changed after https://github.com/grafana/k6/issues/3265
const http = require("k6/http");
const execution = require("k6/execution");

// saving the original values
const request = http.request;
const asyncRequest = http.asyncRequest;

function pyroBaggage(url, _, params) {
return { baggage: `k6.test_run_id=${__ENV.K6_CLOUDRUN_TEST_RUN_ID}, k6.scenario=${execution.scenario.name}, k6.name=${params?.tags?.name ? params.tags.name : url}` }
}

class Client {
generateHeaders;

constructor(generateHeaders) {
if (generateHeaders == null) {
generateHeaders = pyroBaggage
}
this.generateHeaders = generateHeaders
}

// request instruments the http module's request function with tracing headers,
// and ensures the trace_id is emitted as part of the output's data points metadata.
request(method, url, ...args) {
let headers = this.generateHeaders(url, ...args)
return request(method, url, ...instrumentArguments(headers, ...args));
}

// asyncRequest instruments the http module's asyncRequest function with tracing headers,
// and ensures the trace_id is emitted as part of the output's data points metadata.
async asyncRequest(method, url, ...args) {
await asyncRequest(method, url, ...instrumentArguments(this.generateHeaders(url, ...args), ...args));
}

del(url, ...args) { return this.request("DELETE", url, ...args) }
get(url, ...args) { return this.request("GET", url, null, ...args) }
head(url, ...args) { return this.request("HEAD", url, null, ...args) }
options(url, ...args) { return this.request("OPTIONS", url, ...args) }
patch(url, ...args) { return this.request("PATCH", url, ...args) }
post(url, ...args) { return this.request("POST", url, ...args) }
put(url, ...args) { return this.request("PUT", url, ...args) }
}

function instrumentArguments(headers, ...args) {
switch (args.length) {
case 0:
args.push(null)
// fallthrough to add the header
case 1:
// We only received a body argument
args.push({ headers: headers })
break;
default: // this handles 2 and more just in case someone provided more arguments
// We received both a body and a params argument. In the
// event params would be nullish, we'll instantiate
// a new object.
if (args[1] == null) args[1] = {}

let params = args[1]
if (params.headers == null) {
params.headers = {}
}
Object.assign(params.headers, headers)
break;
}

return args
}

function instrumentHTTP() {
const client = new Client()

http.del = client.del.bind(client);
http.get = client.get.bind(client);
http.head = client.head.bind(client);
http.options = client.options.bind(client);
http.patch = client.patch.bind(client);
http.post = client.post.bind(client);
http.put = client.put.bind(client);
http.request = client.request.bind(client)
http.asyncRequest = client.asyncRequest.bind(client)
}

const exp = { Client: Client, instrumentHTTP: instrumentHTTP };

module.exports = {
default: exp,
__esModule: true,
...exp
}
2 changes: 1 addition & 1 deletion lib/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -632,7 +632,7 @@ <h2>Available libs</h2>
<td><a href="https://grafana.com/docs/k6/latest/javascript-api/jslib/aws">https://grafana.com/docs/k6/latest/javascript-api/jslib/aws</a></td>
</tr><tr>
<td>http-instrumentation-pyroscope</td>
<td><a target="_blank" href="https://jslib.k6.io/http-instrumentation-pyroscope/1.0.0/index.js">1.0.0</a></td>
<td><a target="_blank" href="https://jslib.k6.io/http-instrumentation-pyroscope/1.0.0/index.js">1.0.0</a>, <a target="_blank" href="https://jslib.k6.io/http-instrumentation-pyroscope/1.0.1/index.js">1.0.1</a></td>
<td><a href="https://grafana.com/docs/k6/latest/javascript-api/jslib/http-instrumentation-pyroscope/">https://grafana.com/docs/k6/latest/javascript-api/jslib/http-instrumentation-pyroscope/</a></td>
</tr><tr>
<td>http-instrumentation-tempo</td>
Expand Down
2 changes: 1 addition & 1 deletion supported.json
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@
"docs-url": "https://grafana.com/docs/k6/latest/javascript-api/jslib/aws"
},
"http-instrumentation-pyroscope": {
"versions": ["1.0.0"],
"versions": ["1.0.0", "1.0.1"],
"docs-url": "https://grafana.com/docs/k6/latest/javascript-api/jslib/http-instrumentation-pyroscope/"
},
"http-instrumentation-tempo": {
Expand Down
Loading