gomyck-tools 1.2.3__py3-none-any.whl → 1.2.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.
- ctools/metrics.py +48 -41
- {gomyck_tools-1.2.3.dist-info → gomyck_tools-1.2.4.dist-info}/METADATA +2 -1
- {gomyck_tools-1.2.3.dist-info → gomyck_tools-1.2.4.dist-info}/RECORD +5 -5
- {gomyck_tools-1.2.3.dist-info → gomyck_tools-1.2.4.dist-info}/WHEEL +0 -0
- {gomyck_tools-1.2.3.dist-info → gomyck_tools-1.2.4.dist-info}/top_level.txt +0 -0
ctools/metrics.py
CHANGED
@@ -1,29 +1,34 @@
|
|
1
1
|
import os
|
2
2
|
import threading
|
3
|
+
import time
|
3
4
|
from enum import Enum
|
4
5
|
|
5
|
-
from prometheus_client import Counter, Gauge, Summary, Histogram
|
6
|
-
|
7
|
-
from ctools import call, cjson, sys_log
|
8
|
-
from ctools.application import Server
|
6
|
+
from prometheus_client import Counter, Gauge, Summary, Histogram, start_http_server
|
7
|
+
from ctools import call, cjson, sys_log, work_path
|
9
8
|
|
10
9
|
log = sys_log.flog
|
11
10
|
|
12
|
-
|
11
|
+
_metrics_port = 8011
|
12
|
+
_persistent_json = {}
|
13
13
|
_metrics_initial = {}
|
14
|
-
persistent_json = {}
|
15
|
-
temp_metrics_json = {}
|
16
|
-
is_metrics_init: bool = True
|
17
14
|
_lock = threading.Lock()
|
18
15
|
|
19
16
|
|
17
|
+
# 认证中间件
|
18
|
+
# @app.before_request
|
19
|
+
# def check_authentication():
|
20
|
+
# auth = request.authorization
|
21
|
+
# if not auth or auth.username != USERNAME or auth.password != PASSWORD:
|
22
|
+
# return Response(
|
23
|
+
# "Unauthorized", 401, {"WWW-Authenticate": 'Basic realm="Login Required"'}
|
24
|
+
# )
|
25
|
+
|
20
26
|
class MetricType(Enum):
|
21
27
|
COUNTER = 'counter'
|
22
28
|
GAUGE = 'gauge'
|
23
29
|
SUMMARY = 'summary'
|
24
30
|
HISTOGRAM = 'histogram'
|
25
31
|
|
26
|
-
|
27
32
|
class Metric:
|
28
33
|
def __init__(self, metric_type: MetricType, metric_key: str, metric_labels: [],
|
29
34
|
persistent: bool = False, buckets: [] = None, reset: bool = False, desc: str = ""):
|
@@ -47,60 +52,51 @@ class Metric:
|
|
47
52
|
raise Exception('metric type not found')
|
48
53
|
_metrics_initial[metric_key] = self
|
49
54
|
|
50
|
-
|
51
55
|
@call.once
|
52
|
-
def init():
|
53
|
-
|
54
|
-
|
55
|
-
persistent_path = os.path.join(Server.indicatorsPath, 'persistent.json')
|
56
|
-
if os.path.exists(persistent_path):
|
56
|
+
def init(reset_persistent: bool = False):
|
57
|
+
persistent_path = os.path.join(work_path.get_current_path(), 'persistent.json')
|
58
|
+
if os.path.exists(persistent_path) and not reset_persistent:
|
57
59
|
with open(persistent_path, 'r') as persistent_file:
|
58
|
-
global
|
60
|
+
global _persistent_json
|
59
61
|
try:
|
60
62
|
content = persistent_file.readline()
|
61
|
-
|
62
|
-
|
63
|
+
log.info("persistent初始化: %s" % content)
|
64
|
+
_persistent_json = cjson.loads(content)
|
63
65
|
except Exception:
|
64
66
|
log.error('persistent.json is not valid json!!!!!')
|
65
|
-
|
67
|
+
_persistent_json = {}
|
66
68
|
_init_all_metrics()
|
67
|
-
for key, item in
|
69
|
+
for key, item in _persistent_json.items():
|
68
70
|
metrics_key = key.split("-")[0]
|
69
71
|
if '_labels' in key or metrics_key not in _metrics_initial: continue
|
70
|
-
opt(metrics_key,
|
72
|
+
opt(metrics_key, _persistent_json[key + '_labels'], _persistent_json[key])
|
71
73
|
persistent_metrics()
|
72
|
-
|
74
|
+
start_http_server(port=_metrics_port)
|
73
75
|
|
74
|
-
|
75
|
-
@call.schd(60, start_by_call=True)
|
76
|
+
@call.schd(5, start_by_call=True)
|
76
77
|
def persistent_metrics():
|
77
|
-
if
|
78
|
-
|
79
|
-
|
78
|
+
if _persistent_json and not _lock.locked():
|
79
|
+
log.info('begin persistent metrics to file...')
|
80
|
+
with open(os.path.join(work_path.get_current_path(), 'persistent.json'), 'w') as persistent_file:
|
81
|
+
persistent_file.write(cjson.dumps(_persistent_json))
|
80
82
|
persistent_file.flush()
|
81
83
|
|
82
|
-
|
83
84
|
def opt(metric_key: str, label_values: [], metric_value: int):
|
84
85
|
_lock.acquire(timeout=5)
|
85
86
|
try:
|
86
87
|
persistent_key = "%s-%s" % (metric_key, "_".join(map(str, label_values)))
|
87
|
-
metric_entity: Metric =
|
88
|
-
if not metric_entity:
|
89
|
-
metric_entity = metrics[persistent_key] = _metrics_initial[metric_key]
|
90
|
-
|
88
|
+
metric_entity: Metric = _metrics_initial[metric_key]
|
91
89
|
if metric_entity.persistent:
|
92
|
-
if not
|
93
|
-
|
90
|
+
if not metric_entity.reset and persistent_key in _persistent_json:
|
91
|
+
_persistent_json[persistent_key] += metric_value
|
94
92
|
else:
|
95
|
-
|
96
|
-
|
93
|
+
_persistent_json[persistent_key] = metric_value
|
94
|
+
_persistent_json[persistent_key + '_labels'] = label_values
|
97
95
|
|
98
|
-
if
|
99
|
-
|
96
|
+
if _persistent_json[persistent_key] < 0:
|
97
|
+
_persistent_json[persistent_key] = 0
|
100
98
|
metric_value = 0
|
101
99
|
|
102
|
-
temp_metrics_json[persistent_key] = metric_value
|
103
|
-
|
104
100
|
if metric_entity.metric_type == MetricType.COUNTER or metric_entity.metric_type == MetricType.GAUGE:
|
105
101
|
if label_values is None or len(label_values) == 0:
|
106
102
|
if metric_entity.metric_type == MetricType.COUNTER and metric_entity.reset:
|
@@ -123,4 +119,15 @@ def opt(metric_key: str, label_values: [], metric_value: int):
|
|
123
119
|
_lock.release()
|
124
120
|
|
125
121
|
def _init_all_metrics():
|
126
|
-
Metric(MetricType.
|
122
|
+
Metric(MetricType.GAUGE, 'data_reported_time', ['asdasd', 'sdfsdf'], persistent=True, reset=True)
|
123
|
+
Metric(MetricType.GAUGE, 'data_received_time', ['asdasd', 'sdfsdf'], persistent=True, reset=True)
|
124
|
+
Metric(MetricType.GAUGE, 'data_insert_time', ['asdasd', 'sdfsdf'], persistent=True, reset=True)
|
125
|
+
|
126
|
+
if __name__ == '__main__':
|
127
|
+
init()
|
128
|
+
import random
|
129
|
+
while True:
|
130
|
+
opt('data_reported_time', ['123', '123'], random.randint(1, 10))
|
131
|
+
opt('data_received_time', ['123', '123'], random.randint(1, 10))
|
132
|
+
opt('data_insert_time', ['123', '123'], random.randint(1, 10))
|
133
|
+
time.sleep(1)
|
@@ -1,6 +1,6 @@
|
|
1
1
|
Metadata-Version: 2.1
|
2
2
|
Name: gomyck-tools
|
3
|
-
Version: 1.2.
|
3
|
+
Version: 1.2.4
|
4
4
|
Summary: A ctools for python development by hao474798383
|
5
5
|
Home-page: https://blog.gomyck.com
|
6
6
|
Author: gomyck
|
@@ -27,5 +27,6 @@ Requires-Dist: paho-mqtt ~=2.1.0
|
|
27
27
|
Requires-Dist: fuzzywuzzy ~=0.18.0
|
28
28
|
Requires-Dist: pymysql ~=1.1.1
|
29
29
|
Requires-Dist: pyzipper ==0.3.6
|
30
|
+
Requires-Dist: prometheus-client ==0.21.1
|
30
31
|
|
31
32
|
this package is for python development
|
@@ -26,7 +26,7 @@ ctools/html_soup.py,sha256=rnr8M3gn3gQGo-wNaNFXDjdzp8AAkv9o4yqfIIfO-zw,1567
|
|
26
26
|
ctools/http_utils.py,sha256=dG26aci1_YxAyKwYqMKFw4wZAryLkDyvnQ3hURjB6Lk,768
|
27
27
|
ctools/images_tools.py,sha256=TapXYCPqC7GesgrALecxxa_ApuT_dxUG5fqQIJF2bNY,670
|
28
28
|
ctools/imgDialog.py,sha256=zFeyPmpnEn9Ih7-yuJJrePqW8Myj3jC9UYMTDk-umTs,1385
|
29
|
-
ctools/metrics.py,sha256=
|
29
|
+
ctools/metrics.py,sha256=BKNtOEskq5fvSkGX1MbAO1aG9DWjBBN9PDQZ8e-zaVM,5085
|
30
30
|
ctools/mqtt_utils.py,sha256=ZWSZiiNJLLlkHF95S6LmRmkYt-iIL4K73VdN3b1HaHw,10702
|
31
31
|
ctools/obj.py,sha256=GYS1B8NyjtUIh0HlK9r8avC2eGbK2SJac4C1CGnfGhI,479
|
32
32
|
ctools/pacth.py,sha256=MJ9Du-J9Gv62y4cZKls1jKbl5a5kL2y9bD-gzYUCveQ,2604
|
@@ -51,7 +51,7 @@ ctools/wordFill.py,sha256=dB1OLt6GLmWdkDV8H20VWbJmY4ggNNI8iHD1ocae2iM,875
|
|
51
51
|
ctools/word_fill.py,sha256=xeo-P4DOjQUqd-o9XL3g66wQrE2diUPGwFywm8TdVyw,18210
|
52
52
|
ctools/word_fill_entity.py,sha256=eX3G0Gy16hfGpavQSEkCIoKDdTnNgRRJrFvKliETZK8,985
|
53
53
|
ctools/work_path.py,sha256=i4MTUobqNW2WMrT3mwEC_XYQ0_IhFmKoNpTX2W6A8Tc,1680
|
54
|
-
gomyck_tools-1.2.
|
55
|
-
gomyck_tools-1.2.
|
56
|
-
gomyck_tools-1.2.
|
57
|
-
gomyck_tools-1.2.
|
54
|
+
gomyck_tools-1.2.4.dist-info/METADATA,sha256=q9kd9ZRTLTnINHKnwzRXOIcjTo8an74RmCBjW57AT0Y,1046
|
55
|
+
gomyck_tools-1.2.4.dist-info/WHEEL,sha256=yQN5g4mg4AybRjkgi-9yy4iQEFibGQmlz78Pik5Or-A,92
|
56
|
+
gomyck_tools-1.2.4.dist-info/top_level.txt,sha256=-MiIH9FYRVKp1i5_SVRkaI-71WmF1sZSRrNWFU9ls3s,7
|
57
|
+
gomyck_tools-1.2.4.dist-info/RECORD,,
|
File without changes
|
File without changes
|