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 resource_type tag #4140

Merged
merged 7 commits into from
Jan 3, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 61 additions & 2 deletions js/modules/k6/browser/common/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,30 @@ import (
k6modules "go.k6.io/k6/js/modules"
)

// These ResourceTypes are duplicates of CDP's network.ResourceType. We want to work
// with our version of ResourceType to catch any breaking changes early.
const (
ResourceTypeDocument string = "Document"
ResourceTypeStylesheet string = "Stylesheet"
ResourceTypeImage string = "Image"
ResourceTypeMedia string = "Media"
ResourceTypeFont string = "Font"
ResourceTypeScript string = "Script"
ResourceTypeTextTrack string = "TextTrack"
ResourceTypeXHR string = "XHR"
ResourceTypeFetch string = "Fetch"
ResourceTypePrefetch string = "Prefetch"
ResourceTypeEventSource string = "EventSource"
ResourceTypeWebSocket string = "WebSocket"
ResourceTypeManifest string = "Manifest"
ResourceTypeSignedExchange string = "SignedExchange"
ResourceTypePing string = "Ping"
ResourceTypeCSPViolationReport string = "CSPViolationReport"
ResourceTypePreflight string = "Preflight"
ResourceTypeOther string = "Other"
ResourceTypeUnknown string = "Unknown"
)

