Skip to content
This repository has been archived by the owner on May 27, 2024. It is now read-only.

Simple per-request singleton/delegator for metrics instance in Rails. #4

Merged
merged 1 commit into from
Aug 8, 2020

Conversation

metaskills
Copy link
Member

Using Rails?

Want to instrument metrics deep in your code during the request/response lifecycle? Consider creating a PORO like this Metrics example.

class MyMetrics < Aws::Embedded::Metrics::Instance
end

This object is ready to use as a per-request singleton that acts as a simple delegator to all metrics/logger methods. A great way to hook it up for your application is in ApplicationController.

class ApplicationController < ActionController::Base
  around_action :embedded_metrics
  private
  def embedded_metrics
    Aws::Embedded::Metrics.logger do |metrics|
      MyMetrics.instance = MyMetrics.new(metrics)
      yield
    end
  end
end

Now you can happily instrument your code.

proof, time = MyMetrics.benchmark { @imagebuilder.data }
MyMetrics.put_metric 'ImageBuilderTime', time, 'Milliseconds'
MyMetrics.set_property 'ImageId', params[:image_id]

@metaskills metaskills requested a review from drinks August 8, 2020 13:14
@metaskills metaskills force-pushed the EasySingletonDelegatorPattern branch 2 times, most recently from d19a9d8 to 183098a Compare August 8, 2020 13:27
@metaskills metaskills force-pushed the EasySingletonDelegatorPattern branch from 183098a to 0a7bb80 Compare August 8, 2020 13:28
@metaskills metaskills merged commit d84e37a into master Aug 8, 2020
@delete-merged-branch delete-merged-branch bot deleted the EasySingletonDelegatorPattern branch August 8, 2020 13:29
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

1 participant