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)
| 242 | // NewMetricWithExemplars works best with MustNewConstMetric and |
| 243 | // MustNewConstHistogram, see example. |
| 244 | func 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. |