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

Commit

Permalink
Simple per-request singleton/delegator for metrics instance in Rails.
Browse files Browse the repository at this point in the history
  • Loading branch information
metaskills committed Aug 8, 2020
1 parent 0e49e69 commit 0a7bb80
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 2 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,12 @@

See this http:https://keepachangelog.com link for information on how we want this documented formatted.

## v0.5.0

### Added

- Simple singleton/delegator for metrics instance in Rails.

## v0.4.0

### Changed
Expand Down
2 changes: 1 addition & 1 deletion Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
PATH
remote: .
specs:
aws-embedded-metrics-customink (0.4.0)
aws-embedded-metrics-customink (0.5.0)

GEM
remote: https://rubygems.org/
Expand Down
32 changes: 32 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,38 @@ Aws::Embedded::Metrics.logger do |metrics|
end
```

## Using Rails?

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

```ruby
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.

```ruby
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.

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

## Development

After checking out the repo, run `bin/setup` to install dependencies. Then, run `bin/test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
Expand Down
1 change: 1 addition & 0 deletions lib/aws-embedded-metrics-customink.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
require 'aws-embedded-metrics-customink/sinks'
require 'aws-embedded-metrics-customink/config'
require 'aws-embedded-metrics-customink/logger'
require 'aws-embedded-metrics-customink/instance' if defined?(Rails)

module Aws
module Embedded
Expand Down
18 changes: 18 additions & 0 deletions lib/aws-embedded-metrics-customink/instance.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
module Aws
module Embedded
module Metrics
class Instance < SimpleDelegator
mattr_accessor :instance

extend SingleForwardable

def_delegators :instance,
:flush,
:benchmark,
:put_dimension,
:put_metric,
:set_property
end
end
end
end
2 changes: 1 addition & 1 deletion lib/aws-embedded-metrics-customink/version.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
module Aws
module Embedded
module Metrics
VERSION = '0.4.0'.freeze
VERSION = '0.5.0'.freeze
end
end
end

0 comments on commit 0a7bb80

Please sign in to comment.