jobninja-tools 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.
- jobninja_tools-0.1.0/.gitignore +152 -0
- jobninja_tools-0.1.0/PKG-INFO +67 -0
- jobninja_tools-0.1.0/README.md +40 -0
- jobninja_tools-0.1.0/jn_tools/__init__.py +0 -0
- jobninja_tools-0.1.0/jn_tools/_version.py +24 -0
- jobninja_tools-0.1.0/jn_tools/cloud_watch_client.py +296 -0
- jobninja_tools-0.1.0/pyproject.toml +69 -0
|
@@ -0,0 +1,152 @@
|
|
|
1
|
+
bin/
|
|
2
|
+
lib/
|
|
3
|
+
dist/
|
|
4
|
+
include/
|
|
5
|
+
share/
|
|
6
|
+
*.egg-info
|
|
7
|
+
.idea
|
|
8
|
+
/.project
|
|
9
|
+
|
|
10
|
+
# Byte-compiled / optimized / DLL files
|
|
11
|
+
__pycache__/
|
|
12
|
+
*.py[cod]
|
|
13
|
+
*$py.class
|
|
14
|
+
|
|
15
|
+
# C extensions
|
|
16
|
+
*.so
|
|
17
|
+
|
|
18
|
+
# Distribution / packaging
|
|
19
|
+
.Python
|
|
20
|
+
build/
|
|
21
|
+
develop-eggs/
|
|
22
|
+
dist/
|
|
23
|
+
downloads/
|
|
24
|
+
eggs/
|
|
25
|
+
.eggs/
|
|
26
|
+
lib/
|
|
27
|
+
lib64/
|
|
28
|
+
parts/
|
|
29
|
+
sdist/
|
|
30
|
+
var/
|
|
31
|
+
wheels/
|
|
32
|
+
pip-wheel-metadata/
|
|
33
|
+
share/python-wheels/
|
|
34
|
+
*.egg-info/
|
|
35
|
+
.installed.cfg
|
|
36
|
+
*.egg
|
|
37
|
+
MANIFEST
|
|
38
|
+
|
|
39
|
+
# PyInstaller
|
|
40
|
+
# Usually these files are written by a python script from a template
|
|
41
|
+
# before PyInstaller builds the exe, so as to inject date/other infos into it.
|
|
42
|
+
*.manifest
|
|
43
|
+
*.spec
|
|
44
|
+
|
|
45
|
+
# Installer logs
|
|
46
|
+
pip-log.txt
|
|
47
|
+
pip-delete-this-directory.txt
|
|
48
|
+
|
|
49
|
+
# Unit test / coverage reports
|
|
50
|
+
htmlcov/
|
|
51
|
+
.tox/
|
|
52
|
+
.nox/
|
|
53
|
+
.coverage
|
|
54
|
+
.coverage.*
|
|
55
|
+
.cache
|
|
56
|
+
nosetests.xml
|
|
57
|
+
coverage.xml
|
|
58
|
+
*.cover
|
|
59
|
+
*.py,cover
|
|
60
|
+
.hypothesis/
|
|
61
|
+
.pytest_cache/
|
|
62
|
+
|
|
63
|
+
# Translations
|
|
64
|
+
*.mo
|
|
65
|
+
*.pot
|
|
66
|
+
|
|
67
|
+
# Django stuff:
|
|
68
|
+
*.log
|
|
69
|
+
local_settings.py
|
|
70
|
+
db.sqlite3
|
|
71
|
+
db.sqlite3-journal
|
|
72
|
+
|
|
73
|
+
# Flask stuff:
|
|
74
|
+
instance/
|
|
75
|
+
.webassets-cache
|
|
76
|
+
|
|
77
|
+
# Scrapy stuff:
|
|
78
|
+
.scrapy
|
|
79
|
+
|
|
80
|
+
# Sphinx documentation
|
|
81
|
+
docs/_build/
|
|
82
|
+
|
|
83
|
+
# PyBuilder
|
|
84
|
+
target/
|
|
85
|
+
|
|
86
|
+
# Jupyter Notebook
|
|
87
|
+
.ipynb_checkpoints
|
|
88
|
+
|
|
89
|
+
# IPython
|
|
90
|
+
profile_default/
|
|
91
|
+
ipython_config.py
|
|
92
|
+
|
|
93
|
+
# pyenv
|
|
94
|
+
.python-version
|
|
95
|
+
|
|
96
|
+
# pipenv
|
|
97
|
+
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
|
|
98
|
+
# However, in case of collaboration, if having platform-specific dependencies or dependencies
|
|
99
|
+
# having no cross-platform support, pipenv may install dependencies that don't work, or not
|
|
100
|
+
# install all needed dependencies.
|
|
101
|
+
#Pipfile.lock
|
|
102
|
+
|
|
103
|
+
# PEP 582; used by e.g. github.com/David-OConnor/pyflow
|
|
104
|
+
__pypackages__/
|
|
105
|
+
|
|
106
|
+
# Celery stuff
|
|
107
|
+
celerybeat-schedule
|
|
108
|
+
celerybeat.pid
|
|
109
|
+
|
|
110
|
+
# SageMath parsed files
|
|
111
|
+
*.sage.py
|
|
112
|
+
|
|
113
|
+
# Environments
|
|
114
|
+
.env
|
|
115
|
+
.venv
|
|
116
|
+
env/
|
|
117
|
+
venv/
|
|
118
|
+
ENV/
|
|
119
|
+
env.bak/
|
|
120
|
+
venv.bak/
|
|
121
|
+
|
|
122
|
+
# Spyder project settings
|
|
123
|
+
.spyderproject
|
|
124
|
+
.spyproject
|
|
125
|
+
|
|
126
|
+
# Rope project settings
|
|
127
|
+
.ropeproject
|
|
128
|
+
|
|
129
|
+
# mkdocs documentation
|
|
130
|
+
/site
|
|
131
|
+
|
|
132
|
+
# mypy
|
|
133
|
+
.mypy_cache/
|
|
134
|
+
.dmypy.json
|
|
135
|
+
dmypy.json
|
|
136
|
+
|
|
137
|
+
# Pyre type checker
|
|
138
|
+
.pyre/
|
|
139
|
+
|
|
140
|
+
.vscode/
|
|
141
|
+
.idea/
|
|
142
|
+
|
|
143
|
+
# uv
|
|
144
|
+
.venv/
|
|
145
|
+
|
|
146
|
+
# hatch-vcs generated
|
|
147
|
+
jn_tools/_version.py
|
|
148
|
+
|
|
149
|
+
# build output
|
|
150
|
+
dist/
|
|
151
|
+
build/
|
|
152
|
+
*.egg-info/
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: jobninja-tools
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: Shared utilities for JobNinja services.
|
|
5
|
+
Project-URL: Homepage, https://github.com/JobNinja/JobNinjaServer2Shared
|
|
6
|
+
Project-URL: Repository, https://github.com/JobNinja/JobNinjaServer2Shared
|
|
7
|
+
Project-URL: Issues, https://github.com/JobNinja/JobNinjaServer2Shared/issues
|
|
8
|
+
Author-email: JobNinja <devops@jobninja.com>
|
|
9
|
+
License: Proprietary
|
|
10
|
+
Keywords: aws,cloudwatch,jobninja
|
|
11
|
+
Classifier: Intended Audience :: Developers
|
|
12
|
+
Classifier: Operating System :: OS Independent
|
|
13
|
+
Classifier: Programming Language :: Python :: 3
|
|
14
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
15
|
+
Classifier: Topic :: Software Development :: Libraries
|
|
16
|
+
Requires-Python: >=3.13
|
|
17
|
+
Requires-Dist: boto3
|
|
18
|
+
Requires-Dist: botocore
|
|
19
|
+
Requires-Dist: python-dotenv
|
|
20
|
+
Provides-Extra: dev
|
|
21
|
+
Requires-Dist: build>=1.2; extra == 'dev'
|
|
22
|
+
Requires-Dist: pre-commit>=3.7; extra == 'dev'
|
|
23
|
+
Requires-Dist: pytest-mock>=3.12; extra == 'dev'
|
|
24
|
+
Requires-Dist: pytest>=8; extra == 'dev'
|
|
25
|
+
Requires-Dist: ruff>=0.6; extra == 'dev'
|
|
26
|
+
Description-Content-Type: text/markdown
|
|
27
|
+
|
|
28
|
+
# jobninja-tools
|
|
29
|
+
|
|
30
|
+
Shared utilities for JobNinja services.
|
|
31
|
+
|
|
32
|
+
## Install
|
|
33
|
+
|
|
34
|
+
From PyPI (once published):
|
|
35
|
+
|
|
36
|
+
```bash
|
|
37
|
+
pip install jobninja-tools
|
|
38
|
+
# or
|
|
39
|
+
uv add jobninja-tools
|
|
40
|
+
```
|
|
41
|
+
|
|
42
|
+
Import name is `jn_tools`:
|
|
43
|
+
|
|
44
|
+
```python
|
|
45
|
+
from jn_tools.cloud_watch_client import CloudWatchClient
|
|
46
|
+
```
|
|
47
|
+
|
|
48
|
+
## Development
|
|
49
|
+
|
|
50
|
+
Requires Python 3.13 and [uv](https://docs.astral.sh/uv/).
|
|
51
|
+
|
|
52
|
+
```bash
|
|
53
|
+
uv sync --extra dev
|
|
54
|
+
uv run pre-commit install
|
|
55
|
+
uv run pytest
|
|
56
|
+
uv run ruff check .
|
|
57
|
+
uv run ruff format .
|
|
58
|
+
```
|
|
59
|
+
|
|
60
|
+
## Releasing
|
|
61
|
+
|
|
62
|
+
Versions are derived from git tags via `hatch-vcs`.
|
|
63
|
+
|
|
64
|
+
1. Pre-release to TestPyPI: `git tag v0.1.0rc1 && git push origin v0.1.0rc1`
|
|
65
|
+
2. Production release to PyPI: `git tag v0.1.0 && git push origin v0.1.0`
|
|
66
|
+
|
|
67
|
+
The `release.yml` workflow builds the sdist+wheel and publishes via PyPI Trusted Publishers (OIDC).
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
# jobninja-tools
|
|
2
|
+
|
|
3
|
+
Shared utilities for JobNinja services.
|
|
4
|
+
|
|
5
|
+
## Install
|
|
6
|
+
|
|
7
|
+
From PyPI (once published):
|
|
8
|
+
|
|
9
|
+
```bash
|
|
10
|
+
pip install jobninja-tools
|
|
11
|
+
# or
|
|
12
|
+
uv add jobninja-tools
|
|
13
|
+
```
|
|
14
|
+
|
|
15
|
+
Import name is `jn_tools`:
|
|
16
|
+
|
|
17
|
+
```python
|
|
18
|
+
from jn_tools.cloud_watch_client import CloudWatchClient
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
## Development
|
|
22
|
+
|
|
23
|
+
Requires Python 3.13 and [uv](https://docs.astral.sh/uv/).
|
|
24
|
+
|
|
25
|
+
```bash
|
|
26
|
+
uv sync --extra dev
|
|
27
|
+
uv run pre-commit install
|
|
28
|
+
uv run pytest
|
|
29
|
+
uv run ruff check .
|
|
30
|
+
uv run ruff format .
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
## Releasing
|
|
34
|
+
|
|
35
|
+
Versions are derived from git tags via `hatch-vcs`.
|
|
36
|
+
|
|
37
|
+
1. Pre-release to TestPyPI: `git tag v0.1.0rc1 && git push origin v0.1.0rc1`
|
|
38
|
+
2. Production release to PyPI: `git tag v0.1.0 && git push origin v0.1.0`
|
|
39
|
+
|
|
40
|
+
The `release.yml` workflow builds the sdist+wheel and publishes via PyPI Trusted Publishers (OIDC).
|
|
File without changes
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
# file generated by vcs-versioning
|
|
2
|
+
# don't change, don't track in version control
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
__all__ = [
|
|
6
|
+
"__version__",
|
|
7
|
+
"__version_tuple__",
|
|
8
|
+
"version",
|
|
9
|
+
"version_tuple",
|
|
10
|
+
"__commit_id__",
|
|
11
|
+
"commit_id",
|
|
12
|
+
]
|
|
13
|
+
|
|
14
|
+
version: str
|
|
15
|
+
__version__: str
|
|
16
|
+
__version_tuple__: tuple[int | str, ...]
|
|
17
|
+
version_tuple: tuple[int | str, ...]
|
|
18
|
+
commit_id: str | None
|
|
19
|
+
__commit_id__: str | None
|
|
20
|
+
|
|
21
|
+
__version__ = version = '0.1.0'
|
|
22
|
+
__version_tuple__ = version_tuple = (0, 1, 0)
|
|
23
|
+
|
|
24
|
+
__commit_id__ = commit_id = None
|
|
@@ -0,0 +1,296 @@
|
|
|
1
|
+
import logging
|
|
2
|
+
import sys
|
|
3
|
+
|
|
4
|
+
import boto3
|
|
5
|
+
from botocore.client import Config
|
|
6
|
+
|
|
7
|
+
AWS_CLOUD_WATCH_REGION = "eu-central-1"
|
|
8
|
+
|
|
9
|
+
DEBUG_AWS_CLOUD_WATCH_REGION = "af-south-1"
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
class CloudWatchClient:
|
|
13
|
+
"""
|
|
14
|
+
CloudWatchClient to send monitoring data to AWS CloudWatch.
|
|
15
|
+
Read basic info about AWS CloudWatch, and concepts liek namespaces, metric, dimensions, etc here:
|
|
16
|
+
https://docs.aws.amazon.com/AmazonCloudWatch/latest/monitoring/cloudwatch_concepts.html
|
|
17
|
+
"""
|
|
18
|
+
|
|
19
|
+
_client = None
|
|
20
|
+
_logger_default = None
|
|
21
|
+
_raise_exceptions = False
|
|
22
|
+
|
|
23
|
+
def __init__(
|
|
24
|
+
self,
|
|
25
|
+
namespace="",
|
|
26
|
+
metric_dimensions={},
|
|
27
|
+
alarm_config={},
|
|
28
|
+
unit="None",
|
|
29
|
+
logger=None,
|
|
30
|
+
debug_mode=False,
|
|
31
|
+
raise_exceptions=False,
|
|
32
|
+
AWS_ACCESS_KEY_ID="",
|
|
33
|
+
AWS_ACCESS_KEY_SECRET="",
|
|
34
|
+
):
|
|
35
|
+
"""
|
|
36
|
+
Initialize client before calling any methods
|
|
37
|
+
Args:
|
|
38
|
+
namespace: (str) AWS namespace. Use format: jn.importer
|
|
39
|
+
metric_dimensions: (dict) Set default dimension for this client which are added to all calls of submit_value().
|
|
40
|
+
Use dimension name as key and dimension value as value
|
|
41
|
+
unit: (str, default: 'Unit') Set a default unit for this client. Note: Needs to be one of AWS pre-defined unit types.
|
|
42
|
+
debug_mode: (boolean, default: False) If set to True, all data is submitted to aws region "af-south-1" (CapeTown)
|
|
43
|
+
raise_exceptions: (boolean, default: False) If False, no exceptions will be raised from submit_value(). Exceptions are logged only.
|
|
44
|
+
AWS_ACCESS_KEY_ID: (str) AWS access key id - Shared between all instances!! - Set it once in the first instance
|
|
45
|
+
AWS_ACCESS_KEY_SECRET: (str) AWS access key secret - Shared between all instances!! - Set it once in the first instance
|
|
46
|
+
"""
|
|
47
|
+
self._namespace = self._normalise_string(namespace)
|
|
48
|
+
self._metric_dimensions = self._normalise_dimensions(metric_dimensions)
|
|
49
|
+
self._alarm_config = alarm_config
|
|
50
|
+
self._unit = unit
|
|
51
|
+
self._debug = debug_mode
|
|
52
|
+
self._raise_exceptions = raise_exceptions
|
|
53
|
+
self._logger = logger or CloudWatchClient._get_default_logger()
|
|
54
|
+
|
|
55
|
+
# This is dirty!
|
|
56
|
+
# Users might not expect, that all instances share the same boto3.client
|
|
57
|
+
# and passing other creaditals to later instances is futile.
|
|
58
|
+
if CloudWatchClient._client is None:
|
|
59
|
+
CloudWatchClient._client = boto3.client(
|
|
60
|
+
"cloudwatch",
|
|
61
|
+
aws_access_key_id=AWS_ACCESS_KEY_ID,
|
|
62
|
+
aws_secret_access_key=AWS_ACCESS_KEY_SECRET,
|
|
63
|
+
config=Config(
|
|
64
|
+
region_name=AWS_CLOUD_WATCH_REGION
|
|
65
|
+
if not self._debug
|
|
66
|
+
else DEBUG_AWS_CLOUD_WATCH_REGION
|
|
67
|
+
),
|
|
68
|
+
)
|
|
69
|
+
self._client = CloudWatchClient._client
|
|
70
|
+
|
|
71
|
+
def submit_value(self, metric_name, val, dimensions=None, unit=None, additional_params={}):
|
|
72
|
+
"""
|
|
73
|
+
Send a value to CloudWatch
|
|
74
|
+
Boto3 documentation: https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/cloudwatch.html#CloudWatch.Client.put_metric_data
|
|
75
|
+
Args:
|
|
76
|
+
metric_name: (str)
|
|
77
|
+
val:
|
|
78
|
+
dimensions: (dict) add more dimensions to this specific value. Gets merged with default dimensions, if same name, this one will orverride defaults
|
|
79
|
+
unit: (str) set specific unit
|
|
80
|
+
|
|
81
|
+
Returns: None
|
|
82
|
+
|
|
83
|
+
"""
|
|
84
|
+
metric_name = self._normalise_string(metric_name)
|
|
85
|
+
my_conf = dict(
|
|
86
|
+
Namespace=self._namespace,
|
|
87
|
+
MetricData=[
|
|
88
|
+
{
|
|
89
|
+
"MetricName": metric_name,
|
|
90
|
+
"Dimensions": self._get_dimensions_as_list(dimensions),
|
|
91
|
+
"Value": val,
|
|
92
|
+
"Unit": unit or self._unit,
|
|
93
|
+
}
|
|
94
|
+
],
|
|
95
|
+
)
|
|
96
|
+
my_conf = {**my_conf, **additional_params}
|
|
97
|
+
try:
|
|
98
|
+
resp = self._client.put_metric_data(**my_conf)
|
|
99
|
+
except Exception as e:
|
|
100
|
+
self._logger.exception(
|
|
101
|
+
f"CloudWatchClient::submit_value() {'DEBUG_MODE' if self._debug else ''} - EXCEPTION: {e}\n"
|
|
102
|
+
f"Request config: {my_conf}"
|
|
103
|
+
)
|
|
104
|
+
if self._raise_exceptions:
|
|
105
|
+
raise e
|
|
106
|
+
else:
|
|
107
|
+
request_id = resp.get("ResponseMetadata", {}).get("RequestId")
|
|
108
|
+
self._logger.info(
|
|
109
|
+
f"CloudWatchClient::submit_value() {'DEBUG_MODE' if self._debug else ''} - OK {metric_name}: {val}"
|
|
110
|
+
f" - request_id: {request_id}, request config: {my_conf}"
|
|
111
|
+
)
|
|
112
|
+
return my_conf
|
|
113
|
+
|
|
114
|
+
def set_alarm(
|
|
115
|
+
self,
|
|
116
|
+
alarm_name,
|
|
117
|
+
metric_name,
|
|
118
|
+
threshold=0,
|
|
119
|
+
dimensions=None,
|
|
120
|
+
unit=None,
|
|
121
|
+
update_if_exists=False,
|
|
122
|
+
additional_params={},
|
|
123
|
+
):
|
|
124
|
+
full_alarm_name = self._get_full_normalized_alarm_name(alarm_name)
|
|
125
|
+
metric_name = self._normalise_string(metric_name)
|
|
126
|
+
alarm_exists = None
|
|
127
|
+
if not update_if_exists:
|
|
128
|
+
alarm_exists = self._alarm_existing(full_alarm_name, metric_name, dimensions=dimensions)
|
|
129
|
+
if update_if_exists or not alarm_exists:
|
|
130
|
+
# https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/cloudwatch.html#CloudWatch.Client.put_metric_alarm
|
|
131
|
+
my_conf = dict(
|
|
132
|
+
Namespace=self._namespace,
|
|
133
|
+
AlarmName=full_alarm_name,
|
|
134
|
+
# AlarmDescription='string',
|
|
135
|
+
MetricName=metric_name,
|
|
136
|
+
Statistic="Average", # 'SampleCount' | 'Average' | 'Sum' | 'Minimum' | 'Maximum',
|
|
137
|
+
# ExtendedStatistic='string',
|
|
138
|
+
Dimensions=self._get_dimensions_as_list(dimensions),
|
|
139
|
+
Period=60 * 60 * 3,
|
|
140
|
+
Unit=unit
|
|
141
|
+
or self._unit, # 'Seconds'|'Microseconds'|'Milliseconds'|'Bytes'|'Kilobytes'|'Megabytes'|'Gigabytes'|'Terabytes'|'Bits'|'Kilobits'|'Megabits'|'Gigabits'|'Terabits'|'Percent'|'Count'|'Bytes/Second'|'Kilobytes/Second'|'Megabytes/Second'|'Gigabytes/Second'|'Terabytes/Second'|'Bits/Second'|'Kilobits/Second'|'Megabits/Second'|'Gigabits/Second'|'Terabits/Second'|'Count/Second'|'None',
|
|
142
|
+
EvaluationPeriods=8,
|
|
143
|
+
DatapointsToAlarm=8,
|
|
144
|
+
Threshold=threshold,
|
|
145
|
+
ComparisonOperator="GreaterThanThreshold",
|
|
146
|
+
# 'GreaterThanOrEqualToThreshold' | 'GreaterThanThreshold' | 'LessThanThreshold' | 'LessThanOrEqualToThreshold' | 'LessThanLowerOrGreaterThanUpperThreshold' | 'LessThanLowerThreshold' | 'GreaterThanUpperThreshold',
|
|
147
|
+
TreatMissingData="breaching", # breaching | notBreaching | ignore | missing
|
|
148
|
+
)
|
|
149
|
+
my_conf = {**my_conf, **self._alarm_config, **additional_params}
|
|
150
|
+
try:
|
|
151
|
+
resp = self._client.put_metric_alarm(**my_conf)
|
|
152
|
+
except Exception as e:
|
|
153
|
+
self._logger.exception(
|
|
154
|
+
f"CloudWatchClient::set_alarm() {'DEBUG_MODE' if self._debug else ''} - EXCEPTION: {e}\n"
|
|
155
|
+
f"Request config: {my_conf}"
|
|
156
|
+
)
|
|
157
|
+
if self._raise_exceptions:
|
|
158
|
+
raise e
|
|
159
|
+
else:
|
|
160
|
+
request_id = resp.get("ResponseMetadata", {}).get("RequestId")
|
|
161
|
+
self._logger.info(
|
|
162
|
+
f"CloudWatchClient::set_alarm() {'DEBUG_MODE' if self._debug else ''} - OK "
|
|
163
|
+
f"alarm name: {full_alarm_name}, threshold: {threshold}"
|
|
164
|
+
f" - request_id: {request_id}, request config: {my_conf}"
|
|
165
|
+
)
|
|
166
|
+
return my_conf
|
|
167
|
+
|
|
168
|
+
def delete_alarm(self, alarm_name):
|
|
169
|
+
full_alarm_name = self._get_full_normalized_alarm_name(alarm_name)
|
|
170
|
+
my_conf = dict(
|
|
171
|
+
AlarmNames=[full_alarm_name],
|
|
172
|
+
)
|
|
173
|
+
my_conf = {**my_conf, **self._alarm_config}
|
|
174
|
+
try:
|
|
175
|
+
resp = self._client.delete_alarms(**my_conf)
|
|
176
|
+
except Exception as e:
|
|
177
|
+
self._logger.exception(
|
|
178
|
+
f"CloudWatchClient::delete_alarm() {'DEBUG_MODE' if self._debug else ''} - EXCEPTION: {e}\n"
|
|
179
|
+
f"Request config: {my_conf}"
|
|
180
|
+
)
|
|
181
|
+
if self._raise_exceptions:
|
|
182
|
+
raise e
|
|
183
|
+
else:
|
|
184
|
+
request_id = resp.get("ResponseMetadata", {}).get("RequestId")
|
|
185
|
+
self._logger.info(
|
|
186
|
+
f"CloudWatchClient::delete_alarm() {'DEBUG_MODE' if self._debug else ''} - OK "
|
|
187
|
+
f"alarm name: {full_alarm_name}"
|
|
188
|
+
f" - request_id: {request_id}, request config: {my_conf}"
|
|
189
|
+
)
|
|
190
|
+
return my_conf
|
|
191
|
+
|
|
192
|
+
def get_alarms(self, state="ALARM"):
|
|
193
|
+
res = []
|
|
194
|
+
my_conf = dict(StateValue=state, MaxRecords=100)
|
|
195
|
+
if self._namespace:
|
|
196
|
+
# Cloud watch does not accept an empty sting for AlarmNamePrefix
|
|
197
|
+
my_conf["AlarmNamePrefix"] = self._get_full_normalized_alarm_name("")
|
|
198
|
+
try:
|
|
199
|
+
resp = None
|
|
200
|
+
next_token = None
|
|
201
|
+
while resp is None or next_token is not None:
|
|
202
|
+
resp = self._client.describe_alarms(**my_conf)
|
|
203
|
+
res.extend(resp.get("MetricAlarms", []))
|
|
204
|
+
next_token = resp.get("NextToken")
|
|
205
|
+
my_conf["NextToken"] = next_token
|
|
206
|
+
except Exception as e:
|
|
207
|
+
self._logger.exception(
|
|
208
|
+
f"CloudWatchClient::get_alarms() {'DEBUG_MODE' if self._debug else ''} - EXCEPTION: {e}\n"
|
|
209
|
+
f"Request config: {my_conf}"
|
|
210
|
+
)
|
|
211
|
+
if self._raise_exceptions:
|
|
212
|
+
raise e
|
|
213
|
+
else:
|
|
214
|
+
request_id = resp.get("ResponseMetadata", {}).get("RequestId")
|
|
215
|
+
self._logger.info(
|
|
216
|
+
f"CloudWatchClient::get_alarms() {'DEBUG_MODE' if self._debug else ''} - OK "
|
|
217
|
+
f" - Found {len(res)} alarms for namespace {self._namespace}"
|
|
218
|
+
f" - request_id: {request_id}, request config: {my_conf}"
|
|
219
|
+
)
|
|
220
|
+
return res
|
|
221
|
+
|
|
222
|
+
def _alarm_existing(self, full_alarm_name, metric_name, dimensions={}):
|
|
223
|
+
# add only params which describe an alarm in a unique way.
|
|
224
|
+
# Don't add things like 'unit' b/c you will not find an existing alarm if you filter with the incorrect unit,
|
|
225
|
+
# e.g. if the unit got changed manually.
|
|
226
|
+
my_conf = dict(
|
|
227
|
+
Namespace=self._namespace,
|
|
228
|
+
MetricName=metric_name,
|
|
229
|
+
Dimensions=self._get_dimensions_as_list(dimensions),
|
|
230
|
+
)
|
|
231
|
+
try:
|
|
232
|
+
resp = self._client.describe_alarms_for_metric(**my_conf)
|
|
233
|
+
except Exception as e:
|
|
234
|
+
self._logger.exception(
|
|
235
|
+
f"CloudWatchClient::_alarm_existing() {'DEBUG_MODE' if self._debug else ''} - EXCEPTION: {e}\n"
|
|
236
|
+
f"Request config: {my_conf}"
|
|
237
|
+
)
|
|
238
|
+
if self._raise_exceptions:
|
|
239
|
+
raise e
|
|
240
|
+
return False
|
|
241
|
+
else:
|
|
242
|
+
request_id = resp.get("ResponseMetadata", {}).get("RequestId")
|
|
243
|
+
existing = (
|
|
244
|
+
len([a for a in resp.get("MetricAlarms") if a.get("AlarmName") == full_alarm_name])
|
|
245
|
+
> 0
|
|
246
|
+
)
|
|
247
|
+
self._logger.info(
|
|
248
|
+
f"CloudWatchClient::_alarm_existing() {'DEBUG_MODE' if self._debug else ''} - OK "
|
|
249
|
+
f"alarm name: {full_alarm_name}, existing: {existing}, request_id: {request_id}, request config: {my_conf}"
|
|
250
|
+
)
|
|
251
|
+
return existing
|
|
252
|
+
|
|
253
|
+
def _normalise_string(self, val_string):
|
|
254
|
+
line = (
|
|
255
|
+
str(val_string)
|
|
256
|
+
.replace("ä", "ae")
|
|
257
|
+
.replace("Ä", "Äe")
|
|
258
|
+
.replace("ö", "oe")
|
|
259
|
+
.replace("Ö", "oe")
|
|
260
|
+
.replace("ü", "ue")
|
|
261
|
+
.replace("Ü", "Ue")
|
|
262
|
+
.replace("ß", "ss")
|
|
263
|
+
)
|
|
264
|
+
line = line.encode("ascii", "ignore").decode("utf-8")
|
|
265
|
+
return line
|
|
266
|
+
|
|
267
|
+
def _normalise_dimensions(self, dimensions: dict):
|
|
268
|
+
result = {}
|
|
269
|
+
for key, val in dimensions.items():
|
|
270
|
+
result[self._normalise_string(key)] = self._normalise_string(val)
|
|
271
|
+
return result
|
|
272
|
+
|
|
273
|
+
def _get_dimensions_as_list(self, local_dimensions):
|
|
274
|
+
local_dimensions = self._normalise_dimensions(local_dimensions or {})
|
|
275
|
+
return self._dict_as_list({**self._metric_dimensions, **local_dimensions}, key_name="Name")
|
|
276
|
+
|
|
277
|
+
def _dict_as_list(self, my_dict, key_name="Name"):
|
|
278
|
+
return [{key_name: k, "Value": v} for k, v in my_dict.items()]
|
|
279
|
+
|
|
280
|
+
def _get_full_normalized_alarm_name(self, alarm_name):
|
|
281
|
+
if not self._namespace:
|
|
282
|
+
return self._normalise_string(alarm_name)
|
|
283
|
+
return f"{self._namespace}.{self._normalise_string(alarm_name)}"
|
|
284
|
+
|
|
285
|
+
@staticmethod
|
|
286
|
+
def _get_default_logger():
|
|
287
|
+
if CloudWatchClient._logger_default is None:
|
|
288
|
+
my_logger = logging.getLogger("CloudWatchClient")
|
|
289
|
+
my_logger.setLevel(logging.DEBUG)
|
|
290
|
+
handler = logging.StreamHandler(sys.stdout)
|
|
291
|
+
handler.setLevel(logging.DEBUG)
|
|
292
|
+
formatter = logging.Formatter("%(asctime)s - %(name)s - %(levelname)s - %(message)s")
|
|
293
|
+
handler.setFormatter(formatter)
|
|
294
|
+
my_logger.addHandler(handler)
|
|
295
|
+
CloudWatchClient._logger_default = my_logger
|
|
296
|
+
return CloudWatchClient._logger_default
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
[build-system]
|
|
2
|
+
requires = ["hatchling", "hatch-vcs"]
|
|
3
|
+
build-backend = "hatchling.build"
|
|
4
|
+
|
|
5
|
+
[project]
|
|
6
|
+
name = "jobninja-tools"
|
|
7
|
+
description = "Shared utilities for JobNinja services."
|
|
8
|
+
readme = "README.md"
|
|
9
|
+
requires-python = ">=3.13"
|
|
10
|
+
license = { text = "Proprietary" }
|
|
11
|
+
authors = [{ name = "JobNinja", email = "devops@jobninja.com" }]
|
|
12
|
+
keywords = ["jobninja", "cloudwatch", "aws"]
|
|
13
|
+
classifiers = [
|
|
14
|
+
"Intended Audience :: Developers",
|
|
15
|
+
"Operating System :: OS Independent",
|
|
16
|
+
"Programming Language :: Python :: 3",
|
|
17
|
+
"Programming Language :: Python :: 3.13",
|
|
18
|
+
"Topic :: Software Development :: Libraries",
|
|
19
|
+
]
|
|
20
|
+
dependencies = [
|
|
21
|
+
"boto3",
|
|
22
|
+
"botocore",
|
|
23
|
+
"python-dotenv",
|
|
24
|
+
]
|
|
25
|
+
dynamic = ["version"]
|
|
26
|
+
|
|
27
|
+
[project.urls]
|
|
28
|
+
Homepage = "https://github.com/JobNinja/JobNinjaServer2Shared"
|
|
29
|
+
Repository = "https://github.com/JobNinja/JobNinjaServer2Shared"
|
|
30
|
+
Issues = "https://github.com/JobNinja/JobNinjaServer2Shared/issues"
|
|
31
|
+
|
|
32
|
+
[project.optional-dependencies]
|
|
33
|
+
dev = [
|
|
34
|
+
"pytest>=8",
|
|
35
|
+
"pytest-mock>=3.12",
|
|
36
|
+
"ruff>=0.6",
|
|
37
|
+
"pre-commit>=3.7",
|
|
38
|
+
"build>=1.2",
|
|
39
|
+
]
|
|
40
|
+
|
|
41
|
+
[tool.hatch.version]
|
|
42
|
+
source = "vcs"
|
|
43
|
+
|
|
44
|
+
[tool.hatch.build.hooks.vcs]
|
|
45
|
+
version-file = "jn_tools/_version.py"
|
|
46
|
+
|
|
47
|
+
[tool.hatch.build.targets.wheel]
|
|
48
|
+
packages = ["jn_tools"]
|
|
49
|
+
|
|
50
|
+
[tool.hatch.build.targets.sdist]
|
|
51
|
+
include = ["jn_tools", "README.md", "pyproject.toml"]
|
|
52
|
+
|
|
53
|
+
[tool.ruff]
|
|
54
|
+
line-length = 100
|
|
55
|
+
target-version = "py313"
|
|
56
|
+
|
|
57
|
+
[tool.ruff.lint]
|
|
58
|
+
select = ["E", "F", "I", "UP", "B", "SIM"]
|
|
59
|
+
ignore = ["B008"]
|
|
60
|
+
|
|
61
|
+
[tool.ruff.lint.per-file-ignores]
|
|
62
|
+
"jn_tools/cloud_watch_client.py" = ["E501", "B006"]
|
|
63
|
+
|
|
64
|
+
[tool.ruff.format]
|
|
65
|
+
quote-style = "double"
|
|
66
|
+
|
|
67
|
+
[tool.pytest.ini_options]
|
|
68
|
+
testpaths = ["tests"]
|
|
69
|
+
addopts = "-ra"
|