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