Skip to content

Commit a68f440

Browse files
authored
Improve metrics integration tests (#2308)
* Improve metrics integration tests Signed-off-by: Marco Pracucci <marco@pracucci.com> * Added alertmanager metric prefix to integration tests Signed-off-by: Marco Pracucci <marco@pracucci.com> * Fixed linter and improved tests Signed-off-by: Marco Pracucci <marco@pracucci.com> * Remove cortex_alertmanager prefix check (I've been too much optimistic) Signed-off-by: Marco Pracucci <marco@pracucci.com> * Fixed integration tests after rebasing master Signed-off-by: Marco Pracucci <marco@pracucci.com>
1 parent a32919e commit a68f440

File tree

11 files changed

+127
-120
lines changed

11 files changed

+127
-120
lines changed

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ protos: $(PROTO_GOS)
123123

124124
lint:
125125
misspell -error docs
126-
golangci-lint run --build-tags netgo --timeout=5m --enable golint --enable misspell --enable gofmt
126+
golangci-lint run --build-tags netgo,require_docker --timeout=5m --enable golint --enable misspell --enable gofmt
127127

128128
# Ensure no blacklisted package is imported.
129129
faillint -paths "github.com/bmizerany/assert=github.com/stretchr/testify/assert" ./pkg/... ./cmd/... ./tools/... ./integration/...

integration/alertmanager_test.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,4 +36,7 @@ func TestAlertmanager(t *testing.T) {
3636
require.Equal(t, "example_groupby", cfg.Route.GroupByStr[0])
3737
require.Len(t, cfg.Receivers, 1)
3838
require.Equal(t, "example_receiver", cfg.Receivers[0].Name)
39+
40+
// Ensure no service-specific metrics prefix is used by the wrong service.
41+
assertServiceMetricsPrefixes(t, AlertManager, alertmanager)
3942
}

integration/api_ruler_test.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,4 +70,7 @@ func TestRulerAPI(t *testing.T) {
7070
// Check to ensure the rule groups are no longer active
7171
_, err = c.GetRuleGroups()
7272
require.Error(t, err)
73+
74+
// Ensure no service-specific metrics prefix is used by the wrong service.
75+
assertServiceMetricsPrefixes(t, Ruler, ruler)
7376
}

integration/asserts.go

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
// +build requires_docker
2+
3+
package main
4+
5+
import (
6+
"strings"
7+
"testing"
8+
9+
"github.com/stretchr/testify/assert"
10+
"github.com/stretchr/testify/require"
11+
12+
"github.com/cortexproject/cortex/integration/e2ecortex"
13+
)
14+
15+
type ServiceType int
16+
17+
const (
18+
Distributor = iota
19+
Ingester
20+
Querier
21+
QueryFrontend
22+
TableManager
23+
AlertManager
24+
Ruler
25+
)
26+
27+
var (
28+
// Service-specific metrics prefixes which shouldn't be used by any other service.
29+
serviceMetricsPrefixes = map[ServiceType][]string{
30+
Distributor: []string{},
31+
Ingester: []string{},
32+
Querier: []string{},
33+
QueryFrontend: []string{"cortex_frontend", "cortex_query_frontend"},
34+
TableManager: []string{},
35+
AlertManager: []string{},
36+
Ruler: []string{},
37+
}
38+
39+
// Blacklisted metrics prefixes across any Cortex service.
40+
blacklistedMetricsPrefixes = []string{
41+
"cortex_alert_manager", // It should be "cortex_alertmanager"
42+
}
43+
)
44+
45+
func assertServiceMetricsPrefixes(t *testing.T, serviceType ServiceType, service *e2ecortex.CortexService) {
46+
metrics, err := service.Metrics()
47+
require.NoError(t, err)
48+
49+
// Build the list of blacklisted metrics prefixes for this specific service.
50+
blacklist := getBlacklistedMetricsPrefixesByService(serviceType)
51+
52+
// Ensure no metric name matches the blacklisted prefixes.
53+
for _, metricLine := range strings.Split(metrics, "\n") {
54+
metricLine = strings.TrimSpace(metricLine)
55+
if metricLine == "" || strings.HasPrefix(metricLine, "#") {
56+
continue
57+
}
58+
59+
for _, prefix := range blacklist {
60+
assert.NotRegexp(t, "^"+prefix, metricLine, "service: %s", service.Name())
61+
}
62+
}
63+
}
64+
65+
func getBlacklistedMetricsPrefixesByService(serviceType ServiceType) []string {
66+
blacklist := append([]string{}, blacklistedMetricsPrefixes...)
67+
68+
// Add any service-specific metrics prefix excluding the service itself.
69+
for t, prefixes := range serviceMetricsPrefixes {
70+
if t == serviceType {
71+
continue
72+
}
73+
74+
blacklist = append(blacklist, prefixes...)
75+
}
76+
77+
return blacklist
78+
}

integration/chunks_storage_backends_test.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,4 +108,9 @@ func TestChunksStorageAllIndexBackends(t *testing.T) {
108108
require.Equal(t, model.ValVector, result.Type())
109109
assert.Equal(t, expectedVector, result.(model.Vector))
110110
}
111+
112+
// Ensure no service-specific metrics prefix is used by the wrong service.
113+
assertServiceMetricsPrefixes(t, Distributor, distributor)
114+
assertServiceMetricsPrefixes(t, Ingester, ingester)
115+
assertServiceMetricsPrefixes(t, Querier, querier)
111116
}

integration/e2ecortex/services.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,7 @@ func NewRuler(name string, flags map[string]string, image string) *CortexService
224224
"-log.level": "warn",
225225
}, flags))...),
226226
// The alertmanager doesn't expose a readiness probe, so we just check if the / returns 200
227-
e2e.NewReadinessProbe(httpPort, "/", 200),
227+
e2e.NewHTTPReadinessProbe(httpPort, "/", 200),
228228
httpPort,
229229
grpcPort,
230230
)

