Skip to content

Commit a406a4a

Browse files
committed
feat: do not output timeseries whose resource_type does not match an allowlist
1 parent d3201d3 commit a406a4a

File tree

2 files changed

+37
-3
lines changed

2 files changed

+37
-3
lines changed

README.md

+8
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,14 @@
55
66
Output k6 extension used by the [synthetic monitoring agent](https://github.com/grafana/synthetic-monitoring-agent).
77

8+
## Configuration
9+
10+
By default, this extension will drop all metrics which have a `resource_type` tag when the value for the tag is not `Document`. This is done to avoid generating metrics for every URL that a k6 script using browser would normally generate, including those for images, scripts, and others.
11+
12+
This `resource_type` allowlist is configurable by means of the `SM_K6_BROWSER_RESOURCE_TYPES` environment variable, which should be set to a comma-separated list of `resource_type`s that should not be dropped. The list of known `resource_type`s can be found [here](https://github.com/grafana/k6/blob/v0.57.0/internal/js/modules/k6/browser/common/http.go#L25).
13+
14+
This matching is case-insensitive, and the special value `*` will cause every `resource_type` to be retained. Conversely, an empty list will cause all metrics from browser to be omitted.
15+
816
## Build
917

1018
Use [xk6](https://github.com/grafana/xk6). See the CI/CD pipelines for a full example of a build command.

output.go

+29-3
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"fmt"
66
"io"
77
"math"
8+
"os"
89
"slices"
910
"strconv"
1011
"strings"
@@ -425,9 +426,7 @@ func (ms *metricStore) RemoveLabels() {
425426
func (ms *metricStore) RemoveMetrics() {
426427
log := ms.logger.WithField("step", "removeMetrics")
427428

428-
// As per BenchmarkRemove, it is better to store needles on a map. Experimentally, 8 needles or less are faster to
429-
// store and check against in a slice, but 16 or more are faster on a map. Slices likely stop being worth it when
430-
// the full slice does not fit on a cache line.
429+
// Timeseries for metrics whose name is in this map will be removed.
431430
deletable := map[string]bool{
432431
// Not useful in SM context:
433432
"vus": true,
@@ -454,6 +453,33 @@ func (ms *metricStore) RemoveMetrics() {
454453
log.Tracef("Dropping %q", ts.name)
455454
}
456455
}
456+
457+
// List of resource types that are worth keeping metrics for. All timeseries with a `resource_type` tag not present
458+
// here will be removed.
459+
allowedResourceTypes := map[string]bool{"document": true}
460+
// If SM_K6_BROWSER_RESOURCE_TYPES is defined, parse it as a comma-separated list of `resource_type`s to allow.
461+
if envTypes := os.Getenv("SM_K6_BROWSER_RESOURCE_TYPES"); envTypes != "" {
462+
allowedResourceTypes := map[string]bool{}
463+
for _, rt := range strings.Split(envTypes, ",") {
464+
allowedResourceTypes[strings.ToLower(rt)] = true
465+
}
466+
467+
log.WithField("allowlist", allowedResourceTypes).Debug("Using configured resource_type allowlist")
468+
}
469+
470+
// Treat "*" as a wildcard. If it is present on the allowlist, then there is no point in walking it.
471+
if allowedResourceTypes["*"] {
472+
return
473+
}
474+
475+
log = log.WithField("allowlist", allowedResourceTypes)
476+
477+
for ts := range ms.store {
478+
if rt, _ := ts.tags.Get("resource_type"); rt != "" && !allowedResourceTypes[strings.ToLower(rt)] {
479+
delete(ms.store, ts)
480+
log.Tracef("Dropping %q as resource type %q is not in allowlist", ts.name, rt)
481+
}
482+
}
457483
}
458484

459485
// Output is a k6 output plugin that writes metrics to an io.Writer in

0 commit comments

Comments
 (0)