Skip to content

Commit

Permalink
stats/opencensus: remove leading slash for per call metrics (#6141)
Browse files Browse the repository at this point in the history
  • Loading branch information
zasweq committed Mar 20, 2023
1 parent 78099db commit 4efec30
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 6 deletions.
73 changes: 68 additions & 5 deletions stats/opencensus/e2e_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ func (vi *viewInformation) Equal(vi2 *viewInformation) bool {
// less. This must be called with non nil view information that is aggregated
// with distribution data. Returns a nil error if correct count information
// found, non nil error if correct information not found.
func distributionDataLatencyCount(vi *viewInformation, countWant int64) error {
func distributionDataLatencyCount(vi *viewInformation, countWant int64, wantTags [][]tag.Tag) error {
var totalCount int64
var largestIndexWithFive int
for i, bucket := range vi.aggBuckets {
Expand All @@ -204,9 +204,21 @@ func distributionDataLatencyCount(vi *viewInformation, countWant int64) error {
break
}
}
// Sort rows by string name. This is to take away non determinism in the row
// ordering passed to the Exporter, while keeping the row data.
sort.Slice(vi.rows, func(i, j int) bool {
return vi.rows[i].String() > vi.rows[j].String()
})
// Iterating through rows sums up data points for all methods. In this case,
// a data point for the unary and for the streaming RPC.
for _, row := range vi.rows {
for i, row := range vi.rows {
// The method names corresponding to unary and streaming call should
// have the leading slash removed.
if diff := cmp.Diff(row.Tags, wantTags[i], cmp.Comparer(func(a tag.Key, b tag.Key) bool {
return a.Name() == b.Name()
})); diff != "" {
return fmt.Errorf("wrong tag keys for unary method -got, +want: %v", diff)
}
// This could potentially have an extra measurement in buckets above 5s,
// but that's fine. Count of buckets that could contain up to 5s is a
// good enough assertion.
Expand Down Expand Up @@ -305,8 +317,9 @@ func (s) TestAllMetricsOneFunction(t *testing.T) {
cstk := tag.MustNewKey("grpc_client_status")
sstk := tag.MustNewKey("grpc_server_status")
wantMetrics := []struct {
metric *view.View
wantVI *viewInformation
metric *view.View
wantVI *viewInformation
wantTags [][]tag.Tag // for non determinstic (i.e. latency) metrics. First dimension represents rows.
}{
{
metric: ClientStartedRPCsView,
Expand Down Expand Up @@ -913,13 +926,63 @@ func (s) TestAllMetricsOneFunction(t *testing.T) {
},
{
metric: ClientRoundtripLatencyView,
wantTags: [][]tag.Tag{
{
{
Key: cmtk,
Value: "grpc.testing.TestService/UnaryCall",
},
},
{
{
Key: cmtk,
Value: "grpc.testing.TestService/FullDuplexCall",
},
},
},
},
{
metric: ServerLatencyView,
wantTags: [][]tag.Tag{
{
{
Key: smtk,
Value: "grpc.testing.TestService/UnaryCall",
},
},
{
{
Key: smtk,
Value: "grpc.testing.TestService/FullDuplexCall",
},
},
},
},
// Per call metrics:
{
metric: ClientAPILatencyView,
wantTags: [][]tag.Tag{
{
{
Key: cmtk,
Value: "grpc.testing.TestService/UnaryCall",
},
{
Key: cstk,
Value: "OK",
},
},
{
{
Key: cmtk,
Value: "grpc.testing.TestService/FullDuplexCall",
},
{
Key: cstk,
Value: "OK",
},
},
},
},
}
// Unregister all the views. Unregistering a view causes a synchronous
Expand All @@ -945,7 +1008,7 @@ func (s) TestAllMetricsOneFunction(t *testing.T) {
// RPCs have a context timeout of 5s, so all the recorded
// measurements (one per RPC - two total) should fall within 5
// second buckets.
if err := distributionDataLatencyCount(vi, 2); err != nil {
if err := distributionDataLatencyCount(vi, 2, wantMetric.wantTags); err != nil {
t.Fatalf("Invalid OpenCensus export view data for metric %v: %v", metricName, err)
}
continue
Expand Down
2 changes: 1 addition & 1 deletion stats/opencensus/opencensus.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ func perCallTracesAndMetrics(err error, span *trace.Span, startTime time.Time, m
callLatency := float64(time.Since(startTime)) / float64(time.Millisecond)
ocstats.RecordWithOptions(context.Background(),
ocstats.WithTags(
tag.Upsert(keyClientMethod, method),
tag.Upsert(keyClientMethod, removeLeadingSlash(method)),
tag.Upsert(keyClientStatus, canonicalString(s.Code())),
),
ocstats.WithMeasurements(
Expand Down

0 comments on commit 4efec30

Please sign in to comment.