Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RFC: Analytics middleware & recording #11681

Draft
wants to merge 12 commits into
base: main
Choose a base branch
from

Conversation

matthinz
Copy link
Member

@matthinz matthinz commented Dec 20, 2024

Motivated by a bunch of broken analytics specs over on #11674, I wanted to play around with potentially recording analytics event emitted during our IDV end-to-end feature specs and then just asserting that, the next time we run the end-to-end specs, the analytics events look mostly the same.

To record analytics events for the end-to-end spec, you do:

RECORD_ANALYTICS=yes bundle exec rspec 'spec/features/idv/end_to_end_idv_spec.rb'

Then, the next time you run the spec, the logged analytics events will be compared against the recording.

This PR doesn't actually remove the IDV analytics feature spec, but the idea is that, with a little care, you could someday.

Left to do:

  • Add a spec for asserting two event lists match

Introduce middleware to the Analytics class to allow separating out all the different things we like to do with analytics event logging.
Run spec with RECORD_ANALYTICS=1 to dump analytics events to a newline-delimited JSON file.
app/services/analytics.rb Outdated Show resolved Hide resolved
matthinz and others added 6 commits December 20, 2024 16:13
- Automatic normalization of _most_ of the happy path
- Add AnalyticsRecordingHelper
- Get the desktop happy path tests actually passing
Co-authored-by: Zach Margolis <zachmargolis@users.noreply.github.com>
@matthinz matthinz changed the title POC: Analytics middleware & recording Analytics middleware & recording Dec 23, 2024
@matthinz matthinz changed the title Analytics middleware & recording RFC: Analytics middleware & recording Dec 23, 2024
Comment on lines +7 to +37
# Analytics middleware that sends the event to Ahoy
class AhoyMiddleware
attr_reader :ahoy

def initialize(ahoy: nil, request: nil)
@ahoy = ahoy || Ahoy::Tracker.new(request:)
end

def call(event)
event.tap do |event|
ahoy.track(event[:name], event[:properties])
end
end
end

# Analytics middleware that augments NewRelic APM trace with additional metadata.
class NewRelicMiddleware
def call(event)
event.tap do
# Tag NewRelic APM trace with a handful of useful metadata
# https://www.rubydoc.info/github/newrelic/rpm/NewRelic/Agent#add_custom_attributes-instance_method
::NewRelic::Agent.add_custom_attributes(
user_id: event.dig(:properties, :user_id),
user_ip: event.dig(:properties, :user_ip),
service_provider: event.dig(:properties, :service_provider),
event_name: event[:name],
git_sha: IdentityConfig::GIT_SHA,
)
end
end
end
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think ideally these would live in app/services/analytics/ and ApplicationController would add them when it's creating its own Analytics instance

# @param [Hash[]] expected_events Array of analytics event hashes.
# @param [String,nil] file_name File name events were loaded from (for error reporting)
# @param [Number] window_size Number of events we look at when trying to find a match.
def assert_logged_analytics_events_match(
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is more thorough than our current analytics feature spec, since it is requiring that every event seen match 1:1 with an event that was previously recorded.

The current spec defines a set of events, and then asserts that each one was logged. If the app starts firing a new analytics event, that won't cause the analytics spec to fail--it needs to be manually updated to know about the new event.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants