github.com/yankunsam/loki/v2@v2.6.3-0.20220817130409-389df5235c27/pkg/runtime/config.go (about)

     1  package runtime
     2  
     3  type Config struct {
     4  	LogStreamCreation     bool `yaml:"log_stream_creation"`
     5  	LogPushRequest        bool `yaml:"log_push_request"`
     6  	LogPushRequestStreams bool `yaml:"log_push_request_streams"`
     7  }
     8  
     9  // TenantConfig is a function that returns configs for given tenant, or
    10  // nil, if there are no tenant-specific configs.
    11  type TenantConfig func(userID string) *Config
    12  
    13  // TenantConfigs periodically fetch a set of per-user configs, and provides convenience
    14  // functions for fetching the correct value.
    15  type TenantConfigs struct {
    16  	defaultConfig *Config
    17  	tenantConfig  TenantConfig
    18  }
    19  
    20  // DefaultTenantConfigs creates and returns a new TenantConfigs with the defaults populated.
    21  func DefaultTenantConfigs() *TenantConfigs {
    22  	return &TenantConfigs{
    23  		defaultConfig: &Config{},
    24  		tenantConfig:  nil,
    25  	}
    26  }
    27  
    28  // NewTenantConfig makes a new TenantConfigs
    29  func NewTenantConfigs(tenantConfig TenantConfig) (*TenantConfigs, error) {
    30  	return &TenantConfigs{
    31  		defaultConfig: DefaultTenantConfigs().defaultConfig,
    32  		tenantConfig:  tenantConfig,
    33  	}, nil
    34  }
    35  
    36  func (o *TenantConfigs) getOverridesForUser(userID string) *Config {
    37  	if o.tenantConfig != nil {
    38  		l := o.tenantConfig(userID)
    39  		if l != nil {
    40  			return l
    41  		}
    42  	}
    43  	return o.defaultConfig
    44  }
    45  
    46  func (o *TenantConfigs) LogStreamCreation(userID string) bool {
    47  	return o.getOverridesForUser(userID).LogStreamCreation
    48  }
    49  
    50  func (o *TenantConfigs) LogPushRequest(userID string) bool {
    51  	return o.getOverridesForUser(userID).LogPushRequest
    52  }
    53  
    54  func (o *TenantConfigs) LogPushRequestStreams(userID string) bool {
    55  	return o.getOverridesForUser(userID).LogPushRequestStreams
    56  }