Skip to content

Commit a2e7251

Browse files
committed
Use finish_with_state inside RailsSemanticLogger::Rack::Logger
This ensures that the finish instrumenter is sent to the same subscribers that the initial request was sent to. This solves an issue where in a threaded environment the dynamic subscriptions from ActionDispatch::ServerTiming would cause a mismatch in the number of subscriptions to a topic, which would pop too many values off of the thread-local stacks, leading to invalid events being sent to subscribers which leads nil value for `time` in Event object which throw exception.
1 parent 6fac221 commit a2e7251

File tree

1 file changed

+7
-10
lines changed

1 file changed

+7
-10
lines changed

lib/rails_semantic_logger/rack/logger.rb

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -35,16 +35,18 @@ def call(env)
3535
@started_request_log_level = :debug
3636

3737
def call_app(request, env)
38-
instrumenter = ActiveSupport::Notifications.instrumenter
39-
instrumenter.start "request.action_dispatch", request: request
38+
instrumenter = ActiveSupport::Notifications.instrumenter
39+
instrumenter_state = instrumenter.start "request.action_dispatch", request: request
40+
instrumenter_finish = -> () {
41+
instrumenter.finish_with_state(instrumenter_state, "request.action_dispatch", request: request)
42+
}
4043

4144
logger.send(self.class.started_request_log_level) { started_request_message(request) }
42-
4345
status, headers, body = @app.call(env)
44-
body = ::Rack::BodyProxy.new(body) { finish(request) }
46+
body = ::Rack::BodyProxy.new(body, &instrumenter_finish)
4547
[status, headers, body]
4648
rescue Exception
47-
finish(request)
49+
instrumenter_finish.call
4850
raise
4951
end
5052

@@ -90,11 +92,6 @@ def compute_named_tags(request)
9092
tagged
9193
end
9294

93-
def finish(request)
94-
instrumenter = ActiveSupport::Notifications.instrumenter
95-
instrumenter.finish "request.action_dispatch", request: request
96-
end
97-
9895
def logger
9996
self.class.logger
10097
end

0 commit comments

Comments
 (0)