Skip to content

Commit

Permalink
Add Event driven Instrumentation (#1)
Browse files Browse the repository at this point in the history
  • Loading branch information
wenderjean authored and LucasHFS committed Jan 10, 2025
1 parent 08e2832 commit 0867086
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 0 deletions.
4 changes: 4 additions & 0 deletions lib/recaptcha.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
require 'recaptcha/helpers'
require 'recaptcha/adapters/controller_methods'
require 'recaptcha/adapters/view_methods'
require 'recaptcha/instrumentation'
if defined?(Rails)
require 'recaptcha/railtie'
end
Expand Down Expand Up @@ -99,6 +100,9 @@ def self.verify_via_api_call_free(response, options)
verify_hash['remoteip'] = options[:remote_ip] if options.key?(:remote_ip)

reply = api_verification_free(verify_hash, timeout: options[:timeout], json: options[:json])
Instrumentation.report(
{ token: response, options: options, response: reply }
)
success = reply['success'].to_s == 'true' &&
hostname_valid?(reply['hostname'], options[:hostname]) &&
action_valid?(reply['action'], options[:action]) &&
Expand Down
19 changes: 19 additions & 0 deletions lib/recaptcha/instrumentation.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# frozen_string_literal: true

module Recaptcha
class Instrumentation
CHANNEL = 'Recaptcha::API::Response'

def self.report(payload = {})
ActiveSupport::Notifications.instrument(CHANNEL, payload)
end

def self.subscribe(&block)
ActiveSupport::Notifications.subscribe(CHANNEL) do |*args|
event = ActiveSupport::Notifications::Event.new(*args)

yield event if block_given?
end
end
end
end

0 comments on commit 0867086

Please sign in to comment.