forked from cockroachdb/cockroach
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmetrics_tracker.go
110 lines (98 loc) · 3.16 KB
/
metrics_tracker.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
// Copyright 2022 The Cockroach Authors.
//
// Use of this software is governed by the Business Source License
// included in the file licenses/BSL.txt.
//
// As of the Change Date specified in that file, in accordance with
// the Business Source License, use of this software will be governed
// by the Apache License, Version 2.0, included in the file
// licenses/APL.txt.
package asim
import (
"fmt"
"io"
"time"
"github.com/cockroachdb/cockroach/pkg/kv/kvserver/asim/state"
"github.com/cockroachdb/cockroach/pkg/util/encoding/csv"
)
// MetricsTracker gathers metrics and prints those to stdout.
type MetricsTracker struct {
writers []*csv.Writer
}
// NewMetricsTracker returns a MetricsTracker object that prints tick metrics to
// Stdout, in a CSV format.
func NewMetricsTracker(writers ...io.Writer) *MetricsTracker {
m := &MetricsTracker{}
for _, w := range writers {
m.writers = append(m.writers, csv.NewWriter(w))
}
headline := []string{
// The rest of the data is cumulative, up to this tick.
"tick",
// The number of ranges in the cluster and the total load.
"c_ranges", "c_write", "c_write_b", "c_read", "c_read_b",
// The max value seen on a single store.
"s_ranges", "s_write", "s_write_b", "s_read", "s_read_b",
// The churn in the cluster.
"c_lease_moves", "c_replica_moves", "c_replica_b_moves",
}
_ = m.write(headline)
return m
}
func (m *MetricsTracker) write(record []string) error {
for _, w := range m.writers {
if err := w.Write(record); err != nil {
return err
}
w.Flush()
}
return nil
}
func max(a, b int64) int64 {
if a > b {
return a
}
return b
}
// Tick collects cluster information and prints it.
func (m *MetricsTracker) Tick(tick time.Time, state state.State) error {
usage := state.ClusterUsageInfo()
var (
totalWriteKeys int64
totalWriteBytes int64
totalReadKeys int64
totalReadBytes int64
maxWriteKeys int64
maxWriteBytes int64
maxReadKeys int64
maxReadBytes int64
)
for _, u := range usage.StoreUsage {
totalWriteKeys += u.WriteKeys
totalWriteBytes += u.WriteBytes
totalReadKeys += u.ReadKeys
totalReadBytes += u.ReadBytes
maxWriteKeys = max(maxWriteKeys, u.WriteKeys)
maxWriteBytes = max(maxWriteBytes, u.WriteBytes)
maxReadKeys = max(maxReadKeys, u.ReadKeys)
maxReadBytes = max(maxReadBytes, u.ReadBytes)
}
record := make([]string, 0, 10)
record = append(record, tick.String())
record = append(record, fmt.Sprintf("%d", state.RangeCount()))
record = append(record, fmt.Sprintf("%d", totalWriteKeys))
record = append(record, fmt.Sprintf("%d", totalWriteBytes))
record = append(record, fmt.Sprintf("%d", totalReadKeys))
record = append(record, fmt.Sprintf("%d", totalReadBytes))
record = append(record, fmt.Sprintf("%d", maxWriteKeys))
record = append(record, fmt.Sprintf("%d", maxWriteBytes))
record = append(record, fmt.Sprintf("%d", maxReadKeys))
record = append(record, fmt.Sprintf("%d", maxReadBytes))
record = append(record, fmt.Sprintf("%d", usage.LeaseTransfers))
record = append(record, fmt.Sprintf("%d", usage.Rebalances))
record = append(record, fmt.Sprintf("%d", usage.BytesRebalanced))
if err := m.write(record); err != nil {
return err
}
return nil
}