Skip to content
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
58 changes: 58 additions & 0 deletions metrics/payload/line_metric_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -386,3 +386,61 @@ func BenchmarkMetricValueLabels(b *testing.B) {
}
}
}

func TestAcceptRejectRegex(t *testing.T) {
ts := time.Now()
tsString := fmt.Sprintf("%d", ts.Unix())
input := "op_total{probe=\"p1\"} 50\nop_success{probe=\"p1\"} 48"
tests := []struct {
desc string
acceptRe string
rejectRe string
wantEM []string
}{
{
desc: "no regex",
wantEM: []string{"labels=probe=p1 op_total=50.000", "labels=probe=p1 op_success=48.000"},
},
{
desc: "accept regex",
acceptRe: "op_total",
wantEM: []string{"labels=probe=p1 op_total=50.000"},
},
{
desc: "reject regex",
rejectRe: "op_total",
wantEM: []string{"labels=probe=p1 op_success=48.000"},
},
{
desc: "accept and reject regex",
acceptRe: "op_success",
rejectRe: "op_total",
wantEM: []string{"labels=probe=p1 op_success=48.000"},
},
}

for _, test := range tests {
t.Run(test.desc, func(t *testing.T) {
c := &configpb.OutputMetricsOptions{
LineAcceptRegex: proto.String(test.acceptRe),
LineRejectRegex: proto.String(test.rejectRe),
}
p, err := NewParser(c, nil)
if err != nil {
t.Error(err)
}

ems := p.PayloadMetrics(&Input{Text: []byte(input)}, testTarget)

got := make([]string, len(ems))
for i, em := range ems {
em.Timestamp = ts
got[i] = em.String()
}
for i := range test.wantEM {
test.wantEM[i] = tsString + " " + test.wantEM[i]
}
assert.Equal(t, test.wantEM, got)
})
}
}
6 changes: 6 additions & 0 deletions metrics/payload/line_metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,12 @@ func (p *Parser) processLine(line, targetKey string) *metrics.EventMetrics {
func (p *Parser) lineBasedMetrics(text []byte, targetKey string) []*metrics.EventMetrics {
var results []*metrics.EventMetrics
for _, line := range strings.Split(string(text), "\n") {
if p.lineAcceptRe != nil && !p.lineAcceptRe.MatchString(line) {
continue
}
if p.lineRejectRe != nil && p.lineRejectRe.MatchString(line) {
continue
}
line = strings.TrimSpace(line)
if len(line) == 0 {
continue
Expand Down
23 changes: 22 additions & 1 deletion metrics/payload/payload.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ package payload

import (
"fmt"
"regexp"
"strings"
"time"

Expand Down Expand Up @@ -45,7 +46,11 @@ type Parser struct {
aggregate bool

jsonMetrics []*jsonMetric
l *logger.Logger

lineAcceptRe *regexp.Regexp
lineRejectRe *regexp.Regexp

l *logger.Logger
}

type Input struct {
Expand All @@ -71,6 +76,22 @@ func NewParser(opts *configpb.OutputMetricsOptions, l *logger.Logger) (*Parser,
l: l,
}

if opts.GetLineAcceptRegex() != "" {
re, err := regexp.Compile(opts.GetLineAcceptRegex())
if err != nil {
return nil, fmt.Errorf("payload.NewParser: error compiling line accept regex: %v", err)
}
parser.lineAcceptRe = re
}

if opts.GetLineRejectRegex() != "" {
re, err := regexp.Compile(opts.GetLineRejectRegex())
if err != nil {
return nil, fmt.Errorf("payload.NewParser: error compiling line reject regex: %v", err)
}
parser.lineRejectRe = re
}

jsonMetricGroups, err := parseJSONMetricConfig(opts.GetJsonMetric())
if err != nil {
return nil, err
Expand Down
73 changes: 48 additions & 25 deletions metrics/payload/proto/config.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions metrics/payload/proto/config.proto
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,12 @@ message OutputMetricsOptions {
// Note: This option doesn't work on header_metric and json_metric.
map<string, metrics.Dist> dist_metric = 4;

// If provided we process only lines that match this regex.
optional string line_accept_regex = 7;

// If provided we skip lines that match this regex.
optional string line_reject_regex = 8;

// Header to metric mapping. If this mapping is defined and probe's response
// is an HTTP response, we will extract the header value and create a metric
// from it.
Expand Down
5 changes: 4 additions & 1 deletion probes/browser/browser.go
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,10 @@ func (p *Probe) Init(name string, opts *options.Options) error {
p.outputDir = filepath.Join(p.workdir, "output")

if !p.c.GetTestMetricsOptions().GetDisableTestMetrics() {
omo := &payload_configpb.OutputMetricsOptions{}
omo := &payload_configpb.OutputMetricsOptions{
// All our metrics start with "test_".
LineAcceptRegex: proto.String(`^test_.+`),
}
if !p.c.GetTestMetricsOptions().GetDisableAggregation() {
omo.MetricsKind = payload_configpb.OutputMetricsOptions_CUMULATIVE.Enum()
omo.AggregateInCloudprober = proto.Bool(true)
Expand Down