Skip to content

Commit

Permalink
PMM-9919 Add support for meta metrics
Browse files Browse the repository at this point in the history
Signed-off-by: Shashank Sinha <[email protected]>
  • Loading branch information
ShashankSinha252 committed Jun 30, 2022
1 parent 4ad265f commit d576f06
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 1 deletion.
63 changes: 63 additions & 0 deletions prometheus/collector_metric.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
// Copyright 2022 Percona
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package prometheus

var (
descScrapeTime *Desc
metricCache chan Metric
)

type MetaMetrics struct {
scrapeTime *Desc
metricCache chan Metric
}

func NewMetaMetricsCollector() Collector {
descScrapeTime = NewDesc(
"collector_scrape_time_ms",
"Time taken for scrape by collector",
[]string{"exporter", "collector"},
nil)

return &MetaMetrics{
scrapeTime: descScrapeTime,
metricCache: makeMetricsCache(),
}
}

func makeMetricsCache() chan Metric {
// TODO : Use a more appropriate collector count
metricCache = make(chan Metric, 100)
return metricCache
}

func GetScrapeDescripter() *Desc {
return descScrapeTime
}

func PushMetaMetrics(m Metric) {
metricCache <- m
}

func (c *MetaMetrics) Describe(ch chan<- *Desc) {
ch <- c.scrapeTime
}

func (c *MetaMetrics) Collect(ch chan<- Metric) {
close(c.metricCache)
for m := range c.metricCache {
ch <- m
}
c.metricCache = makeMetricsCache()
}
11 changes: 10 additions & 1 deletion prometheus/registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ var (
func init() {
MustRegister(NewProcessCollector(ProcessCollectorOpts{}))
MustRegister(NewGoCollector())
MustRegister(NewMetaMetricsCollector())
}

// NewRegistry creates a new vanilla Registry without any Collectors
Expand Down Expand Up @@ -446,11 +447,16 @@ func (r *Registry) Gather() ([]*dto.MetricFamily, error) {

wg.Add(goroutineBudget)

var metaMetricCollector Collector
collectWorker := func() {
for {
select {
case collector := <-checkedCollectors:
collector.Collect(checkedMetricChan)
if _, ok := collector.(*MetaMetrics); ok {
metaMetricCollector = collector
} else {
collector.Collect(checkedMetricChan)
}
case collector := <-uncheckedCollectors:
collector.Collect(uncheckedMetricChan)
default:
Expand All @@ -468,6 +474,9 @@ func (r *Registry) Gather() ([]*dto.MetricFamily, error) {
// are collected.
go func() {
wg.Wait()
if metaMetricCollector != nil {
metaMetricCollector.Collect(checkedMetricChan)
}
close(checkedMetricChan)
close(uncheckedMetricChan)
}()
Expand Down

0 comments on commit d576f06

Please sign in to comment.