flexmetric 0.5.2__py3-none-any.whl → 0.5.4__py3-none-any.whl

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.
@@ -218,7 +218,10 @@ def measure(args):
218
218
  # print(label_dict)
219
219
 
220
220
  try:
221
- gauge.labels(**label_dict).set(convert_to_data_type(result["value"]))
221
+ if len(label_dict) > 0:
222
+ gauge.labels(**label_dict).set(convert_to_data_type(result["value"]))
223
+ else:
224
+ gauge.set(result["value"])
222
225
  except Exception as ex:
223
226
  logger.error(f"Failed to set gauge for labels {label_dict}: {ex}")
224
227
 
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: flexmetric
3
- Version: 0.5.2
3
+ Version: 0.5.4
4
4
  Summary: A secure flexible Prometheus exporter for commands, databases, functions.
5
5
  Home-page: https://github.com/nikhillingadhal1999/flexmetric
6
6
  Author: Nikhil Lingadhal
@@ -22,6 +22,7 @@ Requires-Dist: twine
22
22
  Requires-Dist: flask
23
23
  Requires-Dist: clickhouse-connect
24
24
  Requires-Dist: psycopg2-binary
25
+ Requires-Dist: ollama
25
26
  Dynamic: author
26
27
  Dynamic: classifier
27
28
  Dynamic: description
@@ -33,25 +34,38 @@ Dynamic: requires-dist
33
34
  Dynamic: requires-python
34
35
  Dynamic: summary
35
36
 
36
- # FlexMetric
37
+ FlexMetric is a lightweight, pluggable, and extensible Prometheus exporter that helps you collect, expose, and visualize custom metrics with minimal setup and maximum flexibility.
37
38
 
38
- FlexMetric is a lightweight, flexible, and extensible Prometheus exporter that allows you to securely expose system metrics, database query results, Python function outputs, and externally submitted metrics—via an optional Flask API with HTTPS support—as Prometheus-compatible metrics, all with minimal setup and maximum customization.
39
+ With FlexMetric, you can:
39
40
 
40
- ---
41
+ Run system commands and expose their output as metrics
42
+
43
+ Execute SQL queries against databases like SQLite, PostgreSQL, and ClickHouse
41
44
 
42
- ## Features
45
+ Call custom Python functions and export their results
43
46
 
44
- - Run shell commands and expose the results as Prometheus metrics.
45
- ➔ **Harmful commands (e.g., file deletion, system shutdown) are blocked for safety.**
46
- - Execute SQL queries (e.g., SQLite) and monitor database statistics.
47
- ➔ **Potentially dangerous queries (e.g., `DROP`, `DELETE`, `TRUNCATE`) are not allowed.**
48
- - Automatically discover and expose Python function outputs as metrics.
49
- - Expose an optional **Flask API** (`/update_metric`) to receive external metrics dynamically.
50
- - Modular and easy to extend—add your own custom integrations.
51
- - Built-in Prometheus HTTP server (`/metrics`) with configurable port.
52
- - **Supports HTTPS** to securely expose both metrics and API endpoints.
53
- - **Input sanitization** is performed to ensure only safe commands and queries are executed.
47
+ Receive externally submitted metrics via a secure Flask-based API with optional HTTPS (TLS)
48
+
49
+ All metrics are exposed in Prometheus-compatible format, making them ready for visualization in Grafana or any Prometheus-based monitoring stack.
50
+ ---
54
51
 
52
+ Paste your rich text content here. You can paste directly from Word or other rich text sources.
53
+
54
+ * Run **shell commands** and expose the results as **Prometheus metrics**.
55
+ ➔ _Harmful commands (e.g., file deletion, system shutdown) are blocked for safety._
56
+
57
+ * Execute **SQL queries** and monitor database statistics:
58
+
59
+ * **SQLite** (lightweight, file-based databases)
60
+ * **PostgreSQL** (robust, production-grade relational databases)
61
+ * **ClickHouse** (high-performance, analytical databases)
62
+ * Potentially dangerous queries (e.g., `DROP`, `DELETE`, `TRUNCATE`) are blocked by default.
63
+ * Automatically discover and expose **Python function outputs** as metrics.
64
+ * Expose an optional **Flask API** (`/update_metric`) to receive and update external metrics **dynamically**.
65
+ * Modular and **easy to extend**—add your own **custom integrations** with minimal effort.
66
+ * Built-in **Prometheus-compatible HTTP server** (`/metrics`) with configurable port.
67
+ * Supports **HTTPS** to securely expose both the metrics endpoint and API.
68
+ * Input **sanitization and validation** ensure only safe commands and queries are executed.
55
69
 
