Skip to content

Commit

Permalink
update(logs): restricting the duplicate logs (#86)
Browse files Browse the repository at this point in the history
Signed-off-by: shubhamchaudhary <[email protected]>
  • Loading branch information
ispeakc0de authored Jan 31, 2021
1 parent f9fa4af commit 5026b4f
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 7 deletions.
8 changes: 7 additions & 1 deletion controller/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,20 @@ func Exporter(clients clients.ClientSets) {
gaugeMetrics.InitializeGaugeMetrics().
RegisterFixedMetrics()

monitoringEnabled := MonitoringEnabled{
IsChaosResultsAvailable: true,
IsChaosEnginesAvailable: true,
}

for {
if err := gaugeMetrics.GetLitmusChaosMetrics(clients, &overallChaosResults); err != nil {
if err := gaugeMetrics.GetLitmusChaosMetrics(clients, &overallChaosResults, &monitoringEnabled); err != nil {
log.Errorf("err: %v", err)
}
time.Sleep(1000 * time.Millisecond)
}
}

// RegisterFixedMetrics register the prometheus metrics
func (gaugeMetrics *GaugeMetrics) RegisterFixedMetrics() {
prometheus.MustRegister(gaugeMetrics.ResultPassedExperiments)
prometheus.MustRegister(gaugeMetrics.ResultFailedExperiments)
Expand Down
28 changes: 23 additions & 5 deletions controller/scrape.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ import (
var err error

// GetLitmusChaosMetrics derive and send the chaos metrics
func (gaugeMetrics *GaugeMetrics) GetLitmusChaosMetrics(clients clients.ClientSets, overallChaosResults *litmuschaosv1alpha1.ChaosResultList) error {
func (gaugeMetrics *GaugeMetrics) GetLitmusChaosMetrics(clients clients.ClientSets, overallChaosResults *litmuschaosv1alpha1.ChaosResultList, monitoringEnabled *MonitoringEnabled) error {
// initialising the parameters for the namespaced scope metrics
namespacedScopeMetrics := NamespacedScopeMetrics{
PassedExperiments: 0,
Expand All @@ -57,7 +57,7 @@ func (gaugeMetrics *GaugeMetrics) GetLitmusChaosMetrics(clients clients.ClientSe
}
watchNamespace := os.Getenv("WATCH_NAMESPACE")
// Getting list of all the chaosresults for the monitoring
resultList, err := GetResultList(clients, watchNamespace)
resultList, err := GetResultList(clients, watchNamespace, monitoringEnabled)
if err != nil {
return err
}
Expand Down Expand Up @@ -244,7 +244,7 @@ func (awsConfig *AWSConfig) putAwsMetricData(sess *session.Session, metricName s
}

// GetResultList return the result list correspond to the monitoring enabled chaosengine
func GetResultList(clients clients.ClientSets, chaosNamespace string) (litmuschaosv1alpha1.ChaosResultList, error) {
func GetResultList(clients clients.ClientSets, chaosNamespace string, monitoringEnabled *MonitoringEnabled) (litmuschaosv1alpha1.ChaosResultList, error) {

finalChaosResultList := litmuschaosv1alpha1.ChaosResultList{}
chaosEngineList, err := clients.LitmusClient.ChaosEngines(chaosNamespace).List(metav1.ListOptions{})
Expand All @@ -254,19 +254,37 @@ func GetResultList(clients clients.ClientSets, chaosNamespace string) (litmuscha
// filter the chaosengines based on monitoring enabled
filteredChaosEngineList := filterMonitoringEnabledEngines(chaosEngineList)
if len(filteredChaosEngineList.Items) == 0 {
log.Warnf("No chaosengine found with monitoring enabled")
if monitoringEnabled.IsChaosEnginesAvailable {
monitoringEnabled.IsChaosEnginesAvailable = false
log.Warn("No chaosengine found with monitoring enabled")
log.Info("[Wait]: Waiting for the chaosengine with monitoring enabled ... ")
}
return litmuschaosv1alpha1.ChaosResultList{}, nil
}

if !monitoringEnabled.IsChaosEnginesAvailable {
log.Info("[Wait]: Cheers! Wait is over, found desired chaosengine")
monitoringEnabled.IsChaosEnginesAvailable = true
}

chaosResultList, err := clients.LitmusClient.ChaosResults(chaosNamespace).List(metav1.ListOptions{})
if err != nil {
return litmuschaosv1alpha1.ChaosResultList{}, err
}
if len(chaosResultList.Items) == 0 {
log.Warnf("No chaosresult found!")
if monitoringEnabled.IsChaosResultsAvailable {
monitoringEnabled.IsChaosResultsAvailable = false
log.Warnf("No chaosresult found!")
log.Info("[Wait]: Waiting for the chaosresult ... ")
}
return litmuschaosv1alpha1.ChaosResultList{}, nil
}

if !monitoringEnabled.IsChaosResultsAvailable {
log.Info("[Wait]: Cheers! Wait is over, found desired chaosresult")
monitoringEnabled.IsChaosResultsAvailable = true
}

// pick only those chaosresults, which correspond to the filtered chaosengines
for _, chaosresult := range chaosResultList.Items {
for _, chaosengine := range filteredChaosEngineList.Items {
Expand Down
6 changes: 6 additions & 0 deletions controller/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -239,3 +239,9 @@ type GaugeMetrics struct {
ClusterScopedExperimentsInstalledCount *prometheus.GaugeVec
ClusterScopedExperimentsRunCount *prometheus.GaugeVec
}

// MonitoringEnabled contains existance/availability of chaosengines and chaosresults
type MonitoringEnabled struct {
IsChaosResultsAvailable bool
IsChaosEnginesAvailable bool
}
6 changes: 5 additions & 1 deletion tests/bdd/bdd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,11 @@ var _ = Describe("BDD on chaos-exporter", func() {
overallChaosResults := litmuschaosv1alpha1.ChaosResultList{}
gaugeMetrics.InitializeGaugeMetrics().
RegisterFixedMetrics()
err := gaugeMetrics.GetLitmusChaosMetrics(client, &overallChaosResults)
monitoringEnabled := controller.MonitoringEnabled{
IsChaosResultsAvailable: true,
IsChaosEnginesAvailable: true,
}
err := gaugeMetrics.GetLitmusChaosMetrics(client, &overallChaosResults, &monitoringEnabled)
Expect(err).To(BeNil())

})
Expand Down

0 comments on commit 5026b4f

Please sign in to comment.