使用Prometheus监控API

2023-05-16

[可以使用新的方案: 拨测API接口+监控方案_一直学下去的博客-CSDN博客]

写在最前面

  我心里也打鼓,是不是应该把这个文章写出来,因为这个项目里面的代码不能拿来就用,需要做修改(其实后端API监控本来就和业务相关,应该没有一个成品可以做到对每一个业务的API都能监控).其次文章里面涉及的知识点比较多,Prometheus,Prometheus-Operator,K8S,Python ,如果你已经具备这些基础,也愿意花时间来读这篇文章,请继续。如果按照步骤做了有问题,可以留言,我会回复的。

简介:

目前公司有一个项目,使用前后端分离。前端采样VUE,后端使用Java Spring全家桶,后端的接口为Restful API.为了能第一时间发现后端服务的故障和检测后端API的响应时间,自己使用Python+Prometheus_Client(python sdk) 写了一个exporter,然后对接prometheus,并配置告警。后端有故障的时候,可以第一时间发现,而不是等用户有感觉来才发现。 

后端项目的接口认证采用在header中附加token的形式,用户第一次登录的时候会返回token。

PS: 前端项目运行在nginx容器中,已经使用blackbox的http模块监控

实施步骤:

1. 将代码克隆到本地,或者使用pycharm导入

git clone https://gitee.com/kevinliu_CQ/api-monitoring.git

2. 修改代码中的配置

   a. 修改config_dir 下面的app.yaml文件。里面的域名和接口地址都需要修改,以我的项目为例,请查看描述信息

config:
     testset: 'APP API monitoring'  #描述性信息,代码中未使用
     timeout: 15 #调用API的超时时间
     scrape_interval: 15 #检测API的时间间隔,单位为秒
     base_url: 'https://app.×××.com/app' #API接口的URL,我的项目是微服务,这个app是微服务的url前缀
token:
     base_url: 'https://app.××××.com/app' #获取token的URL地址
     url: '/login/pwdLogin' #获取token的接口地址
     method: "POST" #获取接口的HTTP方法
     body: '' #获取接口时在post请求的payload内容
     params: {"mobile": "17320491234","pwd": "your_password"} #这个是在请求的URL中的参数列表,HTTP请求的参数可以放在URL里面,也可以放在payload里面,我的应用时放在URL里面的
     headers: {} # POST请求的header 
     token_key_in_request: 'authKey' # 在post请求中后端认证时在header里面读取的值名字,后面截图说明
     token_key_in_response: 'token' #在用户第一次登录后返回数据里面token的值的名字,
verify:
    base_url: 'https://app.****.com/app' #验证token的URL,因为token一般都有较长时间的有效期,不能监控一次就取一次token
    url: '/user/userAddress/list' #验证token的接口地址
    method: "GET" #验证token的HTTP方法
    token_key_in_request: 'authKey' # header里面token值的名字
    headers: {'Content-Type': 'application/json'} #HTTP请求的header
cases: #所有需要监控的API接口地址
- UserAddress:  #接口名字
    url: '/user/userAddress/list' #接口地址,相对于config段里base_url
    method: "GET" #请求方式
    headers: {'Content-Type': 'application/json'} # HTTP headers
- UserBalance:
    url: '/user/getBalance'
    method: "GET"
    headers: {'Content-Type': 'application/json'}
- UserRecord:
    url: '/user/userRecord/getTypeList'
    method: "GET"
    headers: {'Content-Type': 'application/json'}
- UserBankCard:
    url: '/user/bankCard/payList'
    method: "GET"
    headers: {'Content-Type': 'application/json'}

b. 如果有多个微服务API需要监控,直接添加另外一个文件就可以了,比如我的im.yaml这个文件,im是另外一个微服务

3. 制作Docker镜像,我这里的镜像上传到私有habor仓库中。

docker build -t harbor.***.work/monitor/api-monitoring:latest .
docker push harbor.***.work/monitor/api-monitoring:latest

4.修改k8s.yaml文件中的镜像地址,镜像地址和步骤3里面的地址一致

5. 在k8s中部署服务

kubectl apply -f k8s.yaml

6. 查看运行的容器

[root@controller ~]# kubectl get po -n monitoring
NAME                                READY   STATUS    RESTARTS   AGE
api-monitoring-5584fb9bbb-l2xb8     1/1     Running   0          32h
[root@controller ~]# kubectl get svc -n monitoring
NAME               TYPE        CLUSTER-IP        EXTERNAL-IP   PORT(S)    AGE
api-monitoring     ClusterIP   192.168.180.128   <none>        80/TCP     3d8h
[root@controller ~]# 

7. 查看prometheus采集到的数据,API 和API_ResponseTime,这两个Metrics分别代表API是否可用(1正常,0不正常)以及API的响应时间,单位是秒

