mirror of https://github.com/grafana/loki
Update delete integration test (#6192)
* Add wait until processed Signed-off-by: Michel Hollands <michel.hollands@grafana.com> * Add query lines Signed-off-by: Michel Hollands <michel.hollands@grafana.com> * Remove Cortex reference Signed-off-by: Michel Hollands <michel.hollands@grafana.com> * Add deleted lines metric and check in test Signed-off-by: Michel Hollands <michel.hollands@grafana.com> * Set negative cancel period so default 1 minute delay is compensated for Signed-off-by: Michel Hollands <michel.hollands@grafana.com> * Reduce timeout Signed-off-by: Michel Hollands <michel.hollands@grafana.com> * Add helper function Signed-off-by: Michel Hollands <michel.hollands@grafana.com> * Comment out assertions that only work with flush Signed-off-by: Michel Hollands <michel.hollands@grafana.com> * Remove unused metric type param Signed-off-by: Michel Hollands <michel.hollands@grafana.com> * Check counter in unit test Signed-off-by: Michel Hollands <michel.hollands@grafana.com> * Rename ClientOption to Option as per the linter Signed-off-by: Michel Hollands <michel.hollands@grafana.com> * Remove redundant check Signed-off-by: Michel Hollands <michel.hollands@grafana.com> * fill in MsgAndArgs field of test assertion Signed-off-by: Michel Hollands <michel.hollands@grafana.com>pull/6330/head
parent
a877febf75
commit
65e3148bc9
@ -0,0 +1,44 @@ |
||||
package integration |
||||
|
||||
import ( |
||||
"fmt" |
||||
"strings" |
||||
|
||||
io_prometheus_client "github.com/prometheus/client_model/go" |
||||
"github.com/prometheus/common/expfmt" |
||||
) |
||||
|
||||
var ( |
||||
ErrNoMetricFound = fmt.Errorf("metric not found") |
||||
ErrInvalidMetricType = fmt.Errorf("invalid metric type") |
||||
) |
||||
|
||||
func extractMetric(metricName, metrics string) (float64, map[string]string, error) { |
||||
var parser expfmt.TextParser |
||||
mfs, err := parser.TextToMetricFamilies(strings.NewReader(metrics)) |
||||
if err != nil { |
||||
return 0, nil, err |
||||
} |
||||
|
||||
mf, found := mfs[metricName] |
||||
if !found { |
||||
return 0, nil, ErrNoMetricFound |
||||
} |
||||
|
||||
var val float64 |
||||
switch mf.GetType() { |
||||
case io_prometheus_client.MetricType_COUNTER: |
||||
val = *mf.Metric[0].Counter.Value |
||||
case io_prometheus_client.MetricType_GAUGE: |
||||
val = *mf.Metric[0].Gauge.Value |
||||
default: |
||||
return 0, nil, ErrInvalidMetricType |
||||
} |
||||
|
||||
labels := make(map[string]string) |
||||
for _, l := range mf.Metric[0].Label { |
||||
labels[*l.Name] = *l.Value |
||||
} |
||||
|
||||
return val, labels, nil |
||||
} |
||||
@ -0,0 +1,41 @@ |
||||
package integration |
||||
|
||||
import ( |
||||
"testing" |
||||
|
||||
"github.com/stretchr/testify/require" |
||||
) |
||||
|
||||
var exampleMetricOutput = ` |
||||
# HELP loki_compactor_delete_requests_processed_total Number of delete requests processed per user |
||||
# TYPE loki_compactor_delete_requests_processed_total counter |
||||
loki_compactor_delete_requests_processed_total{user="eEWxEcgwRQcf"} 1 |
||||
# HELP loki_compactor_delete_requests_received_total Number of delete requests received per user |
||||
# TYPE loki_compactor_delete_requests_received_total counter |
||||
loki_compactor_delete_requests_received_total{user="eEWxEcgwRQcf"} 1 |
||||
# HELP loki_compactor_load_pending_requests_attempts_total Number of attempts that were made to load pending requests with status |
||||
# TYPE loki_compactor_load_pending_requests_attempts_total counter |
||||
loki_compactor_load_pending_requests_attempts_total{status="success"} 57 |
||||
# HELP loki_compactor_oldest_pending_delete_request_age_seconds Age of oldest pending delete request in seconds, since they are over their cancellation period |
||||
# TYPE loki_compactor_oldest_pending_delete_request_age_seconds gauge |
||||
loki_compactor_oldest_pending_delete_request_age_seconds 0 |
||||
# HELP loki_compactor_pending_delete_requests_count Count of delete requests which are over their cancellation period and have not finished processing yet |
||||
# TYPE loki_compactor_pending_delete_requests_count gauge |
||||
loki_compactor_pending_delete_requests_count 0 |
||||
` |
||||
|
||||
func TestExtractCounterMetric(t *testing.T) { |
||||
val, labels, err := extractMetric("loki_compactor_oldest_pending_delete_request_age_seconds", exampleMetricOutput) |
||||
require.NoError(t, err) |
||||
require.NotNil(t, labels) |
||||
require.Len(t, labels, 0) |
||||
require.Equal(t, float64(0), val) |
||||
|
||||
val, labels, err = extractMetric("loki_compactor_delete_requests_processed_total", exampleMetricOutput) |
||||
require.NoError(t, err) |
||||
require.NotNil(t, labels) |
||||
require.Len(t, labels, 1) |
||||
require.Contains(t, labels, "user") |
||||
require.Equal(t, labels["user"], "eEWxEcgwRQcf") |
||||
require.Equal(t, float64(1), val) |
||||
} |
||||
Loading…
Reference in new issue