/
metrics.go
218 lines (185 loc) · 6.53 KB
/
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
// Copyright (c) 2016-2018, Jan Cajthaml <jan.cajthaml@gmail.com>
//
// 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 daemon
import (
"context"
"os"
"path/filepath"
"time"
"github.com/jancajthaml-openbank/fio-bco-unit/config"
"github.com/jancajthaml-openbank/fio-bco-unit/utils"
metrics "github.com/rcrowley/go-metrics"
log "github.com/sirupsen/logrus"
)
// Metrics represents metrics subroutine
type Metrics struct {
Support
output string
tenant string
createdTokens metrics.Counter
deletedTokens metrics.Counter
refreshRate time.Duration
syncLatency metrics.Timer
importAccountLatency metrics.Timer
exportAccountLatency metrics.Timer
importTransactionLatency metrics.Timer
exportTransactionLatency metrics.Timer
importedAccounts metrics.Meter
exportedAccounts metrics.Meter
importedTransfers metrics.Meter
exportedTransfers metrics.Meter
}
// Snapshot holds metrics snapshot status
type Snapshot struct {
CreatedTokens int64 `json:"createdTokens"`
DeletedTokens int64 `json:"deletedTokens"`
SyncLatency float64 `json:"syncLatency"`
ImportAccountLatency float64 `json:"importAccountLatency"`
ExportAccountLatency float64 `json:"exportAccountLatency"`
ImportTransactionLatency float64 `json:"importTransactionLatency"`
ExportTransactionLatency float64 `json:"exportTransactionLatency"`
ImportedAccounts int64 `json:"importedAccounts"`
ExportedAccounts int64 `json:"exportedAccounts"`
ImportedTransfers int64 `json:"importedTransfers"`
ExportedTransfers int64 `json:"exportedTransfers"`
}
// NewMetrics returns metrics fascade
func NewMetrics(ctx context.Context, cfg config.Configuration) Metrics {
return Metrics{
Support: NewDaemonSupport(ctx),
output: cfg.MetricsOutput,
tenant: cfg.Tenant,
refreshRate: cfg.MetricsRefreshRate,
createdTokens: metrics.NewCounter(),
deletedTokens: metrics.NewCounter(),
syncLatency: metrics.NewTimer(),
importAccountLatency: metrics.NewTimer(),
exportAccountLatency: metrics.NewTimer(),
importTransactionLatency: metrics.NewTimer(),
exportTransactionLatency: metrics.NewTimer(),
importedAccounts: metrics.NewMeter(),
exportedAccounts: metrics.NewMeter(),
importedTransfers: metrics.NewMeter(),
exportedTransfers: metrics.NewMeter(),
}
}
// NewSnapshot returns metrics snapshot
func NewSnapshot(metrics Metrics) Snapshot {
return Snapshot{
CreatedTokens: metrics.createdTokens.Count(),
DeletedTokens: metrics.deletedTokens.Count(),
SyncLatency: metrics.syncLatency.Percentile(0.95),
ImportAccountLatency: metrics.importAccountLatency.Percentile(0.95),
ExportAccountLatency: metrics.exportAccountLatency.Percentile(0.95),
ImportTransactionLatency: metrics.importTransactionLatency.Percentile(0.95),
ExportTransactionLatency: metrics.exportTransactionLatency.Percentile(0.95),
ImportedAccounts: metrics.importedAccounts.Count(),
ExportedAccounts: metrics.exportedAccounts.Count(),
ImportedTransfers: metrics.importedTransfers.Count(),
ExportedTransfers: metrics.exportedTransfers.Count(),
}
}
// TokenCreated increments token created by one
func (metrics Metrics) TokenCreated() {
metrics.createdTokens.Inc(1)
}
// TokenDeleted increments token deleted by one
func (metrics Metrics) TokenDeleted() {
metrics.deletedTokens.Inc(1)
}
func (metrics Metrics) TimeSyncLatency(f func()) {
metrics.syncLatency.Time(f)
}
func (metrics Metrics) TimeImportAccount(f func()) {
metrics.importAccountLatency.Time(f)
}
func (metrics Metrics) TimeExportAccount(f func()) {
metrics.exportAccountLatency.Time(f)
}
func (metrics Metrics) TimeImportTransaction(f func()) {
metrics.importTransactionLatency.Time(f)
}
func (metrics Metrics) TimeExportTransaction(f func()) {
metrics.exportTransactionLatency.Time(f)
}
func (metrics Metrics) ImportedAccounts(num int64) {
metrics.importedAccounts.Mark(num)
}
func (metrics Metrics) ExportedAccounts(num int64) {
metrics.exportedAccounts.Mark(num)
}
func (metrics Metrics) ImportedTransfers(num int64) {
metrics.importedTransfers.Mark(num)
}
func (metrics Metrics) ExportedTransfers(num int64) {
metrics.exportedTransfers.Mark(num)
}
func (metrics Metrics) persist(filename string) {
tempFile := filename + "_temp"
data, err := utils.JSON.Marshal(NewSnapshot(metrics))
if err != nil {
log.Warnf("unable to create serialize metrics with error: %v", err)
return
}
f, err := os.OpenFile(tempFile, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, os.ModePerm)
if err != nil {
log.Warnf("unable to create file with error: %v", err)
return
}
defer f.Close()
if _, err := f.Write(data); err != nil {
log.Warnf("unable to write file with error: %v", err)
return
}
if err := os.Rename(tempFile, filename); err != nil {
log.Warnf("unable to move file with error: %v", err)
return
}
return
}
func getFilename(path, tenant string) string {
if tenant == "" {
return path
}
dirname := filepath.Dir(path)
ext := filepath.Ext(path)
filename := filepath.Base(path)
filename = filename[:len(filename)-len(ext)]
return dirname + "/" + filename + "." + tenant + ext
}
// Start handles everything needed to start metrics daemon
func (metrics Metrics) Start() {
defer metrics.MarkDone()
if metrics.output == "" {
log.Warnf("no metrics output defined, skipping metrics persistence")
metrics.MarkReady()
return
}
output := getFilename(metrics.output, metrics.tenant)
ticker := time.NewTicker(metrics.refreshRate)
defer ticker.Stop()
log.Infof("Start metrics daemon, update each %v into %v", metrics.refreshRate, output)
metrics.MarkReady()
for {
select {
case <-metrics.Done():
log.Info("Stopping metrics daemon")
metrics.persist(output)
log.Info("Stop metrics daemon")
return
case <-ticker.C:
metrics.persist(output)
}
}
}