...

Source file src/k8s.io/component-base/logs/logreduction/logreduction.go

Documentation: k8s.io/component-base/logs/logreduction

     1  /*
     2  Copyright 2018 The Kubernetes Authors.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package logreduction
    18  
    19  import (
    20  	"sync"
    21  	"time"
    22  )
    23  
    24  var nowfunc = func() time.Time { return time.Now() }
    25  
    26  // LogReduction provides a filter for consecutive identical log messages;
    27  // a message will be printed no more than once per interval.
    28  // If a string of messages is interrupted by a different message,
    29  // the interval timer will be reset.
    30  type LogReduction struct {
    31  	lastError           map[string]string
    32  	errorPrinted        map[string]time.Time
    33  	errorMapLock        sync.Mutex
    34  	identicalErrorDelay time.Duration
    35  }
    36  
    37  // NewLogReduction returns an initialized LogReduction
    38  func NewLogReduction(identicalErrorDelay time.Duration) *LogReduction {
    39  	l := new(LogReduction)
    40  	l.lastError = make(map[string]string)
    41  	l.errorPrinted = make(map[string]time.Time)
    42  	l.identicalErrorDelay = identicalErrorDelay
    43  	return l
    44  }
    45  
    46  func (l *LogReduction) cleanupErrorTimeouts() {
    47  	for name, timeout := range l.errorPrinted {
    48  		if nowfunc().Sub(timeout) >= l.identicalErrorDelay {
    49  			delete(l.errorPrinted, name)
    50  			delete(l.lastError, name)
    51  		}
    52  	}
    53  }
    54  
    55  // ShouldMessageBePrinted determines whether a message should be printed based
    56  // on how long ago this particular message was last printed
    57  func (l *LogReduction) ShouldMessageBePrinted(message string, parentID string) bool {
    58  	l.errorMapLock.Lock()
    59  	defer l.errorMapLock.Unlock()
    60  	l.cleanupErrorTimeouts()
    61  	lastMsg, ok := l.lastError[parentID]
    62  	lastPrinted, ok1 := l.errorPrinted[parentID]
    63  	if !ok || !ok1 || message != lastMsg || nowfunc().Sub(lastPrinted) >= l.identicalErrorDelay {
    64  		l.errorPrinted[parentID] = nowfunc()
    65  		l.lastError[parentID] = message
    66  		return true
    67  	}
    68  	return false
    69  }
    70  
    71  // ClearID clears out log reduction records pertaining to a particular parent
    72  // (e. g. container ID)
    73  func (l *LogReduction) ClearID(parentID string) {
    74  	l.errorMapLock.Lock()
    75  	defer l.errorMapLock.Unlock()
    76  	delete(l.lastError, parentID)
    77  	delete(l.errorPrinted, parentID)
    78  }
    79  

View as plain text