blob: 8f4131e867a190bd5ac0c0193067ba3202fd7d1d [file] [log] [blame]
Girish Kumare9d35bb2020-08-18 06:47:59 +00001/*
Joey Armstrong9cdee9f2024-01-03 04:56:14 -05002 * Copyright 2018-2024 Open Networking Foundation (ONF) and the ONF Contributors
Girish Kumare9d35bb2020-08-18 06:47:59 +00003
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 Kumare9d35bb2020-08-18 06:47:59 +000022 "os"
23 "strings"
khenaidoo26721882021-08-11 17:42:52 -040024
25 "github.com/opencord/voltha-lib-go/v7/pkg/log"
Girish Kumare9d35bb2020-08-18 06:47:59 +000026)
27
28const (
Neha Sharmaa43ef272020-08-25 07:25:13 +000029 defaultTracingStatusKey = "trace_publish" // kvstore key containing tracing configuration status
30 defaultLogCorrelationStatusKey = "log_correlation" // kvstore key containing log correlation configuration status
Girish Kumare9d35bb2020-08-18 06:47:59 +000031)
32
33// ComponentLogFeatureController represents Configuration for Logging related features of Tracing and Log
34// Correlation of specific Voltha component.
35type ComponentLogFeaturesController struct {
Neha Sharmaa43ef272020-08-25 07:25:13 +000036 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 Kumare9d35bb2020-08-18 06:47:59 +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()
Neha Sharmaa43ef272020-08-25 07:25:13 +000051 logCorrelationStatus := log.GetGlobalLFM().GetLogCorrelationStatus()
Girish Kumare9d35bb2020-08-18 06:47:59 +000052
53 return &ComponentLogFeaturesController{
Neha Sharmaa43ef272020-08-25 07:25:13 +000054 ComponentName: componentName,
55 componentNameConfig: nil,
56 configManager: cm,
57 initialTracingStatus: tracingStatus,
58 initialLogCorrelationStatus: logCorrelationStatus,
Girish Kumare9d35bb2020-08-18 06:47:59 +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 }
Neha Sharmaa43ef272020-08-25 07:25:13 +000095
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 Kumare9d35bb2020-08-18 06:47:59 +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
Neha Sharmaa43ef272020-08-25 07:25:13 +0000113 // Load and apply Tracing Status and log correlation status for first time
Girish Kumare9d35bb2020-08-18 06:47:59 +0000114 cc.loadAndApplyTracingStatusUpdate(ctx)
Neha Sharmaa43ef272020-08-25 07:25:13 +0000115 cc.loadAndApplyLogCorrelationStatusUpdate(ctx)
Girish Kumare9d35bb2020-08-18 06:47:59 +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)
Neha Sharmaa43ef272020-08-25 07:25:13 +0000131 } else if strings.HasSuffix(configEvent.ConfigAttribute, defaultLogCorrelationStatusKey) {
132 cc.loadAndApplyLogCorrelationStatusUpdate(ctx)
Girish Kumare9d35bb2020-08-18 06:47:59 +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" {
Neha Sharmaa43ef272020-08-25 07:25:13 +0000148 logger.Warnw(ctx, "unsupported-tracing-status-configured-in-config-store", log.Fields{"failed-tracing-status": desiredTracingStatus, "tracing-status": log.GetGlobalLFM().GetTracePublishingStatus()})
Girish Kumare9d35bb2020-08-18 06:47:59 +0000149 return
150 }
151
152 logger.Debugw(ctx, "retrieved-tracing-status", log.Fields{"tracing-status": desiredTracingStatus})
153
154 log.GetGlobalLFM().SetTracePublishingStatus(desiredTracingStatus == "ENABLED")
155}
Neha Sharmaa43ef272020-08-25 07:25:13 +0000156
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}