Skip to content

Commit c2c11e6

Browse files
authored
[chore] Avoid long switch call in zap.Any if the logs will not be logged (#39917)
This is a very large performance impact for transform heavy services. Signed-off-by: Bogdan Drutu <[email protected]>
1 parent 65bb39e commit c2c11e6

File tree

2 files changed

+15
-9
lines changed

2 files changed

+15
-9
lines changed

pkg/ottl/context_inferrer.go

+8-6
Original file line numberDiff line numberDiff line change
@@ -109,12 +109,14 @@ func (s *priorityContextInferrer) infer(statements []string, conditions []string
109109
return "", err
110110
}
111111
}
112-
s.telemetrySettings.Logger.Debug("Inferring context from statements and conditions",
113-
zap.Strings("candidates", maps.Keys(s.contextCandidate)),
114-
zap.Any("priority", s.contextPriority),
115-
zap.Strings("statements", statements),
116-
zap.Strings("conditions", conditions),
117-
)
112+
if s.telemetrySettings.Logger.Core().Enabled(zap.DebugLevel) {
113+
s.telemetrySettings.Logger.Debug("Inferring context from statements and conditions",
114+
zap.Strings("candidates", maps.Keys(s.contextCandidate)),
115+
zap.Any("priority", s.contextPriority),
116+
zap.Strings("statements", statements),
117+
zap.Strings("conditions", conditions),
118+
)
119+
}
118120
return s.inferFromHints(append(statementsHints, conditionsHints...))
119121
}
120122

pkg/ottl/parser.go

+7-3
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ type Statement[K any] struct {
3232
func (s *Statement[K]) Execute(ctx context.Context, tCtx K) (any, bool, error) {
3333
condition, err := s.condition.Eval(ctx, tCtx)
3434
defer func() {
35-
if s.telemetrySettings.Logger != nil {
35+
if s.telemetrySettings.Logger.Core().Enabled(zap.DebugLevel) {
3636
s.telemetrySettings.Logger.Debug("TransformContext after statement execution", zap.String("statement", s.origText), zap.Bool("condition matched", condition), zap.Any("TransformContext", tCtx))
3737
}
3838
}()
@@ -381,7 +381,9 @@ func NewStatementSequence[K any](statements []*Statement[K], telemetrySettings c
381381
// When the ErrorMode of the StatementSequence is `ignore`, errors are logged and execution continues to the next statement.
382382
// When the ErrorMode of the StatementSequence is `silent`, errors are not logged and execution continues to the next statement.
383383
func (s *StatementSequence[K]) Execute(ctx context.Context, tCtx K) error {
384-
s.telemetrySettings.Logger.Debug("initial TransformContext before executing StatementSequence", zap.Any("TransformContext", tCtx))
384+
if s.telemetrySettings.Logger.Core().Enabled(zap.DebugLevel) {
385+
s.telemetrySettings.Logger.Debug("initial TransformContext before executing StatementSequence", zap.Any("TransformContext", tCtx))
386+
}
385387
for _, statement := range s.statements {
386388
_, _, err := statement.Execute(ctx, tCtx)
387389
if err != nil {
@@ -454,7 +456,9 @@ func (c *ConditionSequence[K]) Eval(ctx context.Context, tCtx K) (bool, error) {
454456
var atLeastOneMatch bool
455457
for _, condition := range c.conditions {
456458
match, err := condition.Eval(ctx, tCtx)
457-
c.telemetrySettings.Logger.Debug("condition evaluation result", zap.String("condition", condition.origText), zap.Bool("match", match), zap.Any("TransformContext", tCtx))
459+
if c.telemetrySettings.Logger.Core().Enabled(zap.DebugLevel) {
460+
c.telemetrySettings.Logger.Debug("condition evaluation result", zap.String("condition", condition.origText), zap.Bool("match", match), zap.Any("TransformContext", tCtx))
461+
}
458462
if err != nil {
459463
if c.errorMode == PropagateError {
460464
err = fmt.Errorf("failed to eval condition: %v, %w", condition.origText, err)

0 commit comments

Comments
 (0)