-
Notifications
You must be signed in to change notification settings - Fork 116
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
base: main
Are you sure you want to change the base?
Conversation
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.
- 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>
# 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 |
There was a problem hiding this comment.
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( |
There was a problem hiding this comment.
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.
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:
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: