用于 Pyroscope 的 HTTP 插装
注意
此库的源代码可在 grafana/jslib.k6.io GitHub 仓库中找到。
http-instrumentation-pyroscope
模块允许您以一种特殊的方式*插装* HTTP 请求,以便使用 k6 测试生成的*相关信息*标记 Grafana Cloud Profiles。
关于 baggage header
*baggage header* 是一个标准化的 HTTP header,用于传播分布式上下文。 W3C 规范 对其具体细节有更详细的说明,但与许多其他 header 一样,baggage header 是一个键值对列表。
默认情况下,此模块会添加三个键值对
- 场景名称
- 请求名称(如果未设置,则为 URL)
__ENV.K6_CLOUDRUN_TEST_RUN_ID
的值,此值在 Grafana Cloud k6 中自动设置。
API
类/函数 | 描述 |
---|---|
instrumentHTTP | 使用 baggage header 插装 k6 http 模块。 |
Client | 可配置的 Client,暴露插装后的 HTTP 操作。 |
示例
此示例演示了如何使用此库为脚本中的每个 HTTP 请求插装 baggage
header。
import { check } from 'k6';
import pyroscope from 'https://jslib.k6.io/http-instrumentation-pyroscope/1.0.2/index.js';
import http from 'k6/http';
// instrumentHTTP will ensure that all requests made by the http module
// from this point forward will have a baggage context attached.
pyroscope.instrumentHTTP();
export default () => {
// the instrumentHTTP call in the init context replaced
// the http module with a version that will automatically
// attach a baggage header to every request.
const res = http.get('http://httpbin.org/get', {
headers: {
'X-Example-Header': 'instrumented/get',
},
});
};