Skip to content

Commit

Permalink
Use struct for perf_schema_memory_events
Browse files Browse the repository at this point in the history
  • Loading branch information
marctc committed Oct 5, 2023
1 parent 98e6c6b commit f7a1837
Showing 1 changed file with 13 additions and 11 deletions.
24 changes: 13 additions & 11 deletions collector/perf_schema_memory_events.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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 {
Expand All @@ -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
Expand All @@ -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,
)
Expand Down

0 comments on commit f7a1837

Please sign in to comment.