Grafana xk6

Search for a command to run...

No comments yet. Be the first to comment.
書本連結 前言 會挑這本書看的人,應該都是關心自己負責專案的軟體架構的人。不只希望開發的軟體能滿足客戶的明確需求,也希望能滿足可維護性(maintainability)的隱性需求,以及自己對結構與美觀習慣的要求。 要能滿足上述這些要求很難,因為專案通常不會按照計畫進行。可能變因有 deadline,最後的 API 與承諾的不同,又或者我們的設計無法很好的貼合需求的變化所需。。因此完美的架構只有在一

我們的 AIOps agent 有個很具體的問題:它會捏造 trace ID。 當 on-call 工程師問「payment service 有沒有 error trace?」,agent 有時會信心滿滿地回答「是的,見 trace a1b2c3d4...」——但這串 ID 根本不存在 Tempo 裡。工程師點進去,404。壞的不只是使用者體驗,而是這讓整個 RCA 結論失去可信度。 另一個問題是

o11y-bench 深入剖析:讓 AI 真正面對 on-call 現場 從任務設計、合成環境、Agent 架構、評分機制到報告輸出,逐一解析這個開放 benchmark 的每個組件——以及 Gemini 3 Flash Preview 的完整實測結果 先說清楚這在解決什麼問題 目前多數 LLM benchmark 測的是「知識」:模型知不知道 PromQL 的語法,知不知道什麼是 p99

臥龍神算奇術完全兵書:從兵法原理到實戰,徹底搞懂奇術機制

稟告主公:此乃司馬懿進呈之兵書,詳解如何以 OpenTelemetry 陣法,令臥龍神算之一舉一動盡在掌握,知糧草消耗、察兵器效能、辨戰報異常,使主公運籌帷幄於大帳之中。 為何需要斥候情報? 司馬懿稟告主公: 臥龍神算(Claude Code)乃當世利器,然若無斥候回報,主公便如蒙眼行軍——兵器耗損幾何、糧草消費幾許、哪路斥候出了差錯,一概不知。臣以為,此乃兵家大忌。 無情報之弊,有四: 軍

最近因為任務,需要對公司的平台做 smoke testing 與一些基本的 load testing。但因為我們是 Web3 的產品有點麻煩,登入要 OTP 驗證,還有很多簽章的流程需要處理。剛好又是做借貸撮合的平台,需要開多個瀏覽器登入不同角色跟帳號進行操作。剛好 k6 有提供 xk6 來建置出 k6 的插件。
除此之外這測試腳本還是能跟原本的 k6 http load testing 腳本混合執行。同時能執行瀏覽器的測試以及 API 的壓測的 solution 不算多,k6 算是其中之一。
但這篇先來寫 xk6 .

