MCPcopy
hub / github.com/prometheus/client_golang / NewMetricWithExemplars

Function NewMetricWithExemplars

prometheus/metric.go:244–266  ·  view source on GitHub ↗

NewMetricWithExemplars returns a new Metric wrapping the provided Metric with given exemplars. Exemplars are validated. Only last applicable exemplar is injected from the list. For example for Counter it means last exemplar is injected. For Histogram, it means last applicable exemplar for each buck

(m Metric, exemplars ...Exemplar)

Source from the content-addressed store, hash-verified

242// NewMetricWithExemplars works best with MustNewConstMetric and
243// MustNewConstHistogram, see example.
244func NewMetricWithExemplars(m Metric, exemplars ...Exemplar) (Metric, error) {
245 if len(exemplars) == 0 {
246 return nil, errors.New("no exemplar was passed for NewMetricWithExemplars")
247 }
248
249 var (
250 now = time.Now()
251 exs = make([]*dto.Exemplar, len(exemplars))
252 err error
253 )
254 for i, e := range exemplars {
255 ts := e.Timestamp
256 if ts.IsZero() {
257 ts = now
258 }
259 exs[i], err = newExemplar(e.Value, ts, e.Labels)
260 if err != nil {
261 return nil, err
262 }
263 }
264
265 return &withExemplarsMetric{Metric: m, exemplars: exs}, nil
266}
267
268// MustNewMetricWithExemplars is a version of NewMetricWithExemplars that panics where
269// NewMetricWithExemplars would have returned an error.

Calls 1

newExemplarFunction · 0.85

Tested by 1