Skip to content

Commit

Permalink
store geoip_info directly on event
Browse files Browse the repository at this point in the history
  • Loading branch information
segiddins committed Jan 31, 2024
1 parent 4baa10a commit 264cbe7
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 11 deletions.
5 changes: 2 additions & 3 deletions app/models/events/recordable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@ module Events::Recordable

def record_event!(tag, request: Current.request, **additional)
ip_address = request&.ip_address

additional[:geoip_info] = ip_address.geoip_info if ip_address&.geoip_info.present?
geoip_info = ip_address&.geoip_info

if (user_agent = request&.user_agent.presence)
begin
Expand All @@ -18,7 +17,7 @@ def record_event!(tag, request: Current.request, **additional)
end
end

event = events.create!(tag:, ip_address:, additional:, trace_id: Datadog::Tracing.correlation.trace_id)
event = events.create!(tag:, ip_address:, geoip_info:, additional:, trace_id: Datadog::Tracing.correlation.trace_id)
logger.info("Recorded event #{tag}", record: cache_key,
event: event.as_json, tag:, ip_address: ip_address.as_json, additional: event.additional)
event
Expand Down
8 changes: 4 additions & 4 deletions app/views/components/events/table_component.rb
Original file line number Diff line number Diff line change
Expand Up @@ -72,16 +72,16 @@ def event_details(event)
render component.new(event:)
end

def additional_info(event) # rubocop:disable Metrics
def additional_info(event)
return unless event.tags.key?(event.tag)
return if event.additional.blank? || (event.additional.geoip_info.nil? && event.additional.user_agent_info.nil?)
return if event.geoip_info.nil? && (event.additional.blank? && event.additional.user_agent_info.nil?)

p(class: "!tw-mb-0") do
break div { t(".redacted") } unless show_additional_info?(event)

if (geoip_info = event.additional&.geoip_info)
if event.geoip_info.present?
div do
plain geoip_info.to_s
plain event.geoip_info.to_s
end
end

Expand Down
9 changes: 5 additions & 4 deletions test/views/events/table_component_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ def has_attribute?(name) = respond_to?(name) # rubocop:disable Naming/PredicateN

attribute :additional
attribute :created_at, :datetime, default: -> { Time.zone.now }
attribute :geoip_info, Types::JsonDeserializable.new(GeoipInfo)

include Events::Tags
end
Expand Down Expand Up @@ -61,8 +62,8 @@ def page(array, page: 0, per: 10)
user = create(:user)

page = render table(page([
UserTestEvent.new(tag: tag, user_id: user.id + 1, additional: {
geoip_info: build(:geoip_info), user_agent_info: build(:events_user_agent_info)
UserTestEvent.new(tag: tag, user_id: user.id + 1, geoip_info: build(:geoip_info), additional: {
user_agent_info: build(:events_user_agent_info)
})
]), stubs: { current_user: user })

Expand All @@ -79,8 +80,8 @@ def page(array, page: 0, per: 10)
user = create(:user)

page = render table(page([
UserTestEvent.new(tag: tag, user_id: user.id, additional: {
geoip_info: build(:geoip_info), user_agent_info: build(:events_user_agent_info)
UserTestEvent.new(tag: tag, user_id: user.id, geoip_info: build(:geoip_info), additional: {
user_agent_info: build(:events_user_agent_info)
})
]), stubs: { current_user: user })

Expand Down

0 comments on commit 264cbe7

Please sign in to comment.