[root@controller ~]# curl 192.168.180.128
# HELP python_gc_objects_collected_total Objects collected during gc
# TYPE python_gc_objects_collected_total counter
python_gc_objects_collected_total{generation="0"} 113122.0
python_gc_objects_collected_total{generation="1"} 10240.0
python_gc_objects_collected_total{generation="2"} 987.0
# HELP python_gc_objects_uncollectable_total Uncollectable object found during GC
# TYPE python_gc_objects_uncollectable_total counter
python_gc_objects_uncollectable_total{generation="0"} 0.0
python_gc_objects_uncollectable_total{generation="1"} 0.0
python_gc_objects_uncollectable_total{generation="2"} 0.0
# HELP python_gc_collections_total Number of times this generation was collected
# TYPE python_gc_collections_total counter
python_gc_collections_total{generation="0"} 7069.0
python_gc_collections_total{generation="1"} 642.0
python_gc_collections_total{generation="2"} 58.0
# HELP python_info Python platform information
# TYPE python_info gauge
python_info{implementation="CPython",major="3",minor="6",patchlevel="1",version="3.6.1"} 1.0
# HELP process_virtual_memory_bytes Virtual memory size in bytes.
# TYPE process_virtual_memory_bytes gauge
process_virtual_memory_bytes 9.0904576e+08
# HELP process_resident_memory_bytes Resident memory size in bytes.
# TYPE process_resident_memory_bytes gauge
process_resident_memory_bytes 3.485696e+07
# HELP process_start_time_seconds Start time of the process since unix epoch in seconds.
# TYPE process_start_time_seconds gauge
process_start_time_seconds 1.5912509406e+09
# HELP process_cpu_seconds_total Total user and system CPU time spent in seconds.
# TYPE process_cpu_seconds_total counter
process_cpu_seconds_total 967.65
# HELP process_open_fds Number of open file descriptors.
# TYPE process_open_fds gauge
process_open_fds 7.0
# HELP process_max_fds Maximum number of open file descriptors.
# TYPE process_max_fds gauge
process_max_fds 1.048576e+06
# HELP request_process_sencods Time spent processing request
# TYPE request_process_sencods summary
request_process_sencods_count 15472.0
request_process_sencods_sum 637.3630767939612
# HELP request_process_sencods_created Time spent processing request
# TYPE request_process_sencods_created gauge
request_process_sencods_created 1.5912509420943658e+09
# HELP API API Status
# TYPE API gauge
API{apiModule="IMFriend"} 1.0
API{apiModule="IMQrcode"} 1.0
API{apiModule="UserRecord"} 1.0
API{apiModule="UserBankCard"} 1.0
API{apiModule="UserBalance"} 1.0
API{apiModule="UserAddress"} 1.0
# HELP API_ResponseTime API Response Time
# TYPE API_ResponseTime gauge
API_ResponseTime{apiModule="IMFriend"} 0.0
API_ResponseTime{apiModule="IMQrcode"} 0.0
API_ResponseTime{apiModule="UserRecord"} 0.0
API_ResponseTime{apiModule="UserBankCard"} 0.0
API_ResponseTime{apiModule="UserBalance"} 0.0
API_ResponseTime{apiModule="UserAddress"} 0.0
[root@controller ~]# 

8.接下来就是在Prometheus中配置监控target和告警规则,我的集群里面使用Prometheus-Operator部署的Prometheus,所以我使用CRD来配置告警规则。

kubectl create secret generic prometheus-operator-prometheus-scrape-confg --from-file=additional-scrape-configs.yaml --dry-run -oyaml -nmonitoring > additional.yaml && kubectl apply -f additional.yaml
kubectl apply -f alertrule.yaml

9.在prometheus中查看监控target和告警规则已经生效

 10.Prometheus的告警会发送给Alertmanager,告警的媒介就自己配置了。 我配置了一个阿里云的电话告警,也是自己写的一个服务

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

