blob: 9c36241ba6d30a679d7098da2cbd42d37f378c9d [file] [log] [blame]
divyadesai8bf96862020-02-07 12:24:26 +00001/*
2 * Copyright 2018-present Open Networking Foundation
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
Girish Kumar78cdb4f2020-03-16 13:39:42 +000017// Package Config provides dynamic logging configuration for specific Voltha component with loglevel lookup
Matteo Scandolo2e677482020-04-09 11:56:27 -070018// from etcd kvstore implemented using Backend.
Girish Kumar78cdb4f2020-03-16 13:39:42 +000019// Any Voltha component can start utilizing dynamic logging by starting goroutine of StartLogLevelConfigProcessing after
20// starting kvClient for the component.
divyadesai8bf96862020-02-07 12:24:26 +000021
22package config
23
24import (
25 "context"
26 "crypto/md5"
27 "encoding/json"
28 "errors"
29 "github.com/opencord/voltha-lib-go/v3/pkg/log"
30 "os"
31 "strings"
32)
33
Girish Kumar21972fb2020-03-13 13:27:47 +000034const (
Girish Kumar78cdb4f2020-03-16 13:39:42 +000035 defaultLogLevelKey = "default" // kvstore key containing default loglevel
36 globalConfigRootNode = "global" // Root Node in kvstore containing global config
37 initialGlobalDefaultLogLevelValue = "WARN" // Hard-coded Global Default loglevel pushed at PoD startup
Girish Kumar21972fb2020-03-13 13:27:47 +000038)
39
divyadesai8bf96862020-02-07 12:24:26 +000040// ComponentLogController represents a Configuration for Logging Config of specific Voltha component type
41// It stores ComponentConfig and GlobalConfig of loglevel config of specific Voltha component type
42// For example,ComponentLogController instance will be created for rw-core component
43type ComponentLogController struct {
44 ComponentName string
45 componentNameConfig *ComponentConfig
46 GlobalConfig *ComponentConfig
47 configManager *ConfigManager
48 logHash [16]byte
Girish Kumar21972fb2020-03-13 13:27:47 +000049 initialLogLevel string // Initial default log level set by helm chart
divyadesai8bf96862020-02-07 12:24:26 +000050}
51
52func NewComponentLogController(cm *ConfigManager) (*ComponentLogController, error) {
53
Girish Kumare6f45e82020-03-20 10:46:54 +000054 logger.Debug("creating-new-component-log-controller")
divyadesai8bf96862020-02-07 12:24:26 +000055 componentName := os.Getenv("COMPONENT_NAME")
56 if componentName == "" {
57 return nil, errors.New("Unable to retrieve PoD Component Name from Runtime env")
58 }
59
Girish Kumar21972fb2020-03-13 13:27:47 +000060 var defaultLogLevel string
61 var err error
62 // Retrieve and save default log level; used for fallback if all loglevel config is cleared in etcd
63 if defaultLogLevel, err = log.LogLevelToString(log.GetDefaultLogLevel()); err != nil {
64 defaultLogLevel = "DEBUG"
65 }
66
divyadesai8bf96862020-02-07 12:24:26 +000067 return &ComponentLogController{
68 ComponentName: componentName,
69 componentNameConfig: nil,
70 GlobalConfig: nil,
71 configManager: cm,
Girish Kumar21972fb2020-03-13 13:27:47 +000072 initialLogLevel: defaultLogLevel,
divyadesai8bf96862020-02-07 12:24:26 +000073 }, nil
74
75}
76
Girish Kumar78cdb4f2020-03-16 13:39:42 +000077// StartLogLevelConfigProcessing initialize component config and global config
78// Then, it persists initial default Loglevels into Config Store before
79// starting the loading and processing of all Log Configuration
80func StartLogLevelConfigProcessing(cm *ConfigManager, ctx context.Context) {
divyadesai8bf96862020-02-07 12:24:26 +000081 cc, err := NewComponentLogController(cm)
82 if err != nil {
Girish Kumare6f45e82020-03-20 10:46:54 +000083 logger.Errorw("unable-to-construct-component-log-controller-instance-for-log-config-monitoring", log.Fields{"error": err})
divyadesai8bf96862020-02-07 12:24:26 +000084 return
85 }
86
Girish Kumar21972fb2020-03-13 13:27:47 +000087 cc.GlobalConfig = cm.InitComponentConfig(globalConfigRootNode, ConfigTypeLogLevel)
Girish Kumare6f45e82020-03-20 10:46:54 +000088 logger.Debugw("global-log-config", log.Fields{"cc-global-config": cc.GlobalConfig})
divyadesai8bf96862020-02-07 12:24:26 +000089
90 cc.componentNameConfig = cm.InitComponentConfig(cc.ComponentName, ConfigTypeLogLevel)
Girish Kumare6f45e82020-03-20 10:46:54 +000091 logger.Debugw("component-log-config", log.Fields{"cc-component-name-config": cc.componentNameConfig})
divyadesai8bf96862020-02-07 12:24:26 +000092
Girish Kumar78cdb4f2020-03-16 13:39:42 +000093 cc.persistInitialDefaultLogConfigs(ctx)
94
divyadesai8bf96862020-02-07 12:24:26 +000095 cc.processLogConfig(ctx)
96}
97
Girish Kumar78cdb4f2020-03-16 13:39:42 +000098// Method to persist Global default loglevel into etcd, if not set yet
99// It also checks and set Component default loglevel into etcd with initial loglevel set from command line
100func (c *ComponentLogController) persistInitialDefaultLogConfigs(ctx context.Context) {
101
102 _, err := c.GlobalConfig.Retrieve(ctx, defaultLogLevelKey)
103 if err != nil {
Girish Kumare6f45e82020-03-20 10:46:54 +0000104 logger.Debugw("failed-to-retrieve-global-default-log-config-at-startup", log.Fields{"error": err})
Girish Kumar78cdb4f2020-03-16 13:39:42 +0000105
106 err = c.GlobalConfig.Save(ctx, defaultLogLevelKey, initialGlobalDefaultLogLevelValue)
107 if err != nil {
Girish Kumare6f45e82020-03-20 10:46:54 +0000108 logger.Errorw("failed-to-persist-global-default-log-config-at-startup", log.Fields{"error": err, "loglevel": initialGlobalDefaultLogLevelValue})
Girish Kumar78cdb4f2020-03-16 13:39:42 +0000109 }
110 }
111
112 _, err = c.componentNameConfig.Retrieve(ctx, defaultLogLevelKey)
113 if err != nil {
Girish Kumare6f45e82020-03-20 10:46:54 +0000114 logger.Debugw("failed-to-retrieve-component-default-log-config-at-startup", log.Fields{"error": err})
Girish Kumar78cdb4f2020-03-16 13:39:42 +0000115
116 err = c.componentNameConfig.Save(ctx, defaultLogLevelKey, c.initialLogLevel)
117 if err != nil {
Girish Kumare6f45e82020-03-20 10:46:54 +0000118 logger.Errorw("failed-to-persist-component-default-log-config-at-startup", log.Fields{"error": err, "loglevel": c.initialLogLevel})
Girish Kumar78cdb4f2020-03-16 13:39:42 +0000119 }
120 }
121}
122
Girish Kumar21972fb2020-03-13 13:27:47 +0000123// ProcessLogConfig will first load and apply log config and then start waiting on component config and global config
Matteo Scandolo2e677482020-04-09 11:56:27 -0700124// channels for any changes. Event channel will be recieved from Backend for valid change type
125// Then data for componentn log config and global log config will be retrieved from Backend and stored in updatedLogConfig in precedence order
divyadesai8bf96862020-02-07 12:24:26 +0000126// If any changes in updatedLogConfig will be applied on component
127func (c *ComponentLogController) processLogConfig(ctx context.Context) {
128
Girish Kumar21972fb2020-03-13 13:27:47 +0000129 // Load and apply Log Config for first time
130 initialLogConfig, err := c.buildUpdatedLogConfig(ctx)
131 if err != nil {
Girish Kumare6f45e82020-03-20 10:46:54 +0000132 logger.Warnw("unable-to-load-log-config-at-startup", log.Fields{"error": err})
Girish Kumar21972fb2020-03-13 13:27:47 +0000133 } else {
134 if err := c.loadAndApplyLogConfig(initialLogConfig); err != nil {
Girish Kumare6f45e82020-03-20 10:46:54 +0000135 logger.Warnw("unable-to-apply-log-config-at-startup", log.Fields{"error": err})
Girish Kumar21972fb2020-03-13 13:27:47 +0000136 }
137 }
138
divyadesai8bf96862020-02-07 12:24:26 +0000139 componentConfigEventChan := c.componentNameConfig.MonitorForConfigChange(ctx)
140
141 globalConfigEventChan := c.GlobalConfig.MonitorForConfigChange(ctx)
142
143 // process the events for componentName and global config
144 var configEvent *ConfigChangeEvent
145 for {
146 select {
147 case configEvent = <-globalConfigEventChan:
148 case configEvent = <-componentConfigEventChan:
149
150 }
Girish Kumare6f45e82020-03-20 10:46:54 +0000151 logger.Debugw("processing-log-config-change", log.Fields{"ChangeType": configEvent.ChangeType, "Package": configEvent.ConfigAttribute})
divyadesai8bf96862020-02-07 12:24:26 +0000152
153 updatedLogConfig, err := c.buildUpdatedLogConfig(ctx)
154 if err != nil {
Girish Kumare6f45e82020-03-20 10:46:54 +0000155 logger.Warnw("unable-to-fetch-updated-log-config", log.Fields{"error": err})
divyadesai8bf96862020-02-07 12:24:26 +0000156 continue
157 }
158
Girish Kumare6f45e82020-03-20 10:46:54 +0000159 logger.Debugw("applying-updated-log-config", log.Fields{"updated-log-config": updatedLogConfig})
divyadesai8bf96862020-02-07 12:24:26 +0000160
161 if err := c.loadAndApplyLogConfig(updatedLogConfig); err != nil {
Girish Kumare6f45e82020-03-20 10:46:54 +0000162 logger.Warnw("unable-to-load-and-apply-log-config", log.Fields{"error": err})
divyadesai8bf96862020-02-07 12:24:26 +0000163 }
164 }
165
166}
167
168// get active loglevel from the zap logger
Girish Kumar21972fb2020-03-13 13:27:47 +0000169func getActiveLogLevels() map[string]string {
170 loglevels := make(map[string]string)
divyadesai8bf96862020-02-07 12:24:26 +0000171
172 // now do the default log level
173 if level, err := log.LogLevelToString(log.GetDefaultLogLevel()); err == nil {
Girish Kumar21972fb2020-03-13 13:27:47 +0000174 loglevels[defaultLogLevelKey] = level
divyadesai8bf96862020-02-07 12:24:26 +0000175 }
176
177 // do the per-package log levels
178 for _, packageName := range log.GetPackageNames() {
179 level, err := log.GetPackageLogLevel(packageName)
180 if err != nil {
Girish Kumare6f45e82020-03-20 10:46:54 +0000181 logger.Warnw("unable-to-fetch-current-active-loglevel-for-package-name", log.Fields{"package-name": packageName, "error": err})
Girish Kumar21972fb2020-03-13 13:27:47 +0000182 continue
divyadesai8bf96862020-02-07 12:24:26 +0000183 }
184
divyadesai8bf96862020-02-07 12:24:26 +0000185 if l, err := log.LogLevelToString(level); err == nil {
Girish Kumar21972fb2020-03-13 13:27:47 +0000186 loglevels[packageName] = l
divyadesai8bf96862020-02-07 12:24:26 +0000187 }
divyadesai8bf96862020-02-07 12:24:26 +0000188 }
divyadesai8bf96862020-02-07 12:24:26 +0000189
Girish Kumare6f45e82020-03-20 10:46:54 +0000190 logger.Debugw("retreived-log-levels-from-zap-logger", log.Fields{"loglevels": loglevels})
Girish Kumar21972fb2020-03-13 13:27:47 +0000191
192 return loglevels
divyadesai8bf96862020-02-07 12:24:26 +0000193}
194
195func (c *ComponentLogController) getGlobalLogConfig(ctx context.Context) (string, error) {
196
Girish Kumar78cdb4f2020-03-16 13:39:42 +0000197 globalDefaultLogLevel, err := c.GlobalConfig.Retrieve(ctx, defaultLogLevelKey)
divyadesai8bf96862020-02-07 12:24:26 +0000198 if err != nil {
199 return "", err
200 }
201
Girish Kumar78cdb4f2020-03-16 13:39:42 +0000202 // Handle edge cases when global default loglevel is deleted directly from etcd or set to a invalid value
203 // We should use hard-coded initial default value in such cases
204 if globalDefaultLogLevel == "" {
Girish Kumare6f45e82020-03-20 10:46:54 +0000205 logger.Warn("global-default-loglevel-not-found-in-config-store")
Girish Kumar78cdb4f2020-03-16 13:39:42 +0000206 globalDefaultLogLevel = initialGlobalDefaultLogLevelValue
divyadesai8bf96862020-02-07 12:24:26 +0000207 }
Girish Kumar21972fb2020-03-13 13:27:47 +0000208
Girish Kumar78cdb4f2020-03-16 13:39:42 +0000209 if _, err := log.StringToLogLevel(globalDefaultLogLevel); err != nil {
Girish Kumare6f45e82020-03-20 10:46:54 +0000210 logger.Warnw("unsupported-loglevel-config-defined-at-global-default", log.Fields{"log-level": globalDefaultLogLevel})
Girish Kumar78cdb4f2020-03-16 13:39:42 +0000211 globalDefaultLogLevel = initialGlobalDefaultLogLevelValue
212 }
213
Girish Kumare6f45e82020-03-20 10:46:54 +0000214 logger.Debugw("retrieved-global-default-loglevel", log.Fields{"level": globalDefaultLogLevel})
divyadesai8bf96862020-02-07 12:24:26 +0000215
216 return globalDefaultLogLevel, nil
217}
218
Girish Kumar21972fb2020-03-13 13:27:47 +0000219func (c *ComponentLogController) getComponentLogConfig(ctx context.Context, globalDefaultLogLevel string) (map[string]string, error) {
divyadesai8bf96862020-02-07 12:24:26 +0000220 componentLogConfig, err := c.componentNameConfig.RetrieveAll(ctx)
221 if err != nil {
222 return nil, err
223 }
224
Girish Kumar21972fb2020-03-13 13:27:47 +0000225 effectiveDefaultLogLevel := ""
226 for logConfigKey, logConfigValue := range componentLogConfig {
227 if _, err := log.StringToLogLevel(logConfigValue); err != nil || logConfigKey == "" {
Girish Kumare6f45e82020-03-20 10:46:54 +0000228 logger.Warnw("unsupported-loglevel-config-defined-at-component-context", log.Fields{"package-name": logConfigKey, "log-level": logConfigValue})
Girish Kumar21972fb2020-03-13 13:27:47 +0000229 delete(componentLogConfig, logConfigKey)
divyadesai8bf96862020-02-07 12:24:26 +0000230 } else {
Girish Kumar21972fb2020-03-13 13:27:47 +0000231 if logConfigKey == defaultLogLevelKey {
232 effectiveDefaultLogLevel = componentLogConfig[defaultLogLevelKey]
divyadesai8bf96862020-02-07 12:24:26 +0000233 }
234 }
235 }
Girish Kumar21972fb2020-03-13 13:27:47 +0000236
237 // if default loglevel is not configured for the component, component should use
Girish Kumar78cdb4f2020-03-16 13:39:42 +0000238 // default loglevel configured at global level
Girish Kumar21972fb2020-03-13 13:27:47 +0000239 if effectiveDefaultLogLevel == "" {
240 effectiveDefaultLogLevel = globalDefaultLogLevel
divyadesai8bf96862020-02-07 12:24:26 +0000241 }
Girish Kumar21972fb2020-03-13 13:27:47 +0000242
243 componentLogConfig[defaultLogLevelKey] = effectiveDefaultLogLevel
244
Girish Kumare6f45e82020-03-20 10:46:54 +0000245 logger.Debugw("retrieved-component-log-config", log.Fields{"component-log-level": componentLogConfig})
divyadesai8bf96862020-02-07 12:24:26 +0000246
247 return componentLogConfig, nil
248}
249
Matteo Scandolo2e677482020-04-09 11:56:27 -0700250// buildUpdatedLogConfig retrieve the global logConfig and component logConfig from Backend
divyadesai8bf96862020-02-07 12:24:26 +0000251// component logConfig stores the log config with precedence order
252// For example, If the global logConfig is set and component logConfig is set only for specific package then
253// component logConfig is stored with global logConfig and component logConfig of specific package
254// For example, If the global logConfig is set and component logConfig is set for specific package and as well as for default then
255// component logConfig is stored with component logConfig data only
256func (c *ComponentLogController) buildUpdatedLogConfig(ctx context.Context) (map[string]string, error) {
257 globalLogLevel, err := c.getGlobalLogConfig(ctx)
258 if err != nil {
Girish Kumare6f45e82020-03-20 10:46:54 +0000259 logger.Errorw("unable-to-retrieve-global-log-config", log.Fields{"err": err})
divyadesai8bf96862020-02-07 12:24:26 +0000260 }
261
Girish Kumar21972fb2020-03-13 13:27:47 +0000262 componentLogConfig, err := c.getComponentLogConfig(ctx, globalLogLevel)
divyadesai8bf96862020-02-07 12:24:26 +0000263 if err != nil {
264 return nil, err
265 }
266
Girish Kumar21972fb2020-03-13 13:27:47 +0000267 finalLogConfig := make(map[string]string)
268 for packageName, logLevel := range componentLogConfig {
269 finalLogConfig[strings.ReplaceAll(packageName, "#", "/")] = logLevel
270 }
271
272 return finalLogConfig, nil
divyadesai8bf96862020-02-07 12:24:26 +0000273}
274
275// load and apply the current configuration for component name
276// create hash of loaded configuration using GenerateLogConfigHash
277// if there is previous hash stored, compare the hash to stored hash
278// if there is any change will call UpdateLogLevels
279func (c *ComponentLogController) loadAndApplyLogConfig(logConfig map[string]string) error {
280 currentLogHash, err := GenerateLogConfigHash(logConfig)
281 if err != nil {
282 return err
283 }
284
divyadesai8bf96862020-02-07 12:24:26 +0000285 if c.logHash != currentLogHash {
286 UpdateLogLevels(logConfig)
287 c.logHash = currentLogHash
Girish Kumar21972fb2020-03-13 13:27:47 +0000288 } else {
Girish Kumare6f45e82020-03-20 10:46:54 +0000289 logger.Debug("effective-loglevel-config-same-as-currently-active")
divyadesai8bf96862020-02-07 12:24:26 +0000290 }
Girish Kumar21972fb2020-03-13 13:27:47 +0000291
divyadesai8bf96862020-02-07 12:24:26 +0000292 return nil
293}
294
Girish Kumar21972fb2020-03-13 13:27:47 +0000295// createModifiedLogLevels loops through the activeLogLevels recieved from zap logger and updatedLogLevels recieved from buildUpdatedLogConfig
296// to identify and create map of modified Log Levels of 2 types:
297// - Packages for which log level has been changed
298// - Packages for which log level config has been cleared - set to default log level
299func createModifiedLogLevels(activeLogLevels, updatedLogLevels map[string]string) map[string]string {
300 defaultLevel := updatedLogLevels[defaultLogLevelKey]
divyadesai8bf96862020-02-07 12:24:26 +0000301
Girish Kumar21972fb2020-03-13 13:27:47 +0000302 modifiedLogLevels := make(map[string]string)
divyadesai8bf96862020-02-07 12:24:26 +0000303 for activeKey, activeLevel := range activeLogLevels {
304 if _, exist := updatedLogLevels[activeKey]; !exist {
Girish Kumar21972fb2020-03-13 13:27:47 +0000305 if activeLevel != defaultLevel {
306 modifiedLogLevels[activeKey] = defaultLevel
divyadesai8bf96862020-02-07 12:24:26 +0000307 }
Girish Kumar21972fb2020-03-13 13:27:47 +0000308 } else if activeLevel != updatedLogLevels[activeKey] {
309 modifiedLogLevels[activeKey] = updatedLogLevels[activeKey]
divyadesai8bf96862020-02-07 12:24:26 +0000310 }
311 }
Girish Kumar21972fb2020-03-13 13:27:47 +0000312
313 // Log warnings for all invalid packages for which log config has been set
314 for key, value := range updatedLogLevels {
315 if _, exist := activeLogLevels[key]; !exist {
Girish Kumare6f45e82020-03-20 10:46:54 +0000316 logger.Warnw("ignoring-loglevel-set-for-invalid-package", log.Fields{"package": key, "log-level": value})
Girish Kumar21972fb2020-03-13 13:27:47 +0000317 }
318 }
319
320 return modifiedLogLevels
divyadesai8bf96862020-02-07 12:24:26 +0000321}
322
323// updateLogLevels update the loglevels for the component
324// retrieve active confguration from logger
325// compare with entries one by one and apply
Girish Kumar21972fb2020-03-13 13:27:47 +0000326func UpdateLogLevels(updatedLogConfig map[string]string) {
divyadesai8bf96862020-02-07 12:24:26 +0000327
Girish Kumar21972fb2020-03-13 13:27:47 +0000328 activeLogLevels := getActiveLogLevels()
329 changedLogLevels := createModifiedLogLevels(activeLogLevels, updatedLogConfig)
330
331 // If no changed log levels are found, just return. It may happen on configuration of a invalid package
332 if len(changedLogLevels) == 0 {
Girish Kumare6f45e82020-03-20 10:46:54 +0000333 logger.Debug("no-change-in-effective-loglevel-config")
Girish Kumar21972fb2020-03-13 13:27:47 +0000334 return
335 }
336
Girish Kumare6f45e82020-03-20 10:46:54 +0000337 logger.Debugw("applying-log-level-for-modified-packages", log.Fields{"changed-log-levels": changedLogLevels})
Girish Kumar21972fb2020-03-13 13:27:47 +0000338 for key, level := range changedLogLevels {
339 if key == defaultLogLevelKey {
divyadesai8bf96862020-02-07 12:24:26 +0000340 if l, err := log.StringToLogLevel(level); err == nil {
341 log.SetDefaultLogLevel(l)
342 }
343 } else {
divyadesai8bf96862020-02-07 12:24:26 +0000344 if l, err := log.StringToLogLevel(level); err == nil {
Girish Kumar21972fb2020-03-13 13:27:47 +0000345 log.SetPackageLogLevel(key, l)
divyadesai8bf96862020-02-07 12:24:26 +0000346 }
347 }
348 }
divyadesai8bf96862020-02-07 12:24:26 +0000349}
350
351// generate md5 hash of key value pairs appended into a single string
352// in order by key name
353func GenerateLogConfigHash(createHashLog map[string]string) ([16]byte, error) {
354 createHashLogBytes := []byte{}
355 levelData, err := json.Marshal(createHashLog)
356 if err != nil {
357 return [16]byte{}, err
358 }
359 createHashLogBytes = append(createHashLogBytes, levelData...)
360 return md5.Sum(createHashLogBytes), nil
361}