forked from cashapp/blip
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcollector.go
135 lines (113 loc) · 3.92 KB
/
collector.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
// Copyright 2022 Block, Inc.
package blip
import (
"context"
"database/sql"
"fmt"
)
// Collector collects metrics for a single metric domain.
type Collector interface {
// Domain returns the Blip domain name.
Domain() string
// Help returns collector descipiton, options, and other usage printed by
// blip --print-domains. Blip uses this information to validate user-provided
// values in plans.
Help() CollectorHelp
// Prepare prepares a plan for future calls to Collect. The return function
// is called once when the collector is destroyed; it allows the collector
// to clean up. If Prepare returns an error, Blip will retry preparing the
// plan. Therefore, Prepare should not retry on error (for example, if MySQL
// is not online yet).
Prepare(ctx context.Context, plan Plan) (func(), error)
// Collect collects metrics for the previously prepared plan. Collect is only
// called after Prepare returns nil.
Collect(ctx context.Context, levelName string) ([]MetricValue, error)
}
// Help represents information about a collector.
type CollectorHelp struct {
Domain string
Description string
Options map[string]CollectorHelpOption
Errors map[string]CollectorHelpError
Groups []CollectorKeyValue
Meta []CollectorKeyValue
Metrics []CollectorMetric
}
type CollectorHelpOption struct {
Name string
Desc string // describes Name
Default string // key in Values
Values map[string]string // value => description
}
type CollectorHelpError struct {
Name string
Handles string
Default string
}
type CollectorMetric struct {
Name string
Desc string // describes Name
Type byte
}
type CollectorKeyValue struct {
Key string
Value string
}
// Validate returns nil if all the given options are valid, else it an error.
func (h CollectorHelp) Validate(opts map[string]string) error {
// No input? No error.
if len(opts) == 0 {
return nil
}
// At least 1 opt given, so error if the collector has no options
if len(h.Options) == 0 {
return fmt.Errorf("collector has no options but %d given", len(h.Options))
}
// Check each given key and value
for givenKey, givenValue := range opts {
// Error if the given key is not accpeted by collector
o, ok := h.Options[givenKey]
if !ok {
return fmt.Errorf("unknown option: %s (run 'blip --print-domains' to list collectors and options)", givenKey)
}
// If the collector option has a list of allowed values,
// error if the given value isn't one of the allowed values
if len(o.Values) > 0 {
allowed := false
for allowedVal := range o.Values {
if givenValue == allowedVal {
allowed = true
break
}
}
if !allowed {
return fmt.Errorf("invalid value for option %s: %s (run 'blip --print-domains' to list collectors and options)",
givenKey, givenValue)
}
}
}
return nil
}
// CollectorFactoryArgs are provided by Blip to a CollectorFactory when making
// a Collector. The factory must use the args to create the collector.
type CollectorFactoryArgs struct {
// Config is the full and final monitor config. Most collectors do not need
// this, but some that collect metrics outside MySQL, like cloud metrics,
// might need additional monitor config values.
Config ConfigMonitor
// DB is the connection to MySQL. It is safe for concurrent use, and it is
// used concurrently by other parts of a monitor. The Collector must not
// modify the connection, reconnect, and so forth--only use the connection.
DB *sql.DB
// MonitorId is the monitor identifier. The Collector must include
// this value in all errors, output, and so forth. Everything monitor-related
// in Blip is keyed on monitor ID.
MonitorId string
// Validate is true only when the plan loader is validating collectors.
// Do not use this field.
Validate bool
}
// A CollectorFactory makes one or more Collector.
type CollectorFactory interface {
Make(domain string, args CollectorFactoryArgs) (Collector, error)
}