From 29718c192b69c7fc14d27077df587ad84c1ef3cf Mon Sep 17 00:00:00 2001 From: Shahar Glazner Date: Mon, 30 Sep 2024 14:04:15 +0300 Subject: [PATCH] feat(provider): add logger option to console provider (#2043) Co-authored-by: Tal Borenstein --- .../documentation/console-provider.mdx | 15 ++++++++--- examples/workflows/aks_basic.yml | 2 +- examples/workflows/change.yml | 2 +- examples/workflows/console_example.yml | 12 +++++++++ examples/workflows/gke.yml | 2 +- examples/workflows/severity_changed.yml | 2 +- .../console_provider/console_provider.py | 26 +++++++++++++------ pyproject.toml | 2 +- 8 files changed, 46 insertions(+), 17 deletions(-) create mode 100644 examples/workflows/console_example.yml diff --git a/docs/providers/documentation/console-provider.mdx b/docs/providers/documentation/console-provider.mdx index a9524241e8..4ef5a3c9ff 100644 --- a/docs/providers/documentation/console-provider.mdx +++ b/docs/providers/documentation/console-provider.mdx @@ -5,24 +5,31 @@ description: "Console provider is sort of a mock provider that projects given al --- ## Inputs -- alert_message: The alert message to print to the console + +- message: The alert message to print to the console ## Outputs + This provider has no outputs ## Authentication Parameters + This provider has no authentication ## Connecting with the Provider + This provider doesn't require any connection ## Notes -*No information yet, feel free to contribute it using the "Edit this page" link the buttom of the page* + +_No information yet, feel free to contribute it using the "Edit this page" link the buttom of the page_ ## Useful Links -*No information yet, feel free to contribute it using the "Edit this page" link the buttom of the page* + +_No information yet, feel free to contribute it using the "Edit this page" link the buttom of the page_ ## Example + ```python config = { "description": "Console Output Provider", @@ -32,7 +39,7 @@ provider = ProvidersFactory.get_provider( provider_id='mock', provider_type="console", provider_config=config ) provider.notify( - alert_message="Simple alert showing context with name: {name}".format( + message="Simple alert showing context with name: {name}".format( name="John Doe" ) ) diff --git a/examples/workflows/aks_basic.yml b/examples/workflows/aks_basic.yml index aeeb7f1403..52aa985b05 100644 --- a/examples/workflows/aks_basic.yml +++ b/examples/workflows/aks_basic.yml @@ -17,4 +17,4 @@ workflow: provider: type: console with: - alert_message: "Pod name: {{ foreach.value.metadata.name }} || Namespace: {{ foreach.value.metadata.namespace }} || Status: {{ foreach.value.status.phase }}" + message: "Pod name: {{ foreach.value.metadata.name }} || Namespace: {{ foreach.value.metadata.namespace }} || Status: {{ foreach.value.status.phase }}" diff --git a/examples/workflows/change.yml b/examples/workflows/change.yml index 605b010beb..82f800504a 100644 --- a/examples/workflows/change.yml +++ b/examples/workflows/change.yml @@ -10,4 +10,4 @@ workflow: provider: type: console with: - alert_message: "Hello world" + message: "Hello world" diff --git a/examples/workflows/console_example.yml b/examples/workflows/console_example.yml new file mode 100644 index 0000000000..6e800c07ef --- /dev/null +++ b/examples/workflows/console_example.yml @@ -0,0 +1,12 @@ +workflow: + id: console-example + description: console-example + triggers: + - type: manual + actions: + - name: echo + provider: + type: console + with: + logger: true + message: "Hey" diff --git a/examples/workflows/gke.yml b/examples/workflows/gke.yml index 78629bca93..458e27152e 100644 --- a/examples/workflows/gke.yml +++ b/examples/workflows/gke.yml @@ -17,4 +17,4 @@ alert: provider: type: console with: - alert_message: "Pod name: {{ foreach.value.metadata.name }} || Namespace: {{ foreach.value.metadata.namespace }} || Status: {{ foreach.value.status.phase }}" + message: "Pod name: {{ foreach.value.metadata.name }} || Namespace: {{ foreach.value.metadata.namespace }} || Status: {{ foreach.value.status.phase }}" diff --git a/examples/workflows/severity_changed.yml b/examples/workflows/severity_changed.yml index db852f3f4a..9f9523c137 100644 --- a/examples/workflows/severity_changed.yml +++ b/examples/workflows/severity_changed.yml @@ -10,4 +10,4 @@ workflow: type: console with: # "The severity has changed from warning to info (it has decreased from last alert)" - alert_message: "The severity has changed from {{ alert.previous_severity }} to {{ alert.severity }} (it has {{ alert.severity_change }} since last alert)" + message: "The severity has changed from {{ alert.previous_severity }} to {{ alert.severity }} (it has {{ alert.severity_change }} since last alert)" diff --git a/keep/providers/console_provider/console_provider.py b/keep/providers/console_provider/console_provider.py index cf2af86002..97e135c9b2 100644 --- a/keep/providers/console_provider/console_provider.py +++ b/keep/providers/console_provider/console_provider.py @@ -1,6 +1,7 @@ """ Simple Console Output Provider """ + from keep.contextmanager.contextmanager import ContextManager from keep.providers.base.base_provider import BaseProvider from keep.providers.models.provider_config import ProviderConfig @@ -25,11 +26,7 @@ def dispose(self): # No need to dispose of anything, so just do nothing. pass - def _notify( - self, - message: str = "", - **kwargs: dict - ): + def _notify(self, message: str = "", **kwargs: dict): """ Output alert message simply using the print method. @@ -37,8 +34,17 @@ def _notify( alert_message (str): The alert message to be printed in to the console """ self.logger.debug("Outputting alert message to console") - # message = kwargs.get("alert_message") - print(message) + if kwargs.get("logger", False): + severity = kwargs.get("severity", "info") + try: + getattr(self.logger, severity)(message) + except AttributeError: + self.logger.error(f"Invalid log level {severity}") + # default to print + print(message) + # use print + else: + print(message) self.logger.debug("Alert message outputted to console") return message @@ -63,4 +69,8 @@ def _notify( provider_type="console", provider_config=config, ) - provider.notify(alert_message="Simple alert showing context with name: John Doe") + provider.notify( + alert_message="Simple alert showing context with name: John Doe", + logger=True, + severity="critical", + ) diff --git a/pyproject.toml b/pyproject.toml index b5092e2862..0ccc9b1de1 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,6 +1,6 @@ [tool.poetry] name = "keep" -version = "0.25.0" +version = "0.25.1" description = "Alerting. for developers, by developers." authors = ["Keep Alerting LTD"] readme = "README.md"