Skip to content

Commit 10e448c

Browse files
Add connection tracing.
1 parent db462ee commit 10e448c

File tree

16 files changed

+209
-16
lines changed

16 files changed

+209
-16
lines changed

config/sus.rb

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
# frozen_string_literal: true
22

33
# Released under the MIT License.
4-
# Copyright, 2023-2024, by Samuel Williams.
4+
# Copyright, 2023-2025, by Samuel Williams.
5+
6+
ENV["TRACES_BACKEND"] ||= "traces/backend/test"
7+
require "traces"
58

69
require "covered/sus"
710
include Covered::Sus

config/traces.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# frozen_string_literal: true
2+
3+
# Released under the MIT License.
4+
# Copyright, 2025, by Samuel Williams.
5+
6+
def prepare
7+
require "traces/provider/protocol/http1/connection"
8+
end

examples/http1/client.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
# frozen_string_literal: true
33

44
# Released under the MIT License.
5-
# Copyright, 2019-2024, by Samuel Williams.
5+
# Copyright, 2019-2025, by Samuel Williams.
66

77
$LOAD_PATH.unshift File.expand_path("../../../lib", __dir__)
88

fuzz/request/script.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
# frozen_string_literal: true
33

44
# Released under the MIT License.
5-
# Copyright, 2020-2024, by Samuel Williams.
5+
# Copyright, 2020-2025, by Samuel Williams.
66

77
require "socket"
88
require_relative "../../lib/protocol/http1"

gems.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# frozen_string_literal: true
22

33
# Released under the MIT License.
4-
# Copyright, 2019-2024, by Samuel Williams.
4+
# Copyright, 2019-2025, by Samuel Williams.
55

66
source "https://rubygems.org"
77

lib/protocol/http1/body/chunked.rb

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,16 @@ def read
116116

117117
# @returns [String] a human-readable representation of the body.
118118
def inspect
119-
"\#<#{self.class} #{@length} bytes read in #{@count} chunks>"
119+
"\#<#{self.class} #{@length} bytes read in #{@count} chunks, #{@finished ? 'finished' : 'reading'}>"
120+
end
121+
122+
# @returns [Hash] JSON representation for tracing and debugging.
123+
def as_json(...)
124+
super.merge(
125+
count: @count,
126+
finished: @finished,
127+
state: @connection ? "open" : "closed"
128+
)
120129
end
121130

122131
private

lib/protocol/http1/body/fixed.rb

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,15 @@ def read
7474

7575
# @returns [String] a human-readable representation of the body.
7676
def inspect
77-
"\#<#{self.class} length=#{@length} remaining=#{@remaining} state=#{@connection ? 'open' : 'closed'}>"
77+
"#<#{self.class} #{@length} bytes, #{@remaining} remaining, #{empty? ? 'finished' : 'reading'}>"
78+
end
79+
80+
# @returns [Hash] JSON representation for tracing and debugging.
81+
def as_json(...)
82+
super.merge(
83+
remaining: @remaining,
84+
state: @connection ? "open" : "closed"
85+
)
7886
end
7987
end
8088
end

lib/protocol/http1/body/remainder.rb

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,15 @@ def read
6060

6161
# @returns [String] a human-readable representation of the body.
6262
def inspect
63-
"\#<#{self.class} state=#{@connection ? 'open' : 'closed'}>"
63+
"#<#{self.class} #{@block_size} byte blocks, #{empty? ? 'finished' : 'reading'}>"
64+
end
65+
66+
# @returns [Hash] JSON representation for tracing and debugging.
67+
def as_json(...)
68+
super.merge(
69+
block_size: @block_size,
70+
state: @connection ? "open" : "closed"
71+
)
6472
end
6573
end
6674
end

lib/protocol/http1/connection.rb

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -274,7 +274,9 @@ def write_request(authority, method, target, version, headers)
274274
# @parameter status [Integer] the HTTP status code.
275275
# @parameter headers [Hash] the HTTP headers.
276276
# @parameter reason [String] the reason phrase, defaults to the standard reason phrase for the status code.
277-
def write_response(version, status, headers, reason = Reason::DESCRIPTIONS[status])
277+
def write_response(version, status, headers, reason = nil)
278+
reason ||= Reason::DESCRIPTIONS[status]
279+
278280
unless @state == :open or @state == :half_closed_remote
279281
raise ProtocolError, "Cannot write response in state: #{@state}!"
280282
end
@@ -292,7 +294,9 @@ def write_response(version, status, headers, reason = Reason::DESCRIPTIONS[statu
292294
# @parameter headers [Hash] the HTTP headers.
293295
# @parameter reason [String] the reason phrase, defaults to the standard reason phrase for the status code.
294296
# @raises [ProtocolError] if the connection is not in the open or half-closed remote state.
295-
def write_interim_response(version, status, headers, reason = Reason::DESCRIPTIONS[status])
297+
def write_interim_response(version, status, headers, reason = nil)
298+
reason ||= Reason::DESCRIPTIONS[status]
299+
296300
unless @state == :open or @state == :half_closed_remote
297301
raise ProtocolError, "Cannot write interim response in state: #{@state}!"
298302
end
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# frozen_string_literal: true
2+
3+
# Released under the MIT License.
4+
# Copyright, 2025, by Samuel Williams.
5+
6+
require_relative "http1/connection"

0 commit comments

Comments
 (0)