-
Notifications
You must be signed in to change notification settings - Fork 0
/
stats.go
133 lines (123 loc) · 3.45 KB
/
stats.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
package psqlfront
import (
"context"
"log"
"os"
"runtime"
"sync"
"sync/atomic"
"time"
sq "github.com/Masterminds/squirrel"
"github.com/Songmu/flextime"
ltsv "github.com/Songmu/go-ltsv"
"github.com/jackc/pgx/v4"
)
type ServerStats struct {
Hostname string `ltsv:"hostname"`
Pid int `ltsv:"pid"`
Uptime int64 `ltsv:"uptime"`
Time int64 `ltsv:"time"`
Version string `ltsv:"version"`
CurrConnections int64 `ltsv:"curr_connections"`
TotalConnections int64 `ltsv:"total_connections"`
Queries int64 `ltsv:"queries"`
CacheHits int64 `ltsv:"cache_hits"`
CacheMisses int64 `ltsv:"cache_misses"`
MemoryAlloc uint64 `ltsv:"memory_alloc"`
}
func (stats *ServerStats) String() string {
bs, err := ltsv.Marshal(stats)
if err != nil {
return ""
}
return string(bs)
}
func (stats *ServerStats) InsertInto(ctx context.Context, tx pgx.Tx) error {
sql, args, err := psqlQueryBuilder.Insert(statsTable.String()).Columns(
"hostname", "pid", "uptime", "time", "version",
"curr_connections", "total_connections",
"queries", "cache_hits", "cache_misses", "memory_alloc",
).Values(
stats.Hostname, stats.Pid, stats.Uptime, time.Unix(stats.Time, 0), stats.Version,
stats.CurrConnections, stats.TotalConnections,
stats.Queries, stats.CacheHits, stats.CacheMisses, stats.MemoryAlloc,
).ToSql()
if err != nil {
return err
}
_, err = tx.Exec(ctx, sql, args...)
return err
}
func (stats *ServerStats) Loatate(ctx context.Context, tx pgx.Tx) error {
sql, args, err := psqlQueryBuilder.Delete(statsTable.String()).Where(
sq.Lt{
"time": sq.Expr("NOW() - interval '30 day'"),
},
).ToSql()
if err != nil {
return err
}
_, err = tx.Exec(ctx, sql, args...)
return err
}
// GetStats returns ServerStats of app
func (server *Server) GetStats() *ServerStats {
now := flextime.Now()
var hostname string
hostname, _ = os.Hostname()
var mem runtime.MemStats
runtime.ReadMemStats(&mem)
return &ServerStats{
Hostname: hostname,
Pid: os.Getpid(),
Uptime: int64(now.Sub(server.startedAt).Seconds()),
Time: time.Now().Unix(),
Version: Version,
CurrConnections: atomic.LoadInt64(&server.currConnections),
TotalConnections: atomic.LoadInt64(&server.totalConnections),
Queries: atomic.LoadInt64(&server.queries),
CacheHits: atomic.LoadInt64(&server.cacheHits),
CacheMisses: atomic.LoadInt64(&server.cacheMisses),
MemoryAlloc: mem.Alloc,
}
}
func (server *Server) monitoring(ctx context.Context, wg *sync.WaitGroup) {
ticker := time.NewTicker(server.statsCfg.MonitoringInterval)
defer func() {
ticker.Stop()
wg.Done()
}()
for {
select {
case <-ticker.C:
case <-ctx.Done():
return
}
stats := server.GetStats()
log.Printf("[notice] %s", stats)
if server.statsCfg.StoreDatabase {
tx, err := server.db.Begin(ctx)
if err != nil {
log.Printf("[warn] can not store stats: %v", err)
continue
}
if err := stats.InsertInto(ctx, tx); err != nil {
tx.Rollback(ctx)
log.Printf("[warn] can not store stats: %v", err)
continue
}
if stats.Uptime > 86400*30 {
if err := stats.Loatate(ctx, tx); err != nil {
tx.Rollback(ctx)
log.Printf("[warn] can not stored stats loatate: %v", err)
continue
}
}
if err := tx.Commit(ctx); err != nil {
tx.Rollback(ctx)
log.Printf("[warn] can not store stats: %v", err)
continue
}
}
}
}