56
70
  ---
57
71
 
@@ -128,7 +142,31 @@ curl -X POST http://localhost:5000/update_metric \
128
142
  "labels": ["metric_type", "core"],
129
143
  "main_label": "cpu_usage_metric"
130
144
  }'
145
+ ```
146
+ Python client
147
+ ```python
148
+ import requests
149
+ import json
150
+
151
+ url = "http://localhost:5000/update_metric"
152
+ headers = {
153
+ "Content-Type": "application/json"
154
+ }
155
+ data = {
156
+ "result": [
157
+ {
158
+ "label": ["cpu", "core_1"],
159
+ "value": 42.5
160
+ }
161
+ ],
162
+ "labels": ["metric_type", "core"],
163
+ "main_label": "cpu_usage_metric"
164
+ }
131
165
 
166
+ response = requests.post(url, headers=headers, data=json.dumps(data))
167
+
168
+ print(f"Status Code: {response.status_code}")
169
+ print("Response:", response.text)
132
170
  ```
133
171
 
134
172
  ### Using flex metrics in secure mode
@@ -190,52 +228,99 @@ Filesystem Size Used Avail Use% Mounted on
190
228
  | `timeout_seconds` | Maximum time (in seconds) to wait for the command to complete. If it exceeds this time, the command is aborted. |
191
229
 
192
230
  ## Database mode
231
+ ## 🔗 Supported Database Connections
232
+
233
+ FlexMetric supports fetching and exposing metrics from multiple databases using a simple YAML-based configuration. This allows you to monitor data directly from your existing data sources without writing custom code.
234
+
235
+ ### ✅ Currently Supported Databases:
236
+
237
+ | Database | Type Name | Description |
238
+ |--------------|---------------|--------------------------------------------------|
239
+ | **SQLite** | `sqlite` | Lightweight, file-based relational database |
240
+ | **PostgreSQL** | `postgres` | Robust, production-grade relational database |
241
+ | **ClickHouse** | `clickhouse` | High-performance, analytical columnar database |
242
+
243
+ ---
244
+
245
+ ### 📄 Example `database.yaml` Configuration:
193
246
  file - database.yaml
194
247
  ```yaml
195
248
  databases:
196
- - id: "active_user_count"
249
+ - id: "local_sqlite"
250
+ type: "sqlite"
251
+ db_connection: "/path/to/example.db"
252
+
253
+ - id: "analytics_pg"
254
+ type: "postgres"
255
+ host: "localhost"
256
+ port: 5432
257
+ database: "metricsdb"
258
+ username: "postgres"
259
+ password: "postgres_password"
260
+ sslmode: "disable"
261
+ client_cert: "/path/to/cert.pem"
262
+ client_key: "/path/to/key.pem"
263
+ ca_cert: "/path/to/ca.pem"
264
+
265
+ - id: "clickhouse_cluster"
197
266
  type: "clickhouse"
198
267
  host: "localhost"
199
- port: 8123
268
+ port: 8443
200
269
  username: "default"
201
- password: ""
202
- client_cert: ""
203
- client_key: ""
204
- ca_cert: ""
205
-
206
- - id: "userdb"
207
- type: "sqlite"
208
- db_connection: "/path/to/my.db"
270
+ password: "clickhouse_password"
271
+ secure: true
272
+ client_cert: "/path/to/cert.pem"
273
+ client_key: "/path/to/key.pem"
274
+ ca_cert: "/path/to/ca.pem"
209
275
  ```
210
- file - queries.yaml
276
+
277
+ ## Supported Query Configuration
278
+
279
+ FlexMetric allows you to define custom queries in a simple YAML format. Each query is linked to a database using the `database_id` and can expose the results as Prometheus metrics with flexible labeling and value extraction.
280
+
281
+ ### Example `queries.yaml` Configuration:
282
+
211
283
  ```yaml
212
284
  commands:
213
- - id: "active_user_count"
214
- type: "clickhouse"
215
- database_id: "active_user_count"
285
+ - id: "active_user_count_pg"
286
+ type: "postgres"
287
+ database_id: "analytics_pg"
216
288
  query: |
217
289
  SELECT
218
290
  country AS country_name,
219
- COUNT() AS active_user_count
291
+ COUNT(*) AS active_user_count
220
292
  FROM users
