Girish Kumar | e9d35bb | 2020-08-18 06:47:59 +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 | |
| 17 | package config |
| 18 | |
| 19 | import ( |
| 20 | "context" |
| 21 | "errors" |
Girish Gowdra | 248971a | 2021-06-01 15:14:15 -0700 | [diff] [blame] | 22 | "github.com/opencord/voltha-lib-go/v5/pkg/log" |
Girish Kumar | e9d35bb | 2020-08-18 06:47:59 +0000 | [diff] [blame] | 23 | "os" |
| 24 | "strings" |
| 25 | ) |
| 26 | |
| 27 | const ( |
Neha Sharma | a43ef27 | 2020-08-25 07:25:13 +0000 | [diff] [blame] | 28 | defaultTracingStatusKey = "trace_publish" // kvstore key containing tracing configuration status |
| 29 | defaultLogCorrelationStatusKey = "log_correlation" // kvstore key containing log correlation configuration status |
Girish Kumar | e9d35bb | 2020-08-18 06:47:59 +0000 | [diff] [blame] | 30 | ) |
| 31 | |
| 32 | // ComponentLogFeatureController represents Configuration for Logging related features of Tracing and Log |
| 33 | // Correlation of specific Voltha component. |
| 34 | type ComponentLogFeaturesController struct { |
Neha Sharma | a43ef27 | 2020-08-25 07:25:13 +0000 | [diff] [blame] | 35 | ComponentName string |
| 36 | componentNameConfig *ComponentConfig |
| 37 | configManager *ConfigManager |
| 38 | initialTracingStatus bool // Initial default tracing status set by helm chart |
| 39 | initialLogCorrelationStatus bool // Initial default log correlation status set by helm chart |
Girish Kumar | e9d35bb | 2020-08-18 06:47:59 +0000 | [diff] [blame] | 40 | } |
| 41 | |
| 42 | func NewComponentLogFeaturesController(ctx context.Context, cm *ConfigManager) (*ComponentLogFeaturesController, error) { |
| 43 | logger.Debug(ctx, "creating-new-component-log-features-controller") |
| 44 | componentName := os.Getenv("COMPONENT_NAME") |
| 45 | if componentName == "" { |
| 46 | return nil, errors.New("Unable to retrieve PoD Component Name from Runtime env") |
| 47 | } |
| 48 | |
| 49 | tracingStatus := log.GetGlobalLFM().GetTracePublishingStatus() |
Neha Sharma | a43ef27 | 2020-08-25 07:25:13 +0000 | [diff] [blame] | 50 | logCorrelationStatus := log.GetGlobalLFM().GetLogCorrelationStatus() |
Girish Kumar | e9d35bb | 2020-08-18 06:47:59 +0000 | [diff] [blame] | 51 | |
| 52 | return &ComponentLogFeaturesController{ |
Neha Sharma | a43ef27 | 2020-08-25 07:25:13 +0000 | [diff] [blame] | 53 | ComponentName: componentName, |
| 54 | componentNameConfig: nil, |
| 55 | configManager: cm, |
| 56 | initialTracingStatus: tracingStatus, |
| 57 | initialLogCorrelationStatus: logCorrelationStatus, |
Girish Kumar | e9d35bb | 2020-08-18 06:47:59 +0000 | [diff] [blame] | 58 | }, nil |
| 59 | |
| 60 | } |
| 61 | |
| 62 | // StartLogFeaturesConfigProcessing persists initial config of Log Features into Config Store before |
| 63 | // starting the loading and processing of Configuration updates |
| 64 | func StartLogFeaturesConfigProcessing(cm *ConfigManager, ctx context.Context) { |
| 65 | cc, err := NewComponentLogFeaturesController(ctx, cm) |
| 66 | if err != nil { |
| 67 | logger.Errorw(ctx, "unable-to-construct-component-log-features-controller-instance-for-monitoring", log.Fields{"error": err}) |
| 68 | return |
| 69 | } |
| 70 | |
| 71 | cc.componentNameConfig = cm.InitComponentConfig(cc.ComponentName, ConfigTypeLogFeatures) |
| 72 | logger.Debugw(ctx, "component-log-features-config", log.Fields{"cc-component-name-config": cc.componentNameConfig}) |
| 73 | |
| 74 | cc.persistInitialLogFeaturesConfigs(ctx) |
| 75 | |
| 76 | cc.processLogFeaturesConfig(ctx) |
| 77 | } |
| 78 | |
| 79 | // Method to persist Initial status of Log Correlation and Tracing features (as set from command line) |
| 80 | // into config store (etcd kvstore), if not set yet |
| 81 | func (cc *ComponentLogFeaturesController) persistInitialLogFeaturesConfigs(ctx context.Context) { |
| 82 | |
| 83 | _, err := cc.componentNameConfig.Retrieve(ctx, defaultTracingStatusKey) |
| 84 | if err != nil { |
| 85 | statusString := "DISABLED" |
| 86 | if cc.initialTracingStatus { |
| 87 | statusString = "ENABLED" |
| 88 | } |
| 89 | err = cc.componentNameConfig.Save(ctx, defaultTracingStatusKey, statusString) |
| 90 | if err != nil { |
| 91 | logger.Errorw(ctx, "failed-to-persist-component-initial-tracing-status-at-startup", log.Fields{"error": err, "tracingstatus": statusString}) |
| 92 | } |
| 93 | } |
Neha Sharma | a43ef27 | 2020-08-25 07:25:13 +0000 | [diff] [blame] | 94 | |
| 95 | _, err = cc.componentNameConfig.Retrieve(ctx, defaultLogCorrelationStatusKey) |
| 96 | if err != nil { |
| 97 | statusString := "DISABLED" |
| 98 | if cc.initialLogCorrelationStatus { |
| 99 | statusString = "ENABLED" |
| 100 | } |
| 101 | err = cc.componentNameConfig.Save(ctx, defaultLogCorrelationStatusKey, statusString) |
| 102 | if err != nil { |
| 103 | logger.Errorw(ctx, "failed-to-persist-component-initial-log-correlation-status-at-startup", log.Fields{"error": err, "logcorrelationstatus": statusString}) |
| 104 | } |
| 105 | } |
Girish Kumar | e9d35bb | 2020-08-18 06:47:59 +0000 | [diff] [blame] | 106 | } |
| 107 | |
| 108 | // processLogFeaturesConfig will first load and apply configuration of log features. Then it will start waiting for any changes |
| 109 | // made to configuration in config store (etcd) and apply the same |
| 110 | func (cc *ComponentLogFeaturesController) processLogFeaturesConfig(ctx context.Context) { |
| 111 | |
Neha Sharma | a43ef27 | 2020-08-25 07:25:13 +0000 | [diff] [blame] | 112 | // Load and apply Tracing Status and log correlation status for first time |
Girish Kumar | e9d35bb | 2020-08-18 06:47:59 +0000 | [diff] [blame] | 113 | cc.loadAndApplyTracingStatusUpdate(ctx) |
Neha Sharma | a43ef27 | 2020-08-25 07:25:13 +0000 | [diff] [blame] | 114 | cc.loadAndApplyLogCorrelationStatusUpdate(ctx) |
Girish Kumar | e9d35bb | 2020-08-18 06:47:59 +0000 | [diff] [blame] | 115 | |
| 116 | componentConfigEventChan := cc.componentNameConfig.MonitorForConfigChange(ctx) |
| 117 | |
| 118 | // process the change events received on the channel |
| 119 | var configEvent *ConfigChangeEvent |
| 120 | for { |
| 121 | select { |
| 122 | case <-ctx.Done(): |
| 123 | return |
| 124 | |
| 125 | case configEvent = <-componentConfigEventChan: |
| 126 | logger.Debugw(ctx, "processing-log-features-config-change", log.Fields{"ChangeType": configEvent.ChangeType, "Package": configEvent.ConfigAttribute}) |
| 127 | |
| 128 | if strings.HasSuffix(configEvent.ConfigAttribute, defaultTracingStatusKey) { |
| 129 | cc.loadAndApplyTracingStatusUpdate(ctx) |
Neha Sharma | a43ef27 | 2020-08-25 07:25:13 +0000 | [diff] [blame] | 130 | } else if strings.HasSuffix(configEvent.ConfigAttribute, defaultLogCorrelationStatusKey) { |
| 131 | cc.loadAndApplyLogCorrelationStatusUpdate(ctx) |
Girish Kumar | e9d35bb | 2020-08-18 06:47:59 +0000 | [diff] [blame] | 132 | } |
| 133 | } |
| 134 | } |
| 135 | |
| 136 | } |
| 137 | |
| 138 | func (cc *ComponentLogFeaturesController) loadAndApplyTracingStatusUpdate(ctx context.Context) { |
| 139 | |
| 140 | desiredTracingStatus, err := cc.componentNameConfig.Retrieve(ctx, defaultTracingStatusKey) |
| 141 | if err != nil || desiredTracingStatus == "" { |
| 142 | logger.Warn(ctx, "unable-to-retrieve-tracing-status-from-config-store") |
| 143 | return |
| 144 | } |
| 145 | |
| 146 | if desiredTracingStatus != "ENABLED" && desiredTracingStatus != "DISABLED" { |
Neha Sharma | a43ef27 | 2020-08-25 07:25:13 +0000 | [diff] [blame] | 147 | logger.Warnw(ctx, "unsupported-tracing-status-configured-in-config-store", log.Fields{"failed-tracing-status": desiredTracingStatus, "tracing-status": log.GetGlobalLFM().GetTracePublishingStatus()}) |
Girish Kumar | e9d35bb | 2020-08-18 06:47:59 +0000 | [diff] [blame] | 148 | return |
| 149 | } |
| 150 | |
| 151 | logger.Debugw(ctx, "retrieved-tracing-status", log.Fields{"tracing-status": desiredTracingStatus}) |
| 152 | |
| 153 | log.GetGlobalLFM().SetTracePublishingStatus(desiredTracingStatus == "ENABLED") |
| 154 | } |
Neha Sharma | a43ef27 | 2020-08-25 07:25:13 +0000 | [diff] [blame] | 155 | |
| 156 | func (cc *ComponentLogFeaturesController) loadAndApplyLogCorrelationStatusUpdate(ctx context.Context) { |
| 157 | |
| 158 | desiredLogCorrelationStatus, err := cc.componentNameConfig.Retrieve(ctx, defaultLogCorrelationStatusKey) |
| 159 | if err != nil || desiredLogCorrelationStatus == "" { |
| 160 | logger.Warn(ctx, "unable-to-retrieve-log-correlation-status-from-config-store") |
| 161 | return |
| 162 | } |
| 163 | |
| 164 | if desiredLogCorrelationStatus != "ENABLED" && desiredLogCorrelationStatus != "DISABLED" { |
| 165 | logger.Warnw(ctx, "unsupported-log-correlation-status-configured-in-config-store", log.Fields{"failed-log-correlation-status": desiredLogCorrelationStatus, "log-correlation-status": log.GetGlobalLFM().GetLogCorrelationStatus()}) |
| 166 | return |
| 167 | } |
| 168 | |
| 169 | logger.Debugw(ctx, "retrieved-log-correlation-status", log.Fields{"log-correlation-status": desiredLogCorrelationStatus}) |
| 170 | |
| 171 | log.GetGlobalLFM().SetLogCorrelationStatus(desiredLogCorrelationStatus == "ENABLED") |
| 172 | } |