blob: 95c5bdecd891af5ece1668017d6ced925cd86ca6 [file] [log] [blame]
Girish Kumarcd402012020-08-18 12:17:38 +00001/*
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
17package config
18
19import (
20 "context"
21 "errors"
Andrea Campanella18448bc2021-07-08 18:47:22 +020022 "github.com/opencord/voltha-lib-go/v5/pkg/log"
Girish Kumarcd402012020-08-18 12:17:38 +000023 "os"
24 "strings"
25)
26
27const (
Maninder12b909f2020-10-23 14:23:36 +053028 defaultTracingStatusKey = "trace_publish" // kvstore key containing tracing configuration status
29 defaultLogCorrelationStatusKey = "log_correlation" // kvstore key containing log correlation configuration status
Girish Kumarcd402012020-08-18 12:17:38 +000030)
31
32// ComponentLogFeatureController represents Configuration for Logging related features of Tracing and Log
33// Correlation of specific Voltha component.
34type ComponentLogFeaturesController struct {
Maninder12b909f2020-10-23 14:23:36 +053035 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 Kumarcd402012020-08-18 12:17:38 +000040}
41
42func 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()
Maninder12b909f2020-10-23 14:23:36 +053050 logCorrelationStatus := log.GetGlobalLFM().GetLogCorrelationStatus()
Girish Kumarcd402012020-08-18 12:17:38 +000051
52 return &ComponentLogFeaturesController{
Maninder12b909f2020-10-23 14:23:36 +053053 ComponentName: componentName,
54 componentNameConfig: nil,
55 configManager: cm,
56 initialTracingStatus: tracingStatus,
57 initialLogCorrelationStatus: logCorrelationStatus,
Girish Kumarcd402012020-08-18 12:17:38 +000058 }, 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
64func 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
81func (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 }
Maninder12b909f2020-10-23 14:23:36 +053094
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 Kumarcd402012020-08-18 12:17:38 +0000106}
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
110func (cc *ComponentLogFeaturesController) processLogFeaturesConfig(ctx context.Context) {
111
Maninder12b909f2020-10-23 14:23:36 +0530112 // Load and apply Tracing Status and log correlation status for first time
Girish Kumarcd402012020-08-18 12:17:38 +0000113 cc.loadAndApplyTracingStatusUpdate(ctx)
Maninder12b909f2020-10-23 14:23:36 +0530114 cc.loadAndApplyLogCorrelationStatusUpdate(ctx)
Girish Kumarcd402012020-08-18 12:17:38 +0000115
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)
Maninder12b909f2020-10-23 14:23:36 +0530130 } else if strings.HasSuffix(configEvent.ConfigAttribute, defaultLogCorrelationStatusKey) {
131 cc.loadAndApplyLogCorrelationStatusUpdate(ctx)
Girish Kumarcd402012020-08-18 12:17:38 +0000132 }
133 }
134 }
135
136}
137
138func (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" {
Maninder12b909f2020-10-23 14:23:36 +0530147 logger.Warnw(ctx, "unsupported-tracing-status-configured-in-config-store", log.Fields{"failed-tracing-status": desiredTracingStatus, "tracing-status": log.GetGlobalLFM().GetTracePublishingStatus()})
Girish Kumarcd402012020-08-18 12:17:38 +0000148 return
149 }
150
151 logger.Debugw(ctx, "retrieved-tracing-status", log.Fields{"tracing-status": desiredTracingStatus})
152
153 log.GetGlobalLFM().SetTracePublishingStatus(desiredTracingStatus == "ENABLED")
154}
Maninder12b909f2020-10-23 14:23:36 +0530155
156func (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}