|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +# Released under the MIT License. |
| 4 | +# Copyright, 2025, by Samuel Williams. |
| 5 | + |
| 6 | +require "metrics/provider/async/http/server" |
| 7 | +require "async/http/server" |
| 8 | +require "async/http/endpoint" |
| 9 | +require "async/http/client" |
| 10 | +require "protocol/http/middleware" |
| 11 | +require "sus/fixtures/async" |
| 12 | + |
| 13 | +describe Async::HTTP::Server do |
| 14 | + include Sus::Fixtures::Async::ReactorContext |
| 15 | + |
| 16 | + let(:endpoint) {Async::HTTP::Endpoint.parse("http://localhost:0")} |
| 17 | + |
| 18 | + with "metrics provider" do |
| 19 | + let(:app) do |
| 20 | + Protocol::HTTP::Middleware.for do |request| |
| 21 | + Protocol::HTTP::Response[200, {}, ["Hello, World!"]] |
| 22 | + end |
| 23 | + end |
| 24 | + |
| 25 | + let(:server) {subject.new(app, endpoint)} |
| 26 | + |
| 27 | + it "emits queue time metric when x-request-start header is present" do |
| 28 | + # Start the server |
| 29 | + server_task = server.run |
| 30 | + bound_endpoint = server_task.wait_until_ready |
| 31 | + |
| 32 | + # Calculate a timestamp 100ms in the past (nginx format with 't=' prefix) |
| 33 | + request_start = Process.clock_gettime(Process::CLOCK_REALTIME) - 0.1 |
| 34 | + |
| 35 | + # Expect the histogram metric to be emitted with a value around 0.1 seconds |
| 36 | + expect(Async::HTTP::Server::ASYNC_HTTP_SERVER_REQUEST_QUEUE_TIME).to receive(:emit) do |value, tags:| |
| 37 | + expect(value).to be_within(0.05).of(0.1) |
| 38 | + expect(tags).to be == ["method:GET"] |
| 39 | + end |
| 40 | + |
| 41 | + # Make a request with the x-request-start header |
| 42 | + client = Async::HTTP::Client.new(bound_endpoint) |
| 43 | + headers = [["x-request-start", "t=#{request_start}"]] |
| 44 | + response = client.get("/", headers) |
| 45 | + |
| 46 | + expect(response.status).to be == 200 |
| 47 | + response.finish |
| 48 | + |
| 49 | + client.close |
| 50 | + server_task.stop |
| 51 | + end |
| 52 | + |
| 53 | + it "handles nginx-style timestamp format (t=prefix)" do |
| 54 | + server_task = server.run |
| 55 | + bound_endpoint = server_task.wait_until_ready |
| 56 | + |
| 57 | + request_start = Process.clock_gettime(Process::CLOCK_REALTIME) - 0.05 |
| 58 | + |
| 59 | + expect(Async::HTTP::Server::ASYNC_HTTP_SERVER_REQUEST_QUEUE_TIME).to receive(:emit) do |value, tags:| |
| 60 | + expect(value).to be > 0 |
| 61 | + expect(value).to be < 1 |
| 62 | + end |
| 63 | + |
| 64 | + client = Async::HTTP::Client.new(bound_endpoint) |
| 65 | + headers = [["x-request-start", "t=#{request_start}"]] |
| 66 | + response = client.get("/", headers) |
| 67 | + |
| 68 | + expect(response.status).to be == 200 |
| 69 | + response.finish |
| 70 | + |
| 71 | + client.close |
| 72 | + server_task.stop |
| 73 | + end |
| 74 | + |
| 75 | + it "handles plain Unix timestamp format" do |
| 76 | + server_task = server.run |
| 77 | + bound_endpoint = server_task.wait_until_ready |
| 78 | + |
| 79 | + request_start = Process.clock_gettime(Process::CLOCK_REALTIME) - 0.05 |
| 80 | + |
| 81 | + expect(Async::HTTP::Server::ASYNC_HTTP_SERVER_REQUEST_QUEUE_TIME).to receive(:emit) do |value, tags:| |
| 82 | + expect(value).to be > 0 |
| 83 | + expect(value).to be < 1 |
| 84 | + end |
| 85 | + |
| 86 | + client = Async::HTTP::Client.new(bound_endpoint) |
| 87 | + headers = [["x-request-start", request_start.to_s]] |
| 88 | + response = client.get("/", headers) |
| 89 | + |
| 90 | + expect(response.status).to be == 200 |
| 91 | + response.finish |
| 92 | + |
| 93 | + client.close |
| 94 | + server_task.stop |
| 95 | + end |
| 96 | + |
| 97 | + it "handles millisecond timestamp format" do |
| 98 | + server_task = server.run |
| 99 | + bound_endpoint = server_task.wait_until_ready |
| 100 | + |
| 101 | + request_start_ms = (Process.clock_gettime(Process::CLOCK_REALTIME) * 1000).to_i - 50 |
| 102 | + |
| 103 | + expect(Async::HTTP::Server::ASYNC_HTTP_SERVER_REQUEST_QUEUE_TIME).to receive(:emit) do |value, tags:| |
| 104 | + expect(value).to be > 0 |
| 105 | + expect(value).to be < 1 |
| 106 | + end |
| 107 | + |
| 108 | + client = Async::HTTP::Client.new(bound_endpoint) |
| 109 | + headers = [["x-request-start", request_start_ms.to_s]] |
| 110 | + response = client.get("/", headers) |
| 111 | + |
| 112 | + expect(response.status).to be == 200 |
| 113 | + response.finish |
| 114 | + |
| 115 | + client.close |
| 116 | + server_task.stop |
| 117 | + end |
| 118 | + |
| 119 | + it "does not emit queue time metric when x-request-start header is missing" do |
| 120 | + server_task = server.run |
| 121 | + bound_endpoint = server_task.wait_until_ready |
| 122 | + |
| 123 | + # Should not emit the queue time metric |
| 124 | + expect(Async::HTTP::Server::ASYNC_HTTP_SERVER_REQUEST_QUEUE_TIME).not.to receive(:emit) |
| 125 | + |
| 126 | + client = Async::HTTP::Client.new(bound_endpoint) |
| 127 | + response = client.get("/") |
| 128 | + |
| 129 | + expect(response.status).to be == 200 |
| 130 | + response.finish |
| 131 | + |
| 132 | + client.close |
| 133 | + server_task.stop |
| 134 | + end |
| 135 | + |
| 136 | + it "ignores invalid timestamp formats" do |
| 137 | + server_task = server.run |
| 138 | + bound_endpoint = server_task.wait_until_ready |
| 139 | + |
| 140 | + # Should not emit the queue time metric for invalid timestamp |
| 141 | + expect(Async::HTTP::Server::ASYNC_HTTP_SERVER_REQUEST_QUEUE_TIME).not.to receive(:emit) |
| 142 | + |
| 143 | + client = Async::HTTP::Client.new(bound_endpoint) |
| 144 | + headers = [["x-request-start", "invalid-timestamp"]] |
| 145 | + response = client.get("/", headers) |
| 146 | + |
| 147 | + expect(response.status).to be == 200 |
| 148 | + response.finish |
| 149 | + |
| 150 | + client.close |
| 151 | + server_task.stop |
| 152 | + end |
| 153 | + |
| 154 | + it "ignores unreasonable queue times" do |
| 155 | + server_task = server.run |
| 156 | + bound_endpoint = server_task.wait_until_ready |
| 157 | + |
| 158 | + # Timestamp from 2 hours ago (unreasonable) |
| 159 | + request_start = Process.clock_gettime(Process::CLOCK_REALTIME) - 7200 |
| 160 | + |
| 161 | + # Should not emit the queue time metric for unreasonable timestamp |
| 162 | + expect(Async::HTTP::Server::ASYNC_HTTP_SERVER_REQUEST_QUEUE_TIME).not.to receive(:emit) |
| 163 | + |
| 164 | + client = Async::HTTP::Client.new(bound_endpoint) |
| 165 | + headers = [["x-request-start", "t=#{request_start}"]] |
| 166 | + response = client.get("/", headers) |
| 167 | + |
| 168 | + expect(response.status).to be == 200 |
| 169 | + response.finish |
| 170 | + |
| 171 | + client.close |
| 172 | + server_task.stop |
| 173 | + end |
| 174 | + end |
| 175 | +end |
| 176 | + |
0 commit comments