Skip to content
This repository was archived by the owner on Nov 7, 2025. It is now read-only.

Commit dc539c6

Browse files
authored
v2 - Named sub logger - logger refactoring (#1124)
This PR adds the ability to inject a named sublogger into the component. The Logger interface is part of V2 API. There are some TODOs, that will be done in the subsequent PRs. - initialize logger - global logger removal (if necessary) - use component names from the config
1 parent 1a78cdf commit dc539c6

21 files changed

+355
-82
lines changed

quesma/backend_connectors/clickhouse_backend_connector.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,3 +96,7 @@ func NewClickHouseBackendConnector(endpoint string) *ClickHouseBackendConnector
9696
Endpoint: endpoint,
9797
}
9898
}
99+
100+
func (p *ClickHouseBackendConnector) InstanceName() string {
101+
return "clickhouse" // TODO add name taken from config
102+
}

quesma/backend_connectors/elasticsearch_backend_connector.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,10 @@ func NewElasticsearchBackendConnector(cfg config.ElasticsearchConfiguration) *El
4141
return conn
4242
}
4343

44+
func (e *ElasticsearchBackendConnector) InstanceName() string {
45+
return "elasticsearch" // TODO return name from config
46+
}
47+
4448
func (e *ElasticsearchBackendConnector) GetConfig() config.ElasticsearchConfiguration {
4549
return e.config
4650
}

quesma/backend_connectors/mysql_backend_connector.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,10 @@ type MySqlBackendConnector struct {
3838
connection *sql.DB
3939
}
4040

41+
func (p *MySqlBackendConnector) InstanceName() string {
42+
return "mysql"
43+
}
44+
4145
func (p *MySqlBackendConnector) GetId() quesma_api.BackendConnectorType {
4246
return quesma_api.MySQLBackend
4347
}

quesma/frontend_connectors/basic_http_frontend_connector.go

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ type BasicHTTPFrontendConnector struct {
2929

3030
phoneHomeClient diag.PhoneHomeClient
3131
debugInfoCollector diag.DebugInfoCollector
32+
logger quesma_api.QuesmaLogger
3233
}
3334

