Skip to content

Commit 9487dd4

Browse files
committed
feat: do not output timeseries whose resource_type does not match an allowlist
1 parent 0389dce commit 9487dd4

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"
@@ -432,9 +433,7 @@ func (ms *metricStore) RemoveLabels() {
432433
func (ms *metricStore) RemoveMetrics() {
433434
log := ms.logger.WithField("step", "removeMetrics")
434435

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

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

0 commit comments

Comments
 (0)