blob: 579c1de65627a475e8b7536b2b2bf8e49967068a [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"
Girish Kumarcd402012020-08-18 12:17:38 +000022 "os"
23 "strings"
David K. Bainbridgee05cf0c2021-08-19 03:16:50 +000024
25 "github.com/opencord/voltha-lib-go/v7/pkg/log"
Girish Kumarcd402012020-08-18 12:17:38 +000026)
27
28const (
Maninder12b909f2020-10-23 14:23:36 +053029 defaultTracingStatusKey = "trace_publish" // kvstore key containing tracing configuration status
30 defaultLogCorrelationStatusKey = "log_correlation" // kvstore key containing log correlation configuration status
Girish Kumarcd402012020-08-18 12:17:38 +000031)
32
33// ComponentLogFeatureController represents Configuration for Logging related features of Tracing and Log
34// Correlation of specific Voltha component.
35type ComponentLogFeaturesController struct {
Maninder12b909f2020-10-23 14:23:36 +053036 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 Kumarcd402012020-08-18 12:17:38 +000041}
42
43func 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()
Maninder12b909f2020-10-23 14:23:36 +053051 logCorrelationStatus := log.GetGlobalLFM().GetLogCorrelationStatus()
Girish Kumarcd402012020-08-18 12:17:38 +000052
53 return &ComponentLogFeaturesController{
Maninder12b909f2020-10-23 14:23:36 +053054 ComponentName: componentName,
55 componentNameConfig: nil,
56 configManager: cm,
57 initialTracingStatus: tracingStatus,
58 initialLogCorrelationStatus: logCorrelationStatus,
Girish Kumarcd402012020-08-18 12:17:38 +000059 }, 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
65func 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
82func (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 }
Maninder12b909f2020-10-23 14:23:36 +053095
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 Kumarcd402012020-08-18 12:17:38 +0000107}
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
111func (cc *ComponentLogFeaturesController) processLogFeaturesConfig(ctx context.Context) {
112
Maninder12b909f2020-10-23 14:23:36 +0530113 // Load and apply Tracing Status and log correlation status for first time
Girish Kumarcd402012020-08-18 12:17:38 +0000114 cc.loadAndApplyTracingStatusUpdate(ctx)
Maninder12b909f2020-10-23 14:23:36 +0530115 cc.loadAndApplyLogCorrelationStatusUpdate(ctx)
Girish Kumarcd402012020-08-18 12:17:38 +0000116
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)
Maninder12b909f2020-10-23 14:23:36 +0530131 } else if strings.HasSuffix(configEvent.ConfigAttribute, defaultLogCorrelationStatusKey) {
132 cc.loadAndApplyLogCorrelationStatusUpdate(ctx)
Girish Kumarcd402012020-08-18 12:17:38 +0000133 }
134 }
135 }
136
137}
138
139func (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" {
Maninder12b909f2020-10-23 14:23:36 +0530148 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 +0000149 return
150 }
151
152 logger.Debugw(ctx, "retrieved-tracing-status", log.Fields{"tracing-status": desiredTracingStatus})
153
154 log.GetGlobalLFM().SetTracePublishingStatus(desiredTracingStatus == "ENABLED")
155}
Maninder12b909f2020-10-23 14:23:36 +0530156
157func (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}