简介

在业务运维场景中,需要对核心的API接口进行拨测。而各个接口需要传递的参数或者接口之间的依赖是比较复杂的,通常接口之间都是通过链式请求来完成一个业务场景。常见的就是先登录,拿到token以后,再进行后续的API请求。postman提供了基于GUI的方式完成这种场景适配,但是对于运维来讲,需要定时的基于策略的形式来对API进行监控。本篇文章就带你从0-1打造API监控体系。

知识储备

1. Postman使用方法

2. Docker基础知识

部署步骤

1. 从postman导出collection

以下文件以拨测httpbin.org为例,在Postman的GUI工具中导出拨测的json文件(httpbin.json)。示例中包含两个接口,一个模拟认证,一个模拟接口请求

2. 将导出的文件放到docker中运行

docker run -d -p 8080:8080 -v ./httpbin.json:/runner/collection.json kevinniu666/postman-prometheus:1.0.0

3. 获取拨测指标

curl 10.128.120.52:8080/metrics
# TYPE postman_lifetime_runs_total counter
postman_lifetime_runs_total{collection="httpbin"} 1# TYPE postman_lifetime_iterations_total counter
postman_lifetime_iterations_total{collection="httpbin"} 1# TYPE postman_lifetime_requests_total counter
postman_lifetime_requests_total{collection="httpbin"} 2# TYPE postman_stats_iterations_total gauge
postman_stats_iterations_total{collection="httpbin"} 1# TYPE postman_stats_iterations_failed gauge
postman_stats_iterations_failed{collection="httpbin"} 0# TYPE postman_stats_requests_total gauge
postman_stats_requests_total{collection="httpbin"} 2# TYPE postman_stats_requests_failed gauge
postman_stats_requests_failed{collection="httpbin"} 0# TYPE postman_stats_tests_total gauge
postman_stats_tests_total{collection="httpbin"} 2# TYPE postman_stats_tests_failed gauge
postman_stats_tests_failed{collection="httpbin"} 0# TYPE postman_stats_test_scripts_total gauge
postman_stats_test_scripts_total{collection="httpbin"} 4# TYPE postman_stats_test_scripts_failed gauge
postman_stats_test_scripts_failed{collection="httpbin"} 0# TYPE postman_stats_assertions_total gauge
postman_stats_assertions_total{collection="httpbin"} 3# TYPE postman_stats_assertions_failed gauge
postman_stats_assertions_failed{collection="httpbin"} 0# TYPE postman_stats_transfered_bytes_total gauge
postman_stats_transfered_bytes_total{collection="httpbin"} 794# TYPE postman_stats_resp_avg gauge
postman_stats_resp_avg{collection="httpbin"} 541# TYPE postman_stats_resp_min gauge
postman_stats_resp_min{collection="httpbin"} 494# TYPE postman_stats_resp_max gauge
postman_stats_resp_max{collection="httpbin"} 588# TYPE postman_request_status_code gauge
postman_request_status_code{request_name="authentication",iteration="0",collection="httpbin"} 200# TYPE postman_request_resp_time gauge
postman_request_resp_time{request_name="authentication",iteration="0",collection="httpbin"} 588# TYPE postman_request_resp_size gauge
postman_request_resp_size{request_name="authentication",iteration="0",collection="httpbin"} 54# TYPE postman_request_status_ok gauge
postman_request_status_ok{request_name="authentication",iteration="0",collection="httpbin"} 1# TYPE postman_request_failed_assertions gauge
postman_request_failed_assertions{request_name="authentication",iteration="0",collection="httpbin"} 0# TYPE postman_request_total_assertions gauge
postman_request_total_assertions{request_name="authentication",iteration="0",collection="httpbin"} 1# TYPE postman_request_status_code gauge
postman_request_status_code{request_name="business-request",iteration="0",collection="httpbin"} 200# TYPE postman_request_resp_time gauge
postman_request_resp_time{request_name="business-request",iteration="0",collection="httpbin"} 494# TYPE postman_request_resp_size gauge
postman_request_resp_size{request_name="business-request",iteration="0",collection="httpbin"} 740# TYPE postman_request_status_ok gauge
postman_request_status_ok{request_name="business-request",iteration="0",collection="httpbin"} 1# TYPE postman_request_failed_assertions gauge
postman_request_failed_assertions{request_name="business-request",iteration="0",collection="httpbin"} 0# TYPE postman_request_total_assertions gauge
postman_request_total_assertions{request_name="business-request",iteration="0",collection="httpbin"} 2

4. 将指标接入prometheus中,prometheus的配置文件中添加以下信息。

  - job_name: http-api-monitorscrape_interval: 15sscrape_timeout: 10smetrics_path: /metricsscheme: httpstatic_configs:- targets:- 10.128.120.52:8080 #该ip为容器运行节点IP地址labels:usage: "httpbin接口拨测" 

5. 配置prometheus告警规则,详细的规则,大家可以根据prometheus的指标自己来设置。

  - alert: 接口返回码异常expr: postman_request_status_code  != 200 for: 1mlabels:severity: errorannotations:summary: "接口响应代码非200"description: "后端接口拨测失败"- alert: 接口返回内容判定失败expr: postman_request_failed_assertions  != 0for: 1mlabels:severity: errorannotations:summary: "接口返回内容判定失败"description: "后端接口postman测试未通过"  

接下来的事情就是对接Alertmanager,并把告警发送给运维了。如果需要Grafana的图表,项目中有说明。源代码地址:

