forked from grafana/loki
-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #337 from xperimental/fix-duplicate-condition-5.8
[release-5.8] Backport metrics and status handling from 5.9
- Loading branch information
Showing
15 changed files
with
503 additions
and
417 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
package metrics | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/go-logr/logr" | ||
"github.com/prometheus/client_golang/prometheus" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
"sigs.k8s.io/controller-runtime/pkg/client" | ||
|
||
lokiv1 "github.com/grafana/loki/operator/apis/loki/v1" | ||
) | ||
|
||
const ( | ||
metricsPrefix = "lokistack_" | ||
) | ||
|
||
var ( | ||
metricsCommonLabels = []string{ | ||
"stack_namespace", | ||
"stack_name", | ||
"size", | ||
} | ||
|
||
lokiStackInfoDesc = prometheus.NewDesc( | ||
metricsPrefix+"info", | ||
"Information about deployed LokiStack instances. Value is always 1.", | ||
metricsCommonLabels, nil, | ||
) | ||
|
||
lokiStackConditionsCountDesc = prometheus.NewDesc( | ||
metricsPrefix+"status_condition", | ||
"Counts the current status conditions of the LokiStack.", | ||
append(metricsCommonLabels, "condition", "reason", "status"), nil, | ||
) | ||
) | ||
|
||
func RegisterLokiStackCollector(log logr.Logger, k8sClient client.Client, registry prometheus.Registerer) error { | ||
metrics := &lokiStackCollector{ | ||
log: log, | ||
k8sClient: k8sClient, | ||
} | ||
|
||
return registry.Register(metrics) | ||
} | ||
|
||
type lokiStackCollector struct { | ||
log logr.Logger | ||
k8sClient client.Client | ||
} | ||
|
||
func (l *lokiStackCollector) Describe(ch chan<- *prometheus.Desc) { | ||
ch <- lokiStackInfoDesc | ||
ch <- lokiStackConditionsCountDesc | ||
} | ||
|
||
func (l *lokiStackCollector) Collect(m chan<- prometheus.Metric) { | ||
ctx := context.TODO() | ||
|
||
stackList := &lokiv1.LokiStackList{} | ||
err := l.k8sClient.List(ctx, stackList) | ||
if err != nil { | ||
l.log.Error(err, "failed to get list of LokiStacks for metrics") | ||
return | ||
} | ||
|
||
for _, stack := range stackList.Items { | ||
labels := []string{ | ||
stack.Namespace, | ||
stack.Name, | ||
string(stack.Spec.Size), | ||
} | ||
|
||
m <- prometheus.MustNewConstMetric(lokiStackInfoDesc, prometheus.GaugeValue, 1.0, labels...) | ||
|
||
for _, c := range stack.Status.Conditions { | ||
activeValue := 0.0 | ||
if c.Status == metav1.ConditionTrue { | ||
activeValue = 1.0 | ||
} | ||
|
||
// This mirrors the behavior of kube_state_metrics, which creates two metrics for each condition, | ||
// one for each status (true/false). | ||
m <- prometheus.MustNewConstMetric( | ||
lokiStackConditionsCountDesc, | ||
prometheus.GaugeValue, activeValue, | ||
append(labels, c.Type, c.Reason, "true")..., | ||
) | ||
m <- prometheus.MustNewConstMetric( | ||
lokiStackConditionsCountDesc, | ||
prometheus.GaugeValue, 1.0-activeValue, | ||
append(labels, c.Type, c.Reason, "false")..., | ||
) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
package metrics | ||
|
||
import ( | ||
"context" | ||
"io" | ||
"strings" | ||
"testing" | ||
|
||
"github.com/ViaQ/logerr/v2/log" | ||
"github.com/prometheus/client_golang/prometheus" | ||
"github.com/prometheus/client_golang/prometheus/testutil" | ||
"github.com/stretchr/testify/require" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
"sigs.k8s.io/controller-runtime/pkg/client" | ||
|
||
lokiv1 "github.com/grafana/loki/operator/apis/loki/v1" | ||
"github.com/grafana/loki/operator/internal/external/k8s/k8sfakes" | ||
) | ||
|
||
func TestRegisterLokiStackMetrics(t *testing.T) { | ||
logger := log.NewLogger("test", log.WithOutput(io.Discard)) | ||
client := &k8sfakes.FakeClient{} | ||
registry := prometheus.NewPedanticRegistry() | ||
|
||
err := RegisterLokiStackCollector(logger, client, registry) | ||
require.NoError(t, err) | ||
} | ||
|
||
func TestLokiStackMetricsCollect(t *testing.T) { | ||
tt := []struct { | ||
desc string | ||
k8sError error | ||
stacks *lokiv1.LokiStackList | ||
wantMetrics string | ||
}{ | ||
{ | ||
desc: "no stacks", | ||
k8sError: nil, | ||
stacks: &lokiv1.LokiStackList{}, | ||
wantMetrics: "", | ||
}, | ||
{ | ||
desc: "one demo", | ||
k8sError: nil, | ||
stacks: &lokiv1.LokiStackList{ | ||
Items: []lokiv1.LokiStack{ | ||
{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Name: "test-stack", | ||
Namespace: "test-namespace", | ||
}, | ||
Spec: lokiv1.LokiStackSpec{ | ||
Size: lokiv1.SizeOneXDemo, | ||
}, | ||
}, | ||
}, | ||
}, | ||
wantMetrics: `# HELP lokistack_info Information about deployed LokiStack instances. Value is always 1. | ||
# TYPE lokistack_info gauge | ||
lokistack_info{size="1x.demo",stack_name="test-stack",stack_namespace="test-namespace"} 1 | ||
`, | ||
}, | ||
} | ||
|
||
for _, tc := range tt { | ||
tc := tc | ||
t.Run(tc.desc, func(t *testing.T) { | ||
t.Parallel() | ||
|
||
logger := log.NewLogger("test", log.WithOutput(io.Discard)) | ||
k := &k8sfakes.FakeClient{} | ||
k.ListStub = func(_ context.Context, list client.ObjectList, _ ...client.ListOption) error { | ||
if tc.k8sError != nil { | ||
return tc.k8sError | ||
} | ||
|
||
k.SetClientObjectList(list, tc.stacks) | ||
return nil | ||
} | ||
|
||
expected := strings.NewReader(tc.wantMetrics) | ||
|
||
c := &lokiStackCollector{ | ||
log: logger, | ||
k8sClient: k, | ||
} | ||
|
||
if err := testutil.CollectAndCompare(c, expected); err != nil { | ||
t.Error(err) | ||
} | ||
}) | ||
} | ||
} |
Oops, something went wrong.