最新资讯
dev.to 你的 Agent Harness 应具备的 17 项能力 dev.to 离线 AI 记忆中 0.8 余弦阈值为何失效 dev.to Kimi K3.1 Pro 开放权重:自托管 2.8T 模型前的开发者清单 dev.to 如何通过工具调用引导模型以提升 Harness 质量 dev.to AI Agent 审计轨迹不等于对话记录 dev.to 精简 MCP server 工具数量:更少工具、更小影响面,双重收益 dev.to 为什么 git checkout 无法撤销 AI 编码 agent 的改动? wechat AI短剧门槛降降降!实测即梦短剧工作室:剧本分镜预演一条龙,伙伴计划长期护航 dev.to 让模型改写文档前,先量化事实保真度 dev.to 回放 153 个 Claude Code 与 Codex 会话,审计 Agent 的生产影响范围 dev.to 银行 77 意图分类最便宜 LLM:GPT-6.1 Terra 每次成功任务 $0.000727 theinformation.com Chesky的副业:Airbnb向CEO新设AI实验室出售IP
多元动态
登录 注册
开源工具 dev.to · @Ritom Puzari

不改一行代码,看清服务器发出的每个 API 调用与收到的每个请求

原文 我的收藏

介绍一种基于网络接口旁路观测的 APM 替代方案:在 Linux 上用 socket(AF_PACKET, SOCK_RAW, htons(ETH_P_ALL)) 打开原始套接字,并通过 setsockopt(SO_ATTACH_FILTER) 挂载 tcpdump -dd tcp 生成的九条经典 BPF 指令,在内核中只放行 IPv4/IPv6 的 TCP 帧,避免 UDP、ARP、ICMP 进入用户态。随后按源地址、源端口、目的地址、目的端口四元组配对请求与响应,无需完整流重组即可从段首字节提取信息:明文 HTTP/1.x 可得到方法、路径(去查询串)、Host、状态码、双向字节数和首字节延迟,用于端点计数、4xx/5xx 比例与 p50/p95/max 延迟;TLS 虽看不到载荷,但 ClientHello 的 SNI 扩展在 TLS 1.2/1.3 中明文可见,可构建出站依赖图并测量 ClientHello 到 ServerHello 的往返时间,ECH 会隐藏 SNI 但目前 API 端点采用较少;反代到本地 127.0.0.1:8000 的明文流量可获完整请求级细节。局限是 HTTP/2、gRPC 和 h2c 因多路复用或二进制帧无法解析,需改用 nginx/Apache/Caddy 访问日志或进程内 SDK,跨多个 TCP 段的请求也只能部分还原。

AI推荐理由 文章给出一种不引入厂商 SDK、不改代码的 APM 替代思路:用 AF_PACKET 原始套接字加经典 BPF 过滤(tcpdump -dd tcp 的九条指令)在内核侧只放行 TCP 帧,再按四元组配对请求与响应,从 HTTP/1.x 请求行/状态行和 TLS ClientHello 的 SNI 提取端点、状态码、字节数与延迟。具体新信息是明确列出可观测范围(明文 HTTP/1.x、TLS 的 SNI 与 RTT、127.0.0.1 本地明文流量)和局限(HTTP/2、gRPC、h2c 因多路复用或二进制帧无法解析,需依赖反代访问日志或进程内 SDK;跨段请求只做部分处理)。属于开发者可复用的工程方案,但为个人博客、无实测数据与基准,故未给更高分。

72

正文

Instrumenting an application for APM means picking a vendor SDK, adding it to every service, redeploying, and hoping the framework version is supported. There is an older trick that gets you most of the value with none of that: listen to the network interface. What the host can see A raw socket opened with socket(AF_PACKET, SOCK_RAW, htons(ETH_P_ALL)) on Linux receives a copy of every frame that crosses any interface, the same tap tcpdump uses. On its own that is expensive on a busy box, so the first thing to do is attach a classic BPF program with setsockopt(SO_ATTACH_FILTER). The nine-instruction program that tcpdump -dd tcp prints accepts IPv4 and IPv6 frames whose next header is TCP and rejects everything else inside the kernel, before a single byte is copied to user space. The agent uses exactly that program, so UDP, ARP and ICMP never reach Python. What remains is a stream of TCP segments. You do not need full stream reassembly to get useful data, because the interesting bytes sit at the start of a segment: an HTTP/1.x request line begins a client segment, a status line begins a server segment, and a TLS ClientHello is the first record on a fresh connection. Keying by the 4-tuple (source address, source port, destination address, destination port) and pairing a request with the next response on the reversed tuple gives you a latency sample per request. From that you can reconstruct: Plain HTTP/1.x: method, path (query string stripped), Host, status code, bytes each way, and the time from the request segment to the first byte of the response. That is enough for per-endpoint counts, 4xx/5xx rates and p50/p95/max latency. Absolute-form request targets, which proxies use, are normalised to a path. TLS: not the payload. The ClientHello (handshake type 1 inside a record of content type 22) carries the Server Name Indication extension (type 0) in clear text on TLS 1.2 and 1.3, so you still learn which host the server is talking to, on which port, how many connections, how many bytes each way, and the round-trip time from ClientHello to ServerHello. That is your outgoing dependency map: Stripe, your database host, an S3 endpoint, a third-party API nobody knew about. Encrypted ClientHello (ECH) hides SNI, but adoption on API endpoints is still rare. Local services: traffic between your reverse proxy and your app on 127.0.0.1:8000 is usually plain HTTP, so you get full request-level detail for your own application even when the public side is TLS. What it cannot see HTTP/2 multiplexes streams inside one connection and is almost always inside TLS; without keys it shows up as an opaque flow with byte counts. gRPC likewise. Cleartext HTTP/2 (h2c) is binary-framed, so the request-line parser skips it too. For those you want either the reverse proxy's access log (nginx, Apache and Caddy formats, which the same agent tails from the standard paths) or an in-process SDK. Two more limits worth knowing: a request that spans several segments is only parsed if the request line and Host header are in the first 2 KB of the first segment, and keep-alive connections that pipeline requests are attributed by order, which is correct for HTTP/1.1 because responses must arrive in request order. From packets to something useful Raw requests are noise. The useful transformation is: Normalise paths so cardinality stays bounded: /users/1234/orders/550e8400-e29b-41d4-a716-446655440000 becomes /users/{id}/orders/{uuid}. Purely numeric segments become {id}, UUIDs {uuid}, hex strings of 16 or more characters {hash}, long base64-looking segments {token}. Everything else is kept verbatim so /v1/users and /v2/users stay distinct. Aggregate per reporting interval: count, 4xx and 5xx counts, and p50/p95/max latency per (direction, host, method, path). Percentiles are computed from the raw samples in that interval, not from averages of averages. Keep a small sample of the slowest and failing requests with their timing, so an incident shows evidence, not just a number. Roll up hourly for history, and build the service map from the outgoing side: this server talks to these hosts, this often, this slowly. The reverse-DNS name of each destination is resolved once and cached, so the map shows api.stripe.com, not an IP. Doing it with the Vigil agent The agent is a single Python file that uses only the standard library, so it runs on any Linux with Python 3.8 or newer. Installed as root it enables capture automatically; without root it still reports metrics, processes and connections from /proc/net/tcp, but not per-request detail: curl -fsSL https://puzaricloud.in/install-server-agent.sh | sudo sh -s -- --token vgs_YOUR_TOKEN Enter fullscreen mode Exit fullscreen mode The server's APIs tab then shows incoming endpoints with p95 and 5xx rates, the Network tab shows outgoing hosts with connections and bytes, and the Services tab draws the map. Rules on http_error_pct, http_p95_ms and new_destination open incidents, so a new outbound host appearing at 3 am pages you, which is one of the cheaper ways to notice a compromised box. For request traces across services, drop in the Python or Node SDK; it reports to the same local agent on 127.0.0.1:9111 and links spans to the captured endpoints. Privacy and safety notes The parser reads the request line and the Host header, and the status line of the response. It never parses other headers or any body, so Authorization, Cookie, query strings and payloads are not in memory beyond the kernel buffer and never leave the machine. Only aggregates and a handful of slow-request samples (method, path, status, timing) are reported. If your compliance rules forbid even that, turn capture off per server and rely on access logs and SDK spans instead. Originally published on the PuzariCloud engineering blog. Drafted with AI assistance and reviewed, edited and tested by the author, who builds Vigil by PuzariCloud, the monitoring service the examples use.

来源报道 · 1 篇 · 以下链接均前往原始媒体

← 返回精选动态 原文

更多内容