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" |
| 22 | "github.com/opencord/voltha-lib-go/v3/pkg/log" |
| 23 | "os" |
| 24 | "strings" |
| 25 | ) |
| 26 | |
| 27 | const ( |
| 28 | defaultTracingStatusKey = "trace_publish" // kvstore key containing tracing configuration status |
| 29 | ) |
| 30 | |
| 31 | // ComponentLogFeatureController represents Configuration for Logging related features of Tracing and Log |
| 32 | // Correlation of specific Voltha component. |
| 33 | type ComponentLogFeaturesController struct { |
| 34 | ComponentName string |
| 35 | componentNameConfig *ComponentConfig |
| 36 | configManager *ConfigManager |
| 37 | initialTracingStatus bool // Initial default tracing status set by helm chart |
| 38 | } |
| 39 | |
| 40 | func NewComponentLogFeaturesController(ctx context.Context, cm *ConfigManager) (*ComponentLogFeaturesController, error) { |
| 41 | logger.Debug(ctx, "creating-new-component-log-features-controller") |
| 42 | componentName := os.Getenv("COMPONENT_NAME") |
| 43 | if componentName == "" { |
| 44 | return nil, errors.New("Unable to retrieve PoD Component Name from Runtime env") |
| 45 | } |
| 46 | |
| 47 | tracingStatus := log.GetGlobalLFM().GetTracePublishingStatus() |
| 48 | |
| 49 | return &ComponentLogFeaturesController{ |
| 50 | ComponentName: componentName, |
| 51 | componentNameConfig: nil, |
| 52 | configManager: cm, |
| 53 | initialTracingStatus: tracingStatus, |
| 54 | }, nil |
| 55 | |
| 56 | } |
| 57 | |
| 58 | // StartLogFeaturesConfigProcessing persists initial config of Log Features into Config Store before |
| 59 | // starting the loading and processing of Configuration updates |
| 60 | func StartLogFeaturesConfigProcessing(cm *ConfigManager, ctx context.Context) { |
| 61 | cc, err := NewComponentLogFeaturesController(ctx, cm) |
| 62 | if err != nil { |
| 63 | logger.Errorw(ctx, "unable-to-construct-component-log-features-controller-instance-for-monitoring", log.Fields{"error": err}) |
| 64 | return |
| 65 | } |
| 66 | |
| 67 | cc.componentNameConfig = cm.InitComponentConfig(cc.ComponentName, ConfigTypeLogFeatures) |
| 68 | logger.Debugw(ctx, "component-log-features-config", log.Fields{"cc-component-name-config": cc.componentNameConfig}) |
| 69 | |
| 70 | cc.persistInitialLogFeaturesConfigs(ctx) |
| 71 | |
| 72 | cc.processLogFeaturesConfig(ctx) |
| 73 | } |
| 74 | |
| 75 | // Method to persist Initial status of Log Correlation and Tracing features (as set from command line) |
| 76 | // into config store (etcd kvstore), if not set yet |
| 77 | func (cc *ComponentLogFeaturesController) persistInitialLogFeaturesConfigs(ctx context.Context) { |
| 78 | |
| 79 | _, err := cc.componentNameConfig.Retrieve(ctx, defaultTracingStatusKey) |
| 80 | if err != nil { |
| 81 | statusString := "DISABLED" |
| 82 | if cc.initialTracingStatus { |
| 83 | statusString = "ENABLED" |
| 84 | } |
| 85 | err = cc.componentNameConfig.Save(ctx, defaultTracingStatusKey, statusString) |
| 86 | if err != nil { |
| 87 | logger.Errorw(ctx, "failed-to-persist-component-initial-tracing-status-at-startup", log.Fields{"error": err, "tracingstatus": statusString}) |
| 88 | } |
| 89 | } |
| 90 | } |
| 91 | |
| 92 | // processLogFeaturesConfig will first load and apply configuration of log features. Then it will start waiting for any changes |
| 93 | // made to configuration in config store (etcd) and apply the same |
| 94 | func (cc *ComponentLogFeaturesController) processLogFeaturesConfig(ctx context.Context) { |
| 95 | |
| 96 | // Load and apply Tracing Status for first time |
| 97 | cc.loadAndApplyTracingStatusUpdate(ctx) |
| 98 | |
| 99 | componentConfigEventChan := cc.componentNameConfig.MonitorForConfigChange(ctx) |
| 100 | |
| 101 | // process the change events received on the channel |
| 102 | var configEvent *ConfigChangeEvent |
| 103 | for { |
| 104 | select { |
| 105 | case <-ctx.Done(): |
| 106 | return |
| 107 | |
| 108 | case configEvent = <-componentConfigEventChan: |
| 109 | logger.Debugw(ctx, "processing-log-features-config-change", log.Fields{"ChangeType": configEvent.ChangeType, "Package": configEvent.ConfigAttribute}) |
| 110 | |
| 111 | if strings.HasSuffix(configEvent.ConfigAttribute, defaultTracingStatusKey) { |
| 112 | cc.loadAndApplyTracingStatusUpdate(ctx) |
| 113 | } |
| 114 | } |
| 115 | } |
| 116 | |
| 117 | } |
| 118 | |
| 119 | func (cc *ComponentLogFeaturesController) loadAndApplyTracingStatusUpdate(ctx context.Context) { |
| 120 | |
| 121 | desiredTracingStatus, err := cc.componentNameConfig.Retrieve(ctx, defaultTracingStatusKey) |
| 122 | if err != nil || desiredTracingStatus == "" { |
| 123 | logger.Warn(ctx, "unable-to-retrieve-tracing-status-from-config-store") |
| 124 | return |
| 125 | } |
| 126 | |
| 127 | if desiredTracingStatus != "ENABLED" && desiredTracingStatus != "DISABLED" { |
| 128 | logger.Warnw(ctx, "unsupported-tracing-status-configured-in-config-store", log.Fields{"tracing-status": desiredTracingStatus}) |
| 129 | return |
| 130 | } |
| 131 | |
| 132 | logger.Debugw(ctx, "retrieved-tracing-status", log.Fields{"tracing-status": desiredTracingStatus}) |
| 133 | |
| 134 | log.GetGlobalLFM().SetTracePublishingStatus(desiredTracingStatus == "ENABLED") |
| 135 | } |