当前位置: 首页 > news >正文

拨测API接口+监控方案

简介

  在业务运维场景中,需要对核心的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-monitor
    scrape_interval: 15s
    scrape_timeout: 10s
    metrics_path: /metrics
    scheme: http
    static_configs:
    - targets:
      - 10.128.120.52:8080 #该ip为容器运行节点IP地址
      labels:
        usage: "httpbin接口拨测" 

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

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

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

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

写在后面

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

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

 

相关文章:

  • 第一性原理详解
  • 信息化转型?数字化转型?企业到底该如何选择
  • ES6的Class继承--super关键字
  • Linux 简单命令 - find -grep命令深入学习
  • OP-TEE driver(五):libteec库中的接口在驱动中的实现
  • F-003 FPGA基础配置
  • java-php-python-ssm百分百教育集团教务管理系统设计计算机毕业设计
  • Apple M1 Macos 安装虚拟机软件UTM
  • 167.两数之和II-输入有序数组 || 双指针
  • aspnetcore6.0源代码编译调试
  • 【力扣刷题】Day04——链表专题
  • 云计算以及云计算安全相关的中文概述
  • 【 C++ 】哈希表底层结构剖析
  • Swift 基础语法 - 数据类型
  • js单行代码------对象
  • 「前端」从UglifyJSPlugin强制开启css压缩探究webpack插件运行机制
  • 【每日笔记】【Go学习笔记】2019-01-10 codis proxy处理流程
  • 0x05 Python数据分析,Anaconda八斩刀
  • C语言笔记(第一章:C语言编程)
  • ES6核心特性
  • Java读取Properties文件的六种方法
  • java小心机(3)| 浅析finalize()
  • Laravel Mix运行时关于es2015报错解决方案
  • Redis提升并发能力 | 从0开始构建SpringCloud微服务(2)
  • 从零开始在ubuntu上搭建node开发环境
  • 服务器之间,相同帐号,实现免密钥登录
  • 工作中总结前端开发流程--vue项目
  • 基于OpenResty的Lua Web框架lor0.0.2预览版发布
  • 理解在java “”i=i++;”所发生的事情
  • 如何编写一个可升级的智能合约
  • 手机端车牌号码键盘的vue组件
  • 最简单的无缝轮播
  • ​DB-Engines 11月数据库排名:PostgreSQL坐稳同期涨幅榜冠军宝座
  • # Python csv、xlsx、json、二进制(MP3) 文件读写基本使用
  • #NOIP 2014#day.2 T1 无限网络发射器选址
  • #NOIP 2014#Day.2 T3 解方程
  • (1) caustics\
  • (2)STL算法之元素计数
  • (C语言)strcpy与strcpy详解,与模拟实现
  • (DenseNet)Densely Connected Convolutional Networks--Gao Huang
  • (Matlab)使用竞争神经网络实现数据聚类
  • (八十八)VFL语言初步 - 实现布局
  • (备忘)Java Map 遍历
  • (翻译)Quartz官方教程——第一课:Quartz入门
  • (附源码)spring boot校园拼车微信小程序 毕业设计 091617
  • (附源码)计算机毕业设计SSM基于健身房管理系统
  • (六)c52学习之旅-独立按键
  • (论文阅读26/100)Weakly-supervised learning with convolutional neural networks
  • (一)SpringBoot3---尚硅谷总结
  • (转)jdk与jre的区别
  • (转载)hibernate缓存
  • ***原理与防范
  • .[backups@airmail.cc].faust勒索病毒的最新威胁:如何恢复您的数据?
  • .babyk勒索病毒解析:恶意更新如何威胁您的数据安全
  • .NET 4 并行(多核)“.NET研究”编程系列之二 从Task开始