integration/ingester_flush_test.go

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,12 +33,12 @@ func TestIngesterFlushWithChunksStorage(t *testing.T) {
3333
require.NoError(t, writeFileToSharedDir(s, cortexSchemaConfigFile, []byte(cortexSchemaConfigYaml)))
3434

3535
tableManager := e2ecortex.NewTableManager("table-manager", ChunksStorageFlags, "")
36-
ingester1 := e2ecortex.NewIngester("ingester-1", consul.NetworkHTTPEndpoint(), mergeFlags(ChunksStorageFlags, map[string]string{
36+
ingester := e2ecortex.NewIngester("ingester-1", consul.NetworkHTTPEndpoint(), mergeFlags(ChunksStorageFlags, map[string]string{
3737
"-ingester.max-transfer-retries": "0",
3838
}), "")
3939
querier := e2ecortex.NewQuerier("querier", consul.NetworkHTTPEndpoint(), ChunksStorageFlags, "")
4040
distributor := e2ecortex.NewDistributor("distributor", consul.NetworkHTTPEndpoint(), ChunksStorageFlags, "")
41-
require.NoError(t, s.StartAndWaitReady(distributor, querier, ingester1, tableManager))
41+
require.NoError(t, s.StartAndWaitReady(distributor, querier, ingester, tableManager))
4242

4343
// Wait until the first table-manager sync has completed, so that we're
4444
// sure the tables have been created.
@@ -73,9 +73,12 @@ func TestIngesterFlushWithChunksStorage(t *testing.T) {
7373
require.Equal(t, model.ValVector, result.Type())
7474
assert.Equal(t, expectedVector2, result.(model.Vector))
7575

76+
// Ensure no service-specific metrics prefix is used by the wrong service.
77+
assertServiceMetricsPrefixes(t, Ingester, ingester)
78+
7679
// Stop ingester-1, so that it will flush all chunks to the storage. This function will return
7780
// once the ingester-1 is successfully stopped, which means the flushing is completed.
78-
require.NoError(t, s.Stop(ingester1))
81+
require.NoError(t, s.Stop(ingester))
7982

8083
// Ensure chunks have been uploaded to the storage (DynamoDB).
8184
dynamoURL := "dynamodb://u:p@" + dynamo.Endpoint(8000)
@@ -94,4 +97,9 @@ func TestIngesterFlushWithChunksStorage(t *testing.T) {
9497
out, err = dynamoClient.Scan(&dynamodb.ScanInput{TableName: aws.String(chunksTable)})
9598
require.NoError(t, err)
9699
assert.Equal(t, int64(2), *out.Count)
100+
101+
// Ensure no service-specific metrics prefix is used by the wrong service.
102+
assertServiceMetricsPrefixes(t, Distributor, distributor)
103+
assertServiceMetricsPrefixes(t, Querier, querier)
104+
assertServiceMetricsPrefixes(t, TableManager, tableManager)
97105
}

integration/ingester_hand_over_test.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,4 +89,9 @@ func runIngesterHandOverTest(t *testing.T, flags map[string]string, setup func(t
8989
result, err = c.Query("series_1", now)
9090
require.NoError(t, err)
9191
assert.Equal(t, expectedVector, result.(model.Vector))
92+
93+
// Ensure no service-specific metrics prefix is used by the wrong service.
94+
assertServiceMetricsPrefixes(t, Distributor, distributor)
95+
assertServiceMetricsPrefixes(t, Ingester, ingester2)
96+
assertServiceMetricsPrefixes(t, Querier, querier)
9297
}

integration/metrics_test.go

Lines changed: 0 additions & 112 deletions
This file was deleted.

integration/querier_test.go

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ func TestQuerierWithBlocksStorage(t *testing.T) {
3636
"-experimental.tsdb.bucket-store.index-cache.backend": "inmemory",
3737
}),
3838
},
39-
"queintegration/e2e/service.gorier running with memcached index cache": {
39+
"querier running with memcached index cache": {
4040
flags: mergeFlags(BlocksStorageFlags, map[string]string{
4141
// The address will be inject during the test execution because it's dynamic.
4242
"-experimental.tsdb.bucket-store.index-cache.backend": "memcached",
@@ -169,6 +169,11 @@ func TestQuerierWithBlocksStorage(t *testing.T) {
169169
} else if indexCacheBackend == tsdb.IndexCacheBackendMemcached {
170170
require.NoError(t, querier.WaitSumMetrics(e2e.Equals(11+2), "cortex_querier_blocks_index_cache_memcached_operations_total")) // as before + 2 gets
171171
}
172+
173+
// Ensure no service-specific metrics prefix is used by the wrong service.
174+
assertServiceMetricsPrefixes(t, Distributor, distributor)
175+
assertServiceMetricsPrefixes(t, Ingester, ingester)
176+
assertServiceMetricsPrefixes(t, Querier, querier)
172177
})
173178
}
174179
}
@@ -292,7 +297,7 @@ func TestQuerierWithChunksStorage(t *testing.T) {
292297
expectedVectors := make([]model.Vector, numUsers)
293298

294299
for u := 0; u < numUsers; u++ {
295-
c, err := e2ecortex.NewClient(distributor.HTTPEndpoint(), "", "", fmt.Sprintf("user-%d", u))
300+
c, err := e2ecortex.NewClient(distributor.HTTPEndpoint(), "", "", "", fmt.Sprintf("user-%d", u))
296301
require.NoError(t, err)
297302

298303
var series []prompb.TimeSeries
@@ -323,7 +328,7 @@ func TestQuerierWithChunksStorage(t *testing.T) {
323328
for u := 0; u < numUsers; u++ {
324329
userID := u
325330

326-
c, err := e2ecortex.NewClient("", querier.HTTPEndpoint(), "", fmt.Sprintf("user-%d", userID))
331+
c, err := e2ecortex.NewClient("", querier.HTTPEndpoint(), "", "", fmt.Sprintf("user-%d", userID))
327332
require.NoError(t, err)
328333

329334
for q := 0; q < numQueriesPerUser; q++ {
@@ -339,4 +344,10 @@ func TestQuerierWithChunksStorage(t *testing.T) {
339344
}
340345

341346
wg.Wait()
347+
348+
// Ensure no service-specific metrics prefix is used by the wrong service.
349+
assertServiceMetricsPrefixes(t, Distributor, distributor)
350+
assertServiceMetricsPrefixes(t, Ingester, ingester)
351+
assertServiceMetricsPrefixes(t, Querier, querier)
352+
assertServiceMetricsPrefixes(t, TableManager, tableManager)
342353
}

0 commit comments

Comments
 (0)