github.com/livekit/protocol@v1.16.1-0.20240517185851-47e4c6bba773/logger/config.go (about)

     1  // Copyright 2023 LiveKit, Inc.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package logger
    16  
    17  import "sync"
    18  
    19  type Config struct {
    20  	JSON  bool   `yaml:"json,omitempty"`
    21  	Level string `yaml:"level,omitempty"`
    22  	// true to enable log sampling, where the same log message and level will be throttled.
    23  	// we have two layers of sampling
    24  	// 1. global sampling - within a second, it will log the first SampleInitial, then every SampleInterval messages.
    25  	// 2. per participant/track sampling - to be used with Logger.WithItemSampler(). This would be used to throttle
    26  	//    the logs for a particular participant/track.
    27  	Sample bool `yaml:"sample,omitempty"`
    28  
    29  	ComponentLevels map[string]string `yaml:"component_levels,omitempty"`
    30  
    31  	// global sampling per server
    32  	// when sampling, the first N logs will be logged
    33  	SampleInitial int `yaml:"sample_initial,omitempty"`
    34  	// when sampling, every Mth log will be logged
    35  	SampleInterval int `yaml:"sample_interval,omitempty"`
    36  
    37  	// participant/track level sampling
    38  	ItemSampleSeconds  int `yaml:"item_sample_seconds,omitempty"`
    39  	ItemSampleInitial  int `yaml:"item_sample_initial,omitempty"`
    40  	ItemSampleInterval int `yaml:"item_sample_interval,omitempty"`
    41  
    42  	lock               sync.Mutex       `yaml:"-"`
    43  	onUpdatedCallbacks []ConfigObserver `yaml:"-"`
    44  }
    45  
    46  type ConfigObserver func(*Config) error
    47  
    48  func (c *Config) Update(o *Config) error {
    49  	c.lock.Lock()
    50  	c.JSON = o.JSON
    51  	c.Level = o.Level
    52  	c.Sample = o.Sample
    53  	c.SampleInitial = o.SampleInitial
    54  	c.SampleInterval = o.SampleInterval
    55  	c.ItemSampleSeconds = o.ItemSampleSeconds
    56  	c.ItemSampleInitial = o.ItemSampleInitial
    57  	c.ItemSampleInterval = o.ItemSampleInterval
    58  	c.ComponentLevels = o.ComponentLevels
    59  	callbacks := c.onUpdatedCallbacks
    60  	c.lock.Unlock()
    61  
    62  	for _, cb := range callbacks {
    63  		if err := cb(c); err != nil {
    64  			return err
    65  		}
    66  	}
    67  	return nil
    68  }
    69  
    70  func (c *Config) AddUpdateObserver(cb ConfigObserver) {
    71  	c.lock.Lock()
    72  	defer c.lock.Unlock()
    73  	c.onUpdatedCallbacks = append(c.onUpdatedCallbacks, cb)
    74  }