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

Add tracing #1100

Merged
merged 26 commits into from
Dec 11, 2023
Merged

Add tracing #1100

merged 26 commits into from
Dec 11, 2023

Commits on Dec 11, 2023

  1. Add trace package

    This package provides high level tracing APIs tailored to k6 browser
    needs, defining methods that allow to correlate async events with the
    pages to which they belong to.
    ka3de committed Dec 11, 2023
    Configuration menu
    Copy the full SHA
    71651ad View commit details
    Browse the repository at this point in the history
  2. Add traces registry

    Adds a new traces registry which will hold the references for every
    iteration root span and wrapping context. This span will be extended
    for every instrumented method call during the iteration execution.
    ka3de committed Dec 11, 2023
    Configuration menu
    Copy the full SHA
    dca0b5e View commit details
    Browse the repository at this point in the history
  3. Initialize traces registry on VU iterStart

    The traces registry has to be held per VU scope, as it stores traces
    indexed per iteration, which are only unique per VU. Therefore keep it
    under browser registry so we can use it to handle traces generation
    based on iter events.
    Because VU.State is nil when the browser registry is initialized, we
    have to initialize the traces registry on the first VU iteration so we
    can get access to the k6 TracerProvider. This is done within a sync.Once
    so we ensure the traces registry is only initialized once per VU.
    ka3de committed Dec 11, 2023
    Configuration menu
    Copy the full SHA
    18890e9 View commit details
    Browse the repository at this point in the history
  4. Start/End iteration traces on iter events

    This also sets the trace context, wrapping the iteration's root span, as
    the context for browser, which will be inherited by the other components
    such as browser context, page, etc.
    ka3de committed Dec 11, 2023
    Configuration menu
    Copy the full SHA
    c3d0f49 View commit details
    Browse the repository at this point in the history
  5. Shutdown traces registry on exit event

    Only stop the traces registry if it has been previously initialized,
    which won't happen for the initial VU.
    When stopping, ensure any active live span is ended before exiting.
    ka3de committed Dec 11, 2023
    Configuration menu
    Copy the full SHA
    e7412f6 View commit details
    Browse the repository at this point in the history
  6. Configuration menu
    Copy the full SHA
    c868ca1 View commit details
    Browse the repository at this point in the history
  7. Wrap Tracer into browser context

    This is so the tracer can be retrieved by the other components which
    inherit the browser context (BrowserContext, Page, etc) and use it to
    generate spans that represent their actions.
    ka3de committed Dec 11, 2023
    Configuration menu
    Copy the full SHA
    8c5934e View commit details
    Browse the repository at this point in the history
  8. Trace page navigation

    Generate a span from onFrameNavigated async event parsing. We must also
    keep a reference to the last active span for the main frame so we can
    end it when FrameSession.ctx is Done so the span is flushed before
    exiting.
    ka3de committed Dec 11, 2023
    Configuration menu
    Copy the full SHA
    c351d75 View commit details
    Browse the repository at this point in the history
  9. Add EvaluateGlobal

    Allows to run JS code in the global object instead of a specific
    execution context.
    ka3de committed Dec 11, 2023
    Configuration menu
    Copy the full SHA
    a3ff377 View commit details
    Browse the repository at this point in the history
  10. Inject spanID on frame navigated

    In order to be able to correlate the Web Vitals events to the navigation
    span to which they belong to, we have to add a reference to that span in
    the page context, so it can be retrieved by our Web Vitals script that
    is executed in the page before pushing the WV metrics to k6 browser
    through the set up binding.
    
    The JS code to set the spanID in the page context is executed through
    the EvaluateGlobal method as otherwise it would be executed in the
    current frame context. The problem is that, because we are processing
    a navigation event, the frame context has actually already changed in
    the browser but our event parsing goroutine will not process that until
    we exit the onFrameNavigated method and receive the
    onExecutionContextCreated event. This would result in an error when
    evaluating the JS code due to "execution context changed".
    In this case, because the JS code sets a property in the global object
    (window), it is OK to just execute it in the global context.
    
    Additionally the EvaluateGlobal method has to be called in a separate
    goroutine to avoid deadlocking the event parsing goroutine when the
    frame loading generates CDP events that have to be acted on for the
    loading process to continue, such as when the frame contains a JS
    initiated dialog (alert, confirm, prompt, or onbeforeunload) which we
    have to parse and respond with an explicit accept/dismiss action (see
    onEventJavascriptDialogOpening).
    ka3de committed Dec 11, 2023
    Configuration menu
    Copy the full SHA
    376204a View commit details
    Browse the repository at this point in the history
  11. Add spanID to web_vital_init.js

    To be able to associate a web vital to the PageNavigation span, we need
    to inject the span's id that was previously set in the page in
    onFrameNavigated to the WV event so that the measurement can
    be correctly linked to the page navigation span.
    ka3de committed Dec 11, 2023
    Configuration menu
    Copy the full SHA
    c09804b View commit details
    Browse the repository at this point in the history
  12. Trace WebVital events

    Generate a span that represents a WebVital measurement.
    ka3de committed Dec 11, 2023
    Configuration menu
    Copy the full SHA
    f66f8fc View commit details
    Browse the repository at this point in the history
  13. Trace Browser API

    ka3de committed Dec 11, 2023
    Configuration menu
    Copy the full SHA
    ea19a68 View commit details
    Browse the repository at this point in the history
  14. Trace BrowserContext API

    ka3de committed Dec 11, 2023
    Configuration menu
    Copy the full SHA
    5bfdf28 View commit details
    Browse the repository at this point in the history
  15. Trace Screenshot APIs

    ka3de committed Dec 11, 2023
    Configuration menu
    Copy the full SHA
    0bdafbe View commit details
    Browse the repository at this point in the history
  16. Trace Locator API

    ka3de committed Dec 11, 2023
    Configuration menu
    Copy the full SHA
    c92af9b View commit details
    Browse the repository at this point in the history
  17. Trace Page API

    ka3de committed Dec 11, 2023
    Configuration menu
    Copy the full SHA
    ab5e5cb View commit details
    Browse the repository at this point in the history
  18. Add common's Tracer interface

    Defines a tracer interface with only the methods required for the common
    package tracing. This also allows us to implement higher level helper
    functions that will automatically retrieve the Tracer from a given
    context and use it to generate a span, or return a noop span if no
    tracer is found in the context, avoiding a possible NPE.
    ka3de committed Dec 11, 2023
    Configuration menu
    Copy the full SHA
    5027c81 View commit details
    Browse the repository at this point in the history
  19. Configuration menu
    Copy the full SHA
    05cf6fc View commit details
    Browse the repository at this point in the history
  20. Add tracing helper functions

    This functions will try to call the same method they implement from the
    tracer contained in the given context. If the tracer is not found in the
    context, they return a noop Span. This makes the code in the common
    package cleaner and avoids dealing with possible NPEs. At the sime time
    avoids having to mock the tracer in tests.
    ka3de committed Dec 11, 2023
    Configuration menu
    Copy the full SHA
    88ea879 View commit details
    Browse the repository at this point in the history
  21. Configuration menu
    Copy the full SHA
    149f350 View commit details
    Browse the repository at this point in the history
  22. Configuration menu
    Copy the full SHA
    5a80597 View commit details
    Browse the repository at this point in the history
  23. Add support for WithTracerProvider in test VU

    Allows to set a specific TracerProvider for the test VU implementation.
    If not set, it defaults to NoopTracerProvider.
    ka3de committed Dec 11, 2023
    Configuration menu
    Copy the full SHA
    e425052 View commit details
    Browse the repository at this point in the history
  24. Add tracing integration test

    ka3de committed Dec 11, 2023
    Configuration menu
    Copy the full SHA
    fe7ad96 View commit details
    Browse the repository at this point in the history
  25. Refactor onFrameNavigated tracing

    Reduces nested levels.
    ka3de committed Dec 11, 2023
    Configuration menu
    Copy the full SHA
    4d68e20 View commit details
    Browse the repository at this point in the history
  26. Set browser registry context from root module

    Modifies the browser registry constructor to accept a context that will
    be used in order to build the browser context which will eventually be
    inherited by the other browser components.
    ka3de committed Dec 11, 2023
    Configuration menu
    Copy the full SHA
    0f0e214 View commit details
    Browse the repository at this point in the history