221
- WHERE is_active = 1
222
- GROUP BY country
293
+ WHERE is_active = true
294
+ GROUP BY country;
223
295
  main_label: "active_user_count"
224
296
  labels: ["country_name"]
225
297
  value_column: "active_user_count"
226
298
 
227
- - id: "list_all_users_sqlite"
299
+ - id: "total_user_count_sqlite"
228
300
  type: "sqlite"
229
- database_id: "userdb"
301
+ database_id: "local_sqlite"
230
302
  query: |
231
303
  SELECT
232
- id,
233
- name
304
+ 'all' AS user_group,
305
+ COUNT(*) AS total_user_count
306
+ FROM users;
307
+ main_label: "total_user_count"
308
+ labels: ["user_group"]
309
+ value_column: "total_user_count"
310
+
311
+ - id: "clickhouse_user_summary"
312
+ type: "clickhouse"
313
+ database_id: "clickhouse_cluster"
314
+ query: |
315
+ SELECT
316
+ region AS region_name,
317
+ COUNT() AS user_count
234
318
  FROM users
235
- main_label: "user_list"
236
- labels: ["id", "name"]
237
- value_column: "id"
238
-
319
+ WHERE status = 'active'
320
+ GROUP BY region;
321
+ main_label: "clickhouse_user_count"
322
+ labels: ["region_name"]
323
+ value_column: "user_count"
239
324
  ```
240
325
  ## Functions mode
241
326
 
@@ -10,11 +10,11 @@ flexmetric/metric_process/database_connections.py,sha256=Pa_Lcs8Im6pZxmET2pQRR3-
10
10
  flexmetric/metric_process/database_processing.py,sha256=Sbbk-ktdUSx7zFpjaJdC_8L6r61d82t4VKGOr7oAlB0,5066
11
11
  flexmetric/metric_process/expiring_queue.py,sha256=1oC0MjloitPiRo7yDgVarz81ETEQavKI_W-GFUvp5_Y,1920
12
12
  flexmetric/metric_process/process_commands.py,sha256=clGMQhLNcuJUO1gElpAS9Dyk0KU5w41DIguczjo7ceA,4089
13
- flexmetric/metric_process/prometheus_agent.py,sha256=pDPqiPtHLv1cfF6dPRNg2LtAhVF1UKf8MNZ_S9xuRQY,7712
13
+ flexmetric/metric_process/prometheus_agent.py,sha256=9M6WN-iXKtE5LkweuRH96MwuwLcPh89pRG5YoYhK4Vo,7825
14
14
  flexmetric/metric_process/queries_execution.py,sha256=ed8GnWbBdvCb7WfQOmeJdpR0DarF_EHbXBPc6tKaEyg,1229
15
15
  flexmetric/metric_process/views.py,sha256=BY695dCpTkJRc1KLC9RNpFTieFdHeHvyqyefmHhMauE,3297
16
- flexmetric-0.5.2.dist-info/METADATA,sha256=rDkws5sg0EbOrR9r_yoOp3ZhdM7av_7ddXMg9YIXFPw,11307
17
- flexmetric-0.5.2.dist-info/WHEEL,sha256=_zCd3N1l69ArxyTb8rzEoP9TpbYXkqRFSNOD5OuxnTs,91
18
- flexmetric-0.5.2.dist-info/entry_points.txt,sha256=urVePn5EWr3JqNvkYP7OsB_h2_Bqvv-Wq1MJRBexm8A,79
19
- flexmetric-0.5.2.dist-info/top_level.txt,sha256=zBlrNwKhXUNhgu9RRZnXxYwYnmE-eocRe6wKSmQROA4,11
20
- flexmetric-0.5.2.dist-info/RECORD,,
16
+ flexmetric-0.5.4.dist-info/METADATA,sha256=A4lS-lUf7AG1npmXgha49dNDpZB8tuwI0rBAcDHgmX4,14343
17
+ flexmetric-0.5.4.dist-info/WHEEL,sha256=_zCd3N1l69ArxyTb8rzEoP9TpbYXkqRFSNOD5OuxnTs,91
18
+ flexmetric-0.5.4.dist-info/entry_points.txt,sha256=urVePn5EWr3JqNvkYP7OsB_h2_Bqvv-Wq1MJRBexm8A,79
19
+ flexmetric-0.5.4.dist-info/top_level.txt,sha256=zBlrNwKhXUNhgu9RRZnXxYwYnmE-eocRe6wKSmQROA4,11
20
+ flexmetric-0.5.4.dist-info/RECORD,,