Skip to content

Commit

Permalink
feat(provider): add logger option to console provider (#2043)
Browse files Browse the repository at this point in the history
Co-authored-by: Tal Borenstein <[email protected]>
  • Loading branch information
shahargl and talboren authored Sep 30, 2024
1 parent f06625e commit 29718c1
Show file tree
Hide file tree
Showing 8 changed files with 46 additions and 17 deletions.
15 changes: 11 additions & 4 deletions docs/providers/documentation/console-provider.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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"
)
)
Expand Down
2 changes: 1 addition & 1 deletion examples/workflows/aks_basic.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }}"
2 changes: 1 addition & 1 deletion examples/workflows/change.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,4 @@ workflow:
provider:
type: console
with:
alert_message: "Hello world"
message: "Hello world"
12 changes: 12 additions & 0 deletions examples/workflows/console_example.yml
Original file line number Diff line number Diff line change
@@ -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"
2 changes: 1 addition & 1 deletion examples/workflows/gke.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }}"
2 changes: 1 addition & 1 deletion examples/workflows/severity_changed.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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)"
26 changes: 18 additions & 8 deletions keep/providers/console_provider/console_provider.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -25,20 +26,25 @@ 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.
Args:
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

Expand All @@ -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",
)
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -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"
Expand Down

0 comments on commit 29718c1

Please sign in to comment.