-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.go
186 lines (152 loc) · 4.55 KB
/
config.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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
package kineticaotelexporter // import
import (
"errors"
"fmt"
"net/url"
"os"
"strconv"
"strings"
"go.opentelemetry.io/collector/component"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
"gopkg.in/natefinch/lumberjack.v2"
"gopkg.in/yaml.v3"
)
const (
// The value of "type" key in configuration.
typeStr = "kinetica"
// The stability level of the exporter.
stability = component.StabilityLevelAlpha
maxSize = 10
maxBackups = 5
maxAge = 10
)
var (
kineticaLogger *zap.Logger
)
// Config defines configuration for the Kinetica exporter.
type Config struct {
Host string `mapstructure:"host"`
Schema string `mapstructure:"schema"`
Username string `mapstructure:"username"`
Password string `mapstructure:"password"`
BypassSslCertCheck bool `mapstructure:"bypasssslcertcheck"`
}
// Validate the config
//
// @receiver cfg
// @return error
func (cfg *Config) Validate() error {
kineticaHost, err := url.ParseRequestURI(cfg.Host)
if err != nil {
return err
}
if kineticaHost.Scheme != "http" && kineticaHost.Scheme != "https" {
return errors.New("Protocol must be either `http` or `https`")
}
return nil
}
func parseNumber(s string, fallback int) int {
v, err := strconv.Atoi(s)
if err == nil {
return v
}
return fallback
}
func (cfg *Config) createLogger() *zap.Logger {
if kineticaLogger != nil {
return kineticaLogger
}
var logConfig zap.Config
yamlFile, _ := os.ReadFile("./config_log_zap.yaml")
if err := yaml.Unmarshal(yamlFile, &logConfig); err != nil {
fmt.Println("Error in loading log config file ...")
fmt.Println(err)
kineticaLogger = cfg.createDefaultLogger()
return kineticaLogger
}
fmt.Println("Log Config from Yaml file : ", logConfig)
var (
stdout zapcore.WriteSyncer
file zapcore.WriteSyncer
logFilePath *url.URL
)
for _, path := range logConfig.OutputPaths {
if path == "stdout" || path == "stderror" {
stdout = zapcore.AddSync(os.Stdout)
fmt.Println("Created stdout syncer ...")
} else if strings.HasPrefix(path, "lumberjack://") {
var err error
logFilePath, err = url.Parse(path)
fmt.Println("LogFilePath : ", logFilePath)
if err == nil {
filename := strings.TrimLeft(logFilePath.Path, "/")
fmt.Println("LogFileName : ", filename)
if filename != "" {
q := logFilePath.Query()
l := &lumberjack.Logger{
Filename: filename,
MaxSize: parseNumber(q.Get("maxSize"), maxSize),
MaxAge: parseNumber(q.Get("maxAge"), maxAge),
MaxBackups: parseNumber(q.Get("maxBackups"), maxBackups),
LocalTime: false,
Compress: false,
}
file = zapcore.AddSync(l)
fmt.Println("Created file syncer ...")
}
}
} else {
// Unknown output format
fmt.Println("Invalid output path specified in config ...")
}
}
if stdout == nil && file == nil {
fmt.Println("Both stdout and file not available; creating default logger ...")
return cfg.createDefaultLogger()
}
level := zap.NewAtomicLevelAt(logConfig.Level.Level())
consoleEncoder := zapcore.NewConsoleEncoder(logConfig.EncoderConfig)
fileEncoder := zapcore.NewJSONEncoder(logConfig.EncoderConfig)
core := zapcore.NewTee(
zapcore.NewCore(consoleEncoder, stdout, level),
zapcore.NewCore(fileEncoder, file, level),
)
kineticaLogger = zap.New(core)
return kineticaLogger
}
// createDefaultLogger
//
// @receiver cfg
// @return *zap.Logger
func (cfg *Config) createDefaultLogger() *zap.Logger {
stdout := zapcore.AddSync(os.Stdout)
file := zapcore.AddSync(&lumberjack.Logger{
Filename: "./logs/kinetica-exporter.log",
MaxSize: maxSize, // megabytes
MaxBackups: maxBackups,
MaxAge: maxAge, // days
})
level := zap.NewAtomicLevelAt(zap.InfoLevel)
productionCfg := zap.NewProductionEncoderConfig()
productionCfg.NameKey = "logger"
productionCfg.TimeKey = "ts"
productionCfg.MessageKey = "msg"
productionCfg.StacktraceKey = "stacktrace"
productionCfg.EncodeTime = zapcore.ISO8601TimeEncoder
developmentCfg := zap.NewDevelopmentEncoderConfig()
developmentCfg.NameKey = "logger"
developmentCfg.TimeKey = "ts"
developmentCfg.MessageKey = "msg"
developmentCfg.StacktraceKey = "stacktrace"
developmentCfg.EncodeTime = zapcore.ISO8601TimeEncoder
developmentCfg.EncodeLevel = zapcore.CapitalColorLevelEncoder
consoleEncoder := zapcore.NewConsoleEncoder(developmentCfg)
fileEncoder := zapcore.NewJSONEncoder(productionCfg)
core := zapcore.NewTee(
zapcore.NewCore(consoleEncoder, stdout, level),
zapcore.NewCore(fileEncoder, file, level),
)
return zap.New(core)
}
var _ component.Config = (*Config)(nil)