Skip to content

Add new utility CollectorFunc for simple Collector implementations #1701

@jzelinskie

Description

@jzelinskie

I've used this helper in a few places and wondered if there's interest upstream. Basically, I'm applying a similar pattern to http.Handler/http.HandlerFunc so that you only need to implement one function to implement Collector:

import "github.com/prometheus/client_golang/prometheus"

var _ prometheus.Collector = (CollectorFunc)(nil)

// CollectorFunc is a convenient way to implement a Prometheus Collector
// without interface boilerplate.
//
// This implementation relies on prometheus.DescribeByCollect; familiarize
// yourself with that documentation before using.
type CollectorFunc func(ch chan<- prometheus.Metric)

func (c CollectorFunc) Collect(ch chan<- prometheus.Metric) { c(ch) }
func (c CollectorFunc) Describe(ch chan<- *prometheus.Desc) { prometheus.DescribeByCollect(c, ch) }

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions