-
Notifications
You must be signed in to change notification settings - Fork 538
Expand file tree
/
Copy pathsetup_actors.go
More file actions
174 lines (143 loc) · 4.45 KB
/
setup_actors.go
File metadata and controls
174 lines (143 loc) · 4.45 KB
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
package collector
import (
"context"
"database/sql"
"errors"
"fmt"
"strings"
"time"
"github.com/go-kit/log"
"go.uber.org/atomic"
"github.com/grafana/alloy/internal/runtime/logging/level"
)
const (
SetupActorsCollector = "setup_actors"
selectUserQuery = `SELECT substring_index(current_user(), '@', 1)`
selectQuery = `SELECT enabled, history
FROM performance_schema.setup_actors
WHERE user = ?`
updateQuery = `UPDATE performance_schema.setup_actors
SET enabled='NO', history='NO'
WHERE user = ?`
insertQuery = `INSERT INTO performance_schema.setup_actors
(host, user, role, enabled, history)
VALUES ('%', ?, '%', 'NO', 'NO')`
)
type SetupActorsArguments struct {
DB *sql.DB
Logger log.Logger
CollectInterval time.Duration
AutoUpdateSetupActors bool
}
type SetupActors struct {
dbConnection *sql.DB
collectInterval time.Duration
autoUpdateSetupActors bool
logger log.Logger
running *atomic.Bool
ctx context.Context
cancel context.CancelFunc
}
func NewSetupActors(args SetupActorsArguments) (*SetupActors, error) {
return &SetupActors{
dbConnection: args.DB,
running: &atomic.Bool{},
logger: log.With(args.Logger, "collector", SetupActorsCollector),
collectInterval: args.CollectInterval,
autoUpdateSetupActors: args.AutoUpdateSetupActors,
}, nil
}
func (c *SetupActors) Name() string {
return SetupActorsCollector
}
func (c *SetupActors) Start(ctx context.Context) error {
level.Debug(c.logger).Log("msg", "collector started")
c.running.Store(true)
ctx, cancel := context.WithCancel(ctx)
c.ctx = ctx
c.cancel = cancel
var user string
if err := c.dbConnection.QueryRowContext(ctx, selectUserQuery).Scan(&user); err != nil {
level.Error(c.logger).Log("msg", "failed to get current user", "err", err)
c.running.Store(false)
cancel()
return err
}
go func() {
defer func() {
c.Stop()
c.running.Store(false)
}()
ticker := time.NewTicker(c.collectInterval)
for {
if err := c.checkSetupActors(c.ctx, user); err != nil {
level.Error(c.logger).Log("msg", "collector error", "err", err)
}
select {
case <-c.ctx.Done():
return
case <-ticker.C:
// continue loop
}
}
}()
return nil
}
func (c *SetupActors) Stopped() bool {
return !c.running.Load()
}
func (c *SetupActors) Stop() {
c.cancel()
c.running.Store(false)
}
func (c *SetupActors) checkSetupActors(ctx context.Context, user string) error {
var enabled, history string
err := c.dbConnection.QueryRowContext(ctx, selectQuery, user).Scan(&enabled, &history)
if errors.Is(err, sql.ErrNoRows) {
if c.autoUpdateSetupActors {
return c.insertSetupActors(ctx, user)
} else {
level.Info(c.logger).Log("msg", "setup_actors configuration missing, but auto-update is disabled")
return nil
}
} else if err != nil {
level.Error(c.logger).Log("msg", "failed to query setup_actors table", "err", err)
return err
}
if strings.ToUpper(enabled) != "NO" || strings.ToUpper(history) != "NO" {
if c.autoUpdateSetupActors {
return c.updateSetupActors(ctx, user, enabled, history)
} else {
level.Info(c.logger).Log("msg", "setup_actors configuration is not correct, but auto-update is disabled")
return nil
}
}
return nil
}
func (c *SetupActors) insertSetupActors(ctx context.Context, user string) error {
_, err := c.dbConnection.ExecContext(ctx, insertQuery, user)
if err != nil {
level.Error(c.logger).Log("msg", "failed to insert setup_actors row", "err", err, "user", user)
return err
}
level.Debug(c.logger).Log("msg", "inserted new setup_actors row", "user", user)
return nil
}
func (c *SetupActors) updateSetupActors(ctx context.Context, user string, enabled string, history string) error {
r, err := c.dbConnection.ExecContext(ctx, updateQuery, user)
if err != nil {
level.Error(c.logger).Log("msg", "failed to update setup_actors row", "err", err, "user", user)
return err
}
rowsAffected, err := r.RowsAffected()
if err != nil {
level.Error(c.logger).Log("msg", "failed to get rows affected from setup_actors update", "err", err)
return err
}
if rowsAffected == 0 {
level.Error(c.logger).Log("msg", "no rows affected from setup_actors update", "user", user)
return fmt.Errorf("no rows affected from setup_actors update")
}
level.Debug(c.logger).Log("msg", "updated setup_actors row", "rows_affected", rowsAffected, "previous_enabled", enabled, "previous_history", history, "user", user)
return nil
}