forked from etcd-io/dbtester
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver_system_metrics.go
103 lines (91 loc) · 2.7 KB
/
server_system_metrics.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
// Copyright 2017 CoreOS, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package agent
import (
"fmt"
"os"
"time"
"github.com/gyuho/linux-inspect/inspect"
"github.com/gyuho/linux-inspect/top"
"go.uber.org/zap"
)
// startMetrics starts collecting metrics.
func startMetrics(fs *flags, t *transporterServer) (err error) {
if fs == nil || t == nil || t.cmd == nil {
return fmt.Errorf("cannot find process to track (%+v, %+v)", fs, t)
}
t.lg.Info(
"starting collecting system metrics",
zap.String("database", t.req.DatabaseID.String()),
zap.String("disk-device", fs.diskDevice),
zap.String("network-device", fs.networkInterface),
zap.Int64("pid", t.pid),
)
if err = os.RemoveAll(fs.systemMetricsCSV); err != nil {
return err
}
if err = toFile(fmt.Sprintf("%d", t.req.CurrentClientNumber), t.clientNumPath); err != nil {
return err
}
tcfg := &top.Config{
Exec: top.DefaultExecPath,
IntervalSecond: 1,
PID: t.pid,
}
t.metricsCSV, err = inspect.NewCSV(
fs.systemMetricsCSV,
t.pid,
fs.diskDevice,
fs.networkInterface,
t.clientNumPath,
tcfg,
)
if err := t.metricsCSV.Add(); err != nil {
return err
}
go func() {
for {
select {
case <-time.After(time.Second):
if err := t.metricsCSV.Add(); err != nil {
t.lg.Warn("inspect.CSV.Add error", zap.Error(err))
continue
}
case <-t.uploadSig:
t.lg.Info("upload requested, saving CSV", zap.String("path", t.metricsCSV.FilePath))
if err := t.metricsCSV.Save(); err != nil {
t.lg.Warn("failed to save CSV", zap.Error(err))
} else {
t.lg.Info("saved CSV", zap.String("path", t.metricsCSV.FilePath))
}
interpolated, err := t.metricsCSV.Interpolate()
if err != nil {
t.lg.Fatal("failed to inspect.CSV.Interpolate", zap.Error(err))
}
interpolated.FilePath = fs.systemMetricsCSVInterpolated
if err := interpolated.Save(); err != nil {
t.lg.Warn("failed to save CSV", zap.Error(err))
} else {
t.lg.Info("saved CSV", zap.String("path", interpolated.FilePath))
}
close(t.csvReady)
return
case sig := <-t.notifier:
t.lg.Info("received a signal", zap.String("signal", sig.String()))
return
}
}
}()
return nil
}