Skip to content

Commit

Permalink
Propagate traces metadata to tracer
Browse files Browse the repository at this point in the history
Because the traces registry and its underlying tracer are only
initialized on the first iterStart event for each VU, the browser
registry must keep a reference to the traces metadata until this event
is fired and the traces registry and tracer can be initialized.
  • Loading branch information
ka3de committed Dec 11, 2023
1 parent 47e68d6 commit 9658d79
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 13 deletions.
12 changes: 9 additions & 3 deletions browser/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,15 @@ func (m *RootModule) NewModuleInstance(vu k6modules.VU) k6modules.Instance {
return &ModuleInstance{
mod: &JSModule{
Browser: mapBrowserToGoja(moduleVU{
VU: vu,
pidRegistry: m.PidRegistry,
browserRegistry: newBrowserRegistry(context.Background(), vu, m.remoteRegistry, m.PidRegistry),
VU: vu,
pidRegistry: m.PidRegistry,
browserRegistry: newBrowserRegistry(
context.Background(),
vu,
m.remoteRegistry,
m.PidRegistry,
m.tracesMetadata,
),
taskQueueRegistry: newTaskQueueRegistry(vu),
}),
Devices: common.GetDevices(),
Expand Down
18 changes: 11 additions & 7 deletions browser/registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -173,8 +173,9 @@ func (r *remoteRegistry) isRemoteBrowser() (string, bool) {
type browserRegistry struct {
vu k6modules.VU

tr *tracesRegistry
trInit sync.Once
tr *tracesRegistry
trInit sync.Once
tracesMetadata map[string]string

mu sync.RWMutex
m map[int64]*common.Browser
Expand All @@ -187,7 +188,7 @@ type browserRegistry struct {
type browserBuildFunc func(ctx context.Context) (*common.Browser, error)

func newBrowserRegistry(
ctx context.Context, vu k6modules.VU, remote *remoteRegistry, pids *pidRegistry,
ctx context.Context, vu k6modules.VU, remote *remoteRegistry, pids *pidRegistry, tracesMetadata map[string]string,
) *browserRegistry {
bt := chromium.NewBrowserType(vu)
builder := func(ctx context.Context) (*common.Browser, error) {
Expand Down Expand Up @@ -215,9 +216,10 @@ func newBrowserRegistry(
}

r := &browserRegistry{
vu: vu,
m: make(map[int64]*common.Browser),
buildFn: builder,
vu: vu,
tracesMetadata: tracesMetadata,
m: make(map[int64]*common.Browser),
buildFn: builder,
}

exitSubID, exitCh := vu.Events().Global.Subscribe(
Expand Down Expand Up @@ -370,7 +372,9 @@ func (r *browserRegistry) initTracesRegistry() {
// Use a sync.Once so the traces registry is only initialized once
// per VU, as that is the scope for both browser and traces registry.
r.trInit.Do(func() {
r.tr = newTracesRegistry(browsertrace.NewTracer(r.vu.State().TracerProvider))
r.tr = newTracesRegistry(
browsertrace.NewTracer(r.vu.State().TracerProvider, r.tracesMetadata),
)
})
}

Expand Down
6 changes: 3 additions & 3 deletions browser/registry_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ func TestBrowserRegistry(t *testing.T) {
var (
ctx = context.Background()
vu = k6test.NewVU(t)
browserRegistry = newBrowserRegistry(ctx, vu, remoteRegistry, &pidRegistry{})
browserRegistry = newBrowserRegistry(ctx, vu, remoteRegistry, &pidRegistry{}, nil)
)

vu.ActivateVU()
Expand Down Expand Up @@ -248,7 +248,7 @@ func TestBrowserRegistry(t *testing.T) {
var (
ctx = context.Background()
vu = k6test.NewVU(t)
browserRegistry = newBrowserRegistry(ctx, vu, remoteRegistry, &pidRegistry{})
browserRegistry = newBrowserRegistry(ctx, vu, remoteRegistry, &pidRegistry{}, nil)
)

vu.ActivateVU()
Expand Down Expand Up @@ -283,7 +283,7 @@ func TestBrowserRegistry(t *testing.T) {
var (
ctx = context.Background()
vu = k6test.NewVU(t)
browserRegistry = newBrowserRegistry(ctx, vu, remoteRegistry, &pidRegistry{})
browserRegistry = newBrowserRegistry(ctx, vu, remoteRegistry, &pidRegistry{}, nil)
)

vu.ActivateVU()
Expand Down

0 comments on commit 9658d79

Please sign in to comment.