Skip to content

Commit

Permalink
Change the approach for counting lines as regex
Browse files Browse the repository at this point in the history
  • Loading branch information
riya-17 committed May 6, 2024
1 parent e1e7472 commit 855f39f
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 41 deletions.
71 changes: 40 additions & 31 deletions opl/cluster_read.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

from . import data
from . import date
from collections import Counter
from tenacity import * # noqa: F403


Expand Down Expand Up @@ -66,7 +67,7 @@ def measure(self, ri, **args):

def _dump_raw_data(self, name, mydata):
"""
Dumps raw data for monitoring plagins into CSV files (first column
Dumps raw data for monitoring plugins into CSV files (first column
for timestamp, second for value) into provided directory.
"""
if self.args.monitoring_raw_data_dir is None:
Expand Down Expand Up @@ -374,39 +375,47 @@ def measure(self, ri, name, command, output="text"):


class CountLinePlugin(BasePlugin):
def measure(self, ri, name, command, output="text"):
def check_word_presence(self, word, line):
pattern = r"\b{}\b".format(re.escape(word))
return bool(re.search(pattern, line))

def measure(
self,
ri,
name,
log_source_command,
log_regexp_error,
log_regexp_warning,
output="text",
):
"""
Execute command "command" and return result as per its "output" configuration
"""
error_count = 0
warning_count = 0
line_count = 0
result = execute(log_source_command)

# Execute the command
result = execute(command)
result_lines = result.splitlines()

for line in result_lines:
line_count += 1
if line["level"] == "error":
error_count += 1
elif line["level"] == "warning":
warning_count += 1

final_result = {
"measurements": {
"logs": {
"openshift-pipelines": {
"pipelines-as-code-controller": {
"all": line_count,
"error": error_count,
"warning": warning_count,
}
}
}
}
}
return final_result
regex_lst = ["log_regexp_error"[len("log_regexp_") :]]
regex_lst.append("log_regexp_warning"[len("log_regexp_") :])

countLine = Counter()

for line in result.splitlines():
countLine["all"] += 1
for rex in regex_lst:
rex_split = rex.split("_")
if len(rex_split) > 1:
flag = 0
for word in rex_split:
if not self.check_word_presence(word, line):
flag = 1
break
if flag == 1:
continue
else:
if not self.check_word_presence(rex, line):
continue
countLine[rex] += 1

return name, countLine


class CopyFromPlugin(BasePlugin):
Expand Down Expand Up @@ -434,7 +443,7 @@ def measure(self, ri, name, **kwargs):
"env_variable": EnvironmentPlugin,
"command": CommandPlugin,
"copy_from": CopyFromPlugin,
"count_line": CountLinePlugin,
"log_source_command": CountLinePlugin,
"monitoring_query": PrometheusMeasurementsPlugin,
"grafana_target": GrafanaMeasurementsPlugin,
"metric_query": PerformanceInsightsMeasurementPlugin,
Expand Down
4 changes: 0 additions & 4 deletions opl/cluster_read_config.yaml

This file was deleted.

5 changes: 5 additions & 0 deletions opl/cluster_read_example.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,8 @@

- name: parameters.env.JOB_NAME_ID
env_variable: JOB_NAME_ID

- name: measurements.logs.openshift-pipelines.pipelines-as-code-controller
log_source_command: oc -n openshift-pipelines logs --since=10h --all-containers --selector app.kubernetes.io/component=controller,app.kubernetes.io/instance=default,app.kubernetes.io/name=controller,app.kubernetes.io/part-of=pipelines-as-code,app=pipelines-as-code-controller
log_regexp_error: '"level":"error"'
log_regexp_warning: '"level":"warning"'
10 changes: 4 additions & 6 deletions tests/test_cluster_read.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,17 +31,15 @@ def test_date(self):

def test_count(self):
string = """
- name: measurements.logs.openshift-pipelines.pipelines-as-code-controller
log_source_command: oc -n openshift-pipelines logs --since=10h --all-containers --selector app.kubernetes.io/component=controller,app.kubernetes.io/instance=default
- name: print.output
log_source_command: echo -e "error line_1log_line4"
log_regexp_error: '"level":"error"'
log_regexp_warning: '"level":"warning"'
"""
ri = opl.cluster_read.RequestedInfo(string)
k, v = next(ri)
self.assertEqual(
k, "measurements.logs.openshift-pipelines.pipelines-as-code-controller"
)
self.assertEqual(v, None)
self.assertEqual(k, "print.output")
self.assertEqual(v, {"all": 1, "error": 1})

def test_json(self):
string = """
Expand Down

0 comments on commit 855f39f

Please sign in to comment.