aboutsummaryrefslogtreecommitdiffstats
path: root/library/go/core/metrics/prometheus/stream.go
blob: 44b29630367e05c12e41d8307ce89833c0d9d27b (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
package prometheus

import (
	"context"
	"fmt"
	"io"

	"github.com/prometheus/common/expfmt"
)

const (
	StreamText    expfmt.Format = expfmt.FmtText
	StreamCompact expfmt.Format = expfmt.FmtProtoCompact
)

func (r *Registry) Stream(_ context.Context, w io.Writer) (int, error) {
	metrics, err := r.Gather()
	if err != nil {
		return 0, fmt.Errorf("cannot gather metrics: %w", err)
	}

	enc := expfmt.NewEncoder(w, r.streamFormat)
	for _, mf := range metrics {
		if err := enc.Encode(mf); err != nil {
			return 0, fmt.Errorf("cannot encode metric family: %w", err)
		}
	}

	// prometheus encoder does not report how much bytes have been written
	// so we indicate it by returning -1 instead
	return -1, nil
}