GlamMonitor 4.0.0__tar.gz → 4.2.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.
@@ -56,3 +56,6 @@ env/
56
56
 
57
57
  glam-monitor-api.tar
58
58
  .env
59
+
60
+ # Build metadata
61
+ PKG-INFO
@@ -1,4 +1,4 @@
1
- Copyright (c) 2018 The Python Packaging Authority
1
+ Copyright (c) 2023 Glam Software 2012 SL
2
2
 
3
3
  Permission is hereby granted, free of charge, to any person obtaining a copy
4
4
  of this software and associated documentation files (the "Software"), to deal
@@ -0,0 +1,41 @@
1
+ Metadata-Version: 2.5
2
+ Name: GlamMonitor
3
+ Version: 4.2.0
4
+ Summary: Package to add logs that are stored locally and to report the start and end of processes to the Monitor API
5
+ Project-URL: Homepage, https://github.com/Glam-Software/GlamMonitor
6
+ Project-URL: Bug Tracker, https://github.com/Glam-Software/GlamMonitor/issues
7
+ Author-email: Glam Software 2012 SL <development@glamsw.com>
8
+ License-File: LICENSE
9
+ Classifier: License :: OSI Approved :: MIT License
10
+ Classifier: Operating System :: OS Independent
11
+ Classifier: Programming Language :: Python :: 3
12
+ Requires-Python: >=3.7
13
+ Requires-Dist: requests
14
+ Description-Content-Type: text/markdown
15
+
16
+ # Glam Monitor
17
+
18
+ This is a project to allow monitor your applications storing info and logs in local harddrive.
19
+ It also communicates with an API called GlamMonitorAPI where you are notified when a process starts, ends and the status of it.
20
+
21
+ ## Configuration
22
+
23
+ Environment variables:
24
+
25
+ | Variable | Required | Description |
26
+ |---|---|---|
27
+ | `MONITOR_LOG_PATH` | yes | Folder where the local logs are stored. |
28
+ | `MONITOR_URL` | no | Root URL of GlamMonitorAPI (without `/v2`; a trailing `/v1` from old configurations is ignored). If missing, the package only logs locally. |
29
+ | `MONITOR_KEY` | no | Application token (`x-access-token`) with the `writer` role. |
30
+ | `MONITOR_PROCESS_ID` | no | Id of the process whose executions are reported. |
31
+ | `MONITOR_SSL_VERIFY` | no | `true` by default. `false` disables the certificate check; any other value is the path to a CA bundle. |
32
+
33
+ `MONITOR_KEY` and `MONITOR_PROCESS_ID` can also be passed to the constructor, which takes precedence over the environment:
34
+
35
+ ```python
36
+ monitor = Monitor(MONITOR_KEY="...", MONITOR_PROCESS_ID="...")
37
+ monitor.info("Started")
38
+ monitor.close("Finished")
39
+ ```
40
+
41
+ Since 4.1.0 the package uses `/v2/logs`. Versions up to 4.0.x call `{MONITOR_URL}/events`, which the API keeps as a deprecated endpoint.
@@ -0,0 +1,26 @@
1
+ # Glam Monitor
2
+
3
+ This is a project to allow monitor your applications storing info and logs in local harddrive.
4
+ It also communicates with an API called GlamMonitorAPI where you are notified when a process starts, ends and the status of it.
5
+
6
+ ## Configuration
7
+
8
+ Environment variables:
9
+
10
+ | Variable | Required | Description |
11
+ |---|---|---|
12
+ | `MONITOR_LOG_PATH` | yes | Folder where the local logs are stored. |
13
+ | `MONITOR_URL` | no | Root URL of GlamMonitorAPI (without `/v2`; a trailing `/v1` from old configurations is ignored). If missing, the package only logs locally. |
14
+ | `MONITOR_KEY` | no | Application token (`x-access-token`) with the `writer` role. |
15
+ | `MONITOR_PROCESS_ID` | no | Id of the process whose executions are reported. |
16
+ | `MONITOR_SSL_VERIFY` | no | `true` by default. `false` disables the certificate check; any other value is the path to a CA bundle. |
17
+
18
+ `MONITOR_KEY` and `MONITOR_PROCESS_ID` can also be passed to the constructor, which takes precedence over the environment:
19
+
20
+ ```python
21
+ monitor = Monitor(MONITOR_KEY="...", MONITOR_PROCESS_ID="...")
22
+ monitor.info("Started")
23
+ monitor.close("Finished")
24
+ ```
25
+
26
+ Since 4.1.0 the package uses `/v2/logs`. Versions up to 4.0.x call `{MONITOR_URL}/events`, which the API keeps as a deprecated endpoint.
@@ -0,0 +1,24 @@
1
+ [build-system]
2
+ requires = ["hatchling"]
3
+ build-backend = "hatchling.build"
4
+ [project]
5
+ name = "GlamMonitor"
6
+ version = "4.2.0"
7
+ authors = [
8
+ { name="Glam Software 2012 SL", email="development@glamsw.com" },
9
+ ]
10
+ description = "Package to add logs that are stored locally and to report the start and end of processes to the Monitor API"
11
+ readme = "README.md"
12
+ requires-python = ">=3.7"
13
+ dependencies = [
14
+ "requests",
15
+ ]
16
+ classifiers = [
17
+ "Programming Language :: Python :: 3",
18
+ "License :: OSI Approved :: MIT License",
19
+ "Operating System :: OS Independent",
20
+ ]
21
+
22
+ [project.urls]
23
+ "Homepage" = "https://github.com/Glam-Software/GlamMonitor"
24
+ "Bug Tracker" = "https://github.com/Glam-Software/GlamMonitor/issues"
@@ -4,7 +4,10 @@ import os
4
4
  import requests
