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

customink/aws-embedded-metrics-customink

Repository files navigation

Aws::Embedded::Metrics for Ruby Actions Status

⚠️ Bare minimum code to explore Amazon CloudWatch Embedded Metrics with Ruby. Any and all help welcome to raise the quality of our implementation.

Enhancing workload observability using Amazon CloudWatch Embedded Metric Format

Inspiration

Pulled from these two projects using the Embedded Metric Format Specification as a reference guide.

However, unlike these projects, we differ in the following ways. Again, contributions are very much welcome if you want to see more or change this.

  • Initial focus on Lambda. A TCP sink has been added, but no UDP sink exists.
  • No default Dimensions or Configuration for:
    • ServiceName
    • ServiceType

Installation

Add this line to your application's Gemfile:

gem 'aws-embedded-metrics-customink'

Usage

If using outside of Rails, require the gem:

require 'aws-embedded-metrics-customink'

Simple configuration:

Aws::Embedded::Metrics.configure do |c|
  c.namespace = 'MyApplication'
  # Optional
  c.log_group_name = 'MyLogGroup'
  c.log_stream_name = 'MyLogStream-UniqueID' 
end

Using the Logger sink to write to a log file:

Aws::Embedded::Metrics.configure do |c|
  c.sink = Aws::Embedded::Metrics::Sinks::Logger.new(Rails.logger)
end

Using the Tcp sink to write over a network:

Aws::Embedded::Metrics.configure do |c|
  c.sink = Aws::Embedded::Metrics::Sinks::Tcp.new(conn_str: "tcp://localhost:25888",
                                                  logger: Rails.logger)
end

Usage is in a scope block. All metrics are flushed afterward

Aws::Embedded::Metrics.logger do |metrics|
  metrics.put_dimension 'SomeDimension', 'SomeDimensionValue'
  metrics.set_property  'EventKey', 'some/s3/path'
  metrics.put_metric    'Processor', 232, 'Milliseconds'
  metrics.put_metric    'Total', 4008, 'Milliseconds'
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.

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]

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.

To install this gem onto your local machine, run bundle exec rake install. To release a new version, update the version number in version.rb, and then run bundle exec rake release, which will create a git tag for the version, push git commits and tags, and push the .gem file to rubygems.org.

Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/customink/aws-embedded-metrics-customink. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the code of conduct.

License

The gem is available as open source under the terms of the MIT License.

Code of Conduct

Everyone interacting in the Aws::Embedded::Metrics project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the code of conduct.