Some checks failed
Server CI / Postgres (shard 0) (push) Has been skipped
Server CI / Postgres (shard 3) (push) Has been skipped
BuildEnv Docker Image / build-image (push) Failing after 4s
BuildEnv Docker Image / build-image-fips (push) Failing after 4s
Migration Review & Release Notes / Generate migration review & release notes (push) Failing after 3s
Server CI / Build Environment (push) Failing after 3s
Server CI / Postgres (shard 1) (push) Has been skipped
Tools CI / check-style (mattermost-govet) (push) Failing after 4s
Server CI / Postgres (shard 2) (push) Has been skipped
Tools CI / check-style (mmgotool) (push) Failing after 3s
Server CI / Elasticsearch v8 Compatibility (push) Has been skipped
Server CI / OpenSearch v2 Compatibility (push) Has been skipped
Server CI / Postgres FIPS (shard 0) (push) Has been skipped
Tools CI / Test (mattermost-govet) (push) Failing after 3s
Server CI / Postgres FIPS (shard 1) (push) Has been skipped
Server CI / Postgres FIPS (shard 2) (push) Has been skipped
Tools CI / Test (mmgotool) (push) Failing after 3s
Server CI / Postgres FIPS (shard 3) (push) Has been skipped
Server CI / Run mmctl tests (push) Has been skipped
Server CI / Run mmctl tests (FIPS) (push) Has been skipped
Server CI / Merge Postgres Test Results (push) Has been skipped
Server CI / Merge Postgres FIPS Test Results (push) Has been skipped
Server CI / Check backport migrations (push) Has been skipped
Server CI / Check generated files (push) Has been skipped
Server CI / check-style (push) Has been skipped
Server CI / Vet API (push) Has been skipped
Server CI / Build mattermost server app (push) Has been skipped
Server CI / Server CI Complete (push) Failing after 2s
Server CI / Server CI Artifacts (push) Has been skipped
Server CI / Sentry Upload (push) Has been skipped
Server CI / Server CI Report (push) Failing after 5m1s
Server CI Nightly Race / Race Detector (push) Has been skipped
CodeQL / Analyze (go) (push) Has been cancelled
CodeQL / Analyze (javascript) (push) Has been cancelled
Webapp CI Cache Warm / Warm Webapp node_modules Cache (push) Has been cancelled
E2E CI Cache Warm / Warm E2E npm Registry Cache (push) Has been cancelled
API / build (push) Has been cancelled
Web App CI / check-types (push) Has been cancelled
Web App CI / check-lint (push) Has been cancelled
Web App CI / check-i18n (push) Has been cancelled
Web App CI / check-external-links (push) Has been cancelled
Web App CI / test (platform) (push) Has been cancelled
Web App CI / test (mattermost-redux) (push) Has been cancelled
Web App CI / test (channels shard 1/4) (push) Has been cancelled
Web App CI / test (channels shard 2/4) (push) Has been cancelled
Web App CI / test (channels shard 3/4) (push) Has been cancelled
Web App CI / test (channels shard 4/4) (push) Has been cancelled
Web App CI / upload-coverage (push) Has been cancelled
Web App CI / build (push) Has been cancelled
YAML Lint / yamllint (push) Has been cancelled
Push mirrored docker images / cd/Push mirrored docker images (push) Has been cancelled
docs-cd / Build and deploy docs (push) Has been cancelled
Docs CI / build (push) Has been cancelled
E2E Cache Warm: Cypress deps / Warm Cypress deps cache (push) Has been cancelled
Scorecards supply-chain security / Scorecard analysis (push) Has been cancelled
Server CI Weekly / Postgres FIPS (push) Has been skipped
Server CI Weekly / Run mmctl tests (FIPS) (push) Has been skipped
Server CI Weekly / Postgres with binary parameters (push) Has been skipped
Server CI Weekly / Build Environment (push) Failing after 1s
Server CI Cache Warm / Warm Go Module and Build Cache (push) Failing after 2s
Server CI Nightly Race / Build Environment (push) Failing after 1s
169 lines
5.1 KiB
Go
169 lines
5.1 KiB
Go
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
|
|
// See LICENSE.txt for license information.
|
|
|
|
package platform
|
|
|
|
import (
|
|
"io"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"strings"
|
|
"testing"
|
|
|
|
"github.com/mattermost/mattermost/server/public/model"
|
|
"github.com/mattermost/mattermost/server/public/shared/mlog"
|
|
"github.com/stretchr/testify/assert"
|
|
"github.com/stretchr/testify/require"
|
|
)
|
|
|
|
func TestMetricsRouter(t *testing.T) {
|
|
logger := mlog.CreateTestLogger(t)
|
|
|
|
cfg := &model.Config{}
|
|
cfg.SetDefaults()
|
|
|
|
metrics := &platformMetrics{
|
|
cfgFn: func() *model.Config { return cfg },
|
|
logger: logger,
|
|
}
|
|
|
|
err := metrics.initMetricsRouter()
|
|
require.NoError(t, err)
|
|
|
|
server := httptest.NewServer(metrics.router)
|
|
t.Cleanup(server.Close)
|
|
client := server.Client()
|
|
|
|
for name, tc := range map[string]struct {
|
|
path string
|
|
method string
|
|
ExpectedBodyContains string
|
|
ExpectedCode int
|
|
}{
|
|
"root": {path: "", ExpectedBodyContains: "<div><a href=\"/debug/pprof/\">Profiling Root</a></div>"},
|
|
"root with slash": {path: "/", ExpectedBodyContains: "<div><a href=\"/debug/pprof/\">Profiling Root</a></div>"},
|
|
"debug redirect": {path: "/debug", ExpectedBodyContains: "<div><a href=\"/debug/pprof/\">Profiling Root</a></div>"},
|
|
"debug redirect with slash": {path: "/debug/", ExpectedBodyContains: "<div><a href=\"/debug/pprof/\">Profiling Root</a></div>"},
|
|
"pprof index page": {path: "/debug/pprof", ExpectedBodyContains: "<p>Set debug=1 as a query parameter to export in legacy text format</p>"},
|
|
"pprof index page with slash": {path: "/debug/pprof/", ExpectedBodyContains: "<p>Set debug=1 as a query parameter to export in legacy text format</p>"},
|
|
"pprof allocs": {path: "/debug/pprof/allocs"},
|
|
"pprof block": {path: "/debug/pprof/block"},
|
|
"pprof cmdline": {path: "/debug/pprof/cmdline"},
|
|
"pprof goroutine": {path: "/debug/pprof/goroutine"},
|
|
"pprof heap": {path: "/debug/pprof/heap"},
|
|
"pprof mutex": {path: "/debug/pprof/mutex"},
|
|
"pprof profile": {path: "/debug/pprof/profile?seconds=1"},
|
|
"pprof symbol": {path: "/debug/pprof/symbol"},
|
|
"pprof threadcreate": {path: "/debug/pprof/threadcreate"},
|
|
"pprof trace": {path: "/debug/pprof/trace"},
|
|
} {
|
|
t.Run(name, func(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
method := http.MethodGet
|
|
if tc.method != "" {
|
|
method = tc.method
|
|
}
|
|
url := server.URL + tc.path
|
|
|
|
req, err := http.NewRequest(method, url, nil)
|
|
require.NoError(t, err, name)
|
|
|
|
resp, err := client.Do(req)
|
|
require.NoError(t, err, name)
|
|
t.Cleanup(func() {
|
|
err := resp.Body.Close()
|
|
assert.NoError(t, err)
|
|
})
|
|
|
|
expectedCode := 200
|
|
if tc.ExpectedCode != 0 {
|
|
expectedCode = tc.ExpectedCode
|
|
}
|
|
assert.Equal(t, expectedCode, resp.StatusCode)
|
|
|
|
if tc.ExpectedBodyContains != "" {
|
|
b, err := io.ReadAll(resp.Body)
|
|
require.NoError(t, err, name)
|
|
assert.Contains(t, string(b), tc.ExpectedBodyContains)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestAddPluginLabelToMetrics(t *testing.T) {
|
|
for name, tc := range map[string]struct {
|
|
input []string
|
|
pluginID string
|
|
expected []string
|
|
}{
|
|
"metric without labels": {
|
|
input: []string{"my_metric 42"},
|
|
pluginID: "com.example.plugin",
|
|
expected: []string{
|
|
"# TYPE my_metric untyped",
|
|
`my_metric{plugin_id="com.example.plugin"} 42`,
|
|
},
|
|
},
|
|
"metric with existing labels": {
|
|
input: []string{`my_metric{env="prod"} 42`},
|
|
pluginID: "com.example.plugin",
|
|
expected: []string{
|
|
"# TYPE my_metric untyped",
|
|
`my_metric{env="prod",plugin_id="com.example.plugin"} 42`,
|
|
},
|
|
},
|
|
"help and type comments are preserved": {
|
|
input: []string{
|
|
"# HELP my_metric A metric",
|
|
"# TYPE my_metric gauge",
|
|
"my_metric 42",
|
|
},
|
|
pluginID: "my-plugin",
|
|
expected: []string{
|
|
"# HELP my_metric A metric",
|
|
"# TYPE my_metric gauge",
|
|
`my_metric{plugin_id="my-plugin"} 42`,
|
|
},
|
|
},
|
|
"empty lines are dropped": {
|
|
// blank lines carry no semantic meaning in Prometheus text format
|
|
input: []string{"", "my_metric 1"},
|
|
pluginID: "p",
|
|
expected: []string{
|
|
"# TYPE my_metric untyped",
|
|
`my_metric{plugin_id="p"} 1`,
|
|
},
|
|
},
|
|
"existing plugin_id label is replaced not duplicated": {
|
|
input: []string{`my_metric{plugin_id="old-plugin"} 42`},
|
|
pluginID: "new-plugin",
|
|
expected: []string{
|
|
`my_metric{plugin_id="new-plugin"} 42`,
|
|
},
|
|
},
|
|
"multiple metrics are sorted and separated": {
|
|
input: []string{"metric_a 1", "metric_b 2"},
|
|
pluginID: "plug",
|
|
expected: []string{
|
|
"# TYPE metric_a untyped",
|
|
`metric_a{plugin_id="plug"} 1`,
|
|
"# TYPE metric_b untyped",
|
|
`metric_b{plugin_id="plug"} 2`,
|
|
},
|
|
},
|
|
} {
|
|
t.Run(name, func(t *testing.T) {
|
|
result := addPluginLabelToMetrics(strings.Join(tc.input, "\n")+"\n", tc.pluginID)
|
|
for _, line := range tc.expected {
|
|
assert.Contains(t, result, line)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestAddPluginLabelToMetricsMalformedInput(t *testing.T) {
|
|
result := addPluginLabelToMetrics("not valid prometheus text {{{", "my-plugin")
|
|
assert.Equal(t, "", result)
|
|
}
|