<commit_before>package xprometheus

import (
	"encoding/json"
	"expvar"
	"fmt"
	"strconv"

	"github.com/bradfitz/iter"
	"github.com/prometheus/client_golang/prometheus"
)

// A Prometheus collector that exposes all vars.
type expvarCollector struct {
	descs map[int]*prometheus.Desc
}

func NewExpvarCollector() expvarCollector {
	return expvarCollector{
		descs: make(map[int]*prometheus.Desc),
	}
}

const (
	fqName = "go_expvar"
	help   = "All expvars"
)

var desc = prometheus.NewDesc(fqName, help, nil, nil)

// Describe implements Collector.
func (e expvarCollector) Describe(ch chan<- *prometheus.Desc) {
	ch <- desc
}

// Collect implements Collector.
func (e expvarCollector) Collect(ch chan<- prometheus.Metric) {
	expvar.Do(func(kv expvar.KeyValue) {
		collector{
			f: func(m prometheus.Metric) {
				ch <- m
			},
			labelValues: []string{kv.Key},
			descs:       e.descs,
		}.collectVar(kv.Value)
	})
}

func labels(n int) (ls []string) {
	for i := range iter.N(n) {
		ls = append(ls, "key"+strconv.FormatInt(int64(i), 10))
	}
	return
}
<commit_msg>xprometheus: Don't expose memstats<commit_after>package xprometheus

import (
	"encoding/json"
	"expvar"
	"fmt"
	"strconv"

	"github.com/bradfitz/iter"
	"github.com/prometheus/client_golang/prometheus"
)

// A Prometheus collector that exposes all vars.
type expvarCollector struct {
	descs map[int]*prometheus.Desc
}

func NewExpvarCollector() expvarCollector {
	return expvarCollector{
		descs: make(map[int]*prometheus.Desc),
	}
}

const (
	fqName = "go_expvar"
	help   = "All expvars"
)

var desc = prometheus.NewDesc(fqName, help, nil, nil)

// Describe implements Collector.
func (e expvarCollector) Describe(ch chan<- *prometheus.Desc) {
	ch <- desc
}

// Collect implements Collector.
func (e expvarCollector) Collect(ch chan<- prometheus.Metric) {
	expvar.Do(func(kv expvar.KeyValue) {
		// I think this is very noisy, and there seems to be good support for exporting its
		// information in a more structured way.
		if kv.Key == "memstats" {
			return
		}
		collector{
			f: func(m prometheus.Metric) {
				ch <- m
			},
			labelValues: []string{kv.Key},
			descs:       e.descs,
		}.collectVar(kv.Value)
	})
}

func labels(n int) (ls []string) {
	for i := range iter.N(n) {
		ls = append(ls, "key"+strconv.FormatInt(int64(i), 10))
	}
	return
}

{'fraction_non_alphanumeric': 0.13675582398619499, 'fraction_numerical': 0.003451251078515962, 'mean_word_length': 3.892405063291139, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5790990', 'n_tokens_mistral': 953, 'n_tokens_neox': 869, 'n_words': 203}