5
5
 
6
6
  if os.environ.__contains__('MONITOR_URL'):
7
- MONITOR_URL = os.environ['MONITOR_URL']
7
+ # Arrel de l'API. Les instal·lacions configurades per a l'API v1 hi tenen el sufix /v1, que es descarta.
8
+ MONITOR_URL = os.environ['MONITOR_URL'].rstrip('/')
9
+ if MONITOR_URL.endswith('/v1'):
10
+ MONITOR_URL = MONITOR_URL[:-len('/v1')]
8
11
  else:
9
12
  MONITOR_URL = None
10
13
  if os.environ.__contains__('MONITOR_KEY'):
@@ -19,13 +22,31 @@ else:
19
22
  MONITOR_LOG_PATH = os.environ['MONITOR_LOG_PATH']
20
23
 
21
24
 
25
+ def _ssl_verify_from_env():
26
+ # MONITOR_SSL_VERIFY: per defecte es verifica el certificat de l'API. Amb false/0/no es desactiva
27
+ # la verificació (requests/urllib3 en mostren l'avís) i qualsevol altre valor és la ruta d'un CA bundle.
28
+ value = os.environ.get('MONITOR_SSL_VERIFY', '').strip()
29
+ if value.lower() in ('', 'true', '1', 'yes'):
30
+ return True
31
+ if value.lower() in ('false', '0', 'no'):
32
+ return False
33
+ return value
34
+
35
+
36
+ MONITOR_SSL_VERIFY = _ssl_verify_from_env()
37
+
38
+
22
39
  class Monitor:
23
40
  N_DAYS_LOG_ERROR = 30
24
41
  N_DAYS_LOG_INFO = 5
25
42
 
26
- def __init__(self):
43
+ # Per defecte la key i el process_id s'agafen de les variables d'entorn MONITOR_KEY i MONITOR_PROCESS_ID;
44
+ # si es passen al constructor, s'utilitzen aquests valors en totes les crides a l'API (inici i tancament).
45
+ def __init__(self, MONITOR_KEY=None, MONITOR_PROCESS_ID=None):
27
46
  self._init_time = datetime.datetime.now()
28
47
  self._url_monitor = MONITOR_URL
48
+ self._key = MONITOR_KEY if MONITOR_KEY is not None else os.environ.get('MONITOR_KEY')
49
+ self._process_id = MONITOR_PROCESS_ID if MONITOR_PROCESS_ID is not None else os.environ.get('MONITOR_PROCESS_ID')
29
50
  self._path = MONITOR_LOG_PATH
30
51
  # Si no existeix la carpeta logs, la creem
31
52
  if not os.path.exists(MONITOR_LOG_PATH):
@@ -52,28 +73,40 @@ class Monitor:
52
73
  if self._url_monitor is not None:
53
74
  try:
54
75
  self.info(
55
- "Notify monitor API start of a new event of the process " + str(MONITOR_PROCESS_ID))
76
+ "Notify monitor API start of a new event of the process " + str(self._process_id))
56
77
  r = requests.post(
57
- url=self._url_monitor + "/events",
78
+ url=self._url_monitor + "/v2/logs",
58
79
  data=json.dumps(
59
80
  {
60
- "process_id": MONITOR_PROCESS_ID
81
+ "process_id": self._process_id
61
82
  }),
62
- headers={
63
- "x-access-token": MONITOR_KEY,
64
- "Content-Type": "application/json"
65
- },
66
- verify=False
83
+ headers=self._headers(),
84
+ verify=MONITOR_SSL_VERIFY
67
85
  )
