From f7a18370413df34fc951eab26d9e8208b8c8925e Mon Sep 17 00:00:00 2001 From: Marc Tuduri Date: Thu, 5 Oct 2023 12:05:03 +0200 Subject: [PATCH] Use struct for perf_schema_memory_events --- collector/perf_schema_memory_events.go | 24 +++++++++++++----------- 1 file changed, 13 insertions(+), 11 deletions(-) diff --git a/collector/perf_schema_memory_events.go b/collector/perf_schema_memory_events.go index 288ecdd6..959be576 100644 --- a/collector/perf_schema_memory_events.go +++ b/collector/perf_schema_memory_events.go @@ -33,14 +33,6 @@ const perfMemoryEventsQuery = ` where COUNT_ALLOC > 0; ` -// Tunable flags. -var ( - performanceSchemaMemoryEventsRemovePrefix = kingpin.Flag( - "collect.perf_schema.memory_events.remove_prefix", - "Remove instrument prefix in performance_schema.memory_summary_global_by_event_name", - ).Default("memory/").String() -) - // Metric descriptors. var ( performanceSchemaMemoryBytesAllocDesc = prometheus.NewDesc( @@ -61,7 +53,9 @@ var ( ) // ScrapePerfMemoryEvents collects from `performance_schema.memory_summary_global_by_event_name`. -type ScrapePerfMemoryEvents struct{} +type ScrapePerfMemoryEvents struct { + RemovePrefix string +} // Name of the Scraper. Should be unique. func (ScrapePerfMemoryEvents) Name() string { @@ -78,8 +72,16 @@ func (ScrapePerfMemoryEvents) Version() float64 { return 5.7 } +// RegisterFlags adds flags to configure the Scraper. +func (s *ScrapePerfMemoryEvents) RegisterFlags(application *kingpin.Application) { + application.Flag( + "collect.perf_schema.memory_events.remove_prefix", + "Remove instrument prefix in performance_schema.memory_summary_global_by_event_name", + ).Default("memory/").StringVar(&s.RemovePrefix) +} + // Scrape collects data from database connection and sends it over channel as prometheus metric. -func (ScrapePerfMemoryEvents) Scrape(ctx context.Context, db *sql.DB, ch chan<- prometheus.Metric, logger log.Logger) error { +func (s ScrapePerfMemoryEvents) Scrape(ctx context.Context, db *sql.DB, ch chan<- prometheus.Metric, logger log.Logger) error { perfSchemaMemoryEventsRows, err := db.QueryContext(ctx, perfMemoryEventsQuery) if err != nil { return err @@ -100,7 +102,7 @@ func (ScrapePerfMemoryEvents) Scrape(ctx context.Context, db *sql.DB, ch chan<- return err } - eventName := strings.TrimPrefix(eventName, *performanceSchemaMemoryEventsRemovePrefix) + eventName := strings.TrimPrefix(eventName, s.RemovePrefix) ch <- prometheus.MustNewConstMetric( performanceSchemaMemoryBytesAllocDesc, prometheus.CounterValue, float64(bytesAlloc), eventName, )