Revert "K8s: Refactor metrics to share k8s registry (#77957)" (#79079)

This reverts commit 38bc41651a.
This commit is contained in:
Todd Treece
2023-12-05 09:34:07 -05:00
committed by GitHub
parent c30098e772
commit 7e2aad1d01
13 changed files with 39 additions and 197 deletions
+9 -9
View File
@@ -19,7 +19,7 @@ type FrontendMetricsRecorder func(event FrontendMetricEvent)
// FrontendMetrics contains all the valid frontend metrics and a handler function for recording events
var FrontendMetrics map[string]FrontendMetricsRecorder = map[string]FrontendMetricsRecorder{}
func registerFrontendHistogram(reg prometheus.Registerer, name string, help string) {
func registerFrontendHistogram(name string, help string) {
defBuckets := []float64{.1, .25, .5, 1, 1.5, 2, 5, 10, 20, 40}
histogram := prometheus.NewHistogram(prometheus.HistogramOpts{
@@ -33,14 +33,14 @@ func registerFrontendHistogram(reg prometheus.Registerer, name string, help stri
histogram.Observe(event.Value)
}
reg.MustRegister(histogram)
prometheus.MustRegister(histogram)
}
func initFrontendMetrics(r prometheus.Registerer) {
registerFrontendHistogram(r, "frontend_boot_load_time_seconds", "Frontend boot time measurement")
registerFrontendHistogram(r, "frontend_boot_first_paint_time_seconds", "Frontend boot first paint")
registerFrontendHistogram(r, "frontend_boot_first_contentful_paint_time_seconds", "Frontend boot first contentful paint")
registerFrontendHistogram(r, "frontend_boot_js_done_time_seconds", "Frontend boot initial js load")
registerFrontendHistogram(r, "frontend_boot_css_time_seconds", "Frontend boot initial css load")
registerFrontendHistogram(r, "frontend_plugins_preload_ms", "Frontend preload plugin time measurement")
func initFrontendMetrics() {
registerFrontendHistogram("frontend_boot_load_time_seconds", "Frontend boot time measurement")
registerFrontendHistogram("frontend_boot_first_paint_time_seconds", "Frontend boot first paint")
registerFrontendHistogram("frontend_boot_first_contentful_paint_time_seconds", "Frontend boot first contentful paint")
registerFrontendHistogram("frontend_boot_js_done_time_seconds", "Frontend boot initial js load")
registerFrontendHistogram("frontend_boot_css_time_seconds", "Frontend boot initial css load")
registerFrontendHistogram("frontend_plugins_preload_ms", "Frontend preload plugin time measurement")
}
+6 -6
View File
@@ -613,7 +613,7 @@ func init() {
}
// SetBuildInformation sets the build information for this binary
func SetBuildInformation(reg prometheus.Registerer, version, revision, branch string, buildTimestamp int64) {
func SetBuildInformation(version, revision, branch string, buildTimestamp int64) {
edition := "oss"
if setting.IsEnterprise {
edition = "enterprise"
@@ -631,7 +631,7 @@ func SetBuildInformation(reg prometheus.Registerer, version, revision, branch st
Namespace: ExporterName,
}, []string{"version", "revision", "branch", "goversion", "edition"})
reg.MustRegister(grafanaBuildVersion, grafanaBuildTimestamp)
prometheus.MustRegister(grafanaBuildVersion, grafanaBuildTimestamp)
grafanaBuildVersion.WithLabelValues(version, revision, branch, runtime.Version(), edition).Set(1)
grafanaBuildTimestamp.WithLabelValues(version, revision, branch, runtime.Version(), edition).Set(float64(buildTimestamp))
@@ -639,7 +639,7 @@ func SetBuildInformation(reg prometheus.Registerer, version, revision, branch st
// SetEnvironmentInformation exposes environment values provided by the operators as an `_info` metric.
// If there are no environment metrics labels configured, this metric will not be exposed.
func SetEnvironmentInformation(reg prometheus.Registerer, labels map[string]string) error {
func SetEnvironmentInformation(labels map[string]string) error {
if len(labels) == 0 {
return nil
}
@@ -651,7 +651,7 @@ func SetEnvironmentInformation(reg prometheus.Registerer, labels map[string]stri
ConstLabels: labels,
})
reg.MustRegister(grafanaEnvironmentInfo)
prometheus.MustRegister(grafanaEnvironmentInfo)
grafanaEnvironmentInfo.Set(1)
return nil
@@ -661,8 +661,8 @@ func SetPluginBuildInformation(pluginID, pluginType, version, signatureStatus st
grafanaPluginBuildInfoDesc.WithLabelValues(pluginID, pluginType, version, signatureStatus).Set(1)
}
func initMetricVars(reg prometheus.Registerer) {
reg.MustRegister(
func initMetricVars() {
prometheus.MustRegister(
MInstanceStart,
MPageStatus,
MApiStatus,
+8 -73
View File
@@ -2,17 +2,11 @@ package metrics
import (
"context"
"errors"
"regexp"
"github.com/prometheus/client_golang/prometheus"
dto "github.com/prometheus/client_model/go"
"k8s.io/component-base/metrics/legacyregistry"
"github.com/grafana/grafana/pkg/infra/log"
"github.com/grafana/grafana/pkg/infra/metrics/graphitebridge"
"github.com/grafana/grafana/pkg/services/featuremgmt"
"github.com/grafana/grafana/pkg/setting"
"github.com/prometheus/client_golang/prometheus"
)
var metricsLogger log.Logger = log.New("metrics")
@@ -25,10 +19,12 @@ func (lw *logWrapper) Println(v ...any) {
lw.logger.Info("graphite metric bridge", v...)
}
func ProvideService(cfg *setting.Cfg, reg prometheus.Registerer) (*InternalMetricsService, error) {
initMetricVars(reg)
initFrontendMetrics(reg)
func init() {
initMetricVars()
initFrontendMetrics()
}
func ProvideService(cfg *setting.Cfg) (*InternalMetricsService, error) {
s := &InternalMetricsService{
Cfg: cfg,
}
@@ -59,66 +55,5 @@ func (im *InternalMetricsService) Run(ctx context.Context) error {
return ctx.Err()
}
func ProvideRegisterer(cfg *setting.Cfg) prometheus.Registerer {
if cfg.IsFeatureToggleEnabled(featuremgmt.FlagGrafanaAPIServer) {
return legacyregistry.Registerer()
}
return prometheus.DefaultRegisterer
}
func ProvideGatherer(cfg *setting.Cfg) prometheus.Gatherer {
if cfg.IsFeatureToggleEnabled(featuremgmt.FlagGrafanaAPIServer) {
return newAddPrefixWrapper(legacyregistry.DefaultGatherer)
}
return prometheus.DefaultGatherer
}
func ProvideRegistererForTest() prometheus.Registerer {
return prometheus.NewRegistry()
}
func ProvideGathererForTest(reg prometheus.Registerer) prometheus.Gatherer {
// the registerer provided by ProvideRegistererForTest
// is a *prometheus.Registry, so it also implements prometheus.Gatherer
return reg.(*prometheus.Registry)
}
var _ prometheus.Gatherer = (*addPrefixWrapper)(nil)
// addPrefixWrapper wraps a prometheus.Gatherer, and ensures that all metric names are prefixed with `grafana_`.
// metrics with the prefix `grafana_` or `go_` are not modified.
type addPrefixWrapper struct {
orig prometheus.Gatherer
reg *regexp.Regexp
}
func newAddPrefixWrapper(orig prometheus.Gatherer) *addPrefixWrapper {
return &addPrefixWrapper{
orig: orig,
reg: regexp.MustCompile("^((?:grafana_|go_).*)"),
}
}
func (g *addPrefixWrapper) Gather() ([]*dto.MetricFamily, error) {
mf, err := g.orig.Gather()
if err != nil {
return nil, err
}
names := make(map[string]struct{})
for i := 0; i < len(mf); i++ {
m := mf[i]
if m.Name != nil && !g.reg.MatchString(*m.Name) {
*m.Name = "grafana_" + *m.Name
// since we are modifying the name, we need to check for duplicates in the gatherer
if _, exists := names[*m.Name]; exists {
return nil, errors.New("duplicate metric name: " + *m.Name)
}
}
// keep track of names to detect duplicates
names[*m.Name] = struct{}{}
}
return mf, nil
}
func ProvideRegisterer() prometheus.Registerer { return prometheus.DefaultRegisterer }
func ProvideRegistererForTest() prometheus.Registerer { return prometheus.NewRegistry() }
-61
View File
@@ -1,61 +0,0 @@
package metrics
import (
"testing"
dto "github.com/prometheus/client_model/go"
"github.com/stretchr/testify/require"
)
func TestK8sGathererWrapper_Gather(t *testing.T) {
orig := &mockGatherer{}
g := newAddPrefixWrapper(orig)
t.Run("metrics with grafana and go prefix are not modified", func(t *testing.T) {
originalMF := []*dto.MetricFamily{
{Name: strptr("grafana_metric1")},
{Name: strptr("metric2")},
{Name: strptr("go_metric1")},
}
orig.GatherFunc = func() ([]*dto.MetricFamily, error) {
return originalMF, nil
}
expectedMF := []*dto.MetricFamily{
{Name: strptr("grafana_metric1")},
{Name: strptr("grafana_metric2")},
{Name: strptr("go_metric1")},
}
mf, err := g.Gather()
require.NoError(t, err)
require.Equal(t, expectedMF, mf)
})
t.Run("duplicate metrics result in an error", func(t *testing.T) {
originalMF := []*dto.MetricFamily{
{Name: strptr("grafana_metric1")},
{Name: strptr("metric1")},
}
orig.GatherFunc = func() ([]*dto.MetricFamily, error) {
return originalMF, nil
}
_, err := g.Gather()
require.Error(t, err)
})
}
type mockGatherer struct {
GatherFunc func() ([]*dto.MetricFamily, error)
}
func (m *mockGatherer) Gather() ([]*dto.MetricFamily, error) {
return m.GatherFunc()
}
func strptr(s string) *string {
return &s
}
-17
View File
@@ -1,17 +0,0 @@
package metrics
import (
"github.com/google/wire"
)
var WireSet = wire.NewSet(
ProvideService,
ProvideRegisterer,
ProvideGatherer,
)
var WireSetForTest = wire.NewSet(
ProvideService,
ProvideRegistererForTest,
ProvideGathererForTest,
)