
k6 的 OpenTelemetry 输出怎么配置 Basic Auth 凭据发送指标【免费下载链接】k6A modern load testing tool, using Go and JavaScript项目地址: https://gitcode.com/GitHub_Trending/k6/k6如果你的 OpenTelemetry 接收端Collector 或后端开启了 HTTP Basic Auth直接把 k6 的 OpenTelemetry 输出指过去会在认证上被拒。从 k6 v2.1.0 起见 release notes/v2.1.0.mdOpenTelemetry 输出的 HTTP exporter 原生支持发送 Basic Auth 凭据配置K6_OTEL_HTTP_EXPORTER_USERNAME和K6_OTEL_HTTP_EXPORTER_PASSWORD两个环境变量或在输出配置的 JSON 里写username、password两个键k6 就会在导出请求上自动带上Authorization: Basic base64(用户:密码)头。前提条件k6 版本需要 ≥ v2.1.0Basic Auth 配置项从该版本引入。建议版本 ≥ v2.2.0v2.2.0 修复了一个与这个功能直接相关的启动崩溃——只配置了 Basic Auth 而没有设置K6_OTEL_HEADERS时旧代码往空 map 写Authorization头导致 panicassignment to entry in nil map见 release notes/v2.2.0.md 中 #6145。接收端点需要支持 OTLP。k6 的 exporter 协议默认是 gRPClocalhost:4317HTTP 协议的默认端点是localhost:4318、URL 路径/v1/metrics这些默认值定义在 internal/output/opentelemetry/config.go。注意输出名的迁移OpenTelemetry 输出在 v1.4.0 从实验状态转为稳定名称从experimental-opentelemetry改为opentelemetry旧名称虽然仍可用但已弃用见 release notes/v1.4.0.md本文统一用opentelemetry。方式一环境变量配置凭据这是官方 release notes 给出的写法。凭据只配用户名/密码两个变量不需要也不应该手写Authorization头K6_OTEL_HTTP_EXPORTER_USERNAMEuser \ K6_OTEL_HTTP_EXPORTER_PASSWORDsecret \ k6 run --out opentelemetry script.js这里user、secret是文档示例值替换为你接收端的真实账号密码script.js替换为你的压测脚本。k6 内部的处理逻辑在 internal/output/opentelemetry/exporter.go只要配置了有效用户名就会把用户名:密码做 base64 编码后写入Authorization: Basic ...头再传给 exporter。这段逻辑发生在协议分支之前所以 gRPC 和 HTTP exporter 都会携带这个头。如果你用的是 HTTP 协议默认是 gRPC需要先指定协议并且端点只能写host:port不能带http://或https://前缀——带了会报HTTP exporter endpoint must only be host and port, no schemeK6_OTEL_EXPORTER_PROTOCOLhttp/protobuf \ K6_OTEL_HTTP_EXPORTER_ENDPOINTotel.example.com:4318 \ K6_OTEL_HTTP_EXPORTER_USERNAMEuser \ K6_OTEL_HTTP_EXPORTER_PASSWORDsecret \ k6 run --out opentelemetry script.js默认 gRPC 通道时对应的是K6_OTEL_GRPC_EXPORTER_ENDPOINT默认localhost:4317和K6_OTEL_GRPC_EXPORTER_INSECURE仓库里的端到端示例 examples/docker-compose/opentelemetry/README.md 就是用 gRPC 协议把指标经 OTel Collector 转发到 PrometheusK6_OTEL_GRPC_EXPORTER_ENDPOINTlocalhost:4317 \ K6_OTEL_GRPC_EXPORTER_INSECUREtrue \ K6_OTEL_METRIC_PREFIXk6_ \ k6 run --tag testid1 -o experimental-opentelemetry script.js该示例的-o experimental-opentelemetry是旧输出名按 v1.4.0 的迁移说明应改为--out opentelemetry。方式二JSON 配置里写 username 和 passwordrelease notes 同时支持在输出配置中使用username、password键对应 JSON 配置中的opentelemetry输出条目。k6 run --config config.json script.js加载的配置文件里输出配置放在collectors映射下见 internal/cmd/config.go 中Collectors map[string]json.RawMessage \json:collectors 的定义{ collectors: { opentelemetry: { exporterProtocol: http/protobuf, httpExporterEndpoint: otel.example.com:4318, username: user, password: secret } } }配置项的完整清单JSON 键名与环境变量一一对应都定义在 internal/output/opentelemetry/config.go配置项环境变量用途exporterProtocolK6_OTEL_EXPORTER_PROTOCOL取grpc或http/protobuf默认grpchttpExporterEndpointK6_OTEL_HTTP_EXPORTER_ENDPOINTHTTP exporter 目标仅host:port默认localhost:4318httpExporterURLPathK6_OTEL_HTTP_EXPORTER_URL_PATHHTTP 请求路径默认/v1/metricshttpExporterInsecureK6_OTEL_HTTP_EXPORTER_INSECURE关闭 HTTP 连接的传输层安全username/passwordK6_OTEL_HTTP_EXPORTER_USERNAME/K6_OTEL_HTTP_EXPORTER_PASSWORDBasic Auth 凭据headersK6_OTEL_HEADERS附加请求头keyvalue,key2value2格式grpcExporterEndpointK6_OTEL_GRPC_EXPORTER_ENDPOINTgRPC exporter 目标默认localhost:4317grpcExporterInsecureK6_OTEL_GRPC_EXPORTER_INSECURE关闭 gRPC 连接安全两种来源的优先级遵循 k6 的配置合并规则环境变量值会覆盖 JSON 配置中的值见 internal/output/opentelemetry/config.go 中GetConsolidatedConfig先应用 JSON、再应用环境变量的顺序。也就是说同一个键两边都写了时环境变量生效注意别把密码留在 JSON 文件里又被环境变量静默覆盖。验证配置生效k6 启动测试时会打印每个输出目标的可读描述OpenTelemetry 输出的描述格式为opentelemetry (协议, 端点)见 internal/output/opentelemetry/output.go 的Description()和String()。例如 HTTP 协议配置好后应能看到类似下面这样的行文档示例opentelemetry (http/protobuf, https://otel.example.com:4318/v1/metrics)其中https前缀来自httpExporterInsecure未开启时的拼接逻辑如果开了 insecure 则显示为http。运行时验证k6 的指标缓冲按flushInterval默认 1s注册进 OTel SDK再由周期性 reader 按exportInterval默认 10s导出到接收端见 internal/output/opentelemetry/output.go。发送失败时 k6 会在日志里输出Error dispatching sample或cant flush some metrics之类的错误/告警如果接收端日志显示 401/403 或 k6 侧出现上述导出错误先核对账号密码和端点是否正确。最终确认指标到达以你接收端Collector、Prometheus、Grafana 等实际查到 k6 指标为准。另外v2.2.0 的测试 internal/output/opentelemetry/exporter_test.go 覆盖了「Basic Auth 无/有 headers」「gRPC Basic Auth」等组合场景均要求 exporter 能正常创建可用于核对回归。已知限制端点校验较严格HTTP 端点写host:port时带 scheme 会直接启动失败service name和service version为空同样会报配置错误见Validate()。K6_OTEL_HEADERS的格式是逗号分隔的keyvalue单个条目缺少会报invalid header ... expected format keyvalue。Basic Auth 头对 gRPC 和 HTTP 两种协议都会附加但 v2.1.0 release notes 将其描述为「HTTP exporter 的 Basic Auth」如果你的 gRPC 接收端有独立的认证要求以接收端文档为准。仓库内的 OpenTelemetry 文档以 release notes 和 examples/docker-compose/opentelemetry/ 为主更完整的在线说明以 k6 官方文档站为准。【免费下载链接】k6A modern load testing tool, using Go and JavaScript项目地址: https://gitcode.com/GitHub_Trending/k6/k6创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考