-
Notifications
You must be signed in to change notification settings - Fork 0
/
metrics.go
40 lines (33 loc) · 970 Bytes
/
metrics.go
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
33
34
35
36
37
38
39
40
package stamets
import (
"time"
)
// BaseMetrics provides a foundation for other metrics to build upon.
// Basic information include the duration to perform a task, whether
// the task produced an error, and the result produced by the task.
type BaseMetrics[T any] struct {
// Time it took to perform task
time.Duration
// Metrics produced error
err error
Payload T
}
type Metrics interface {
UnpackAny() (any, error)
Ok() bool
}
// Unpack extracts the payload wrapped in the metrics,
// and an error, if one was produced.
func (m BaseMetrics[T]) Unpack() (T, error) {
return m.Payload, m.err
}
// UnpackAny extracts the payload wrapped in the metrics,
// and an error, if one was produced. UnpackAny allows
// the implementation of the Metrics interface.
func (m BaseMetrics[T]) UnpackAny() (any, error) {
return m.Payload, m.err
}
// Ok checks whether the desired function failed to execute.
func (m BaseMetrics[T]) Ok() bool {
return m.err == nil
}