k6 Browser 本來也是 k6 的插件之一,也是用 xk6 來編譯使用的,但到了 k6 0.56 版本後,就被合併到 k6 的主要程式庫當中了。剛好在寫文章當下也是最新版本也是 0.56。
Starting from k6 version v0.56, this codebase has been merged and is now part of the main k6 repository.
但我們能看一下它原本是怎麼透過 xk6 編譯的。根目錄中的 Makefile 中的 build job.
go install go.k6.io/xk6/cmd/xk6@latest &&
xk6 build --output xk6-browser --with github.com/grafana/xk6-browser=.
https://github.com/grafana/xk6 xk6 主要是利用 Go 語言的 package 管理器來進行套件的下載與編譯出 k6 套件的, 所以套件開發者需要安裝 Go 才方便編譯 xk6 撰寫的套件.
** 安裝 xk6 **
# Install xk6
go install go.k6.io/xk6/cmd/xk6@latest
然後用 Go 指令新建一個專案, 這裡我用簡單的 OTP 產生器為例子. 以下是檔案目錄.
├── otp
│ ├── go.mod
│ ├── go.sum
│ └── otp.go
接著需要在專案中安裝k6套件.
go get go.k6.io/k6
最重要的是modules.Register("k6/x/otp", new(OtpGenerator)), 這裡第一個參數一定要事 k6/x/ 來作為k6模組的名稱前綴. x 我不知道是experimental 還是 xk6的 x 就是了.
** 模組架構設計 (Go 語言部分)**
// 全局模組 root 實例 (每個測試程序只會存在一個)
type RootModule struct{}
// 模組實例 (每個 VU 虛擬用戶獨立一個實例)
type ModuleInstance struct {
vu modules.VU // 持有 VU 上下文
}
// 必須實現的 interface 驗證
var (
_ modules.Module = &RootModule{}
_ modules.Instance = &ModuleInstance{}
)
func init() {
// 註冊模組到 k6 系統 (強制前綴 k6/x/)
modules.Register("k6/x/otp", new(RootModule))
}
// 創建 VU 級實例
func (*RootModule) NewModuleInstance(vu modules.VU) modules.Instance {
return &ModuleInstance{vu: vu}
}
// 匯出 JS 可調用的構建函數
func (m *ModuleInstance) Exports() modules.Exports {
return modules.Exports{
Named: map[string]interface{}{
"OtpGenerator": m.newOtpGenerator, // JS 中 new OtpGenerator()
},
}
}
OTP 產生器實現細節 3. 核心類結構
type OtpGenerator struct {
vu modules.VU // 必備的 VU 引用
secret string // 密鑰
passcode string // 最後生成的驗證碼
}
func (m *ModuleInstance) newOtpGenerator(c sobek.ConstructorCall) *sobek.Object {
rt := m.vu.Runtime()
// 參數驗證邏輯
if len(c.Arguments) != 1 {
common.Throw(rt, fmt.Errorf("需要 1 個參數 (secret)"))
}
secret := c.Argument(0).String()
if secret == "" || len(secret) < 16 {
common.Throw(rt, fmt.Errorf("無效密鑰"))
}
// 創建 Go 實例
generator := &OtpGenerator{
vu: m.vu,
secret: secret,
}
// 創建 JS 對象並綁定方法
obj := rt.NewObject()
obj.Set("generate", generator.generate) // 綁定產生 OTP 方法
obj.Set("getPasscode", generator.getPasscode) // 綁定取值方法
return obj
}
xk6 編譯與使用
cd otp && xk6 build \
--with github.com/$(basename $(pwd))=. \
--output ../k6
檢查編譯出來的 k6 執行檔其包含的模組資訊. 我這裡出現兩個是因為我編譯時 --with 有用到另一個模組.
./k6 version
k6 v0.56.0 (go1.22.1, darwin/arm64)
Extensions:
github.com (devel), k6/x/kv [js]
github.com (devel), k6/x/otp [js]
多模組同時編譯的指令
cd otp && xk6 build \
--with github.com/$(basename $(pwd))=. \
--with github.com/oleiade/xk6-kv \
--output ../k6
也能透過 docker image 來協助編譯. 其中grafana/xk6 build v0.56, 是用來指定用 v0.56版本的 k6 來編譯.
docker run --rm -it -e GOOS=darwin -u "$(id -u):$(id -g)" \
-v "${PWD}:/xk6" \
grafana/xk6 build v0.56 \
--with github.com/demo/otp=./otp \
--with github.com/oleiade/xk6-kv
以下是完整程式
package otp
import (
"fmt"
"time"
"github.com/grafana/sobek"
"github.com/pquerna/otp"
"github.com/pquerna/otp/totp"
"go.k6.io/k6/js/common"
"go.k6.io/k6/js/modules"
)
// Initialize module registration
func init() {
modules.Register("k6/x/otp", new(RootModule))
}
// RootModule implements the global module instance
type RootModule struct{}
// ModuleInstance represents a per-VU module instance
type ModuleInstance struct {
vu modules.VU
}
// Ensure interfaces are implemented
var (
_ modules.Module = &RootModule{}
_ modules.Instance = &ModuleInstance{}
)
// NewModuleInstance creates a new module instance for each VU
func (*RootModule) NewModuleInstance(vu modules.VU) modules.Instance {
return &ModuleInstance{
vu: vu,
}
}
// Exposes constructor functions for JavaScript binding
func (m *ModuleInstance) Exports() modules.Exports {
return modules.Exports{
Named: map[string]interface{}{
"OtpGenerator": m.newOtpGenerator,
},
}
}
// OtpGenerator handles TOTP generation operations
type OtpGenerator struct {
vu modules.VU
secret string // Base32 encoded secret key
passcode string // Last generated passcode
}
// newOtpGenerator constructor creates JS-bound OTP generator object
func (m *ModuleInstance) newOtpGenerator(c sobek.ConstructorCall) *sobek.Object {
rt := m.vu.Runtime()
// Validate constructor arguments
if len(c.Arguments) != 1 {
// 統一使用 common.Throw 拋出異常
common.Throw(rt, fmt.Errorf("OtpGenerator requires 1 argument (secret)"))
}
secret := c.Argument(0).String()
if secret == "" {
common.Throw(rt, fmt.Errorf("secret cannot be empty"))
}
if len(secret) < 16 {
common.Throw(rt, fmt.Errorf("secret too short"))
}
// Initialize generator instance
generator := &OtpGenerator{
vu: m.vu,
secret: secret,
}
// Create JS object and bind methods
obj := rt.NewObject()
// Bind generate method
if err := obj.Set("generate", generator.generate); err != nil {
common.Throw(rt, err)
}
// Bind passcode getter (optional)
if err := obj.Set("getPasscode", func() string {
return generator.passcode
}); err != nil {
common.Throw(rt, err)
}
return obj
}
// generate handles TOTP code generation with optional timestamp parameter
// Returns generated passcode directly to JS
func (g *OtpGenerator) generate(c sobek.FunctionCall) sobek.Value {
rt := g.vu.Runtime()
// Handle timestamp parameter
var now time.Time
if len(c.Arguments) > 0 {
// JavaScript -> Go 參數接收
ms := c.Argument(0).ToInteger()
now = time.UnixMilli(ms)
} else {
now = time.Now()
}
// Generate TOTP with fixed parameters
passcode, err := totp.GenerateCodeCustom(g.secret, now, totp.ValidateOpts{
Period: 30, // 30-second intervals
Skew: 1, // Allow 1 interval before/after
Digits: otp.DigitsSix,
Algorithm: otp.AlgorithmSHA1,
})
if err != nil {
common.Throw(rt, err)
}
// Store and return the generated code
g.passcode = passcode
// Go -> JavaScript 值轉換
return rt.ToValue(passcode)
}
** k6 客戶端使用**
import { OtpGenerator } from 'k6/x/otp';
const generator = new OtpGenerator(otpSecret);
const code = generator.generate();
在公司專案中使用 k6 時, 基本上有很大機會會透過 xk6 來編譯出客製化的 k6 套件. 偏偏官網這部分講的不是太豐富. 就把應用到的部分簡單整理.