Skip to content

Commit 00c9d82

Browse files
committed
Enhance logging and request context in API and Auth modules
- Added start_time to request_context for better performance tracking. - Improved log messages for request processing and authentication validation. - Introduced a logger instance for the Auth module to streamline logging. - Updated tests to mock the logger instance for integration tests.
1 parent f70b145 commit 00c9d82

File tree

3 files changed

+27
-6
lines changed

3 files changed

+27
-6
lines changed

lib/hooks/app/api.rb

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -51,21 +51,23 @@ def self.create(config:, endpoints:, log:)
5151

5252
post(full_path) do
5353
request_id = uuid
54+
start_time = Time.now
55+
5456
request_context = {
5557
request_id:,
5658
path: full_path,
57-
handler: handler_class_name,
58-
start_time: Time.now,
59+
handler: handler_class_name
5960
}
6061

62+
# everything wrapped in the log context has access to the request context and includes it in log messages
63+
# ex: Hooks::Log.info("message") will include request_id, path, handler, etc
6164
Core::LogContext.with(request_context) do
6265
begin
6366
enforce_request_limits(config)
6467
request.body.rewind
6568
raw_body = request.body.read
6669

6770
if endpoint_config[:auth]
68-
log.info "validating request (id: #{request_id}, handler: #{handler_class_name})"
6971
validate_auth!(raw_body, headers, endpoint_config, config)
7072
end
7173

@@ -79,10 +81,11 @@ def self.create(config:, endpoints:, log:)
7981
config: endpoint_config
8082
)
8183

82-
log.info "request processed successfully (id: #{request_id}, handler: #{handler_class_name})"
84+
log.info "request processed successfully by handler: #{handler_class_name}"
85+
log.debug "request duration: #{Time.now - start_time}s"
8386
status 200
8487
content_type "application/json"
85-
(response || { status: "ok" }).to_json
88+
response.to_json
8689
rescue => e
8790
log.error "request failed: #{e.message} (id: #{request_id}, handler: #{handler_class_name})"
8891
error_response = {

lib/hooks/app/auth/auth.rb

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ module Auth
2222
def validate_auth!(payload, headers, endpoint_config, global_config = {})
2323
auth_config = endpoint_config[:auth]
2424

25-
# Security: Ensure auth type is present and valid
25+
# Ensure auth type is present and valid
2626
auth_type = auth_config&.dig(:type)
2727
unless auth_type&.is_a?(String) && !auth_type.strip.empty?
2828
error!("authentication configuration missing or invalid", 500)
@@ -32,9 +32,12 @@ def validate_auth!(payload, headers, endpoint_config, global_config = {})
3232
begin
3333
auth_class = Core::PluginLoader.get_auth_plugin(auth_type)
3434
rescue => e
35+
log.error("failed to load auth plugin '#{auth_type}': #{e.message}")
3536
error!("unsupported auth type '#{auth_type}'", 400)
3637
end
3738

39+
log.debug("validating auth for request with auth_class: #{auth_class.name}")
40+
3841
unless auth_class.valid?(
3942
payload:,
4043
headers:,
@@ -43,6 +46,16 @@ def validate_auth!(payload, headers, endpoint_config, global_config = {})
4346
error!("authentication failed", 401)
4447
end
4548
end
49+
50+
private
51+
52+
# Short logger accessor for auth module
53+
# @return [Hooks::Log] Logger instance
54+
#
55+
# Provides access to the application logger for authentication operations.
56+
def log
57+
Hooks::Log.instance
58+
end
4659
end
4760
end
4861
end

spec/unit/lib/hooks/app/auth/custom_auth_integration_spec.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
require_relative "../../../../spec_helper"
44

55
describe "Custom Auth Plugin Integration" do
6+
let(:log) { instance_double(Logger).as_null_object }
67
let(:custom_auth_plugin_dir) { "/tmp/test_auth_plugins" }
78
let(:plugin_file_content) do
89
<<~RUBY
@@ -51,6 +52,10 @@ def self.valid?(payload:, headers:, config:)
5152
Hooks::Core::PluginLoader.load_all_plugins(global_config)
5253
end
5354

55+
before(:each) do
56+
Hooks::Log.instance = log
57+
end
58+
5459
after do
5560
FileUtils.rm_rf(custom_auth_plugin_dir) if Dir.exist?(custom_auth_plugin_dir)
5661
ENV.delete("SUPER_COOL_SECRET")

0 commit comments

Comments
 (0)