Skip to content

Commit

Permalink
Combine metrics summary into one object (#518)
Browse files Browse the repository at this point in the history
* combine metrics summary into one object

so the whole report goes as one log entry with a JSON object for each target

* linter fixes
  • Loading branch information
roman-kruglov authored Apr 25, 2022
1 parent c351002 commit df46c0e
Showing 1 changed file with 16 additions and 10 deletions.
26 changes: 16 additions & 10 deletions src/utils/metrics/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import (
"sync"

"go.uber.org/zap"
"go.uber.org/zap/zapcore"
)

// Stat is the type of statistical metrics.
Expand Down Expand Up @@ -164,27 +165,32 @@ func (r *Reporter) SumAllStatsByTarget() PerTargetStats {
// WriteSummary dumps Reporter contents into the target.
func (r *Reporter) WriteSummary(logger *zap.Logger) {
stats := r.SumAllStatsByTarget()
statFields := make([]zap.Field, 0, len(stats)+1) // +1 for total stats

var totals Stats

for _, tgt := range stats.sortedTargets() {
tgtStats := stats[tgt]
logger.Info("stats", zap.String("target", tgt),
zap.Uint64("requests_attempted", tgtStats[RequestsAttemptedStat]),
zap.Uint64("requests_sent", tgtStats[RequestsSentStat]),
zap.Uint64("responses_received", tgtStats[ResponsesReceivedStat]),
zap.Uint64("bytes_sent", tgtStats[BytesSentStat]))
statFields = append(statFields, zap.Object(tgt, &tgtStats))

for s := range totals {
totals[s] += tgtStats[s]
}
}

logger.Info("stats", zap.String("target", "total"),
zap.Uint64("requests_attempted", totals[RequestsAttemptedStat]),
zap.Uint64("requests_sent", totals[RequestsSentStat]),
zap.Uint64("responses_received", totals[ResponsesReceivedStat]),
zap.Uint64("bytes_sent", totals[BytesSentStat]))
statFields = append(statFields, zap.Object("total", &totals))

logger.Info("stats", statFields...)
}

// MarshalLogObject is required to log Stats objects to zap above
func (stats *Stats) MarshalLogObject(enc zapcore.ObjectEncoder) error {
enc.AddUint64("requests_attempted", stats[RequestsAttemptedStat])
enc.AddUint64("requests_sent", stats[RequestsSentStat])
enc.AddUint64("responses_received", stats[ResponsesReceivedStat])
enc.AddUint64("bytes_sent", stats[BytesSentStat])

return nil
}

// NewAccumulator returns a new metrics Accumulator for the Reporter.
Expand Down

0 comments on commit df46c0e

Please sign in to comment.