teams-alerter 0.1.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- teams_alerter-0.1.0/PKG-INFO +100 -0
- teams_alerter-0.1.0/README.md +90 -0
- teams_alerter-0.1.0/pyproject.toml +15 -0
- teams_alerter-0.1.0/setup.cfg +4 -0
- teams_alerter-0.1.0/src/__init__.py +4 -0
- teams_alerter-0.1.0/src/core.py +77 -0
- teams_alerter-0.1.0/src/setup.py +11 -0
- teams_alerter-0.1.0/src/teams_alerter.egg-info/PKG-INFO +100 -0
- teams_alerter-0.1.0/src/teams_alerter.egg-info/SOURCES.txt +11 -0
- teams_alerter-0.1.0/src/teams_alerter.egg-info/dependency_links.txt +1 -0
- teams_alerter-0.1.0/src/teams_alerter.egg-info/requires.txt +2 -0
- teams_alerter-0.1.0/src/teams_alerter.egg-info/top_level.txt +4 -0
- teams_alerter-0.1.0/src/utils.py +24 -0
@@ -0,0 +1,100 @@
|
|
1
|
+
Metadata-Version: 2.4
|
2
|
+
Name: teams-alerter
|
3
|
+
Version: 0.1.0
|
4
|
+
Summary: Module pour envoyer des alertes Teams via Pub/Sub
|
5
|
+
Author-email: Toki <t.bakotondrabe-ext@paris-turf.com>
|
6
|
+
Requires-Python: >=3.8
|
7
|
+
Description-Content-Type: text/markdown
|
8
|
+
Requires-Dist: google-cloud-logging
|
9
|
+
Requires-Dist: google-cloud-pubsub
|
10
|
+
|
11
|
+
# 🛠️ Teams Alerter
|
12
|
+
|
13
|
+
A lightweight Python module to send alerts to Microsoft Teams via Google Cloud Pub/Sub.
|
14
|
+
|
15
|
+
---
|
16
|
+
|
17
|
+
## 📦 How to Build the Module
|
18
|
+
|
19
|
+
Make sure you have the `build` package installed:
|
20
|
+
|
21
|
+
```bash
|
22
|
+
python -m pip install --upgrade build
|
23
|
+
python -m build
|
24
|
+
```
|
25
|
+
|
26
|
+
This will generate `.whl` and `.tar.gz` files inside the `dist/` folder.
|
27
|
+
|
28
|
+
---
|
29
|
+
|
30
|
+
## 🔧 Install the Module Locally
|
31
|
+
|
32
|
+
From your project directory (with your virtual environment activated):
|
33
|
+
|
34
|
+
```bash
|
35
|
+
pip install ../teams-alerter/dist/teams_alerter-0.1.0-py3-none-any.whl
|
36
|
+
```
|
37
|
+
|
38
|
+
> Adjust the path according to your actual `.whl` file location.
|
39
|
+
|
40
|
+
---
|
41
|
+
|
42
|
+
## 🚀 How to Use the Module
|
43
|
+
|
44
|
+
```python
|
45
|
+
from teams_alerter import TeamsAlerter
|
46
|
+
|
47
|
+
# Example: error handling
|
48
|
+
try:
|
49
|
+
raise RuntimeError("Test error")
|
50
|
+
except Exception as error:
|
51
|
+
TeamsAlerter.handle_error(error, utils)
|
52
|
+
```
|
53
|
+
|
54
|
+
---
|
55
|
+
|
56
|
+
## 📤 How to Publish to PyPI
|
57
|
+
|
58
|
+
1. Install `twine`:
|
59
|
+
|
60
|
+
```bash
|
61
|
+
pip install twine
|
62
|
+
```
|
63
|
+
|
64
|
+
2. Upload the distribution files:
|
65
|
+
|
66
|
+
```bash
|
67
|
+
twine upload dist/*
|
68
|
+
```
|
69
|
+
|
70
|
+
> A PyPI account is required: https://pypi.org/account/register/
|
71
|
+
|
72
|
+
---
|
73
|
+
|
74
|
+
## 📁 Recommended Module Structure
|
75
|
+
|
76
|
+
```
|
77
|
+
teams-alerter/
|
78
|
+
├── src/
|
79
|
+
│ └── teams_alerter/
|
80
|
+
│ ├── __init__.py
|
81
|
+
│ ├── core.py
|
82
|
+
│ └── utils.py
|
83
|
+
├── pyproject.toml
|
84
|
+
├── README.md
|
85
|
+
└── dist/
|
86
|
+
```
|
87
|
+
|
88
|
+
---
|
89
|
+
|
90
|
+
## ✅ Requirements
|
91
|
+
|
92
|
+
- Python ≥ 3.8
|
93
|
+
- Google Cloud SDK if you are using Pub/Sub
|
94
|
+
- Dependencies:
|
95
|
+
- `google-cloud-logging`
|
96
|
+
- `google-cloud-pubsub`
|
97
|
+
|
98
|
+
These dependencies are already listed in your `pyproject.toml`.
|
99
|
+
|
100
|
+
---
|
@@ -0,0 +1,90 @@
|
|
1
|
+
# 🛠️ Teams Alerter
|
2
|
+
|
3
|
+
A lightweight Python module to send alerts to Microsoft Teams via Google Cloud Pub/Sub.
|
4
|
+
|
5
|
+
---
|
6
|
+
|
7
|
+
## 📦 How to Build the Module
|
8
|
+
|
9
|
+
Make sure you have the `build` package installed:
|
10
|
+
|
11
|
+
```bash
|
12
|
+
python -m pip install --upgrade build
|
13
|
+
python -m build
|
14
|
+
```
|
15
|
+
|
16
|
+
This will generate `.whl` and `.tar.gz` files inside the `dist/` folder.
|
17
|
+
|
18
|
+
---
|
19
|
+
|
20
|
+
## 🔧 Install the Module Locally
|
21
|
+
|
22
|
+
From your project directory (with your virtual environment activated):
|
23
|
+
|
24
|
+
```bash
|
25
|
+
pip install ../teams-alerter/dist/teams_alerter-0.1.0-py3-none-any.whl
|
26
|
+
```
|
27
|
+
|
28
|
+
> Adjust the path according to your actual `.whl` file location.
|
29
|
+
|
30
|
+
---
|
31
|
+
|
32
|
+
## 🚀 How to Use the Module
|
33
|
+
|
34
|
+
```python
|
35
|
+
from teams_alerter import TeamsAlerter
|
36
|
+
|
37
|
+
# Example: error handling
|
38
|
+
try:
|
39
|
+
raise RuntimeError("Test error")
|
40
|
+
except Exception as error:
|
41
|
+
TeamsAlerter.handle_error(error, utils)
|
42
|
+
```
|
43
|
+
|
44
|
+
---
|
45
|
+
|
46
|
+
## 📤 How to Publish to PyPI
|
47
|
+
|
48
|
+
1. Install `twine`:
|
49
|
+
|
50
|
+
```bash
|
51
|
+
pip install twine
|
52
|
+
```
|
53
|
+
|
54
|
+
2. Upload the distribution files:
|
55
|
+
|
56
|
+
```bash
|
57
|
+
twine upload dist/*
|
58
|
+
```
|
59
|
+
|
60
|
+
> A PyPI account is required: https://pypi.org/account/register/
|
61
|
+
|
62
|
+
---
|
63
|
+
|
64
|
+
## 📁 Recommended Module Structure
|
65
|
+
|
66
|
+
```
|
67
|
+
teams-alerter/
|
68
|
+
├── src/
|
69
|
+
│ └── teams_alerter/
|
70
|
+
│ ├── __init__.py
|
71
|
+
│ ├── core.py
|
72
|
+
│ └── utils.py
|
73
|
+
├── pyproject.toml
|
74
|
+
├── README.md
|
75
|
+
└── dist/
|
76
|
+
```
|
77
|
+
|
78
|
+
---
|
79
|
+
|
80
|
+
## ✅ Requirements
|
81
|
+
|
82
|
+
- Python ≥ 3.8
|
83
|
+
- Google Cloud SDK if you are using Pub/Sub
|
84
|
+
- Dependencies:
|
85
|
+
- `google-cloud-logging`
|
86
|
+
- `google-cloud-pubsub`
|
87
|
+
|
88
|
+
These dependencies are already listed in your `pyproject.toml`.
|
89
|
+
|
90
|
+
---
|
@@ -0,0 +1,15 @@
|
|
1
|
+
[project]
|
2
|
+
name = "teams-alerter"
|
3
|
+
version = "0.1.0"
|
4
|
+
description = "Module pour envoyer des alertes Teams via Pub/Sub"
|
5
|
+
authors = [{ name = "Toki", email = "t.bakotondrabe-ext@paris-turf.com" }]
|
6
|
+
readme = "README.md"
|
7
|
+
requires-python = ">=3.8"
|
8
|
+
dependencies = [
|
9
|
+
"google-cloud-logging",
|
10
|
+
"google-cloud-pubsub",
|
11
|
+
]
|
12
|
+
|
13
|
+
[build-system]
|
14
|
+
requires = ["setuptools", "wheel"]
|
15
|
+
build-backend = "setuptools.build_meta"
|
@@ -0,0 +1,77 @@
|
|
1
|
+
import json
|
2
|
+
import traceback
|
3
|
+
|
4
|
+
from google.cloud import pubsub_v1
|
5
|
+
from utils import ErrorUtils, DateUtils
|
6
|
+
|
7
|
+
|
8
|
+
class TeamsAlerter:
|
9
|
+
def __init__(
|
10
|
+
self,
|
11
|
+
utils,
|
12
|
+
app_name,
|
13
|
+
teams_channel,
|
14
|
+
detail,
|
15
|
+
level,
|
16
|
+
environment=None,
|
17
|
+
url_log=None,
|
18
|
+
timestamp=None,
|
19
|
+
teams_template=None,
|
20
|
+
):
|
21
|
+
self.utils = utils
|
22
|
+
self.app_name = app_name
|
23
|
+
self.teams_channel = teams_channel
|
24
|
+
self.detail = detail
|
25
|
+
self.environment = environment
|
26
|
+
self.level = level
|
27
|
+
self.timestamp = timestamp
|
28
|
+
self.url_log = url_log
|
29
|
+
self.teams_template = teams_template
|
30
|
+
|
31
|
+
@staticmethod
|
32
|
+
def handle_error(error: Exception, utils: ErrorUtils) -> None:
|
33
|
+
error_type = type(error).__name__
|
34
|
+
error_message = str(error)
|
35
|
+
error_traceback = traceback.format_exc()
|
36
|
+
|
37
|
+
teams_alerter = TeamsAlerter(
|
38
|
+
utils=utils,
|
39
|
+
app_name="datastream-transaction-pub",
|
40
|
+
teams_channel=f"datastream-{utils['env']}-alerts",
|
41
|
+
detail=f"Error type: {error_type}\nError message: {error_message}\nError traceback: {error_traceback}",
|
42
|
+
level="ERROR",
|
43
|
+
)
|
44
|
+
|
45
|
+
teams_alerter.publish_alert()
|
46
|
+
|
47
|
+
def publish_alert(self):
|
48
|
+
# Formatage du payload
|
49
|
+
utc_timestamp_minus_5min = DateUtils.get_str_utc_timestamp_minus_5min()
|
50
|
+
utc_timestamp = DateUtils.get_str_utc_timestamp()
|
51
|
+
payload = json.dumps(
|
52
|
+
{
|
53
|
+
"app_name": self.app_name,
|
54
|
+
"teams_channel": self.teams_channel,
|
55
|
+
"detail": self.detail,
|
56
|
+
"level": self.level,
|
57
|
+
"environment": self.utils["env"],
|
58
|
+
"url_log": f"https://console.cloud.google.com/logs/query;query=resource.type%3D%22gce_instance%22%0Aresource.labels.project_id%3D%22{self.utils['app_project_id']}%22;cursorTimestamp={utc_timestamp_minus_5min};duration=PT10M?referrer=search&hl=fr&inv=1&invt=Ab3WcQ&project={self.utils['app_project_id']}",
|
59
|
+
"timestamp": utc_timestamp,
|
60
|
+
"teams_template": "card",
|
61
|
+
}
|
62
|
+
)
|
63
|
+
|
64
|
+
# Création d'un éditeur
|
65
|
+
publisher = pubsub_v1.PublisherClient()
|
66
|
+
topic_path = publisher.topic_path(self.utils["topic_project_id"], self.utils["topic_id"])
|
67
|
+
|
68
|
+
# Message à publier
|
69
|
+
data = payload.encode("utf-8")
|
70
|
+
|
71
|
+
# Publier le message
|
72
|
+
try:
|
73
|
+
publish_future = publisher.publish(topic_path, data)
|
74
|
+
publish_future.result()
|
75
|
+
|
76
|
+
except Exception as e:
|
77
|
+
self.utils["logger"](f"🟥Une erreur s'est produite lors de la publication du message : {e}")
|
@@ -0,0 +1,100 @@
|
|
1
|
+
Metadata-Version: 2.4
|
2
|
+
Name: teams-alerter
|
3
|
+
Version: 0.1.0
|
4
|
+
Summary: Module pour envoyer des alertes Teams via Pub/Sub
|
5
|
+
Author-email: Toki <t.bakotondrabe-ext@paris-turf.com>
|
6
|
+
Requires-Python: >=3.8
|
7
|
+
Description-Content-Type: text/markdown
|
8
|
+
Requires-Dist: google-cloud-logging
|
9
|
+
Requires-Dist: google-cloud-pubsub
|
10
|
+
|
11
|
+
# 🛠️ Teams Alerter
|
12
|
+
|
13
|
+
A lightweight Python module to send alerts to Microsoft Teams via Google Cloud Pub/Sub.
|
14
|
+
|
15
|
+
---
|
16
|
+
|
17
|
+
## 📦 How to Build the Module
|
18
|
+
|
19
|
+
Make sure you have the `build` package installed:
|
20
|
+
|
21
|
+
```bash
|
22
|
+
python -m pip install --upgrade build
|
23
|
+
python -m build
|
24
|
+
```
|
25
|
+
|
26
|
+
This will generate `.whl` and `.tar.gz` files inside the `dist/` folder.
|
27
|
+
|
28
|
+
---
|
29
|
+
|
30
|
+
## 🔧 Install the Module Locally
|
31
|
+
|
32
|
+
From your project directory (with your virtual environment activated):
|
33
|
+
|
34
|
+
```bash
|
35
|
+
pip install ../teams-alerter/dist/teams_alerter-0.1.0-py3-none-any.whl
|
36
|
+
```
|
37
|
+
|
38
|
+
> Adjust the path according to your actual `.whl` file location.
|
39
|
+
|
40
|
+
---
|
41
|
+
|
42
|
+
## 🚀 How to Use the Module
|
43
|
+
|
44
|
+
```python
|
45
|
+
from teams_alerter import TeamsAlerter
|
46
|
+
|
47
|
+
# Example: error handling
|
48
|
+
try:
|
49
|
+
raise RuntimeError("Test error")
|
50
|
+
except Exception as error:
|
51
|
+
TeamsAlerter.handle_error(error, utils)
|
52
|
+
```
|
53
|
+
|
54
|
+
---
|
55
|
+
|
56
|
+
## 📤 How to Publish to PyPI
|
57
|
+
|
58
|
+
1. Install `twine`:
|
59
|
+
|
60
|
+
```bash
|
61
|
+
pip install twine
|
62
|
+
```
|
63
|
+
|
64
|
+
2. Upload the distribution files:
|
65
|
+
|
66
|
+
```bash
|
67
|
+
twine upload dist/*
|
68
|
+
```
|
69
|
+
|
70
|
+
> A PyPI account is required: https://pypi.org/account/register/
|
71
|
+
|
72
|
+
---
|
73
|
+
|
74
|
+
## 📁 Recommended Module Structure
|
75
|
+
|
76
|
+
```
|
77
|
+
teams-alerter/
|
78
|
+
├── src/
|
79
|
+
│ └── teams_alerter/
|
80
|
+
│ ├── __init__.py
|
81
|
+
│ ├── core.py
|
82
|
+
│ └── utils.py
|
83
|
+
├── pyproject.toml
|
84
|
+
├── README.md
|
85
|
+
└── dist/
|
86
|
+
```
|
87
|
+
|
88
|
+
---
|
89
|
+
|
90
|
+
## ✅ Requirements
|
91
|
+
|
92
|
+
- Python ≥ 3.8
|
93
|
+
- Google Cloud SDK if you are using Pub/Sub
|
94
|
+
- Dependencies:
|
95
|
+
- `google-cloud-logging`
|
96
|
+
- `google-cloud-pubsub`
|
97
|
+
|
98
|
+
These dependencies are already listed in your `pyproject.toml`.
|
99
|
+
|
100
|
+
---
|
@@ -0,0 +1,11 @@
|
|
1
|
+
README.md
|
2
|
+
pyproject.toml
|
3
|
+
src/__init__.py
|
4
|
+
src/core.py
|
5
|
+
src/setup.py
|
6
|
+
src/utils.py
|
7
|
+
src/teams_alerter.egg-info/PKG-INFO
|
8
|
+
src/teams_alerter.egg-info/SOURCES.txt
|
9
|
+
src/teams_alerter.egg-info/dependency_links.txt
|
10
|
+
src/teams_alerter.egg-info/requires.txt
|
11
|
+
src/teams_alerter.egg-info/top_level.txt
|
@@ -0,0 +1 @@
|
|
1
|
+
|
@@ -0,0 +1,24 @@
|
|
1
|
+
import datetime
|
2
|
+
|
3
|
+
from typing import TypedDict
|
4
|
+
from google.cloud import logging
|
5
|
+
|
6
|
+
|
7
|
+
class ErrorUtils(TypedDict):
|
8
|
+
logger: logging.Logger
|
9
|
+
env: str
|
10
|
+
app_project_id: str
|
11
|
+
topic_project_id: str
|
12
|
+
topic_id: str
|
13
|
+
|
14
|
+
|
15
|
+
class DateUtils:
|
16
|
+
@staticmethod
|
17
|
+
def get_str_utc_timestamp():
|
18
|
+
dt = datetime.datetime.utcnow()
|
19
|
+
return dt.strftime("%Y-%m-%dT%H:%M:%S") + ".000000000Z"
|
20
|
+
|
21
|
+
@staticmethod
|
22
|
+
def get_str_utc_timestamp_minus_5min():
|
23
|
+
dt = datetime.datetime.utcnow() - datetime.timedelta(minutes=5)
|
24
|
+
return dt.strftime("%Y-%m-%dT%H:%M:%S") + ".000000000Z"
|