Skip to content

Commit

Permalink
Move span attributes to http request and http response to make send m…
Browse files Browse the repository at this point in the history
…iddleware transport agnostic
  • Loading branch information
jterapin committed Aug 23, 2024
1 parent 030cccb commit 73d7ce8
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 33 deletions.
17 changes: 17 additions & 0 deletions hearth/lib/hearth/http/request.rb
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,23 @@ def remove_query_param(name)
def prefix_host(prefix)
uri.host = prefix + uri.host
end

# Contains attributes for Telemetry span to emit.
# @return [Hash]
def span_attributes
{
'http.method' => http_method,
'net.protocol.name' => 'http',
'net.protocol.version' => Net::HTTP::HTTPVersion,
'net.peer.name' => uri.host,
'net.peer.port' => uri.port
}.tap do |h|
if headers.key?('Content-Length')
h['http.request_content_length'] =
headers['Content-Length']
end
end
end
end
end
end
13 changes: 13 additions & 0 deletions hearth/lib/hearth/http/response.rb
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,19 @@ def reset
@fields.clear
super
end

# Contains attributes for Telemetry span to emit.
# @return [Hash]
def span_attributes
{
'http.status_code' => status
}.tap do |h|
if headers.key?('Content-Length')
h['http.response_content_length'] =
headers['Content-Length']
end
end
end
end
end
end
37 changes: 4 additions & 33 deletions hearth/lib/hearth/middleware/send.rb
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ def call(input, context)
private

def stub_response(input, context, output)
span_wrapper(context, stub_response: true) do
span_wrapper(context) do
stub = @stubs.next(context.operation_name)
log_debug(context, "Stubbing response with stub: #{stub}")
if @response_events
Expand Down Expand Up @@ -109,42 +109,13 @@ def send_request(context, output)
end
end

def span_wrapper(context, stub_response: false, &block)
def span_wrapper(context, &block)
context.tracer.in_span(
'Middleware.Send',
attributes: request_attrs(context, stub_response: stub_response)
attributes: context.request.span_attributes
) do |span|
block.call
span.add_attributes(response_attrs(context))
end
end

def request_attrs(context, stub_response: false)
{
'http.method' => context.request.http_method,
'net.protocol.name' => 'http',
'net.protocol.version' => Net::HTTP::HTTPVersion
}.tap do |h|
unless stub_response
h['net.peer.name'] = context.request.uri.host
h['net.peer.port'] = context.request.uri.port
end

if context.request.headers.key?('Content-Length')
h['http.request_content_length'] =
context.request.headers['Content-Length']
end
end
end

def response_attrs(context)
{
'http.status_code' => context.response.status
}.tap do |h|
if context.response.headers.key?('Content-Length')
h['http.response_content_length'] =
context.response.headers['Content-Length']
end
span.add_attributes(context.response.span_attributes)
end
end

Expand Down

0 comments on commit 73d7ce8

Please sign in to comment.