I'm unclear on why the real world example provided is preferable to an implementation that calls MyMetricsService.send directly (or indirectly through dependency injection).
MyMetricsService.send
Could you elaborate?
Of course, Charles, and sorry if the example was confusing.
This API is a good use if you want to keep up with what is going on with your application under the hood.
Assuming that MyMetricsService.send is a service that sends a request to segment, now you need also to save this data to a log.
segment
In this example you can add another line in that same event:
ActiveSupport::Notifications.subscribe /metrics/ do |*args| event = ActiveSupport::Notifications::Event.new(*args) MyMetricsService.send(event.name, event.payload) MyLogger.save(event) end
You can also subscribe to a specific type of events and handle differently.
ActiveSupport::Notifications.subscribe /metrics.purchase/ do |*args| event = ActiveSupport::Notifications::Event.new(*args) NotifySales.call(event) end
As you can see, you would need to change this in just one place.
The idea is basically to publish that something happened on the application and you can subscribe to this event.
Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment's permalink.
Hide child comments as well
Confirm
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
I'm unclear on why the real world example provided is preferable to an implementation that calls
MyMetricsService.send
directly (or indirectly through dependency injection).Could you elaborate?
Of course, Charles, and sorry if the example was confusing.
This API is a good use if you want to keep up with what is going on with your application under the hood.
Assuming that
MyMetricsService.send
is a service that sends a request tosegment
, now you need also to save this data to a log.In this example you can add another line in that same event:
You can also subscribe to a specific type of events and handle differently.
As you can see, you would need to change this in just one place.
The idea is basically to publish that something happened on the application and you can subscribe to this event.