diff --git a/browser/mapping_test.go b/browser/mapping_test.go index 585d7a750..6d573124e 100644 --- a/browser/mapping_test.go +++ b/browser/mapping_test.go @@ -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. diff --git a/browser/metric_event_mapping.go b/browser/metric_event_mapping.go index 594400a0f..e4e075626 100644 --- a/browser/metric_event_mapping.go +++ b/browser/metric_event_mapping.go @@ -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) diff --git a/common/page.go b/common/page.go index 0cfa718e2..45b6a6abf 100644 --- a/common/page.go +++ b/common/page.go @@ -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 @@ -412,23 +414,23 @@ 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 } } @@ -436,9 +438,10 @@ func (e *MetricEvent) Tag(matchesRegex K6BrowserCheckRegEx, patterns URLTagPatte 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 @@ -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 } diff --git a/examples/pageon-metric.js b/examples/pageon-metric.js index 978f301e1..a804654b4 100644 --- a/examples/pageon-metric.js +++ b/examples/pageon-metric.js @@ -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]+$/}, ] }); }); diff --git a/tests/page_test.go b/tests/page_test.go index 4d50671ae..ac84037c4 100644 --- a/tests/page_test.go +++ b/tests/page_test.go @@ -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]+$/}, ] }); });`, @@ -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", @@ -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]+$/}, ] }); });`, @@ -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]+$/}, ] }); });