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

Move the name field on the `page.on('metric') API #1489

Merged
merged 6 commits into from
Oct 17, 2024
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
2 changes: 1 addition & 1 deletion browser/mapping_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -378,7 +378,7 @@ type consoleMessageAPI interface {

// metricEventAPI is the interface of a metric event.
type metricEventAPI interface {
Tag(matchesRegex common.K6BrowserCheckRegEx, patterns common.URLTagPatterns) error
Tag(matchesRegex common.K6BrowserCheckRegEx, patterns common.TagMatches) error
}

// frameAPI is the interface of a CDP target frame.
Expand Down
2 changes: 1 addition & 1 deletion browser/metric_event_mapping.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ func mapMetricEvent(vu moduleVU, event common.PageOnEvent) mapping {
em := event.Metric

return mapping{
"tag": func(urls common.URLTagPatterns) error {
"tag": func(urls common.TagMatches) error {
callback := func(pattern, url string) (bool, error) {
js := fmt.Sprintf(`_k6BrowserCheckRegEx(%s, '%s')`, pattern, url)

Expand Down
47 changes: 25 additions & 22 deletions common/page.go
Original file line number Diff line number Diff line change
Expand Up @@ -385,25 +385,27 @@ type MetricEvent struct {
// against the URL grouping regexs.
url string

// When a match is found this userProvidedTagName field should be updated.
userProvidedTagName string
// When a match is found this userProvidedURLTagName field should be updated.
userProvidedURLTagName string

// When a match is found this is set to true.
isUserURLTagNameExist bool
}

// URLTagPatterns will contain all the URL groupings.
type URLTagPatterns struct {
URLs []URLTagPattern `js:"urls"`
// TagMatches contains the name tag and matches used to match against existing
// metric tags that are about to be emitted.
type TagMatches struct {
// The name to send back to the caller of the handler.
TagName string `js:"name"`
// The patterns to match against.
Matches []Match `js:"matches"`
}

// URLTagPattern contains the single url regex and the name to give to the metric
// if a match is found.
type URLTagPattern struct {
// Match contains the fields that will be used to match against metric tags
// that are about to be emitted.
type Match struct {
// This is a regex that will be compared against the existing url tag.
URLRegEx string `js:"url"`
// The name to send back to the caller of the handler.
TagName string `js:"name"`
}

// K6BrowserCheckRegEx is a function that will be used to check the URL tag
Expand All @@ -412,33 +414,34 @@ type K6BrowserCheckRegEx func(pattern, url string) (bool, error)

// Tag will find the first match given the URLTagPatterns and the URL from
// the metric tag and update the name field.
func (e *MetricEvent) Tag(matchesRegex K6BrowserCheckRegEx, patterns URLTagPatterns) error {
for _, o := range patterns.URLs {
name := strings.TrimSpace(o.TagName)
if name == "" {
return fmt.Errorf("name %q is invalid", o.TagName)
}
func (e *MetricEvent) Tag(matchesRegex K6BrowserCheckRegEx, matches TagMatches) error {
name := strings.TrimSpace(matches.TagName)
if name == "" {
return fmt.Errorf("name %q is invalid", matches.TagName)
}

for _, m := range matches.Matches {
// matchesRegex is a function that will perform the regex test in the Sobek
// runtime.
matched, err := matchesRegex(o.URLRegEx, e.url)
matched, err := matchesRegex(m.URLRegEx, e.url)
if err != nil {
return err
}

if matched {
e.isUserURLTagNameExist = true
e.userProvidedTagName = name
e.userProvidedURLTagName = name
return nil
}
}

return nil
}

// urlTagName is used to check the incoming metric url tag against user
// defined url regexes. When a match is found a user defined name, which is to
// be used in the urls place in the url metric tag, is returned.
// urlTagName is used to match the given url with the matches defined by the
// user. Currently matches only contains url. When a match is found a user
// defined name, which is to be used in the urls place in the url metric tag,
// is returned.
//
// The check is done by calling the handlers that were registered with
// `page.on('metric')`. The user will need to use `Tag` to supply the
Expand Down Expand Up @@ -473,7 +476,7 @@ func (p *Page) urlTagName(url string) (string, bool) {

// If a match was found then the name field in em will have been updated.
if em.isUserURLTagNameExist {
newTagName = em.userProvidedTagName
newTagName = em.userProvidedURLTagName
urlMatched = true
}

Expand Down
5 changes: 3 additions & 2 deletions examples/pageon-metric.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,9 @@ export default async function() {

page.on('metric', (metric) => {
metric.tag({
urls: [
{url: /^https:\/\/test\.k6\.io\/\?q=[0-9a-z]+$/, name:'test'},
name:'test',
matches: [
{url: /^https:\/\/test\.k6\.io\/\?q=[0-9a-z]+$/},
]
});
});
Expand Down
42 changes: 25 additions & 17 deletions tests/page_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1919,8 +1919,9 @@ func TestPageOnMetric(t *testing.T) {
name: "single_page.on",
fun: `page.on('metric', (metric) => {
metric.tag({
urls: [
{url: /^http:\/\/127\.0\.0\.1\:[0-9]+\/ping\?h=[0-9a-z]+$/, name:'ping-1'},
name:'ping-1',
matches: [
{url: /^http:\/\/127\.0\.0\.1\:[0-9]+\/ping\?h=[0-9a-z]+$/},
]
});
});`,
Expand All @@ -1931,14 +1932,16 @@ func TestPageOnMetric(t *testing.T) {
name: "multi_tag",
fun: `page.on('metric', (metric) => {
metric.tag({
urls: [
{url: /^http:\/\/127\.0\.0\.1\:[0-9]+\/ping\?h=[0-9a-z]+$/, name:'ping-1'},
name:'ping-1',
matches: [
{url: /^http:\/\/127\.0\.0\.1\:[0-9]+\/ping\?h=[0-9a-z]+$/},
]
});
metric.tag({
urls: [
{url: /^http:\/\/127\.0\.0\.1\:[0-9]+\/ping\?h=[0-9a-z]+$/, name:'ping-2'},
]
name:'ping-2',
matches: [
{url: /^http:\/\/127\.0\.0\.1\:[0-9]+\/ping\?h=[0-9a-z]+$/},
]
});
});`,
want: "ping-2",
Expand All @@ -1948,20 +1951,23 @@ func TestPageOnMetric(t *testing.T) {
name: "multi_tag_page.on",
fun: `page.on('metric', (metric) => {
metric.tag({
urls: [
{url: /^http:\/\/127\.0\.0\.1\:[0-9]+\/ping\?h=[0-9a-z]+$/, name:'ping-1'},
name:'ping-1',
matches: [
{url: /^http:\/\/127\.0\.0\.1\:[0-9]+\/ping\?h=[0-9a-z]+$/},
]
});
metric.tag({
urls: [
{url: /^http:\/\/127\.0\.0\.1\:[0-9]+\/ping\?h=[0-9a-z]+$/, name:'ping-2'},
name:'ping-2',
matches: [
{url: /^http:\/\/127\.0\.0\.1\:[0-9]+\/ping\?h=[0-9a-z]+$/},
]
});
});
page.on('metric', (metric) => {
metric.tag({
urls: [
{url: /^http:\/\/127\.0\.0\.1\:[0-9]+\/ping\?h=[0-9a-z]+$/, name:'ping-3'},
name:'ping-3',
matches: [
{url: /^http:\/\/127\.0\.0\.1\:[0-9]+\/ping\?h=[0-9a-z]+$/},
]
});
});`,
Expand All @@ -1972,14 +1978,16 @@ func TestPageOnMetric(t *testing.T) {
name: "multi_page.on_call",
fun: `page.on('metric', (metric) => {
metric.tag({
urls: [
{url: /^http:\/\/127\.0\.0\.1\:[0-9]+\/ping\?h=[0-9a-z]+$/, name:'ping-1'},
name:'ping-1',
matches: [
{url: /^http:\/\/127\.0\.0\.1\:[0-9]+\/ping\?h=[0-9a-z]+$/},
]
});
page.on('metric', (metric) => {
metric.tag({
urls: [
{url: /^http:\/\/127\.0\.0\.1\:[0-9]+\/ping\?h=[0-9a-z]+$/, name:'ping-4'},
name:'ping-4',
matches: [
{url: /^http:\/\/127\.0\.0\.1\:[0-9]+\/ping\?h=[0-9a-z]+$/},
]
});
});
Expand Down
Loading