-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathproviders.go
147 lines (120 loc) · 4.86 KB
/
providers.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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
package vshard_router //nolint:revive
import (
"context"
"fmt"
"log"
"log/slog"
"time"
)
var (
emptyMetricsProvider MetricsProvider = (*EmptyMetrics)(nil)
emptyLogfProvider LogfProvider = emptyLogger{}
// Ensure StdoutLoggerf implements LogfProvider
_ LogfProvider = StdoutLoggerf{}
// Ensure SlogLoggerf implements LogfProvider
_ LogfProvider = &SlogLoggerf{}
)
// LogfProvider an interface to inject a custom logger.
type LogfProvider interface {
Debugf(ctx context.Context, format string, v ...any)
Infof(ctx context.Context, format string, v ...any)
Warnf(ctx context.Context, format string, v ...any)
Errorf(ctx context.Context, format string, v ...any)
}
type emptyLogger struct{}
func (e emptyLogger) Debugf(_ context.Context, _ string, _ ...any) {}
func (e emptyLogger) Infof(_ context.Context, _ string, _ ...any) {}
func (e emptyLogger) Warnf(_ context.Context, _ string, _ ...any) {}
func (e emptyLogger) Errorf(_ context.Context, _ string, _ ...any) {}
// StdoutLogLevel is a type to control log level for StdoutLoggerf.
type StdoutLogLevel int
const (
// StdoutLogDefault is equal to default value of StdoutLogLevel. Acts like StdoutLogInfo.
StdoutLogDefault StdoutLogLevel = iota
// StdoutLogDebug enables debug or higher level logs for StdoutLoggerf
StdoutLogDebug
// StdoutLogInfo enables only info or higher level logs for StdoutLoggerf
StdoutLogInfo
// StdoutLogWarn enables only warn or higher level logs for StdoutLoggerf
StdoutLogWarn
// StdoutLogError enables error level logs for StdoutLoggerf
StdoutLogError
)
// StdoutLoggerf a logger that prints into stderr
type StdoutLoggerf struct {
// LogLevel controls log level to print, see StdoutLogLevel constants for details.
LogLevel StdoutLogLevel
}
func (s StdoutLoggerf) printLevel(level StdoutLogLevel, prefix string, format string, v ...any) {
var currentLogLevel = s.LogLevel
if currentLogLevel == StdoutLogDefault {
currentLogLevel = StdoutLogInfo
}
if level >= currentLogLevel {
log.Printf(prefix+format, v...)
}
}
// Debugf implements Debugf method for LogfProvider interface
func (s StdoutLoggerf) Debugf(_ context.Context, format string, v ...any) {
s.printLevel(StdoutLogDebug, "[DEBUG] ", format, v...)
}
// Infof implements Infof method for LogfProvider interface
func (s StdoutLoggerf) Infof(_ context.Context, format string, v ...any) {
s.printLevel(StdoutLogInfo, "[INFO] ", format, v...)
}
// Warnf implements Warnf method for LogfProvider interface
func (s StdoutLoggerf) Warnf(_ context.Context, format string, v ...any) {
s.printLevel(StdoutLogWarn, "[WARN] ", format, v...)
}
// Errorf implements Errorf method for LogfProvider interface
func (s StdoutLoggerf) Errorf(_ context.Context, format string, v ...any) {
s.printLevel(StdoutLogError, "[ERROR] ", format, v...)
}
// NewSlogLogger wraps slog logger
func NewSlogLogger(logger *slog.Logger) LogfProvider {
return &SlogLoggerf{
Logger: logger,
}
}
// SlogLoggerf is adapter for slog to Logger interface.
type SlogLoggerf struct {
Logger *slog.Logger
}
// Debugf implements Debugf method for LogfProvider interface
func (s *SlogLoggerf) Debugf(ctx context.Context, format string, v ...any) {
s.Logger.DebugContext(ctx, fmt.Sprintf(format, v...))
}
// Infof implements Infof method for LogfProvider interface
func (s SlogLoggerf) Infof(ctx context.Context, format string, v ...any) {
s.Logger.InfoContext(ctx, fmt.Sprintf(format, v...))
}
// Warnf implements Warnf method for LogfProvider interface
func (s SlogLoggerf) Warnf(ctx context.Context, format string, v ...any) {
s.Logger.WarnContext(ctx, fmt.Sprintf(format, v...))
}
// Errorf implements Errorf method for LogfProvider interface
func (s SlogLoggerf) Errorf(ctx context.Context, format string, v ...any) {
s.Logger.ErrorContext(ctx, fmt.Sprintf(format, v...))
}
// Metrics
// MetricsProvider is an interface for passing library metrics to your prometheus/graphite and other metrics
type MetricsProvider interface {
CronDiscoveryEvent(ok bool, duration time.Duration, reason string)
RetryOnCall(reason string)
RequestDuration(duration time.Duration, ok bool, mapReduce bool)
}
// EmptyMetrics is default empty metrics provider
// you can embed this type and realize just some metrics
type EmptyMetrics struct{}
func (e *EmptyMetrics) CronDiscoveryEvent(_ bool, _ time.Duration, _ string) {}
func (e *EmptyMetrics) RetryOnCall(_ string) {}
func (e *EmptyMetrics) RequestDuration(_ time.Duration, _ bool, _ bool) {}
// TopologyProvider is external module that can lookup current topology of cluster
// it might be etcd/config/consul or smth else
type TopologyProvider interface {
// Init should create the current topology at the beginning
// and change the state during the process of changing the point of receiving the cluster configuration
Init(t TopologyController) error
// Close closes all connections if the provider created them
Close()
}