Skip to content

Commit 9a1c5f2

Browse files
authored
Merge pull request #1 from python-discord/case-sensitive-tokens
2 parents c34b71a + 79fcbbd commit 9a1c5f2

4 files changed

Lines changed: 12 additions & 5 deletions

File tree

docs/configuration.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,11 +51,14 @@ color = "#ff5f5f"
5151

5252
[[olli.tokens]]
5353
token = "INFO"
54+
case_sensitive = true
5455
# When no color is provided it falls back to blurple.
5556
```
5657

5758
Tokens accept a `token` and `color` key, the first one is required. `token` represents the phrase that should be searched for and `color` is a hex color (**with** hashtag) that will be used for the generated embeds.
5859

60+
Tokens also accept a `case_sensitive` key which defaults to `false`. If you want to match the case as written in the `token` field set `case_sensitive` to `true` in the token config.
61+
5962
## `loki`
6063

6164
`api_url`: The base URL of your Loki instance, for example `"http://localhost:3100/"`.
@@ -94,6 +97,7 @@ color = "#ff5f5f"
9497
[[olli.tokens]]
9598
token = "WARN"
9699
color = "#ffe24d"
100+
case_sensitive = true
97101

98102
[[olli.tokens]]
99103
token = "INFO"

olli/alert.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ def get_match(token: TokenConfig) -> TokenMatch:
1414
"""Search the configured service logs for a given token."""
1515
try:
1616
logger.debug(f"Searching for token {token.token}")
17-
svc_logs = api_client.get_token_logs(token.token)
17+
svc_logs = api_client.get_token_logs(token)
1818
except httpx.ConnectError:
1919
logger.error("Could not connect to Loki")
2020
return webhook.send_olli_error("Loki refused to connect.")

olli/api.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
import httpx
66

7-
from olli.config import CONFIG
7+
from olli.config import CONFIG, TokenConfig
88

99

1010
class LokiHTTPClient:
@@ -15,7 +15,7 @@ def route(path: str) -> str:
1515
"""Generate the Loki API route for a given path."""
1616
return CONFIG.loki.api_url + "/loki/api/v1/" + path
1717

18-
def get_token_logs(self, token: str) -> dict[str, Any]:
18+
def get_token_logs(self, token: TokenConfig) -> dict[str, Any]:
1919
"""
2020
Fetch the logs from configured services for a matchign token.
2121
@@ -28,8 +28,10 @@ def get_token_logs(self, token: str) -> dict[str, Any]:
2828

2929
job_regex = "|".join(CONFIG.loki.jobs)
3030

31+
case_filter = '(?i)' if not token.case_sensitive else ''
32+
3133
resp = httpx.get(self.route("query_range"), params={
32-
"query": f'{{job=~"({job_regex})"}} |~ "(?i){token}"',
34+
"query": f'{{job=~"({job_regex})"}} |~ "{case_filter}{token.token}"',
3335
"start": f"{start_ts:0.0f}",
3436
"limit": CONFIG.loki.max_logs
3537
})

olli/config.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@ class TokenConfig(BaseModel):
2424
"""Class representing a token config entry."""
2525

2626
token: str
27-
color: str = "#7289DA"
27+
color: Optional[str] = "#7289DA"
28+
case_sensitive: Optional[bool] = False
2829

2930

3031
class LokiConfig(BaseModel):

0 commit comments

Comments
 (0)