GitHub - kevinniu666/postman-prometheus: Run Postman collections continuously and export results as Prometheus metrics

写在后面

整个实现方案的核心是这个容器,它将postman的运行转化成为了prometheus可识别的指标。这个容器的源代码已经在文档中提供了。如果你对nodejs有了解,可以自己去修改源码。该容器可以通过环境变量控制一些行为,列举如下:

放一张产线的业务拨测图表:

拨测API接口+监控方案相关推荐

  1. 推荐一个api接口监控神器

      大家好,我是为广大程序员兄弟操碎了心的小编,每天推荐一个小工具/源码,装满你的收藏夹,每天分享一个小技巧,让你轻松节省开发效率,实现不加班不熬夜不掉头发,是我的目标!   今天小编推荐一个api接 ...

  2. 淘口令解析api接口/淘口令解密还原api接口/淘口令短链接搜索商品详情接口,API接口获取方案

    一.淘宝/天猫/1688 平台item_password - 获得淘口令真实url接口返回说明 淘宝淘口令短链接地址例子:urlencode(fu置本段内容₤qQkYcb86z2d₤咑幵τao寶或点几 ...

  3. item_search_coupon - 京东优惠券查询接口,京东优惠券查询API接口接入方案

    一.item_search_coupon - 京东优惠券查询接口,京东优惠券查询API接口接入说明: 点击注册获取key和secret测试账号 接口说明:可先注册一个key和secret账号通过商品I ...

  4. 淘宝卖家店铺订单API接口同步方案

    获取淘宝卖家店铺订单背景: 订单是卖家的核心数据,卖家的很多日常工作都是围绕着订单展开,应用的基本功能就是要保证订单实时.完整的展示在卖家面前.由于API请求依赖于网络,存在 着网络不稳定和同步时间长 ...

  5. 通过商品ID和区域ID及cookie插件获取商品快递费用接口,1688快递物流费用接口,1688商品物流费用API接口获取方案

    一.接口参数说明: 1.通过商品ID及区域ID并且安装cookie插件得到cookie值,拿到商品物流费用,宝贝ID,发货地,区域ID,目的地, 数量, 快递费用,快递,EMS费用,物流费用,价格,尺 ...

  6. item_get-获得当当网商品详情,当当网获得商品详情 API接口接入方案

    一.item_get-获得dangdang商品详情接口接入说明: 点击注册获取key和secret测试账号 接口说明:通过商品ID获取商品详情页面的数据,包括标题,价格,库存,视频,图片等信息. 二. ...

  7. 京东优惠券查询API接口接入方案,item_search_coupon - 京东优惠券查询接口

    item_search_coupon - 京东优惠券查询接口说明: Key注册地址:获取Key和secret便进入到查询接口 得出数据: Request address: https://api-gw ...

  8. http拨测是什么意思_快速构建拨测系统

    背景 当前项目需要一个拨测系统来检测服务是否正常运行,拨测系统需要满足以下需求: 支持对接口请求结果做判断. 支持对接口的耗时做判断. 支持重试:可能在某一瞬间网络出现了延迟,导致接口请求失败,所以需 ...

  9. 使用腾讯云轻量应用服务器搭建网络质量拨测工具 SmokePing

    本文原载于 https://www.idc.moe/archives/qcloud-Lighthouse-SmokePing.html 作者:iks SmokePing 是由 RRDtool 的作者 ...

最新文章

  1. 使用Java Servlet,JSP标签和Stormpath快速构建Java Web App
  2. 关于java子类继承以及final问题总结
  3. MATLAB中文显示乱码的问题如何解决
  4. elementui组件_elementui 中 loading 组件源码解析(续)
  5. Mac Big Sur如何更改锁屏时间
  6. 蓝桥2021模拟真题 (跳跃 )DFS
  7. log4net配置mysql_使用独立的log4net.config文件配置log4net,将日志记录到Mysql数据库【原创】...
  8. qpsk相点 matlab,qpsk调制解调matlab仿真程序详解
  9. Win7下用的XP虚拟机
  10. php 获取客户端真实ip_php获取客户端真实ip地址的三种方法
  11. 在哪儿比较好下载建筑学西方近现代的外文文献?
  12. IOS移动应用跳转微信小程序
  13. JS的数据类型分类以及用法,没有这些基础,学不好js
  14. 水星路器服务器无响应,联想M720Q、光网卡、Pon stick、Openwrt我的完美软路由折腾记...
  15. Java删除表格的基本代码,Java 删除 Excel 中的空白行和列
  16. 总说业务,到底业务常用的指标有哪些
  17. 立足小餐饮,“新名酒”江小白能走多远?
  18. Python学习 | pymysql操作数据库?真原生...
  19. android投屏小米电视软件,小米投屏神器安卓版
  20. 计算机跨考英语口译,英语专业跨考

热门文章

  1. css实现文字展开收起
  2. 微机原理知识点总结(第一章-微型计算机系统)
  3. 在线教育的三大主流变现模式
  4. 相关性分析 协方差 相关系数
  5. arm linux之data abort异常处理
  6. php编网页版计算器,php 简单网页计算器的完整代码
  7. java西游记释厄传super,西游记释厄传super出招表
  8. 51Nod 1366 贫富差距
  9. 网络出现502 bad gateway解决方案
  10. Dubbo学习记录(八) -- Spring整合Dubbo中@Reference注解解析原理