-
Notifications
You must be signed in to change notification settings - Fork 9
/
publisher.go
48 lines (40 loc) · 1.31 KB
/
publisher.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
41
42
43
44
45
46
47
48
package experiment
import (
"context"
"log"
)
// Logger represents the interface that experiment expects for a logger.
type Logger interface {
Printf(string, ...interface{})
}
// Publisher represents an interface that allows you to publish results.
type Publisher[C any] interface {
Publish(context.Context, Observation[C]) error
}
// NewLogPublisher returns a new LogPublisher.
func NewLogPublisher[C any](name string, logger Logger) *LogPublisher[C] {
return &LogPublisher[C]{
Name: name,
Logger: logger,
}
}
// LogPublisher is a publisher that writes out the observation values as a log
// line. If no Logger is provided, the standard library logger will be used.
type LogPublisher[C any] struct {
Name string
Logger Logger
}
// Publish will publish all the Observation variables as a log line. It is in
// the following format:
// [Experiment Observation] name=%s duration=%s success=%t value=%v error=%v
func (l *LogPublisher[C]) Publish(_ context.Context, o Observation[C]) error {
msg := "[Experiment Observation: %s] name=%s duration=%s success=%t value=%v error=%v"
args := []interface{}{l.Name, o.Name, o.Duration, o.Success, o.CleanValue, o.Error}
if l.Logger == nil {
log.Printf(msg, args...)
} else {
l.Logger.Printf(msg, args...)
}
return nil
}
var _ Publisher[string] = &LogPublisher[string]{}