SRE 排障利器,接口请求超时试试 httpstat

夜莺资深用户群有人推荐的一个工具,看了一下真挺好的,也推荐给大家。

需求场景

A 服务调用 B 服务的 HTTP 接口,发现 B 服务返回超时,不确定是网络的问题还是 B 服务的问题,需要排查。

工具简介

就类似 curl,httpstat 也可以请求某个后端,而且可以把各个阶段的耗时都展示出来,包括 DNS 解析、TCP 连接、TLS 握手、Server 处理并等待响应、完成最终传输等,非常直观。上图:

看着不错吧,咱们一起测试一下。这个工具是 go 写的,作者没有提供二进制包,所以需要自己编译。

安装 Go 环境

自己编辑就需要有 Go 环境,我这里给大家简单演示一下。我的电脑是 Mac,M1 芯片,首先下载 go 安装包(https://go.dev/dl/):https://go.dev/dl/go1.22.2.darwin-arm64.tar.gz。一般使用 tar.gz 的文件就好,不用 pkg。

shell 复制代码
cd /Users/ulric/works/tgz
wget https://go.dev/dl/go1.22.2.darwin-arm64.tar.gz
tar -zxf go1.22.2.darwin-arm64.tar.gz

操作如上,/Users/ulric/works/tgz/go 这个目录就是 go 的安装目录,然后配置环境变量:

shell 复制代码
export GOROOT=/Users/ulric/works/tgz/go
export GOPATH=/Users/ulric/works/gopath
export PATH=$PATH:$GOROOT/bin:$GOPATH/bin

GOROOT 是 go 的安装目录,GOPATH 是 go 的工作目录,PATH 是环境变量,这样配置之后,就可以使用 go 命令了。上面的几行命令可以保存在 ~/.bash_profile 或者 ~/.zshrc 里,这样每次打开终端都会自动加载。

验证 go 环境是否正常安装:

shell 复制代码
% go version
go version go1.22.2 darwin/arm64

安装 httpstat

有了 go 环境了,安装 httpstat 就很简单了:

shell 复制代码
ulric@ulric-flashcat ~ % go install github.com/davecheney/httpstat@latest
go: downloading github.com/davecheney/httpstat v1.1.0
go: downloading golang.org/x/sys v0.0.0-20201223074533-0d417f636930

测试 httpstat

安装完成之后,就可以使用了,我们看看 httpstat 有哪些参数可用:

shell 复制代码
ulric@ulric-flashcat ~ % httpstat --help
Usage: httpstat [OPTIONS] URL

OPTIONS:
  -4	resolve IPv4 addresses only
  -6	resolve IPv6 addresses only
  -E string
    	client cert file for tls config
  -H value
    	set HTTP header; repeatable: -H 'Accept: ...' -H 'Range: ...'
  -I	don't read body of request
  -L	follow 30x redirects
  -O	save body as remote filename
  -X string
    	HTTP method to use (default "GET")
  -d string
    	the body of a POST or PUT request; from file use @filename
  -k	allow insecure SSL connections
  -o string
    	output file for body
  -v	print version number

ENVIRONMENT:
  HTTP_PROXY    proxy for HTTP requests; complete URL or HOST[:PORT]
                used for HTTPS requests if HTTPS_PROXY undefined
  HTTPS_PROXY   proxy for HTTPS requests; complete URL or HOST[:PORT]
  NO_PROXY      comma-separated list of hosts to exclude from proxy

很多参数和 curl 都很像。比如我用 curl 测试一个请求:

shell 复制代码
ulric@ulric-flashcat ~ % curl -X POST -H "Content-Type: application/json" -d '{"service": "tomcat"}' 'https://httpbin.org/post?name=ulric&city=beijing'
{
  "args": {
    "city": "beijing",
    "name": "ulric"
  },
  "data": "{\"service\": \"tomcat\"}",
  "files": {},
  "form": {},
  "headers": {
    "Accept": "*/*",
    "Content-Length": "21",
    "Content-Type": "application/json",
    "Host": "httpbin.org",
    "User-Agent": "curl/8.4.0",
    "X-Amzn-Trace-Id": "Root=1-6655a6c4-4522374c5b8d68143d638049"
  },
  "json": {
    "service": "tomcat"
  },
  "origin": "123.113.255.104",
  "url": "https://httpbin.org/post?name=ulric&city=beijing"
}

把 curl 换成 httpstat,请求效果如下:

shell 复制代码
ulric@ulric-flashcat ~ % httpstat -X POST -H "Content-Type: application/json" -d '{"service": "tomcat"}' 'https://httpbin.org/post?name=ulric&city=beijing'

Connected to 34.198.16.126:443

HTTP/2.0 200 OK
Server: gunicorn/19.9.0
Access-Control-Allow-Credentials: true
Access-Control-Allow-Origin: *
Content-Length: 529
Content-Type: application/json
Date: Tue, 28 May 2024 09:41:44 GMT

Body discarded

  DNS Lookup   TCP Connection   TLS Handshake   Server Processing   Content Transfer
[     11ms  |         217ms  |        446ms  |            570ms  |             0ms  ]
            |                |               |                   |                  |
   namelookup:11ms           |               |                   |                  |
                       connect:229ms         |                   |                  |
                                   pretransfer:678ms             |                  |
                                                     starttransfer:1248ms           |
                                                                                total:1248ms

可以看到,httpstat 把请求的各个阶段的耗时都展示出来了,非常直观。

本文作者:秦晓辉,flashcat.cloud 联合创始人,开源监控产品 Open-Falcon、Nightingale 创始人,极客时间《运维监控系统实战笔记》作者

相关推荐
XIAOHEZIcode1 天前
Linux系统鼠标偏移常见原因以及修复方案
linux·运维·游戏
用户0328472220702 天前
如何搭建本地yum源(上)
运维
大树885 天前
金刚石散热越强,管路越先见顶
大数据·运维·服务器·人工智能·ai
摇滚侠5 天前
Linux CentOS7 rpm 安装 MySQL 5.7
linux·运维·mysql
霸道流氓气质5 天前
领域驱动设计(DDD)在 Spring Boot 微服务中的实践指南
运维·spring boot·微服务
Inhand陈工5 天前
基于台达PLC与映翰通IG502的智慧水产养殖精准投喂与远程运维解决方案
运维·人工智能·物联网·阿里云·信息与通信
酣大智5 天前
ARP代理--工作原理
运维·网络·arp·arp代理
shushangyun_5 天前
2026年快消品B2B系统推荐:支持终端门店订货、促销政策自动化的工具?
java·运维·网络·数据库·人工智能·spring·自动化
施努卡机器视觉5 天前
SNK施努卡侧滑门锁上滑轮总成自动化装配线,从零件到组件,全流程精密制造方案
运维·自动化·制造
AC赳赳老秦5 天前
用 OpenClaw 搭建服务器故障应急响应系统,自动处理 80% 常见运维故障
android·运维·服务器·python·rxjava·deepseek·openclaw