Skip to content
This repository has been archived by the owner on Jul 31, 2023. It is now read-only.

Commit

Permalink
Have one way to record measurements (#59)
Browse files Browse the repository at this point in the history
The commonly required recording API is batch recording, but we provide two ways of recording measurements. Remove the (Measure).Record and encourage the use of Record every where to reduce the API surface and have better orthogonality.
  • Loading branch information
JBD committed Oct 20, 2017
1 parent b5b4544 commit 9fd9ef8
Show file tree
Hide file tree
Showing 8 changed files with 11 additions and 43 deletions.
2 changes: 0 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -245,8 +245,6 @@ and their registered views. Measurements are implicitly tagged with the
tags in the context:

```go
mi.Record(ctx, 1)
mf.Record(ctx, 5.6)
stats.Record(ctx, mi.M(4), mf.M(10.5))
```

Expand Down
6 changes: 1 addition & 5 deletions examples/stats/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,11 +115,7 @@ func main() {
)
ctx := tags.NewContext(context.Background(), tm)

// Recording single datapoint at a time.
videoSize.Record(ctx, 10.0)
videoSpamCount.Record(ctx, 1)

// Recording multiple datapoints at once.
// Recording datapoints.
stats.Record(ctx, videoSpamCount.M(2), videoSize.M(100.0))

// Wait for a duration longer than reporting duration to ensure the stats
Expand Down
6 changes: 3 additions & 3 deletions plugins/grpc/stats/client_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ func (ch clientHandler) TagRPC(ctx context.Context, info *stats.RPCTagInfo) cont
// TODO(acetechnologist): should we be recording this later? What is the
// point of updating d.reqLen & d.reqCount if we update now?
ctx = tags.NewContext(ctx, tagMap)
RPCClientStartedCount.Record(ctx, 1)
istats.Record(ctx, RPCClientStartedCount.M(1))

return context.WithValue(ctx, grpcClientRPCKey, d)
}
Expand Down Expand Up @@ -132,7 +132,7 @@ func (ch clientHandler) handleRPCOutPayload(ctx context.Context, s *stats.OutPay
return
}

RPCClientRequestBytes.Record(ctx, int64(s.Length))
istats.Record(ctx, RPCClientRequestBytes.M(int64(s.Length)))
atomic.AddUint64(&d.reqCount, 1)
}

Expand All @@ -145,7 +145,7 @@ func (ch clientHandler) handleRPCInPayload(ctx context.Context, s *stats.InPaylo
return
}

RPCClientResponseBytes.Record(ctx, int64(s.Length))
istats.Record(ctx, RPCClientResponseBytes.M(int64(s.Length)))
atomic.AddUint64(&d.respCount, 1)
}

Expand Down
6 changes: 3 additions & 3 deletions plugins/grpc/stats/server_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ func (sh serverHandler) TagRPC(ctx context.Context, info *stats.RPCTagInfo) cont
}
ctx = tags.NewContext(ctx, ts)

RPCServerStartedCount.Record(ctx, 1)
istats.Record(ctx, RPCServerStartedCount.M(1))
return context.WithValue(ctx, grpcServerRPCKey, d)
}

Expand Down Expand Up @@ -137,7 +137,7 @@ func (sh serverHandler) handleRPCInPayload(ctx context.Context, s *stats.InPaylo
return
}

RPCServerRequestBytes.Record(ctx, int64(s.Length))
istats.Record(ctx, RPCServerRequestBytes.M(int64(s.Length)))
atomic.AddUint64(&d.reqCount, 1)
}

Expand All @@ -150,7 +150,7 @@ func (sh serverHandler) handleRPCOutPayload(ctx context.Context, s *stats.OutPay
return
}

RPCServerResponseBytes.Record(ctx, int64(s.Length))
istats.Record(ctx, RPCServerResponseBytes.M(int64(s.Length)))
atomic.AddUint64(&d.respCount, 1)
}

Expand Down
2 changes: 1 addition & 1 deletion stats/measure_float64.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ func (m *MeasureFloat64) removeView(v *View) {
func (m *MeasureFloat64) viewsCount() int { return len(m.views) }

// M creates a new float64 measurement.
// Use Record to record multiple measurements.
// Use Record to record measurements.
func (m *MeasureFloat64) M(v float64) Measurement {
return &measurementFloat64{m: m, v: v}
}
Expand Down
2 changes: 1 addition & 1 deletion stats/measure_int64.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ func (m *MeasureInt64) removeView(v *View) {
func (m *MeasureInt64) viewsCount() int { return len(m.views) }

// M creates a new int64 measurement.
// Use Record to record multiple measurements.
// Use Record to record measurements.
func (m *MeasureInt64) M(v int64) Measurement {
return &measurementInt64{m: m, v: v}
}
Expand Down
28 changes: 1 addition & 27 deletions stats/worker.go
Original file line number Diff line number Diff line change
Expand Up @@ -212,35 +212,9 @@ func (v *View) RetrieveData() ([]*Row, error) {
return resp.rows, resp.err
}

// Record records a float64 value against a measure and the tags passed
// as part of the context.
func (m *MeasureFloat64) Record(ctx context.Context, v float64) {
req := &recordFloat64Req{
now: time.Now(),
tm: tags.FromContext(ctx),
mf: m,
v: v,
}
defaultWorker.c <- req
}

// Record records an int64 value against a measure and the tags passed as
// part of the context.
func (m *MeasureInt64) Record(ctx context.Context, v int64) {
req := &recordInt64Req{
now: time.Now(),
tm: tags.FromContext(ctx),
mi: m,
v: v,
}
defaultWorker.c <- req
}

// Record records one or multiple measurements with the same tags at once.
// If there are any tags in the context, measurements will be tagged with them.
func Record(ctx context.Context, ms ...Measurement) {
// TODO(jbd): Reconsider this API. Is there a use case
// where (Measure).Record is not sufficient enough that
// we provide this API?
req := &recordReq{
now: time.Now(),
tm: tags.FromContext(ctx),
Expand Down
2 changes: 1 addition & 1 deletion stats/worker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -666,7 +666,7 @@ func Test_Worker_RecordFloat64(t *testing.T) {
}

for _, value := range tc.records {
m.Record(ctx, value)
Record(ctx, m.M(value))
}

for _, w := range tc.wants {
Expand Down

0 comments on commit 9fd9ef8

Please sign in to comment.