Add mobile metrics (#27045)
* Add mobile metrics * Fix mocks * Add tests * Fix lint * Address feedback * Fix lint * Fix test * Fix CI --------- Co-authored-by: Mattermost Build <build@mattermost.com>
Этот коммит содержится в:
коммит произвёл
GitHub
родитель
691386a814
Коммит
1ec2de4a95
@@ -166,6 +166,18 @@ func Setup(tb testing.TB, options ...Option) *TestHelper {
|
||||
return setupTestHelper(dbStore, false, true, nil, options, tb)
|
||||
}
|
||||
|
||||
func SetupEnterprise(tb testing.TB, options ...Option) *TestHelper {
|
||||
if testing.Short() {
|
||||
tb.SkipNow()
|
||||
}
|
||||
dbStore := mainHelper.GetStore()
|
||||
dbStore.DropAllTables()
|
||||
dbStore.MarkSystemRanUnitTests()
|
||||
mainHelper.PreloadMigrations()
|
||||
|
||||
return setupTestHelper(dbStore, true, true, nil, options, tb)
|
||||
}
|
||||
|
||||
func SetupConfig(tb testing.TB, updateConfig func(cfg *model.Config)) *TestHelper {
|
||||
if testing.Short() {
|
||||
tb.SkipNow()
|
||||
|
||||
@@ -46,6 +46,12 @@ func (a *App) RegisterPerformanceReport(rctx request.CTX, report *model.Performa
|
||||
a.Metrics().ObserveClientRHSLoadDuration(commonLabels["platform"], commonLabels["agent"], h.Value/1000)
|
||||
case model.ClientGlobalThreadsLoadDuration:
|
||||
a.Metrics().ObserveGlobalThreadsLoadDuration(commonLabels["platform"], commonLabels["agent"], h.Value/1000)
|
||||
case model.MobileClientLoadDuration:
|
||||
a.Metrics().ObserveMobileClientLoadDuration(commonLabels["platform"], h.Value/1000)
|
||||
case model.MobileClientChannelSwitchDuration:
|
||||
a.Metrics().ObserveMobileClientChannelSwitchDuration(commonLabels["platform"], h.Value/1000)
|
||||
case model.MobileClientTeamSwitchDuration:
|
||||
a.Metrics().ObserveMobileClientTeamSwitchDuration(commonLabels["platform"], h.Value/1000)
|
||||
default:
|
||||
// we intentionally skip unknown metrics
|
||||
}
|
||||
|
||||
102
server/channels/app/metrics_test.go
Обычный файл
102
server/channels/app/metrics_test.go
Обычный файл
@@ -0,0 +1,102 @@
|
||||
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
|
||||
// See LICENSE.txt for license information.
|
||||
|
||||
package app
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"testing"
|
||||
|
||||
"github.com/mattermost/mattermost/server/public/model"
|
||||
"github.com/mattermost/mattermost/server/v8/enterprise/metrics"
|
||||
"github.com/prometheus/client_golang/prometheus"
|
||||
prometheusModels "github.com/prometheus/client_model/go"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func configureMetrics(th *TestHelper) {
|
||||
th.App.Srv().SetLicense(nil) // clear license
|
||||
th.App.UpdateConfig(func(cfg *model.Config) {
|
||||
*cfg.MetricsSettings.Enable = true
|
||||
*cfg.MetricsSettings.ListenAddress = ":0"
|
||||
})
|
||||
th.App.Srv().SetLicense(model.NewTestLicense("metrics"))
|
||||
}
|
||||
func TestMobileMetrics(t *testing.T) {
|
||||
th := SetupEnterprise(t, StartMetrics)
|
||||
defer th.TearDown()
|
||||
|
||||
configureMetrics(th)
|
||||
mi := th.App.Metrics()
|
||||
|
||||
miImpl, ok := mi.(*metrics.MetricsInterfaceImpl)
|
||||
require.True(t, ok, fmt.Sprintf("App.Metrics is not *MetricsInterfaceImpl, but %T", mi))
|
||||
m := &prometheusModels.Metric{}
|
||||
|
||||
for _, platform := range []string{"ios", "android"} {
|
||||
ttcc := []struct {
|
||||
name string
|
||||
histogramVec *prometheus.HistogramVec
|
||||
elapsed float64
|
||||
metricName model.MetricType
|
||||
}{
|
||||
{
|
||||
name: "load duration",
|
||||
histogramVec: miImpl.MobileClientLoadDuration,
|
||||
elapsed: 5001,
|
||||
metricName: model.MobileClientLoadDuration,
|
||||
},
|
||||
{
|
||||
name: "channel switch duration",
|
||||
histogramVec: miImpl.MobileClientChannelSwitchDuration,
|
||||
elapsed: 501,
|
||||
metricName: model.MobileClientChannelSwitchDuration,
|
||||
},
|
||||
{
|
||||
name: "team switch duration",
|
||||
histogramVec: miImpl.MobileClientTeamSwitchDuration,
|
||||
elapsed: 301,
|
||||
metricName: model.MobileClientTeamSwitchDuration,
|
||||
},
|
||||
}
|
||||
|
||||
histograms := []*model.MetricSample{}
|
||||
var start float64 = 125
|
||||
end := start + float64(len(ttcc))
|
||||
// Precheck
|
||||
for i, tc := range ttcc {
|
||||
actualMetric, err := tc.histogramVec.GetMetricWith(prometheus.Labels{"platform": platform})
|
||||
require.NoError(t, err)
|
||||
require.NoError(t, actualMetric.(prometheus.Histogram).Write(m))
|
||||
require.Equal(t, uint64(0), m.Histogram.GetSampleCount())
|
||||
require.Equal(t, 0.0, m.Histogram.GetSampleSum())
|
||||
histograms = append(histograms, &model.MetricSample{
|
||||
Metric: tc.metricName,
|
||||
Value: tc.elapsed,
|
||||
Timestamp: start + float64(i),
|
||||
})
|
||||
}
|
||||
|
||||
appErr := th.App.RegisterPerformanceReport(th.Context, &model.PerformanceReport{
|
||||
Version: "0.1.0",
|
||||
ClientID: "",
|
||||
Labels: map[string]string{
|
||||
"platform": platform,
|
||||
},
|
||||
Start: start,
|
||||
End: end,
|
||||
Counters: []*model.MetricSample{},
|
||||
Histograms: histograms,
|
||||
})
|
||||
require.Nil(t, appErr)
|
||||
|
||||
// After check
|
||||
for _, tc := range ttcc {
|
||||
actualMetric, err := tc.histogramVec.GetMetricWith(prometheus.Labels{"platform": platform})
|
||||
require.NoError(t, err)
|
||||
require.NoError(t, actualMetric.(prometheus.Histogram).Write(m))
|
||||
require.Equal(t, uint64(1), m.Histogram.GetSampleCount())
|
||||
require.InDelta(t, tc.elapsed/1000, m.Histogram.GetSampleSum(), 0.001, "not equal value in %s", tc.name)
|
||||
}
|
||||
}
|
||||
}
|
||||
Ссылка в новой задаче
Block a user