Skip to content

Commit

Permalink
新增prometheus接口/metrics域名数据
Browse files Browse the repository at this point in the history
  • Loading branch information
mouday committed Nov 21, 2023
1 parent 0870f88 commit df49347
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 2 deletions.
13 changes: 13 additions & 0 deletions doc/source/manual/question.md
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,19 @@ domain_admin{domain="www.tmall.com"} 37.0
domain_admin{domain="www.baidu.com"} 37.0
domain_admin{domain="www.taobao.com"} 37.0
```
@since v1.5.26 升级为
```
# HELP domain_admin this is a domain admin data
# TYPE domain_admin gauge 证书列表数据
domain_admin{domain="www.baidu.com",group_name="百度系",root_domain="baidu.com"} 258.0
domain_admin{domain="www.163.com",group_name="",root_domain="163.com"} 153.0
# HELP domain_info this is a domain info data
# TYPE domain_info gauge 域名列表数据
domain_info{domain="163.com",group_name="百度系"} 1392.0
domain_info{domain="qq.com",group_name=""} 3535.0
```
## 11、部分域名无法查询到信息
Expand Down
21 changes: 21 additions & 0 deletions domain_admin/api/prometheus_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
from domain_admin.config import PROMETHEUS_KEY
from domain_admin.enums.config_key_enum import ConfigKeyEnum
from domain_admin.log import logger
from domain_admin.model.domain_info_model import DomainInfoModel
from domain_admin.model.domain_model import DomainModel
from domain_admin.service import group_service

Expand All @@ -39,6 +40,7 @@ def metrics():

registry = CollectorRegistry(auto_describe=False)

# 证书数据
gauge = Gauge(
"domain_admin",
"this is a domain admin data",
Expand All @@ -61,4 +63,23 @@ def metrics():

logger.info('success')

# 域名数据
domain_info_gauge = Gauge(
"domain_info",
"this is a domain info data",
["domain", "group_name"],
registry=registry)

domain_info_rows = DomainInfoModel.select()

domain_info_lst = [model_to_dict(row, extra_attrs=['real_domain_expire_days']) for row in domain_info_rows]

# 分组名
group_service.load_group_name(domain_info_lst)
for row in domain_info_lst:
domain_info_gauge.labels(
row['domain'],
row['group_name']
).set(row['real_domain_expire_days'])

return Response(prometheus_client.generate_latest(registry), mimetype='text/plain')
7 changes: 6 additions & 1 deletion domain_admin/utils/flask_ext/handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,9 @@ def error_handler(e):

return ApiResult.error(msg=msg, code=code)
else:
return Response("Internal Server Error: {}".format(e.message), status=500)

message = '未知错误'
if hasattr(e, 'message'):
message = e.message

return Response("Internal Server Error: {}".format(message), status=500)
2 changes: 1 addition & 1 deletion domain_admin/version.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,4 @@
"""
from __future__ import print_function, unicode_literals, absolute_import, division

VERSION = '1.5.25'
VERSION = '1.5.26'

0 comments on commit df49347

Please sign in to comment.