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

k6/cloud module with the testRunId #4092

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open

k6/cloud module with the testRunId #4092

wants to merge 5 commits into from

Conversation

joanlopez
Copy link
Contributor

What?

It adds a new k6/cloud JS module that exposes the testRunId.

This is defined in any of the following scenarios:

  • When the k6 binary is executed in the Cloud (we also need to change the k6-agent).
  • When the k6 binary runs locally, outputting to the Cloud (k6 cloud run --local-execution).

The testRunId could also be read by any other module extension, as the k6/cloud introduced in this changeset does.

Why?

Because this unlocks many different projects we have ongoing, currently blocked by the lack of mechanisms to read the testRunId, either from the JS or the Go (extensions) surface.

Checklist

  • I have performed a self-review of my code.
  • I have added tests for my changes.
  • I have run linter locally (make lint) and all checks pass.
  • I have run tests locally (make tests) and all tests pass.
  • I have commented on my code, particularly in hard-to-understand areas.

Related PR(s)/Issue(s)

Contributes to #4041

@joanlopez joanlopez added the cloud label Dec 5, 2024
@joanlopez joanlopez requested a review from mstoykov December 5, 2024 13:54
@joanlopez joanlopez self-assigned this Dec 5, 2024
@joanlopez joanlopez requested a review from a team as a code owner December 5, 2024 13:54
@joanlopez joanlopez requested review from oleiade and removed request for a team December 5, 2024 13:54
@joanlopez
Copy link
Contributor Author

Note that the approach followed here won't make testRunId available for those executions still using the deprecated command to run a local execution: k6 run -o cloud, mainly because I cannot think of any feasible way to do that other than one that looks along the lines of what I did in k6/cloud-v2, and for which I know @mstoykov have objections to.

But I'd be very happy to see any suggestions or ideas for possible workarounds.
Although, I'm also happy with using that as mechanism to push those still using the deprecated command.

@joanlopez joanlopez added this to the v0.56.0 milestone Dec 5, 2024
Copy link
Member

@oleiade oleiade left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I left a couple of nit comments behind, but none of them blocking, I'd be happy with this merged as-is. Great work!

rt := vu.Runtime()

mi.obj = rt.NewObject()
defProp := func(name string, getter func() (sobek.Value, error)) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Genuine curiosity, is there an advantage to defining a closure for this operation considering it's used only once? Or is it to scope the logic in the code and make it easier to maintain: which I'm completely okay with.

func (mi *ModuleInstance) testRunId() (sobek.Value, error) {
// In case we have a value (e.g. loaded from env), we return it.
// If we're in the init context (where we cannot read the options), we return undefined (the default value).
if !sobek.IsUndefined(mi.testRunID) || mi.vu.State() == nil {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would it make sens to use common.IsNullish here? Or do we want to specifically look only for undefined here?


// Otherwise, we try to read the test run id from options.
// We only try it once for the whole execution, as options won't change.
vuState := mi.vu.State()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: as you already check whether mi.vu.State() is nil before, would it make sense moving this assignment at the top of the function? What do you think?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants