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 创始人,极客时间《运维监控系统实战笔记》作者

相关推荐
曼岛_9 小时前
[架构之美]linux常见故障问题解决方案(十九)
linux·运维·架构
大蚂蚁2号10 小时前
windows文件共享另一台电脑资源管理器网络文件夹无法找到机器
运维·服务器·网络
Lw老王要学习10 小时前
Linux数据库篇、第一章_02_MySQL的使用增删改查
linux·运维·数据库·mysql·云计算·it
斤斤计较11 小时前
Docker 环境安装(2025最新版)
运维·docker·容器
小锋学长生活大爆炸11 小时前
【教程】Docker方式本地部署Overleaf
运维·docker·容器
掘金者说11 小时前
docker系列-DockerDesktop报错信息(Windows Hypervisor is not present)
运维·docker·容器
2302_7995257412 小时前
【Linux】第十六章 分析和存储日志
linux·运维·服务器
愚润求学12 小时前
【Linux】Ext系列文件系统
linux·运维·服务器·笔记
微刻时光12 小时前
影刀RPA网页自动化总结
运维·人工智能·python·低代码·自动化·rpa·影刀rpa
2301_7875528712 小时前
Lightpanda开源浏览器:专为 AI 和自动化而设计的无界面浏览器
运维·自动化