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