-
Notifications
You must be signed in to change notification settings - Fork 1
/
log.go
147 lines (115 loc) · 2.76 KB
/
log.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
package main
import (
"context"
"errors"
"fmt"
"net/http"
"runtime"
"strings"
"sync"
"time"
"github.com/hansmi/paperhooks/pkg/client"
"github.com/prometheus/client_golang/prometheus"
"golang.org/x/sync/errgroup"
)
type logClient interface {
ListLogs(context.Context) ([]string, *client.Response, error)
GetLog(context.Context, string) ([]client.LogEntry, *client.Response, error)
}
type logPosition struct {
valid bool
time time.Time
module string
level string
}
func newLogPosition(e client.LogEntry) logPosition {
return logPosition{
valid: true,
time: e.Time,
module: e.Module,
level: e.Level,
}
}
func (p logPosition) equal(e client.LogEntry) bool {
return p.valid && e.Time.Equal(p.time) && e.Module == p.module && e.Level == p.level
}
type logCollector struct {
cl logClient
mu sync.Mutex
seen map[string]logPosition
totalVec *prometheus.CounterVec
}
func newLogCollector(cl logClient) *logCollector {
return &logCollector{
cl: cl,
seen: map[string]logPosition{},
totalVec: prometheus.NewCounterVec(prometheus.CounterOpts{
Name: "paperless_log_entries_total",
Help: `Best-effort count of log entries.`,
}, []string{"name", "module", "level"}),
}
}
func (c *logCollector) describe(ch chan<- *prometheus.Desc) {
c.totalVec.Describe(ch)
}
func (c *logCollector) collectOne(ctx context.Context, name string) error {
entries, _, err := c.cl.GetLog(ctx, name)
if err != nil {
var reqErr *client.RequestError
if errors.As(err, &reqErr) && reqErr.StatusCode == http.StatusNotFound {
return nil
}
return err
}
if len(entries) == 0 {
return nil
}
entryLabels := func(e client.LogEntry) prometheus.Labels {
return prometheus.Labels{
"name": name,
"module": e.Module,
"level": strings.ToLower(e.Level),
}
}
c.mu.Lock()
defer c.mu.Unlock()
start := 0
if seen := c.seen[name]; seen.valid {
// Find the first log entry which hasn't been seen previously.
for idx, entry := range entries {
if seen.equal(entry) {
start = idx + 1
break
}
}
}
for _, entry := range entries[start:] {
c.totalVec.With(entryLabels(entry)).Inc()
}
newest := entries[len(entries)-1]
newest.Message = ""
c.seen[name] = newLogPosition(newest)
return nil
}
func (c *logCollector) collect(ctx context.Context, ch chan<- prometheus.Metric) error {
names, _, err := c.cl.ListLogs(ctx)
if err != nil {
return fmt.Errorf("listing log names: %w", err)
}
g, ctx := errgroup.WithContext(ctx)
g.SetLimit(runtime.GOMAXPROCS(0))
for _, name := range names {
name := name
g.Go(func() error {
if err := c.collectOne(ctx, name); err != nil {
return fmt.Errorf("log %s: %w", name, err)
}
return nil
})
}
if err := g.Wait(); err != nil {
return err
}
c.totalVec.Collect(ch)
return nil
}