From 3e46e465be3072843ccd891f65ec1e28368c0e5d Mon Sep 17 00:00:00 2001 From: Artem Gavrilov Date: Wed, 1 Nov 2023 20:09:33 +0200 Subject: [PATCH] PMM-12460 Add dump metrics --- managed/cmd/pmm-managed/main.go | 2 + managed/services/dump/metrics.go | 89 ++++++++++++++++++++++++++++++++ 2 files changed, 91 insertions(+) create mode 100644 managed/services/dump/metrics.go diff --git a/managed/cmd/pmm-managed/main.go b/managed/cmd/pmm-managed/main.go index f2aa33e91a..0e3b7616f1 100644 --- a/managed/cmd/pmm-managed/main.go +++ b/managed/cmd/pmm-managed/main.go @@ -961,6 +961,8 @@ func main() { //nolint:cyclop,maintidx emailer := alertmanager.NewEmailer(logrus.WithField("component", "alertmanager-emailer").Logger) dumpService := dump.New(db) + dumpMetricsCollector := dump.NewMetricsCollector(db) + prom.Register(dumpMetricsCollector) kubeStorage := managementdbaas.NewKubeStorage(db) diff --git a/managed/services/dump/metrics.go b/managed/services/dump/metrics.go new file mode 100644 index 0000000000..c92338291d --- /dev/null +++ b/managed/services/dump/metrics.go @@ -0,0 +1,89 @@ +// Copyright (C) 2023 Percona LLC +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU Affero General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU Affero General Public License for more details. +// +// You should have received a copy of the GNU Affero General Public License +// along with this program. If not, see . + +package dump + +import ( + "context" + "time" + + "github.com/pkg/errors" + prom "github.com/prometheus/client_golang/prometheus" + "github.com/sirupsen/logrus" + "gopkg.in/reform.v1" + + "github.com/percona/pmm/managed/models" +) + +const ( + requestTimeout = 3 * time.Second + dumpExists float64 = 1 + prometheusNamespace = "pmm_managed" + prometheusSubsystem = "dumps" +) + +type MetricsCollector struct { + db *reform.DB + l *logrus.Entry + + mDumpsDesc *prom.Desc +} + +func NewMetricsCollector(db *reform.DB) *MetricsCollector { + return &MetricsCollector{ + db: db, + l: logrus.WithField("component", "dumps/metrics"), + mDumpsDesc: prom.NewDesc( + prom.BuildFQName(prometheusNamespace, prometheusSubsystem, "dumps"), + "Dumps", + []string{ + "dump_id", "status", + }, + nil), + } +} + +func (c *MetricsCollector) Describe(ch chan<- *prom.Desc) { + ch <- c.mDumpsDesc +} + +func (c *MetricsCollector) Collect(ch chan<- prom.Metric) { + ctx, cancelCtx := context.WithTimeout(context.Background(), requestTimeout) + defer cancelCtx() + + var dumps []*models.Dump + errTx := c.db.InTransactionContext(ctx, nil, func(t *reform.TX) error { + var err error + dumps, err = models.FindDumps(t.Querier, models.DumpFilters{}) + if err != nil { + return errors.WithStack(err) + } + + return nil + }) + if errTx != nil { + c.l.Warnf("Failed to get dumps: %v", errTx) + return + } + + for _, dump := range dumps { + ch <- prom.MustNewConstMetric( + c.mDumpsDesc, + prom.GaugeValue, + dumpExists, + dump.ID, + string(dump.Status)) + } +}