forked from grafana/loki
-
Notifications
You must be signed in to change notification settings - Fork 0
/
metrics.go
181 lines (151 loc) · 5.15 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
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
175
176
177
178
179
180
181
package metrics
import (
"reflect"
"github.com/prometheus/client_golang/prometheus"
"sigs.k8s.io/controller-runtime/pkg/metrics"
lokiv1 "github.com/grafana/loki/operator/apis/loki/v1"
"github.com/grafana/loki/operator/internal/manifests"
)
// UserDefinedLimitsType defines a label that describes the type of limits
// imposed on the cluster
type UserDefinedLimitsType string
const (
labelGlobal UserDefinedLimitsType = "global"
labelTenant UserDefinedLimitsType = "tenant"
)
var (
deploymentMetric = prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Name: "lokistack_deployments",
Help: "Number of clusters that are deployed",
},
[]string{"size", "stack_id"},
)
userDefinedLimitsMetric = prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Name: "lokistack_user_defined_limits",
Help: "Number of clusters that are using user defined limits",
},
[]string{"size", "stack_id", "type"},
)
globalStreamLimitMetric = prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Name: "lokistack_global_stream_limit",
Help: "Sum of stream limits used globally by the ingesters",
},
[]string{"size", "stack_id"},
)
averageTenantStreamLimitMetric = prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Name: "lokistack_avg_stream_limit_per_tenant",
Help: "Sum of stream limits used for defined tenants by the ingesters",
},
[]string{"size", "stack_id"},
)
lokistackWarningsCount = prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Name: "lokistack_warnings_count",
Help: "Counts the number of warnings set on a LokiStack.",
},
[]string{"reason", "stack_id"},
)
)
// RegisterMetricCollectors registers the prometheus collectors with the k8 default metrics
func RegisterMetricCollectors() {
metricCollectors := []prometheus.Collector{
deploymentMetric,
userDefinedLimitsMetric,
globalStreamLimitMetric,
averageTenantStreamLimitMetric,
lokistackWarningsCount,
}
for _, collector := range metricCollectors {
metrics.Registry.MustRegister(collector)
}
}
// Collect takes metrics based on the spec
func Collect(spec *lokiv1.LokiStackSpec, stackName string) {
defaultSpec := manifests.DefaultLokiStackSpec(spec.Size)
sizes := []lokiv1.LokiStackSizeType{lokiv1.SizeOneXSmall, lokiv1.SizeOneXMedium}
for _, size := range sizes {
var (
globalRate float64
tenantRate float64
isUsingSize = false
isUsingTenantLimits = false
isUsingCustomGlobalLimits = false
)
if spec.Size == size {
isUsingSize = true
if !reflect.DeepEqual(spec.Limits.Global, defaultSpec.Limits.Global) {
isUsingCustomGlobalLimits = true
}
if len(spec.Limits.Tenants) != 0 {
isUsingTenantLimits = true
}
if ingesters := spec.Template.Ingester.Replicas; ingesters > 0 {
tenantRate = streamRate(spec.Limits.Tenants, ingesters)
globalRate = float64(spec.Limits.Global.IngestionLimits.MaxGlobalStreamsPerTenant / ingesters)
}
}
setDeploymentMetric(size, stackName, isUsingSize)
setUserDefinedLimitsMetric(size, stackName, labelGlobal, isUsingCustomGlobalLimits)
setUserDefinedLimitsMetric(size, stackName, labelTenant, isUsingTenantLimits)
setGlobalStreamLimitMetric(size, stackName, globalRate)
setAverageTenantStreamLimitMetric(size, stackName, tenantRate)
}
if len(spec.Storage.Schemas) > 0 && spec.Storage.Schemas[len(spec.Storage.Schemas)-1].Version != lokiv1.ObjectStorageSchemaV13 {
setLokistackSchemaUpgradesRequired(stackName, true)
}
}
func setLokistackSchemaUpgradesRequired(identifier string, active bool) {
lokistackWarningsCount.With(prometheus.Labels{
"reason": string(lokiv1.ReasonStorageNeedsSchemaUpdate),
"stack_id": identifier,
}).Set(boolValue(active))
}
func setDeploymentMetric(size lokiv1.LokiStackSizeType, identifier string, active bool) {
deploymentMetric.With(prometheus.Labels{
"size": string(size),
"stack_id": identifier,
}).Set(boolValue(active))
}
func setUserDefinedLimitsMetric(size lokiv1.LokiStackSizeType, identifier string, limitType UserDefinedLimitsType, active bool) {
userDefinedLimitsMetric.With(prometheus.Labels{
"size": string(size),
"stack_id": identifier,
"type": string(limitType),
}).Set(boolValue(active))
}
func setGlobalStreamLimitMetric(size lokiv1.LokiStackSizeType, identifier string, rate float64) {
globalStreamLimitMetric.With(prometheus.Labels{
"size": string(size),
"stack_id": identifier,
}).Set(rate)
}
func setAverageTenantStreamLimitMetric(size lokiv1.LokiStackSizeType, identifier string, rate float64) {
averageTenantStreamLimitMetric.With(prometheus.Labels{
"size": string(size),
"stack_id": identifier,
}).Set(rate)
}
func boolValue(value bool) float64 {
if value {
return 1
}
return 0
}
func streamRate(tenantLimits map[string]lokiv1.PerTenantLimitsTemplateSpec, ingesters int32) float64 {
var tenants, tenantStreamLimit int32 = 0, 0
for _, tenant := range tenantLimits {
if tenant.IngestionLimits == nil || tenant.IngestionLimits.MaxGlobalStreamsPerTenant == 0 {
continue
}
tenants++
tenantStreamLimit += tenant.IngestionLimits.MaxGlobalStreamsPerTenant
}
if tenants == 0 || ingesters == 0 {
return 0
}
return float64(tenantStreamLimit / ingesters / tenants)
}