// HTTPHeader is a single HTTP header.
type HTTPHeader struct {
Name string `json:"name"`
Expand Down Expand Up @@ -88,7 +112,7 @@ type NewRequestParams struct {
}

// NewRequest creates a new HTTP request.
func NewRequest(ctx context.Context, rp NewRequestParams) (*Request, error) {
func NewRequest(ctx context.Context, logger *log.Logger, rp NewRequestParams) (*Request, error) {
ev := rp.event

documentID := cdp.LoaderID("")
Expand Down Expand Up @@ -129,7 +153,7 @@ func NewRequest(ctx context.Context, rp NewRequestParams) (*Request, error) {
requestID: ev.RequestID,
method: ev.Request.Method,
postDataEntries: pd,
resourceType: ev.Type.String(),
resourceType: validateResourceType(logger, ev.Type.String()),
isNavigationRequest: isNavigationRequest,
allowInterception: rp.allowInterception,
interceptionID: rp.interceptionID,
Expand All @@ -150,6 +174,41 @@ func NewRequest(ctx context.Context, rp NewRequestParams) (*Request, error) {
return &r, nil
}

// validateResourceType will validate network.ResourceType string values against our own
// ResourceType string values.
// - If a new network.ResourceType is added, this will log a warn and return
// ResourceTypeUnknown.
// - If an existing network.ResourceType is amended, this will log a warn and return
// ResourceTypeUnknown.
// - If a network.ResourceType is deleted then we will get a compilation error.
func validateResourceType(logger *log.Logger, t string) string {
switch t {
case ResourceTypeDocument:
case ResourceTypeStylesheet:
case ResourceTypeImage:
case ResourceTypeMedia:
case ResourceTypeFont:
case ResourceTypeScript:
case ResourceTypeTextTrack:
case ResourceTypeXHR:
case ResourceTypeFetch:
case ResourceTypePrefetch:
case ResourceTypeEventSource:
case ResourceTypeWebSocket:
case ResourceTypeManifest:
case ResourceTypeSignedExchange:
case ResourceTypePing:
case ResourceTypeCSPViolationReport:
case ResourceTypePreflight:
case ResourceTypeOther:
default:
t = ResourceTypeUnknown
logger.Warnf("http:resourceType", "unknown network.ResourceType %q detected", t)
}

return t
}

func (r *Request) getFrame() *Frame {
return r.frame
}
Expand Down
47 changes: 45 additions & 2 deletions js/modules/k6/browser/common/http_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package common

import (
"strings"
"testing"
"time"

Expand All @@ -10,6 +11,7 @@ import (
"github.com/stretchr/testify/require"

"go.k6.io/k6/js/modules/k6/browser/k6ext/k6test"
"go.k6.io/k6/js/modules/k6/browser/log"
)

func TestRequest(t *testing.T) {
Expand All @@ -30,7 +32,7 @@ func TestRequest(t *testing.T) {
WallTime: &wt,
}
vu := k6test.NewVU(t)
req, err := NewRequest(vu.Context(), NewRequestParams{
req, err := NewRequest(vu.Context(), log.NewNullLogger(), NewRequestParams{
event: evt,
interceptionID: "intercept",
})
Expand All @@ -51,7 +53,7 @@ func TestRequest(t *testing.T) {
WallTime: &wt,
}
vu := k6test.NewVU(t)
req, err := NewRequest(vu.Context(), NewRequestParams{
req, err := NewRequest(vu.Context(), log.NewNullLogger(), NewRequestParams{
event: evt,
interceptionID: "intercept",
})
Expand Down Expand Up @@ -124,3 +126,44 @@ func TestResponse(t *testing.T) {
assert.Equal(t, "value", got)
})
}

func TestValidateResourceType(t *testing.T) {
t.Parallel()

tests := []struct {
name string
input string
want string
}{
{name: ResourceTypeDocument, input: network.ResourceTypeDocument.String(), want: ResourceTypeDocument},
{name: ResourceTypeStylesheet, input: network.ResourceTypeStylesheet.String(), want: ResourceTypeStylesheet},
{name: ResourceTypeImage, input: network.ResourceTypeImage.String(), want: ResourceTypeImage},
{name: ResourceTypeMedia, input: network.ResourceTypeMedia.String(), want: ResourceTypeMedia},
{name: ResourceTypeFont, input: network.ResourceTypeFont.String(), want: ResourceTypeFont},
{name: ResourceTypeScript, input: network.ResourceTypeScript.String(), want: ResourceTypeScript},
{name: ResourceTypeTextTrack, input: network.ResourceTypeTextTrack.String(), want: ResourceTypeTextTrack},
{name: ResourceTypeXHR, input: network.ResourceTypeXHR.String(), want: ResourceTypeXHR},
{name: ResourceTypeFetch, input: network.ResourceTypeFetch.String(), want: ResourceTypeFetch},
{name: ResourceTypePrefetch, input: network.ResourceTypePrefetch.String(), want: ResourceTypePrefetch},
{name: ResourceTypeEventSource, input: network.ResourceTypeEventSource.String(), want: ResourceTypeEventSource},
{name: ResourceTypeWebSocket, input: network.ResourceTypeWebSocket.String(), want: ResourceTypeWebSocket},
{name: ResourceTypeManifest, input: network.ResourceTypeManifest.String(), want: ResourceTypeManifest},
{name: ResourceTypeSignedExchange, input: network.ResourceTypeSignedExchange.String(), want: ResourceTypeSignedExchange},
{name: ResourceTypePing, input: network.ResourceTypePing.String(), want: ResourceTypePing},
{name: ResourceTypeCSPViolationReport, input: network.ResourceTypeCSPViolationReport.String(), want: ResourceTypeCSPViolationReport},
{name: ResourceTypePreflight, input: network.ResourceTypePreflight.String(), want: ResourceTypePreflight},
{name: ResourceTypeOther, input: network.ResourceTypeOther.String(), want: ResourceTypeOther},
{name: "fake", input: "fake", want: ResourceTypeUnknown},
{name: "amended_existing", input: strings.ToLower(network.ResourceTypeOther.String()), want: ResourceTypeUnknown},
}

for _, tt := range tests {
tt := tt
t.Run(tt.name, func(t *testing.T) {
t.Parallel()

got := validateResourceType(log.NewNullLogger(), tt.input)
assert.Equal(t, got, tt.want)
})
}
}
5 changes: 4 additions & 1 deletion js/modules/k6/browser/common/network_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,7 @@ func (m *NetworkManager) emitRequestMetrics(req *Request) {
if state.Options.SystemTags.Has(k6metrics.TagURL) {
tags = handleURLTag(m.mi, req.URL(), req.method, tags)
}
tags = tags.With("resource_type", req.ResourceType())
Copy link
Contributor Author

Choose a reason for hiding this comment

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

There are two things that I'm not sure of:

  1. This isn't a system tag, but should it be?
  2. This list can change at a moments notice by the chrome team, should we map them from the chrome ResourceType to a k6 version of it. We would be able to detect if we stop matching on any of the types and log a warning, plus create a unit test to catch this as early as possible.

Copy link
Contributor

Choose a reason for hiding this comment

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

We would be able to detect if we stop matching on any of the types and log a warning, plus create a unit test to catch this as early as possible.

This sounds like the way to go! 👍🏻

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I've added a way to validate the ResourceTypes.


k6metrics.PushIfNotDone(m.vu.Context(), state.Samples, k6metrics.ConnectedSamples{
Samples: []k6metrics.Sample{
Expand All @@ -192,6 +193,7 @@ func (m *NetworkManager) emitRequestMetrics(req *Request) {
})
}

//nolint:funlen
func (m *NetworkManager) emitResponseMetrics(resp *Response, req *Request) {
state := m.vu.State()

Expand Down Expand Up @@ -246,6 +248,7 @@ func (m *NetworkManager) emitResponseMetrics(resp *Response, req *Request) {
tags = tags.With("from_cache", strconv.FormatBool(fromCache))
tags = tags.With("from_prefetch_cache", strconv.FormatBool(fromPreCache))
tags = tags.With("from_service_worker", strconv.FormatBool(fromSvcWrk))
tags = tags.With("resource_type", req.ResourceType())

k6metrics.PushIfNotDone(m.vu.Context(), state.Samples, k6metrics.ConnectedSamples{
Samples: []k6metrics.Sample{
Expand Down Expand Up @@ -488,7 +491,7 @@ func (m *NetworkManager) onRequest(event *network.EventRequestWillBeSent, interc
event.Request.URL, event.Request.Method, event.Initiator.Type, event.FrameID)
}

req, err := NewRequest(m.ctx, NewRequestParams{
req, err := NewRequest(m.ctx, m.logger, NewRequestParams{
event: event,
frame: frame,
redirectChain: redirectChain,
Expand Down
2 changes: 1 addition & 1 deletion js/modules/k6/browser/common/network_manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,7 @@ func TestNetworkManagerEmitRequestResponseMetricsTimingSkew(t *testing.T) {
)
vu.ActivateVU()

req, err := NewRequest(vu.Context(), NewRequestParams{
req, err := NewRequest(vu.Context(), log.NewNullLogger(), NewRequestParams{
event: &network.EventRequestWillBeSent{
Request: &network.Request{},
Timestamp: (*cdp.MonotonicTime)(&tt.req.ts),
Expand Down
Loading