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

fix: In grpc test runs we don't have the expected http metrics #3

Closed
wants to merge 1 commit into from
Closed
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
80 changes: 42 additions & 38 deletions output.go
Original file line number Diff line number Diff line change
Expand Up @@ -384,8 +384,10 @@ func (c targetMetricsCollection) Write(w io.Writer) {
// timings might be missing. This means that we might skew the
// results towards 0 if we try to do over-time aggregations.

out.Name(`probe_http_status_code`)
out.Value(ti.status[0])
if len(ti.status) > 0 {
out.Name(`probe_http_status_code`)
out.Value(ti.status[0])
}

if protoVersion := strings.TrimPrefix(strings.ToLower(ti.proto), "http/"); len(protoVersion) > 0 {
out.Name(`probe_http_version`)
Expand Down Expand Up @@ -640,42 +642,44 @@ func (o *bufferedMetricTextOutput) Stats(v []float64) {
o.buf.WriteRune('"')
}

stats := getStats(v)

fmt.Fprint(o.dest, o.name)
fmt.Fprint(o.dest, "_min")
fmt.Fprint(o.dest, "{")
fmt.Fprint(o.dest, o.buf.String())
fmt.Fprint(o.dest, "} ")
fmt.Fprintln(o.dest, stats.min)

fmt.Fprint(o.dest, o.name)
fmt.Fprint(o.dest, "_max")
fmt.Fprint(o.dest, "{")
fmt.Fprint(o.dest, o.buf.String())
fmt.Fprint(o.dest, "} ")
fmt.Fprintln(o.dest, stats.max)

fmt.Fprint(o.dest, o.name)
// fmt.Fprint(o.dest, "_mean")
fmt.Fprint(o.dest, "{")
fmt.Fprint(o.dest, o.buf.String())
fmt.Fprint(o.dest, "} ")
fmt.Fprintln(o.dest, stats.med)

fmt.Fprint(o.dest, o.name)
fmt.Fprint(o.dest, "_count")
fmt.Fprint(o.dest, "{")
fmt.Fprint(o.dest, o.buf.String())
fmt.Fprint(o.dest, "} ")
fmt.Fprintln(o.dest, stats.n)

fmt.Fprint(o.dest, o.name)
fmt.Fprint(o.dest, "_sum")
fmt.Fprint(o.dest, "{")
fmt.Fprint(o.dest, o.buf.String())
fmt.Fprint(o.dest, "} ")
fmt.Fprintln(o.dest, stats.sum)
if len(v) > 0 {
stats := getStats(v)

fmt.Fprint(o.dest, o.name)
fmt.Fprint(o.dest, "_min")
fmt.Fprint(o.dest, "{")
fmt.Fprint(o.dest, o.buf.String())
fmt.Fprint(o.dest, "} ")
fmt.Fprintln(o.dest, stats.min)

fmt.Fprint(o.dest, o.name)
fmt.Fprint(o.dest, "_max")
fmt.Fprint(o.dest, "{")
fmt.Fprint(o.dest, o.buf.String())
fmt.Fprint(o.dest, "} ")
fmt.Fprintln(o.dest, stats.max)

fmt.Fprint(o.dest, o.name)
// fmt.Fprint(o.dest, "_mean")
fmt.Fprint(o.dest, "{")
fmt.Fprint(o.dest, o.buf.String())
fmt.Fprint(o.dest, "} ")
fmt.Fprintln(o.dest, stats.med)

fmt.Fprint(o.dest, o.name)
fmt.Fprint(o.dest, "_count")
fmt.Fprint(o.dest, "{")
fmt.Fprint(o.dest, o.buf.String())
fmt.Fprint(o.dest, "} ")
fmt.Fprintln(o.dest, stats.n)

fmt.Fprint(o.dest, o.name)
fmt.Fprint(o.dest, "_sum")
fmt.Fprint(o.dest, "{")
fmt.Fprint(o.dest, o.buf.String())
fmt.Fprint(o.dest, "} ")
fmt.Fprintln(o.dest, stats.sum)
}
}

type stats struct {
Expand Down
38 changes: 38 additions & 0 deletions output_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -476,6 +476,15 @@ func TestBufferedMetricTextOutputStats(t *testing.T) {
`b_sum{key_4="4",key5="5",key_1="1",key_2="2"} 12`,
),
},
"grpc only tests give us no stats": {
data: []metricData{
{
name: "test",
values: []float64{},
},
},
expected: "",
},
}

for name, tc := range testcases {
Expand Down Expand Up @@ -613,3 +622,32 @@ func TestTargetMetricsCollectionWriteMany(t *testing.T) {

require.Equal(t, expected, buf.String())
}

func TestTargetMetricsCollectionWriteOneGrpcOnly(t *testing.T) {
c := newTargetMetricsCollection()

require.Len(t, c, 0)

c[targetId{
url: "http://example.com",
method: "GET",
scenario: "s",
group: "g",
}] = targetMetrics{
status: []string{}, // status is empty for grpc only tests.
}

var buf bytes.Buffer

c.Write(&buf)

expected := joinNewline(
`probe_http_got_expected_response{url="http://example.com",method="GET",scenario="s",group="g"} 1`,
`probe_http_error_code{url="http://example.com",method="GET",scenario="s",group="g"} 0`,
`probe_http_info{url="http://example.com",method="GET",scenario="s",group="g"} 1`,
`probe_http_requests_total{url="http://example.com",method="GET",scenario="s",group="g"} 0`,
`probe_http_requests_failed_total{url="http://example.com",method="GET",scenario="s",group="g"} 0`,
`probe_http_ssl{url="http://example.com",method="GET",scenario="s",group="g"} 0`)

require.Equal(t, expected, buf.String())
}