86
+ r.raise_for_status()
68
87
  self._event_id = r.json()['id']
69
88
  self.info("Connection successfully with the API. Started event " + str(self._event_id))
70
89
  except Exception as e:
71
- self._log(is_error=True, text='Error connecting with the monitor API: ' + str(e))
90
+ self._log(is_error=True, text='Error connecting with the monitor API: ' + self._describe_error(e))
72
91
  self._event_id = None
73
92
  self._event_with_errors = True
74
93
  else:
75
94
  self.info('Monitor API not defined. Using only as local logger')
76
95
 
96
+ def _headers(self):
97
+ return {
98
+ "x-access-token": self._key,
99
+ "Content-Type": "application/json"
100
+ }
101
+
102
+ @staticmethod
103
+ def _describe_error(e: Exception) -> str:
104
+ # Afegeix el missatge que torna l'API (p. ex. 'Invalid token') a l'error HTTP.
105
+ response = getattr(e, 'response', None)
106
+ if isinstance(e, requests.HTTPError) and response is not None and response.text:
107
+ return str(e) + ' - ' + response.text.strip()
108
+ return str(e)
109
+
77
110
  def _log(self, is_error: bool, text: str):
78
111
  if is_error is True:
79
112
  _file_name = 'e_'
@@ -107,17 +140,13 @@ class Monitor:
107
140
  body.update({'message': message})
108
141
 
109
142
  r = requests.patch(
110
- url=self._url_monitor + "/events/" + str(self._event_id),
143
+ url=self._url_monitor + "/v2/logs/" + str(self._event_id),
111
144
  data=json.dumps(body),
112
- headers={
113
- "x-access-token": MONITOR_KEY,
114
- "Content-Type": "application/json"
115
- },
116
- verify=False
145
+ headers=self._headers(),
146
+ verify=MONITOR_SSL_VERIFY
117
147
  )
118
- if r.status_code not in (200, 201):
119
- raise Exception("Http response: " + str(r.status_code))
148
+ r.raise_for_status()
120
149
  except Exception as e:
121
- self._log(is_error=True, text="Error finishing event with the monitor API: " + str(e))
150
+ self._log(is_error=True, text="Error finishing event with the monitor API: " + self._describe_error(e))
122
151
  else:
123
152
  self.info(f"End of event {self._event_id} with status {_status} notified to the monitor API")
@@ -1,18 +0,0 @@
1
- Metadata-Version: 2.3
2
- Name: GlamMonitor
3
- Version: 4.0.0
4
- Summary: Package to add logs that are stored in local and communicates start and end of processes with monitor API
5
- Project-URL: Homepage, https://github.com/pypa/sampleproject
6
- Project-URL: Bug Tracker, https://github.com/pypa/sampleproject/issues
7
- Author-email: Glam Softwre 2012 SL <development@glamsw.com>
8
- License-File: LICENSE
9
- Classifier: License :: OSI Approved :: MIT License
10
- Classifier: Operating System :: OS Independent
11
- Classifier: Programming Language :: Python :: 3
12
- Requires-Python: >=3.7
13
- Description-Content-Type: text/markdown
14
-
15
- # Glam Monitor
16
-
17
- This is a project to allow monitor your applications storing info and logs in local harddrive.
18
- It also communicates with an API called GlamMonitorAPI where you are notified when a process starts, ends and the status of it.
@@ -1,4 +0,0 @@
1
- # Glam Monitor
2
-
3
- This is a project to allow monitor your applications storing info and logs in local harddrive.
4
- It also communicates with an API called GlamMonitorAPI where you are notified when a process starts, ends and the status of it.
@@ -1,21 +0,0 @@
1
- [build-system]
2
- requires = ["hatchling"]
3
- build-backend = "hatchling.build"
4
- [project]
5
- name = "GlamMonitor"
6
- version = "4.0.0"
7
- authors = [
8
- { name="Glam Softwre 2012 SL", email="development@glamsw.com" },
9
- ]
10
- description = "Package to add logs that are stored in local and communicates start and end of processes with monitor API"
11
- readme = "README.md"
12
- requires-python = ">=3.7"
13
- classifiers = [
14
- "Programming Language :: Python :: 3",
15
- "License :: OSI Approved :: MIT License",
16
- "Operating System :: OS Independent",
17
- ]
18
-
19
- [project.urls]
20
- "Homepage" = "https://github.com/pypa/sampleproject"
21
- "Bug Tracker" = "https://github.com/pypa/sampleproject/issues"
File without changes