3435
func (h *BasicHTTPFrontendConnector) GetChildComponents() []interface{} {
@@ -47,6 +48,8 @@ func (h *BasicHTTPFrontendConnector) GetChildComponents() []interface{} {
4748
func (h *BasicHTTPFrontendConnector) SetDependencies(deps quesma_api.Dependencies) {
4849
h.phoneHomeClient = deps.PhoneHomeAgent()
4950
h.debugInfoCollector = deps.DebugInfoCollector()
51+
h.logger = deps.Logger()
52+
5053
deps.PhoneHomeAgent().FailedRequestsCollector(func() int64 {
5154
return h.routerInstance.FailedRequests.Load()
5255
})
@@ -66,6 +69,10 @@ func NewBasicHTTPFrontendConnector(endpoint string, config *config.QuesmaConfigu
6669
}
6770
}
6871

72+
func (h *BasicHTTPFrontendConnector) InstanceName() string {
73+
return "BasicHTTPFrontendConnector" // TODO return name from config
74+
}
75+
6976
func (h *BasicHTTPFrontendConnector) AddRouter(router quesma_api.Router) {
7077
h.router = router
7178
}
@@ -100,9 +107,9 @@ func (h *BasicHTTPFrontendConnector) Listen() error {
100107
h.listener.Addr = h.endpoint
101108
h.listener.Handler = h
102109
go func() {
110+
h.logger.Info().Msgf("HTTP server started on %s", h.endpoint)
103111
err := h.listener.ListenAndServe()
104-
// TODO: Handle error
105-
_ = err
112+
h.logger.Error().Err(err).Msg("HTTP server stopped")
106113
}()
107114

108115
return nil

quesma/logger/logger.go

Lines changed: 33 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@ import (
1111
"os"
1212
"quesma/stats/errorstats"
1313
asyncQueryTracing "quesma/tracing"
14-
tracing "quesma_v2/core/tracing"
14+
quesma_v2 "quesma_v2/core"
15+
1516
"time"
1617
)
1718

@@ -39,8 +40,6 @@ const (
3940
bufferSizeChannel = 1024
4041
)
4142

42-
var logger zerolog.Logger
43-
4443
// InitLogger returns channel where log messages will be sent
4544
func InitLogger(cfg Configuration, sig chan os.Signal, doneCh chan struct{}, asyncQueryTraceLogger *asyncQueryTracing.AsyncTraceLogger) <-chan LogWithLevel {
4645
zerolog.TimeFieldFormat = time.RFC3339Nano // without this we don't have milliseconds timestamp precision
@@ -89,20 +88,23 @@ func InitLogger(cfg Configuration, sig chan os.Signal, doneCh chan struct{}, asy
8988
}
9089

9190
multi := zerolog.MultiLevelWriter(logWriters...)
92-
logger = zerolog.New(multi).
91+
l := zerolog.New(multi).
9392
Level(cfg.Level).
9493
With().
9594
Timestamp().
9695
Caller().
9796
Logger()
9897

9998
globalError := errorstats.GlobalErrorHook{}
100-
logger = logger.Hook(&globalError)
99+
l = l.Hook(&globalError)
101100
if asyncQueryTraceLogger != nil {
102-
logger = logger.Hook(asyncQueryTraceLogger)
101+
l = l.Hook(asyncQueryTraceLogger)
103102
}
104103

105-
logger.Info().Msgf("Logger initialized with level %s", cfg.Level)
104+
l.Info().Msgf("Logger initialized with level %s", cfg.Level)
105+
106+
logger = quesma_v2.NewQuesmaLogger(l)
107+
106108
return logChannel
107109
}
108110

@@ -111,7 +113,7 @@ func InitLogger(cfg Configuration, sig chan os.Signal, doneCh chan struct{}, asy
111113
// of the test, and calls to the global logger will start appearing in the console.
112114
// Without it, they don't.
113115
func InitSimpleLoggerForTests() {
114-
logger = zerolog.New(
116+
l := zerolog.New(
115117
zerolog.ConsoleWriter{
116118
Out: os.Stderr,
117119
TimeFormat: time.StampMilli,
@@ -120,14 +122,16 @@ func InitSimpleLoggerForTests() {
120122
With().
121123
Timestamp().
122124
Logger()
125+
126+
logger = quesma_v2.NewQuesmaLogger(l)
123127
}
124128

125129
// InitSimpleLoggerForTestsWarnLevel initializes our global logger (level >= Warn) to the console output.
126130
// Useful e.g. in debugging failing tests: you can call this function at the beginning
127131
// of the test, and calls to the global logger will start appearing in the console.
128132
// Without it, they don't.
129133
func InitSimpleLoggerForTestsWarnLevel() {
130-
logger = zerolog.New(
134+
l := zerolog.New(
131135
zerolog.ConsoleWriter{
132136
Out: os.Stderr,
133137
TimeFormat: time.StampMilli,
@@ -136,6 +140,8 @@ func InitSimpleLoggerForTestsWarnLevel() {
136140
With().
137141
Timestamp().
138142
Logger()
143+
144+
logger = quesma_v2.NewQuesmaLogger(l)
139145
}
140146

141147
var testLoggerInitialized bool
@@ -157,15 +163,17 @@ func InitOnlyChannelLoggerForTests() <-chan LogWithLevel {
157163
logChannel := make(chan LogWithLevel, 50000) // small number like 5 or 10 made entire Quesma totally unresponsive during the few seconds where Kibana spams with messages
158164
chanWriter := channelWriter{ch: logChannel}
159165

160-
logger = zerolog.New(chanWriter).
166+
l := zerolog.New(chanWriter).
161167
Level(zerolog.DebugLevel).
162168
With().
163169
Timestamp().
164170
Caller().
165171
Logger()
166172

167173
globalError := errorstats.GlobalErrorHook{}
168-
logger = logger.Hook(&globalError)
174+
l = l.Hook(&globalError)
175+
176+
logger = quesma_v2.NewQuesmaLogger(l)
169177

170178
testLoggerInitialized = true
171179
return logChannel
@@ -191,86 +199,61 @@ func openLogFiles(logsPath string) {
191199
}
192200
}
193201

194-
func addKnownContextValues(event *zerolog.Event, ctx context.Context) *zerolog.Event {
202+
// --- legacy API
195203

196-
if requestId, ok := ctx.Value(tracing.RequestIdCtxKey).(string); ok {
197-
event = event.Str(RID, requestId)
198-
}
199-
if path, ok := ctx.Value(tracing.RequestPath).(string); ok {
200-
event = event.Str(Path, path)
201-
}
202-
if reason, ok := ctx.Value(tracing.ReasonCtxKey).(string); ok {
203-
event = event.Str(Reason, reason)
204-
}
205-
if asyncId, ok := ctx.Value(tracing.AsyncIdCtxKey).(string); ok {
206-
if asyncId != "" {
207-
event = event.Str(AsyncId, asyncId)
208-
}
209-
}
204+
// global logger, TODO this should be removed
205+
var logger = quesma_v2.EmptyQuesmaLogger()
210206

211-
if requestId, ok := ctx.Value(tracing.OpaqueIdCtxKey).(string); ok {
212-
event = event.Str(OpaqueId, requestId)
213-
}
214-
215-
return event
207+
func GlobalLogger() quesma_v2.QuesmaLogger {
208+
return logger
216209
}
217210

211+
// global logger delegates
212+
218213
func Debug() *zerolog.Event {
219214
return logger.Debug()
220215
}
221216

222217
func DebugWithCtx(ctx context.Context) *zerolog.Event {
223-
event := logger.Debug().Ctx(ctx)
224-
event = addKnownContextValues(event, ctx)
225-
return event
218+
return logger.DebugWithCtx(ctx)
226219
}
227220

228221
func Info() *zerolog.Event {
229222
return logger.Info()
230223
}
231224

232225
func InfoWithCtx(ctx context.Context) *zerolog.Event {
233-
event := logger.Info().Ctx(ctx)
234-
event = addKnownContextValues(event, ctx)
235-
return event
226+
return logger.InfoWithCtx(ctx)
236227
}
237228

238229
// MarkTraceEndWithCtx marks the end of a trace with the given context.
239230
// Calling this functions at end of a trace is crucial from the transactional logging perspective.
240231
func MarkTraceEndWithCtx(ctx context.Context) *zerolog.Event {
241-
event := logger.Info().Ctx(ctx)
242-
event = addKnownContextValues(event, ctx)
243-
ctx = context.WithValue(ctx, tracing.TraceEndCtxKey, true)
244-
event = event.Ctx(ctx)
245-
return event
232+
return logger.MarkTraceEndWithCtx(ctx)
246233
}
247234

248235
func Warn() *zerolog.Event {
249236
return logger.Warn()
250237
}
251238

252239
func WarnWithCtx(ctx context.Context) *zerolog.Event {
253-
event := logger.Warn().Ctx(ctx)
254-
event = addKnownContextValues(event, ctx)
255-
return event
240+
return logger.WarnWithCtx(ctx)
256241
}
257242

258243
func WarnWithCtxAndReason(ctx context.Context, reason string) *zerolog.Event {
259-
return WarnWithCtx(context.WithValue(ctx, tracing.ReasonCtxKey, reason))
244+
return logger.WarnWithCtxAndReason(ctx, reason)
260245
}
261246

262247
func Error() *zerolog.Event {
263248
return logger.Error()
264249
}
265250

266251
func ErrorWithCtx(ctx context.Context) *zerolog.Event {
267-
event := logger.Error().Ctx(ctx)
268-
event = addKnownContextValues(event, ctx)
269-
return event
252+
return logger.ErrorWithCtx(ctx)
270253
}
271254

272255
func ErrorWithCtxAndReason(ctx context.Context, reason string) *zerolog.Event {
273-
return ErrorWithCtx(context.WithValue(ctx, tracing.ReasonCtxKey, reason))
256+
return logger.ErrorWithCtxAndReason(ctx, reason)
274257
}
275258

276259
func Fatal() *zerolog.Event {

quesma/main.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,7 @@ const EnableConcurrencyProfiling = false
5050

5151
// buildIngestOnlyQuesma is for now a helper function to help establishing the way of v2 module api import
5252
func buildIngestOnlyQuesma() quesma_api.QuesmaBuilder {
53-
var quesmaBuilder quesma_api.QuesmaBuilder = quesma_api.NewQuesma()
54-
quesmaBuilder.SetDependencies(quesma_api.EmptyDependencies())
53+
var quesmaBuilder quesma_api.QuesmaBuilder = quesma_api.NewQuesma(quesma_api.EmptyDependencies())
5554

5655
ingestFrontendConnector := frontend_connectors.NewElasticsearchIngestFrontendConnector(":8080")
5756

quesma/main_test.go

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,8 @@ func Test_backendConnectorValidation(t *testing.T) {
4242
var tcpProcessor quesma_api.Processor = processors.NewPostgresToMySqlProcessor()
4343
var postgressPipeline quesma_api.PipelineBuilder = quesma_api.NewPipeline()
4444
postgressPipeline.AddProcessor(tcpProcessor)
45-
var quesmaBuilder quesma_api.QuesmaBuilder = quesma_api.NewQuesma()
46-
quesmaBuilder.SetDependencies(quesma_api.EmptyDependencies())
45+
var quesmaBuilder quesma_api.QuesmaBuilder = quesma_api.NewQuesma(quesma_api.EmptyDependencies())
46+
4747
const endpoint = "root:password@tcp(127.0.0.1:3306)/test"
4848
var mySqlBackendConnector quesma_api.BackendConnector = &backend_connectors.MySqlBackendConnector{
4949
Endpoint: endpoint,
@@ -64,8 +64,7 @@ func fallback(_ context.Context, _ *quesma_api.Request, _ http.ResponseWriter) (
6464
}
6565

6666
func ab_testing_scenario() quesma_api.QuesmaBuilder {
67-
var quesmaBuilder quesma_api.QuesmaBuilder = quesma_api.NewQuesma()
68-
quesmaBuilder.SetDependencies(quesma_api.EmptyDependencies())
67+
var quesmaBuilder quesma_api.QuesmaBuilder = quesma_api.NewQuesma(quesma_api.EmptyDependencies())
6968

7069
cfg := &config.QuesmaConfiguration{
7170
DisableAuth: true,
@@ -117,8 +116,8 @@ func ab_testing_scenario() quesma_api.QuesmaBuilder {
117116
}
118117

119118
func fallbackScenario() quesma_api.QuesmaBuilder {
120-
var quesmaBuilder quesma_api.QuesmaBuilder = quesma_api.NewQuesma()
121-
quesmaBuilder.SetDependencies(quesma_api.EmptyDependencies())
119+
var quesmaBuilder quesma_api.QuesmaBuilder = quesma_api.NewQuesma(quesma_api.EmptyDependencies())
120+
122121
cfg := &config.QuesmaConfiguration{
123122
DisableAuth: true,
124123
Elasticsearch: config.ElasticsearchConfiguration{

quesma/processors/ab_test_processor.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,10 @@ func NewABTestProcessor(id string, doResultComparison bool) *ABTestProcessor {
2727
}
2828
}
2929

30+
func (p *ABTestProcessor) InstanceName() string {
31+
return "ABTestProcessor"
32+
}
33+
3034
func (p *ABTestProcessor) GetId() string {
3135
return p.Id
3236
}

quesma/processors/es_to_ch_ingest/elasticsearch_to_clickhouse_ingest_processor.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,10 @@ func NewElasticsearchToClickHouseIngestProcessor(conf config.QuesmaProcessorConf
4343
}
4444
}
4545

46+
func (p *ElasticsearchToClickHouseIngestProcessor) InstanceName() string {
47+
return "elasticsearch_to_clickhouse_ingest" // TODO return name from config
48+
}
49+
4650
func (p *ElasticsearchToClickHouseIngestProcessor) Init() error {
4751
chBackendConnector := p.GetBackendConnector(quesma_api.ClickHouseSQLBackend)
4852
if chBackendConnector == nil {

quesma/processors/postgres_to_mysql_processor.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,10 @@ func NewPostgresToMySqlProcessor() *PostgresToMySqlProcessor {
2121
}
2222
}
2323

24+
func (p *PostgresToMySqlProcessor) InstanceName() string {
25+
return "PostgresToMySqlProcessor"
26+
}
27+
2428
func (p *PostgresToMySqlProcessor) GetId() string {
2529
return "postgrestomysql_processor"
2630
}

0 commit comments

Comments
 (0)