-
Notifications
You must be signed in to change notification settings - Fork 3.4k
/
metrics.go
119 lines (101 loc) · 3.6 KB
/
metrics.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
package client
import (
"context"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promauto"
"github.com/grafana/loki/v3/pkg/storage/chunk"
"github.com/grafana/loki/v3/pkg/util/constants"
)
// takes a chunk client and exposes metrics for its operations.
type MetricsChunkClient struct {
Client Client
metrics ChunkClientMetrics
}
func NewMetricsChunkClient(client Client, metrics ChunkClientMetrics) MetricsChunkClient {
return MetricsChunkClient{
Client: client,
metrics: metrics,
}
}
type ChunkClientMetrics struct {
chunksPutPerUser *prometheus.CounterVec
chunksSizePutPerUser *prometheus.CounterVec
chunksFetchedPerUser *prometheus.CounterVec
chunksSizeFetchedPerUser *prometheus.CounterVec
}
func NewChunkClientMetrics(reg prometheus.Registerer) ChunkClientMetrics {
return ChunkClientMetrics{
chunksPutPerUser: promauto.With(reg).NewCounterVec(prometheus.CounterOpts{
Namespace: constants.Loki,
Name: "chunk_store_stored_chunks_total",
Help: "Total stored chunks per user.",
}, []string{"user"}),
chunksSizePutPerUser: promauto.With(reg).NewCounterVec(prometheus.CounterOpts{
Namespace: constants.Loki,
Name: "chunk_store_stored_chunk_bytes_total",
Help: "Total bytes stored in chunks per user.",
}, []string{"user"}),
chunksFetchedPerUser: promauto.With(reg).NewCounterVec(prometheus.CounterOpts{
Namespace: constants.Loki,
Name: "chunk_store_fetched_chunks_total",
Help: "Total fetched chunks per user.",
}, []string{"user"}),
chunksSizeFetchedPerUser: promauto.With(reg).NewCounterVec(prometheus.CounterOpts{
Namespace: constants.Loki,
Name: "chunk_store_fetched_chunk_bytes_total",
Help: "Total bytes fetched in chunks per user.",
}, []string{"user"}),
}
}
func (c MetricsChunkClient) Stop() {
c.Client.Stop()
}
func (c MetricsChunkClient) PutChunks(ctx context.Context, chunks []chunk.Chunk) error {
if err := c.Client.PutChunks(ctx, chunks); err != nil {
return err
}
// For PutChunks, we explicitly encode the userID in the chunk and don't use context.
userSizes := map[string]int{}
userCounts := map[string]int{}
for _, c := range chunks {
userSizes[c.UserID] += c.Data.Size()
userCounts[c.UserID]++
}
for user, size := range userSizes {
c.metrics.chunksSizePutPerUser.WithLabelValues(user).Add(float64(size))
}
for user, num := range userCounts {
c.metrics.chunksPutPerUser.WithLabelValues(user).Add(float64(num))
}
return nil
}
func (c MetricsChunkClient) GetChunks(ctx context.Context, chunks []chunk.Chunk) ([]chunk.Chunk, error) {
chks, err := c.Client.GetChunks(ctx, chunks)
if err != nil {
return chks, err
}
// For GetChunks, userID is the chunk and we don't need to use context.
// For now, we just load one user chunks at once, but the interface lets us do it for multiple users.
userSizes := map[string]int{}
userCounts := map[string]int{}
for _, c := range chks {
userSizes[c.UserID] += c.Data.Size()
userCounts[c.UserID]++
}
for user, size := range userSizes {
c.metrics.chunksSizeFetchedPerUser.WithLabelValues(user).Add(float64(size))
}
for user, num := range userCounts {
c.metrics.chunksFetchedPerUser.WithLabelValues(user).Add(float64(num))
}
return chks, nil
}
func (c MetricsChunkClient) DeleteChunk(ctx context.Context, userID, chunkID string) error {
return c.Client.DeleteChunk(ctx, userID, chunkID)
}
func (c MetricsChunkClient) IsChunkNotFoundErr(err error) bool {
return c.Client.IsChunkNotFoundErr(err)
}
func (c MetricsChunkClient) IsRetryableErr(err error) bool {
return c.Client.IsRetryableErr(err)
}