divyadesai | 8bf9686 | 2020-02-07 12:24:26 +0000 | [diff] [blame] | 1 | /* |
| 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 Kumar | 78cdb4f | 2020-03-16 13:39:42 +0000 | [diff] [blame] | 17 | // Package Config provides dynamic logging configuration for specific Voltha component with loglevel lookup |
Matteo Scandolo | 2e67748 | 2020-04-09 11:56:27 -0700 | [diff] [blame] | 18 | // from etcd kvstore implemented using Backend. |
Girish Kumar | 78cdb4f | 2020-03-16 13:39:42 +0000 | [diff] [blame] | 19 | // Any Voltha component can start utilizing dynamic logging by starting goroutine of StartLogLevelConfigProcessing after |
| 20 | // starting kvClient for the component. |
divyadesai | 8bf9686 | 2020-02-07 12:24:26 +0000 | [diff] [blame] | 21 | |
| 22 | package config |
| 23 | |
| 24 | import ( |
| 25 | "context" |
| 26 | "crypto/md5" |
| 27 | "encoding/json" |
| 28 | "errors" |
| 29 | "github.com/opencord/voltha-lib-go/v3/pkg/log" |
| 30 | "os" |
Girish Kumar | 472a5c9 | 2020-04-14 09:14:18 +0000 | [diff] [blame] | 31 | "sort" |
divyadesai | 8bf9686 | 2020-02-07 12:24:26 +0000 | [diff] [blame] | 32 | "strings" |
| 33 | ) |
| 34 | |
Girish Kumar | 21972fb | 2020-03-13 13:27:47 +0000 | [diff] [blame] | 35 | const ( |
Girish Kumar | 472a5c9 | 2020-04-14 09:14:18 +0000 | [diff] [blame] | 36 | defaultLogLevelKey = "default" // kvstore key containing default loglevel |
| 37 | globalConfigRootNode = "global" // Root Node in kvstore containing global config |
| 38 | initialGlobalDefaultLogLevelValue = "WARN" // Hard-coded Global Default loglevel pushed at PoD startup |
| 39 | logPackagesListKey = "log_package_list" // kvstore key containing list of allowed log packages |
Girish Kumar | 21972fb | 2020-03-13 13:27:47 +0000 | [diff] [blame] | 40 | ) |
| 41 | |
divyadesai | 8bf9686 | 2020-02-07 12:24:26 +0000 | [diff] [blame] | 42 | // ComponentLogController represents a Configuration for Logging Config of specific Voltha component type |
| 43 | // It stores ComponentConfig and GlobalConfig of loglevel config of specific Voltha component type |
| 44 | // For example,ComponentLogController instance will be created for rw-core component |
| 45 | type ComponentLogController struct { |
| 46 | ComponentName string |
| 47 | componentNameConfig *ComponentConfig |
| 48 | GlobalConfig *ComponentConfig |
| 49 | configManager *ConfigManager |
| 50 | logHash [16]byte |
Girish Kumar | 21972fb | 2020-03-13 13:27:47 +0000 | [diff] [blame] | 51 | initialLogLevel string // Initial default log level set by helm chart |
divyadesai | 8bf9686 | 2020-02-07 12:24:26 +0000 | [diff] [blame] | 52 | } |
| 53 | |
| 54 | func NewComponentLogController(cm *ConfigManager) (*ComponentLogController, error) { |
| 55 | |
Girish Kumar | e6f45e8 | 2020-03-20 10:46:54 +0000 | [diff] [blame] | 56 | logger.Debug("creating-new-component-log-controller") |
divyadesai | 8bf9686 | 2020-02-07 12:24:26 +0000 | [diff] [blame] | 57 | componentName := os.Getenv("COMPONENT_NAME") |
| 58 | if componentName == "" { |
| 59 | return nil, errors.New("Unable to retrieve PoD Component Name from Runtime env") |
| 60 | } |
| 61 | |
Girish Kumar | 21972fb | 2020-03-13 13:27:47 +0000 | [diff] [blame] | 62 | var defaultLogLevel string |
| 63 | var err error |
| 64 | // Retrieve and save default log level; used for fallback if all loglevel config is cleared in etcd |
| 65 | if defaultLogLevel, err = log.LogLevelToString(log.GetDefaultLogLevel()); err != nil { |
| 66 | defaultLogLevel = "DEBUG" |
| 67 | } |
| 68 | |
divyadesai | 8bf9686 | 2020-02-07 12:24:26 +0000 | [diff] [blame] | 69 | return &ComponentLogController{ |
| 70 | ComponentName: componentName, |
| 71 | componentNameConfig: nil, |
| 72 | GlobalConfig: nil, |
| 73 | configManager: cm, |
Girish Kumar | 21972fb | 2020-03-13 13:27:47 +0000 | [diff] [blame] | 74 | initialLogLevel: defaultLogLevel, |
divyadesai | 8bf9686 | 2020-02-07 12:24:26 +0000 | [diff] [blame] | 75 | }, nil |
| 76 | |
| 77 | } |
| 78 | |
Girish Kumar | 78cdb4f | 2020-03-16 13:39:42 +0000 | [diff] [blame] | 79 | // StartLogLevelConfigProcessing initialize component config and global config |
| 80 | // Then, it persists initial default Loglevels into Config Store before |
| 81 | // starting the loading and processing of all Log Configuration |
| 82 | func StartLogLevelConfigProcessing(cm *ConfigManager, ctx context.Context) { |
divyadesai | 8bf9686 | 2020-02-07 12:24:26 +0000 | [diff] [blame] | 83 | cc, err := NewComponentLogController(cm) |
| 84 | if err != nil { |
Girish Kumar | e6f45e8 | 2020-03-20 10:46:54 +0000 | [diff] [blame] | 85 | logger.Errorw("unable-to-construct-component-log-controller-instance-for-log-config-monitoring", log.Fields{"error": err}) |
divyadesai | 8bf9686 | 2020-02-07 12:24:26 +0000 | [diff] [blame] | 86 | return |
| 87 | } |
| 88 | |
Girish Kumar | 21972fb | 2020-03-13 13:27:47 +0000 | [diff] [blame] | 89 | cc.GlobalConfig = cm.InitComponentConfig(globalConfigRootNode, ConfigTypeLogLevel) |
Girish Kumar | e6f45e8 | 2020-03-20 10:46:54 +0000 | [diff] [blame] | 90 | logger.Debugw("global-log-config", log.Fields{"cc-global-config": cc.GlobalConfig}) |
divyadesai | 8bf9686 | 2020-02-07 12:24:26 +0000 | [diff] [blame] | 91 | |
| 92 | cc.componentNameConfig = cm.InitComponentConfig(cc.ComponentName, ConfigTypeLogLevel) |
Girish Kumar | e6f45e8 | 2020-03-20 10:46:54 +0000 | [diff] [blame] | 93 | logger.Debugw("component-log-config", log.Fields{"cc-component-name-config": cc.componentNameConfig}) |
divyadesai | 8bf9686 | 2020-02-07 12:24:26 +0000 | [diff] [blame] | 94 | |
Girish Kumar | 78cdb4f | 2020-03-16 13:39:42 +0000 | [diff] [blame] | 95 | cc.persistInitialDefaultLogConfigs(ctx) |
| 96 | |
Girish Kumar | 472a5c9 | 2020-04-14 09:14:18 +0000 | [diff] [blame] | 97 | cc.persistRegisteredLogPackageList(ctx) |
| 98 | |
divyadesai | 8bf9686 | 2020-02-07 12:24:26 +0000 | [diff] [blame] | 99 | cc.processLogConfig(ctx) |
| 100 | } |
| 101 | |
Girish Kumar | 78cdb4f | 2020-03-16 13:39:42 +0000 | [diff] [blame] | 102 | // Method to persist Global default loglevel into etcd, if not set yet |
| 103 | // It also checks and set Component default loglevel into etcd with initial loglevel set from command line |
| 104 | func (c *ComponentLogController) persistInitialDefaultLogConfigs(ctx context.Context) { |
| 105 | |
| 106 | _, err := c.GlobalConfig.Retrieve(ctx, defaultLogLevelKey) |
| 107 | if err != nil { |
Girish Kumar | e6f45e8 | 2020-03-20 10:46:54 +0000 | [diff] [blame] | 108 | logger.Debugw("failed-to-retrieve-global-default-log-config-at-startup", log.Fields{"error": err}) |
Girish Kumar | 78cdb4f | 2020-03-16 13:39:42 +0000 | [diff] [blame] | 109 | |
| 110 | err = c.GlobalConfig.Save(ctx, defaultLogLevelKey, initialGlobalDefaultLogLevelValue) |
| 111 | if err != nil { |
Girish Kumar | e6f45e8 | 2020-03-20 10:46:54 +0000 | [diff] [blame] | 112 | logger.Errorw("failed-to-persist-global-default-log-config-at-startup", log.Fields{"error": err, "loglevel": initialGlobalDefaultLogLevelValue}) |
Girish Kumar | 78cdb4f | 2020-03-16 13:39:42 +0000 | [diff] [blame] | 113 | } |
| 114 | } |
| 115 | |
| 116 | _, err = c.componentNameConfig.Retrieve(ctx, defaultLogLevelKey) |
| 117 | if err != nil { |
Girish Kumar | e6f45e8 | 2020-03-20 10:46:54 +0000 | [diff] [blame] | 118 | logger.Debugw("failed-to-retrieve-component-default-log-config-at-startup", log.Fields{"error": err}) |
Girish Kumar | 78cdb4f | 2020-03-16 13:39:42 +0000 | [diff] [blame] | 119 | |
| 120 | err = c.componentNameConfig.Save(ctx, defaultLogLevelKey, c.initialLogLevel) |
| 121 | if err != nil { |
Girish Kumar | e6f45e8 | 2020-03-20 10:46:54 +0000 | [diff] [blame] | 122 | logger.Errorw("failed-to-persist-component-default-log-config-at-startup", log.Fields{"error": err, "loglevel": c.initialLogLevel}) |
Girish Kumar | 78cdb4f | 2020-03-16 13:39:42 +0000 | [diff] [blame] | 123 | } |
| 124 | } |
| 125 | } |
| 126 | |
Girish Kumar | 472a5c9 | 2020-04-14 09:14:18 +0000 | [diff] [blame] | 127 | // Method to save list of all registered packages for component into config kvstore. A single string |
| 128 | // is constructed with comma-separated package names in sorted order and persisted |
| 129 | func (c *ComponentLogController) persistRegisteredLogPackageList(ctx context.Context) { |
| 130 | |
| 131 | componentMetadataConfig := c.configManager.InitComponentConfig(c.ComponentName, ConfigTypeMetadata) |
| 132 | logger.Debugw("component-metadata-config", log.Fields{"component-metadata-config": componentMetadataConfig}) |
| 133 | |
| 134 | packageList := log.GetPackageNames() |
| 135 | packageList = append(packageList, defaultLogLevelKey) |
| 136 | sort.Strings(packageList) |
| 137 | |
| 138 | packageNames, err := json.Marshal(packageList) |
| 139 | if err != nil { |
| 140 | logger.Errorw("failed-to-marshal-log-package-list-for-storage", log.Fields{"error": err, "packageList": packageList}) |
| 141 | return |
| 142 | } |
| 143 | |
| 144 | if err := componentMetadataConfig.Save(ctx, logPackagesListKey, string(packageNames)); err != nil { |
| 145 | logger.Errorw("failed-to-persist-component-registered-log-package-list-at-startup", log.Fields{"error": err, "packageNames": packageNames}) |
| 146 | } |
| 147 | } |
| 148 | |
Girish Kumar | 21972fb | 2020-03-13 13:27:47 +0000 | [diff] [blame] | 149 | // ProcessLogConfig will first load and apply log config and then start waiting on component config and global config |
Matteo Scandolo | 2e67748 | 2020-04-09 11:56:27 -0700 | [diff] [blame] | 150 | // channels for any changes. Event channel will be recieved from Backend for valid change type |
| 151 | // Then data for componentn log config and global log config will be retrieved from Backend and stored in updatedLogConfig in precedence order |
divyadesai | 8bf9686 | 2020-02-07 12:24:26 +0000 | [diff] [blame] | 152 | // If any changes in updatedLogConfig will be applied on component |
| 153 | func (c *ComponentLogController) processLogConfig(ctx context.Context) { |
| 154 | |
Girish Kumar | 21972fb | 2020-03-13 13:27:47 +0000 | [diff] [blame] | 155 | // Load and apply Log Config for first time |
| 156 | initialLogConfig, err := c.buildUpdatedLogConfig(ctx) |
| 157 | if err != nil { |
Girish Kumar | e6f45e8 | 2020-03-20 10:46:54 +0000 | [diff] [blame] | 158 | logger.Warnw("unable-to-load-log-config-at-startup", log.Fields{"error": err}) |
Girish Kumar | 21972fb | 2020-03-13 13:27:47 +0000 | [diff] [blame] | 159 | } else { |
| 160 | if err := c.loadAndApplyLogConfig(initialLogConfig); err != nil { |
Girish Kumar | e6f45e8 | 2020-03-20 10:46:54 +0000 | [diff] [blame] | 161 | logger.Warnw("unable-to-apply-log-config-at-startup", log.Fields{"error": err}) |
Girish Kumar | 21972fb | 2020-03-13 13:27:47 +0000 | [diff] [blame] | 162 | } |
| 163 | } |
| 164 | |
divyadesai | 8bf9686 | 2020-02-07 12:24:26 +0000 | [diff] [blame] | 165 | componentConfigEventChan := c.componentNameConfig.MonitorForConfigChange(ctx) |
| 166 | |
| 167 | globalConfigEventChan := c.GlobalConfig.MonitorForConfigChange(ctx) |
| 168 | |
| 169 | // process the events for componentName and global config |
| 170 | var configEvent *ConfigChangeEvent |
| 171 | for { |
| 172 | select { |
| 173 | case configEvent = <-globalConfigEventChan: |
| 174 | case configEvent = <-componentConfigEventChan: |
| 175 | |
| 176 | } |
Girish Kumar | e6f45e8 | 2020-03-20 10:46:54 +0000 | [diff] [blame] | 177 | logger.Debugw("processing-log-config-change", log.Fields{"ChangeType": configEvent.ChangeType, "Package": configEvent.ConfigAttribute}) |
divyadesai | 8bf9686 | 2020-02-07 12:24:26 +0000 | [diff] [blame] | 178 | |
| 179 | updatedLogConfig, err := c.buildUpdatedLogConfig(ctx) |
| 180 | if err != nil { |
Girish Kumar | e6f45e8 | 2020-03-20 10:46:54 +0000 | [diff] [blame] | 181 | logger.Warnw("unable-to-fetch-updated-log-config", log.Fields{"error": err}) |
divyadesai | 8bf9686 | 2020-02-07 12:24:26 +0000 | [diff] [blame] | 182 | continue |
| 183 | } |
| 184 | |
Girish Kumar | e6f45e8 | 2020-03-20 10:46:54 +0000 | [diff] [blame] | 185 | logger.Debugw("applying-updated-log-config", log.Fields{"updated-log-config": updatedLogConfig}) |
divyadesai | 8bf9686 | 2020-02-07 12:24:26 +0000 | [diff] [blame] | 186 | |
| 187 | if err := c.loadAndApplyLogConfig(updatedLogConfig); err != nil { |
Girish Kumar | e6f45e8 | 2020-03-20 10:46:54 +0000 | [diff] [blame] | 188 | logger.Warnw("unable-to-load-and-apply-log-config", log.Fields{"error": err}) |
divyadesai | 8bf9686 | 2020-02-07 12:24:26 +0000 | [diff] [blame] | 189 | } |
| 190 | } |
| 191 | |
| 192 | } |
| 193 | |
| 194 | // get active loglevel from the zap logger |
Girish Kumar | 21972fb | 2020-03-13 13:27:47 +0000 | [diff] [blame] | 195 | func getActiveLogLevels() map[string]string { |
| 196 | loglevels := make(map[string]string) |
divyadesai | 8bf9686 | 2020-02-07 12:24:26 +0000 | [diff] [blame] | 197 | |
| 198 | // now do the default log level |
| 199 | if level, err := log.LogLevelToString(log.GetDefaultLogLevel()); err == nil { |
Girish Kumar | 21972fb | 2020-03-13 13:27:47 +0000 | [diff] [blame] | 200 | loglevels[defaultLogLevelKey] = level |
divyadesai | 8bf9686 | 2020-02-07 12:24:26 +0000 | [diff] [blame] | 201 | } |
| 202 | |
| 203 | // do the per-package log levels |
| 204 | for _, packageName := range log.GetPackageNames() { |
| 205 | level, err := log.GetPackageLogLevel(packageName) |
| 206 | if err != nil { |
Girish Kumar | e6f45e8 | 2020-03-20 10:46:54 +0000 | [diff] [blame] | 207 | logger.Warnw("unable-to-fetch-current-active-loglevel-for-package-name", log.Fields{"package-name": packageName, "error": err}) |
Girish Kumar | 21972fb | 2020-03-13 13:27:47 +0000 | [diff] [blame] | 208 | continue |
divyadesai | 8bf9686 | 2020-02-07 12:24:26 +0000 | [diff] [blame] | 209 | } |
| 210 | |
divyadesai | 8bf9686 | 2020-02-07 12:24:26 +0000 | [diff] [blame] | 211 | if l, err := log.LogLevelToString(level); err == nil { |
Girish Kumar | 21972fb | 2020-03-13 13:27:47 +0000 | [diff] [blame] | 212 | loglevels[packageName] = l |
divyadesai | 8bf9686 | 2020-02-07 12:24:26 +0000 | [diff] [blame] | 213 | } |
divyadesai | 8bf9686 | 2020-02-07 12:24:26 +0000 | [diff] [blame] | 214 | } |
divyadesai | 8bf9686 | 2020-02-07 12:24:26 +0000 | [diff] [blame] | 215 | |
Girish Kumar | e6f45e8 | 2020-03-20 10:46:54 +0000 | [diff] [blame] | 216 | logger.Debugw("retreived-log-levels-from-zap-logger", log.Fields{"loglevels": loglevels}) |
Girish Kumar | 21972fb | 2020-03-13 13:27:47 +0000 | [diff] [blame] | 217 | |
| 218 | return loglevels |
divyadesai | 8bf9686 | 2020-02-07 12:24:26 +0000 | [diff] [blame] | 219 | } |
| 220 | |
| 221 | func (c *ComponentLogController) getGlobalLogConfig(ctx context.Context) (string, error) { |
| 222 | |
Girish Kumar | 78cdb4f | 2020-03-16 13:39:42 +0000 | [diff] [blame] | 223 | globalDefaultLogLevel, err := c.GlobalConfig.Retrieve(ctx, defaultLogLevelKey) |
divyadesai | 8bf9686 | 2020-02-07 12:24:26 +0000 | [diff] [blame] | 224 | if err != nil { |
| 225 | return "", err |
| 226 | } |
| 227 | |
Girish Kumar | 78cdb4f | 2020-03-16 13:39:42 +0000 | [diff] [blame] | 228 | // Handle edge cases when global default loglevel is deleted directly from etcd or set to a invalid value |
| 229 | // We should use hard-coded initial default value in such cases |
| 230 | if globalDefaultLogLevel == "" { |
Girish Kumar | e6f45e8 | 2020-03-20 10:46:54 +0000 | [diff] [blame] | 231 | logger.Warn("global-default-loglevel-not-found-in-config-store") |
Girish Kumar | 78cdb4f | 2020-03-16 13:39:42 +0000 | [diff] [blame] | 232 | globalDefaultLogLevel = initialGlobalDefaultLogLevelValue |
divyadesai | 8bf9686 | 2020-02-07 12:24:26 +0000 | [diff] [blame] | 233 | } |
Girish Kumar | 21972fb | 2020-03-13 13:27:47 +0000 | [diff] [blame] | 234 | |
Girish Kumar | 78cdb4f | 2020-03-16 13:39:42 +0000 | [diff] [blame] | 235 | if _, err := log.StringToLogLevel(globalDefaultLogLevel); err != nil { |
Girish Kumar | e6f45e8 | 2020-03-20 10:46:54 +0000 | [diff] [blame] | 236 | logger.Warnw("unsupported-loglevel-config-defined-at-global-default", log.Fields{"log-level": globalDefaultLogLevel}) |
Girish Kumar | 78cdb4f | 2020-03-16 13:39:42 +0000 | [diff] [blame] | 237 | globalDefaultLogLevel = initialGlobalDefaultLogLevelValue |
| 238 | } |
| 239 | |
Girish Kumar | e6f45e8 | 2020-03-20 10:46:54 +0000 | [diff] [blame] | 240 | logger.Debugw("retrieved-global-default-loglevel", log.Fields{"level": globalDefaultLogLevel}) |
divyadesai | 8bf9686 | 2020-02-07 12:24:26 +0000 | [diff] [blame] | 241 | |
| 242 | return globalDefaultLogLevel, nil |
| 243 | } |
| 244 | |
Girish Kumar | 21972fb | 2020-03-13 13:27:47 +0000 | [diff] [blame] | 245 | func (c *ComponentLogController) getComponentLogConfig(ctx context.Context, globalDefaultLogLevel string) (map[string]string, error) { |
divyadesai | 8bf9686 | 2020-02-07 12:24:26 +0000 | [diff] [blame] | 246 | componentLogConfig, err := c.componentNameConfig.RetrieveAll(ctx) |
| 247 | if err != nil { |
| 248 | return nil, err |
| 249 | } |
| 250 | |
Girish Kumar | 21972fb | 2020-03-13 13:27:47 +0000 | [diff] [blame] | 251 | effectiveDefaultLogLevel := "" |
| 252 | for logConfigKey, logConfigValue := range componentLogConfig { |
| 253 | if _, err := log.StringToLogLevel(logConfigValue); err != nil || logConfigKey == "" { |
Girish Kumar | e6f45e8 | 2020-03-20 10:46:54 +0000 | [diff] [blame] | 254 | logger.Warnw("unsupported-loglevel-config-defined-at-component-context", log.Fields{"package-name": logConfigKey, "log-level": logConfigValue}) |
Girish Kumar | 21972fb | 2020-03-13 13:27:47 +0000 | [diff] [blame] | 255 | delete(componentLogConfig, logConfigKey) |
divyadesai | 8bf9686 | 2020-02-07 12:24:26 +0000 | [diff] [blame] | 256 | } else { |
Girish Kumar | 21972fb | 2020-03-13 13:27:47 +0000 | [diff] [blame] | 257 | if logConfigKey == defaultLogLevelKey { |
| 258 | effectiveDefaultLogLevel = componentLogConfig[defaultLogLevelKey] |
divyadesai | 8bf9686 | 2020-02-07 12:24:26 +0000 | [diff] [blame] | 259 | } |
| 260 | } |
| 261 | } |
Girish Kumar | 21972fb | 2020-03-13 13:27:47 +0000 | [diff] [blame] | 262 | |
| 263 | // if default loglevel is not configured for the component, component should use |
Girish Kumar | 78cdb4f | 2020-03-16 13:39:42 +0000 | [diff] [blame] | 264 | // default loglevel configured at global level |
Girish Kumar | 21972fb | 2020-03-13 13:27:47 +0000 | [diff] [blame] | 265 | if effectiveDefaultLogLevel == "" { |
| 266 | effectiveDefaultLogLevel = globalDefaultLogLevel |
divyadesai | 8bf9686 | 2020-02-07 12:24:26 +0000 | [diff] [blame] | 267 | } |
Girish Kumar | 21972fb | 2020-03-13 13:27:47 +0000 | [diff] [blame] | 268 | |
| 269 | componentLogConfig[defaultLogLevelKey] = effectiveDefaultLogLevel |
| 270 | |
Girish Kumar | e6f45e8 | 2020-03-20 10:46:54 +0000 | [diff] [blame] | 271 | logger.Debugw("retrieved-component-log-config", log.Fields{"component-log-level": componentLogConfig}) |
divyadesai | 8bf9686 | 2020-02-07 12:24:26 +0000 | [diff] [blame] | 272 | |
| 273 | return componentLogConfig, nil |
| 274 | } |
| 275 | |
Matteo Scandolo | 2e67748 | 2020-04-09 11:56:27 -0700 | [diff] [blame] | 276 | // buildUpdatedLogConfig retrieve the global logConfig and component logConfig from Backend |
divyadesai | 8bf9686 | 2020-02-07 12:24:26 +0000 | [diff] [blame] | 277 | // component logConfig stores the log config with precedence order |
| 278 | // For example, If the global logConfig is set and component logConfig is set only for specific package then |
| 279 | // component logConfig is stored with global logConfig and component logConfig of specific package |
| 280 | // For example, If the global logConfig is set and component logConfig is set for specific package and as well as for default then |
| 281 | // component logConfig is stored with component logConfig data only |
| 282 | func (c *ComponentLogController) buildUpdatedLogConfig(ctx context.Context) (map[string]string, error) { |
| 283 | globalLogLevel, err := c.getGlobalLogConfig(ctx) |
| 284 | if err != nil { |
Girish Kumar | e6f45e8 | 2020-03-20 10:46:54 +0000 | [diff] [blame] | 285 | logger.Errorw("unable-to-retrieve-global-log-config", log.Fields{"err": err}) |
divyadesai | 8bf9686 | 2020-02-07 12:24:26 +0000 | [diff] [blame] | 286 | } |
| 287 | |
Girish Kumar | 21972fb | 2020-03-13 13:27:47 +0000 | [diff] [blame] | 288 | componentLogConfig, err := c.getComponentLogConfig(ctx, globalLogLevel) |
divyadesai | 8bf9686 | 2020-02-07 12:24:26 +0000 | [diff] [blame] | 289 | if err != nil { |
| 290 | return nil, err |
| 291 | } |
| 292 | |
Girish Kumar | 21972fb | 2020-03-13 13:27:47 +0000 | [diff] [blame] | 293 | finalLogConfig := make(map[string]string) |
| 294 | for packageName, logLevel := range componentLogConfig { |
| 295 | finalLogConfig[strings.ReplaceAll(packageName, "#", "/")] = logLevel |
| 296 | } |
| 297 | |
| 298 | return finalLogConfig, nil |
divyadesai | 8bf9686 | 2020-02-07 12:24:26 +0000 | [diff] [blame] | 299 | } |
| 300 | |
| 301 | // load and apply the current configuration for component name |
| 302 | // create hash of loaded configuration using GenerateLogConfigHash |
| 303 | // if there is previous hash stored, compare the hash to stored hash |
| 304 | // if there is any change will call UpdateLogLevels |
| 305 | func (c *ComponentLogController) loadAndApplyLogConfig(logConfig map[string]string) error { |
| 306 | currentLogHash, err := GenerateLogConfigHash(logConfig) |
| 307 | if err != nil { |
| 308 | return err |
| 309 | } |
| 310 | |
divyadesai | 8bf9686 | 2020-02-07 12:24:26 +0000 | [diff] [blame] | 311 | if c.logHash != currentLogHash { |
| 312 | UpdateLogLevels(logConfig) |
| 313 | c.logHash = currentLogHash |
Girish Kumar | 21972fb | 2020-03-13 13:27:47 +0000 | [diff] [blame] | 314 | } else { |
Girish Kumar | e6f45e8 | 2020-03-20 10:46:54 +0000 | [diff] [blame] | 315 | logger.Debug("effective-loglevel-config-same-as-currently-active") |
divyadesai | 8bf9686 | 2020-02-07 12:24:26 +0000 | [diff] [blame] | 316 | } |
Girish Kumar | 21972fb | 2020-03-13 13:27:47 +0000 | [diff] [blame] | 317 | |
divyadesai | 8bf9686 | 2020-02-07 12:24:26 +0000 | [diff] [blame] | 318 | return nil |
| 319 | } |
| 320 | |
Girish Kumar | 21972fb | 2020-03-13 13:27:47 +0000 | [diff] [blame] | 321 | // createModifiedLogLevels loops through the activeLogLevels recieved from zap logger and updatedLogLevels recieved from buildUpdatedLogConfig |
| 322 | // to identify and create map of modified Log Levels of 2 types: |
| 323 | // - Packages for which log level has been changed |
| 324 | // - Packages for which log level config has been cleared - set to default log level |
| 325 | func createModifiedLogLevels(activeLogLevels, updatedLogLevels map[string]string) map[string]string { |
| 326 | defaultLevel := updatedLogLevels[defaultLogLevelKey] |
divyadesai | 8bf9686 | 2020-02-07 12:24:26 +0000 | [diff] [blame] | 327 | |
Girish Kumar | 21972fb | 2020-03-13 13:27:47 +0000 | [diff] [blame] | 328 | modifiedLogLevels := make(map[string]string) |
divyadesai | 8bf9686 | 2020-02-07 12:24:26 +0000 | [diff] [blame] | 329 | for activeKey, activeLevel := range activeLogLevels { |
| 330 | if _, exist := updatedLogLevels[activeKey]; !exist { |
Girish Kumar | 21972fb | 2020-03-13 13:27:47 +0000 | [diff] [blame] | 331 | if activeLevel != defaultLevel { |
| 332 | modifiedLogLevels[activeKey] = defaultLevel |
divyadesai | 8bf9686 | 2020-02-07 12:24:26 +0000 | [diff] [blame] | 333 | } |
Girish Kumar | 21972fb | 2020-03-13 13:27:47 +0000 | [diff] [blame] | 334 | } else if activeLevel != updatedLogLevels[activeKey] { |
| 335 | modifiedLogLevels[activeKey] = updatedLogLevels[activeKey] |
divyadesai | 8bf9686 | 2020-02-07 12:24:26 +0000 | [diff] [blame] | 336 | } |
| 337 | } |
Girish Kumar | 21972fb | 2020-03-13 13:27:47 +0000 | [diff] [blame] | 338 | |
| 339 | // Log warnings for all invalid packages for which log config has been set |
| 340 | for key, value := range updatedLogLevels { |
| 341 | if _, exist := activeLogLevels[key]; !exist { |
Girish Kumar | e6f45e8 | 2020-03-20 10:46:54 +0000 | [diff] [blame] | 342 | logger.Warnw("ignoring-loglevel-set-for-invalid-package", log.Fields{"package": key, "log-level": value}) |
Girish Kumar | 21972fb | 2020-03-13 13:27:47 +0000 | [diff] [blame] | 343 | } |
| 344 | } |
| 345 | |
| 346 | return modifiedLogLevels |
divyadesai | 8bf9686 | 2020-02-07 12:24:26 +0000 | [diff] [blame] | 347 | } |
| 348 | |
| 349 | // updateLogLevels update the loglevels for the component |
| 350 | // retrieve active confguration from logger |
| 351 | // compare with entries one by one and apply |
Girish Kumar | 21972fb | 2020-03-13 13:27:47 +0000 | [diff] [blame] | 352 | func UpdateLogLevels(updatedLogConfig map[string]string) { |
divyadesai | 8bf9686 | 2020-02-07 12:24:26 +0000 | [diff] [blame] | 353 | |
Girish Kumar | 21972fb | 2020-03-13 13:27:47 +0000 | [diff] [blame] | 354 | activeLogLevels := getActiveLogLevels() |
| 355 | changedLogLevels := createModifiedLogLevels(activeLogLevels, updatedLogConfig) |
| 356 | |
| 357 | // If no changed log levels are found, just return. It may happen on configuration of a invalid package |
| 358 | if len(changedLogLevels) == 0 { |
Girish Kumar | e6f45e8 | 2020-03-20 10:46:54 +0000 | [diff] [blame] | 359 | logger.Debug("no-change-in-effective-loglevel-config") |
Girish Kumar | 21972fb | 2020-03-13 13:27:47 +0000 | [diff] [blame] | 360 | return |
| 361 | } |
| 362 | |
Girish Kumar | e6f45e8 | 2020-03-20 10:46:54 +0000 | [diff] [blame] | 363 | logger.Debugw("applying-log-level-for-modified-packages", log.Fields{"changed-log-levels": changedLogLevels}) |
Girish Kumar | 21972fb | 2020-03-13 13:27:47 +0000 | [diff] [blame] | 364 | for key, level := range changedLogLevels { |
| 365 | if key == defaultLogLevelKey { |
divyadesai | 8bf9686 | 2020-02-07 12:24:26 +0000 | [diff] [blame] | 366 | if l, err := log.StringToLogLevel(level); err == nil { |
| 367 | log.SetDefaultLogLevel(l) |
| 368 | } |
| 369 | } else { |
divyadesai | 8bf9686 | 2020-02-07 12:24:26 +0000 | [diff] [blame] | 370 | if l, err := log.StringToLogLevel(level); err == nil { |
Girish Kumar | 21972fb | 2020-03-13 13:27:47 +0000 | [diff] [blame] | 371 | log.SetPackageLogLevel(key, l) |
divyadesai | 8bf9686 | 2020-02-07 12:24:26 +0000 | [diff] [blame] | 372 | } |
| 373 | } |
| 374 | } |
divyadesai | 8bf9686 | 2020-02-07 12:24:26 +0000 | [diff] [blame] | 375 | } |
| 376 | |
| 377 | // generate md5 hash of key value pairs appended into a single string |
| 378 | // in order by key name |
| 379 | func GenerateLogConfigHash(createHashLog map[string]string) ([16]byte, error) { |
| 380 | createHashLogBytes := []byte{} |
| 381 | levelData, err := json.Marshal(createHashLog) |
| 382 | if err != nil { |
| 383 | return [16]byte{}, err |
| 384 | } |
| 385 | createHashLogBytes = append(createHashLogBytes, levelData...) |
| 386 | return md5.Sum(createHashLogBytes), nil |
| 387 | } |