Skip to content

Commit

Permalink
fix: update bytes_sent and bytes_received to use Sum (#874)
Browse files Browse the repository at this point in the history
  • Loading branch information
jackwotherspoon authored Sep 30, 2024
1 parent f8bf685 commit 73b6f38
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 8 deletions.
4 changes: 2 additions & 2 deletions internal/trace/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,14 +108,14 @@ var (
Name: "cloudsqlconn/bytes_sent",
Measure: mBytesSent,
Description: "The number of bytes sent to Cloud SQL",
Aggregation: view.LastValue(),
Aggregation: view.Sum(),
TagKeys: []tag.Key{keyInstance, keyDialerID},
}
bytesReceivedView = &view.View{
Name: "cloudsqlconn/bytes_received",
Measure: mBytesReceived,
Description: "The number of bytes received from Cloud SQL",
Aggregation: view.LastValue(),
Aggregation: view.Sum(),
TagKeys: []tag.Key{keyInstance, keyDialerID},
}

Expand Down
32 changes: 26 additions & 6 deletions metrics_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,24 @@ func wantCountMetric(t *testing.T, wantName string, ms []metric) {
)
}

// wantSumMetric ensures the provided metrics include a metric with the wanted
// name and at least one data point.
func wantSumMetric(t *testing.T, wantName string, ms []metric) {
t.Helper()
gotNames := make(map[string]view.AggregationData)
for _, m := range ms {
gotNames[m.name] = m.data
_, ok := m.data.(*view.SumData)
if m.name == wantName && ok {
return
}
}
t.Fatalf(
"metric name want = %v with SumData, all metrics = %v",
wantName, dump(t, gotNames),
)
}

func TestDialerWithMetrics(t *testing.T) {
spy := &spyMetricsExporter{}
view.RegisterExporter(spy)
Expand Down Expand Up @@ -166,14 +184,16 @@ func TestDialerWithMetrics(t *testing.T) {
if err != nil {
t.Fatalf("buf.WriteByte failed: %v", err)
}
_, err = conn2.Write(buf.Bytes())
if err != nil {
t.Fatalf("conn.Write failed: %v", err)
}
// Doing a read before doing a write, because when this unit test runs on
// Windows, it fails when the write is done before the read.
_, err = conn2.Read(buf.Bytes())
if err != nil {
t.Fatalf("conn.Read failed: %v", err)
}
_, err = conn2.Write(buf.Bytes())
if err != nil {
t.Fatalf("conn.Write failed: %v", err)
}
defer conn2.Close()
// dial a bogus instance
_, err = d.Dial(context.Background(), "my-project:my-region:notaninstance")
Expand All @@ -187,8 +207,8 @@ func TestDialerWithMetrics(t *testing.T) {
wantLastValueMetric(t, "cloudsqlconn/open_connections", spy.data(), 2)
wantDistributionMetric(t, "cloudsqlconn/dial_latency", spy.data())
wantCountMetric(t, "cloudsqlconn/refresh_success_count", spy.data())
wantLastValueMetric(t, "cloudsqlconn/bytes_sent", spy.data(), 1)
wantLastValueMetric(t, "cloudsqlconn/bytes_received", spy.data(), 1)
wantSumMetric(t, "cloudsqlconn/bytes_sent", spy.data())
wantSumMetric(t, "cloudsqlconn/bytes_received", spy.data())

// failure metrics from dialing bogus instance
wantCountMetric(t, "cloudsqlconn/dial_failure_count", spy.data())
Expand Down

0 comments on commit 73b6f38

Please sign in to comment.