WithContext returns a new context with a metrics recorder.
(ctx context.Context, name, prefix string)
| 28 | |
| 29 | // WithContext returns a new context with a metrics recorder. |
| 30 | func WithContext(ctx context.Context, name, prefix string) (context.Context, error) { |
| 31 | pm := NewPromMetrics(prometheus.DefaultRegisterer, name, prefix) |
| 32 | if pm == nil { |
| 33 | return nil, errors.New("failed to create prometheus metrics") |
| 34 | } |
| 35 | |
| 36 | return context.WithValue(ctx, ctxKey{}, pm), nil |
| 37 | } |
| 38 | |
| 39 | // FromContext returns the metrics recorder from the context. |
| 40 | func FromContext(ctx context.Context) Metrics { |