-
Notifications
You must be signed in to change notification settings - Fork 6
/
policy_stats.go
97 lines (81 loc) · 2.03 KB
/
policy_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
package main
import (
"log"
"time"
"github.com/jackc/pgx"
)
const query = `SELECT
current_database() AS dbname,
COALESCE(CA.view_name, CAS.view_name, PS.hypertable) AS entity_name,
job_type,
EXTRACT(EPOCH FROM PS.last_start)::int as last_start_on,
EXTRACT(EPOCH FROM PS.last_successful_finish)::int as last_success_on,
PS.total_failures
FROM timescaledb_information.policy_stats PS
LEFT JOIN timescaledb_information.continuous_aggregates CA
ON PS.hypertable = CA.materialization_hypertable
LEFT JOIN timescaledb_information.continuous_aggregate_stats CAS
ON PS.job_id = CAS.job_id;`
type record struct {
Database string `json:"database"`
Entity string `json:"entity"`
JobType string `json:"job_type"`
LastStart int `json:"last_start"`
LastSuccess int `json:"last_success"`
TotalFail int64 `json:"total_fail"`
}
func recordTags(r *record) map[string]string {
return map[string]string{
"database": r.Database,
"entity": r.Entity,
"job_type": r.JobType,
}
}
const (
lastSuccessMetric = "last_success_on"
lastStartMetric = "last_start_on"
totalFailMetric = "total_fail"
)
func policyMetrics(conn *pgx.Conn, tm Telemeter) error {
rows, err := conn.Query(query)
if err != nil {
return err
}
defer rows.Close()
var rec record
for rows.Next() {
if err := rows.Scan(
&rec.Database, &rec.Entity, &rec.JobType,
&rec.LastStart, &rec.LastSuccess, &rec.TotalFail,
); err != nil {
log.Println(err)
break
}
recordToMetrics(&rec, recordTags(&rec), tm)
}
return nil
}
func recordToMetrics(rec *record, tags map[string]string, tm Telemeter) {
ts := time.Now()
tm.Emit(&Metric{
Name: lastStartMetric,
Type: Gauge,
Timestamp: ts,
Tags: tags,
Value: float64(rec.LastStart),
})
tm.Emit(&Metric{
Name: lastSuccessMetric,
Type: Gauge,
Timestamp: ts,
Tags: tags,
Value: float64(rec.LastSuccess),
})
tm.Emit(&Metric{
Name: totalFailMetric,
Type: Counter,
Timestamp: ts,
Tags: tags,
Value: float64(rec.TotalFail),
})
}