使用Prometheus监控API 的相关文章

  • 如何添加更多数据存储在 jenkins Rest api 中

    为了使问题变得简单 我知道我可以通过以下方式获取一些构建信息https jenkins server https jenkins server api json xml python 我获得了有关该构建记录的大量信息 但是 我想向该构建记录
  • Postman如何发送请求? ajax,同源策略

    我发现了这个非常有用的 Chrome 扩展程序 名为 Postman 这是一个非常有用的扩展 特别是当您正在编写 RESTful 应用程序时 我感到困惑的一件事是这个插件 扩展如何能够在不同的域上成功发送 POST 请求 我尝试像这样使用
  • 获取 Youtube 上的游戏直播列表

    我正在尝试使用 Youtube 数据 API 来获取当前与游戏相关的直播流列表 但我找不到任何符合我需要的端点并返回每个频道的观看者数量 你们知道我该如何做到这一点吗 Thanks 游戏直播列表 videoCategoryId 20 是 游
  • 从 celery 工作人员到普罗米修斯的自定义指标

    我有一些 celery 工作人员在 kubernetes 下的容器中运行 它们不会由 celery 自动缩放 并且每个都在单个进程中运行 即没有多处理 我想从他们那里获取一堆不同的指标到普罗米修斯中 我研究过 celery promethe
  • GitHub v3 API - 如何在存储库中创建初始提交?

    我正在使用 v3 API 并设法列出存储库 树 分支 访问文件内容并创建 blob 树 提交 我现在正在尝试创建一个新的存储库 并设法使用 POST user repos 来完成它 但是当我尝试在这个新存储库中创建 blob trees c
  • 使用 SAS EG 通过代理从 API 下载 JSON 文件

    我正在尝试使用瑞士当局提供的 API 对公司网络内的地址进行地理编码 我的公司使用带有用户名和密码的代理服务器 我是 SAS EG 的新手 这是我迄今为止拥有的代码 我必须匿名一些内容才能被允许在此处发布 filename response
  • 如何按城市过滤 WikiVoyage API 结果?

    我目前正在尝试使用 wikivoyage API 我当前的 API 调用如下所示 en wikivoyage org w api php action query list search srwhat text srsearch Pari
  • 使用 Bloomberg .Net API 的每小时数据

    我正在努力解决使用 Net API 3 0 从 Bloomberg 获取每小时开盘价 最高价 最低价和最后价格快照的逻辑 我已经用谷歌搜索了很多次 但没有运气 对此的任何帮助将不胜感激 我试图在 Bloomberg Net API C 中找
  • 如何使用 Prometheus Alert Manager 在 Kubernetes 中触发警报

    我在集群中设置了 kube prometheus https github com coreos prometheus operator tree master contrib kube prometheus https github co
  • 获取 Prometheus 中两个自定义时间戳之间的增量

    我有一个名为的普罗米修斯指标device number 我想要的是显示现在与一天 一周 一个月等之前的价值差异 这意味着减去具有两个不同时间戳的两个值 环顾四周 我没有找到任何关于如何执行此操作的有用文档 我想做但不起作用的是 sum de
  • 如何解决错误:java.lang.ClassNotFoundException:io.netty.util.concurrent.GenericFutureListener?

    昨天我第一次尝试用 Java 制作 Prometheus 客户端 从 Python 开始 最后是 GoLang 是否找到示例 import io prometheus client Counter import io prometheus
  • 如何从网站中提取冠状病毒病例?

    我正在尝试从网站中提取冠状病毒 https www trackcorona live https www trackcorona live 但我得到了一个错误 这是我的代码 response requests get https www t
  • Web Api - 不允许捕获 405 方法

    截至目前 Web api 应用程序针对 405 方法不允许错误返回以下响应正文 我正在尝试更改响应正文 但我不知道如何使用委托处理程序 ApiControllerActionSelector 或过滤器 谁能帮我捕获服务器端的 405 错误
  • 使用 python 更新 Google 搜索方法

    我试图使用xgoogle https github com pkrumins xgoogle但我已经 3 年没有更新了 即使我设置每页 100 个结果 我也只能得到不超过 5 个结果 如果有人使用 xgoogle 没有任何问题 请告诉我 现
  • Yammer API 限制

    Yammer Rest api 文档表示 每个应用程序每个用户在 10 秒内最多可以发出 10 个请求 问题 什么是user这里 是为当前经过身份验证的 yammer 用户发出请求的 IP 地址还是承载令牌 如果我们所有的用户都使用相同的外
  • 我试图根据 Prometheus 黑盒导出器的成功响应来计算 Grafana 的正常运行时间

    我尝试计算probe success的数量 并将其乘以探测间隔 试图获得以秒为单位的正常运行时间 并将值类型设置为总数 问题是随着时间范围的变化 最小步骤发生变化 无法给我们正确的读数并使该选项无效 我们实际上想做的是根据仪表板设置的时间范
  • 如何在 Django Rest 框架中编写“删除”操作的测试

    我正在为 Django Rest Framework API 编写测试 我一直在测试 删除 我对 创建 的测试工作正常 这是我的测试代码 import json from django urls import reverse from re
  • 在Wordpress中通过API创建新用户时如何发送电子邮件密码?

    可以使用以下行通过 API 创建新用户 user id wp insert user user data 我想知道如何向新创建的用户发送包含其密码的电子邮件 Wordpress API 中是否有任何函数可以处理这项工作 还是我应该自己创建并
  • 如何使用 Google 帐户对我们网站中的用户进行身份验证

    如何在我们的网站中使用 Google 帐户对用户进行身份验证 我希望用户重定向到谷歌登录页面 然后将他重定向到我的网站 我想要这个 PHP 实现 你要OAuth http code google com apis accounts docs
  • 将 Apache Camel 执行器指标发送到 Prometheus

    我正在尝试转发 添加 Actuator Camel 指标 actuator camelroutes 将交换 交易数量等指标 发送到 Prometheus Actuator 端点 有没有办法让我配置 Camel 将这些指标添加到 Promet

随机推荐