Skip to content

Commit 4e0dd99

Browse files
committed
feat(observability/metrics): add RecordRequest
1 parent 76e7a51 commit 4e0dd99

4 files changed

Lines changed: 84 additions & 1 deletion

File tree

observability/metrics/BUILD.bazel

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,20 @@ go_library(
77
"emitter.go",
88
"names.go",
99
"options.go",
10+
"recordrequest.go",
1011
],
1112
importpath = "github.com/uber/tango/observability/metrics",
1213
visibility = ["//visibility:public"],
13-
deps = ["@com_github_uber_go_tally//:tally"],
14+
deps = [
15+
"@com_github_uber_go_tally//:tally",
16+
],
1417
)
1518

1619
go_test(
1720
name = "metrics_test",
1821
srcs = [
1922
"emitter_test.go",
23+
"recordrequest_test.go",
2024
],
2125
embed = [":metrics"],
2226
deps = [

observability/metrics/names.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ const (
2222

2323
// Metric names.
2424
const (
25+
Requests = "requests"
26+
TotalDuration = "total_duration"
2527
InFlightRequests = "in_flight_requests"
2628
)
2729

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
// Copyright (c) 2026 Uber Technologies, Inc.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package metrics
16+
17+
import "time"
18+
19+
// RecordRequest records the request's TotalDuration and emits a requests
20+
// counter tagged with the result: result=success when err is nil, otherwise
21+
// result=fail.
22+
func RecordRequest(e Emitter, op string, dur time.Duration, err error) {
23+
e.RecordDur(op, TotalDuration, dur)
24+
25+
result := ResultSuccess
26+
if err != nil {
27+
result = ResultFail
28+
}
29+
// TODO: when err != nil, derive failure_type and failure_source tags from
30+
// err via an error classifier (added in a later change) and attach them here.
31+
e.Inc(op, Requests, WithTags(map[string]string{
32+
TagResult: string(result),
33+
}))
34+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
// Copyright (c) 2026 Uber Technologies, Inc.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package metrics
16+
17+
import (
18+
"errors"
19+
"testing"
20+
"time"
21+
22+
"github.com/stretchr/testify/assert"
23+
"github.com/uber-go/tally"
24+
)
25+
26+
func TestRecordRequestSuccess(t *testing.T) {
27+
s := tally.NewTestScope("", nil)
28+
RecordRequest(New(s), OpGetTargetGraph, 5*time.Millisecond, nil)
29+
30+
assert.Equal(t, int64(1), counterValue(t, s, OpGetTargetGraph+"."+Requests, map[string]string{
31+
TagResult: string(ResultSuccess),
32+
}))
33+
assert.Equal(t, 1, histogramDurationSamples(t, s, OpGetTargetGraph+"."+TotalDuration, map[string]string{}))
34+
}
35+
36+
func TestRecordRequestFailure(t *testing.T) {
37+
s := tally.NewTestScope("", nil)
38+
RecordRequest(New(s), OpGetTargetGraph, time.Millisecond, errors.New("boom"))
39+
40+
assert.Equal(t, int64(1), counterValue(t, s, OpGetTargetGraph+"."+Requests, map[string]string{
41+
TagResult: string(ResultFail),
42+
}))
43+
}

0 commit comments

Comments
 (0)