Sha256: fe4a761b98739b798fbeafcb3373d7fe4f015d43d34540c5ea2fdae7f5240b51

Contents?: true

Size: 1.06 KB

Versions: 10

Compression:

Stored size: 1.06 KB

Contents

require 'datadog/statsd'
require 'singleton'

module RooOnRails
  class Statsd
    include Singleton

    attr_reader :client

    def initialize
      @client = defined?(::STATSD) ? ::STATSD : ::Datadog::Statsd.new(host, port, tags: tags)
    end

    private

    def host
      ENV.fetch('STATSD_HOST', 'localhost')
    end

    def port
      ENV.fetch('STATSD_PORT', 8125)
    end

    def tags
      [
        "env:#{env_name}",
        "source:#{source_name}",
        "app:#{app_name}"
      ]
    end

    def env_name
      ENV['STATSD_ENV'] || ENV['HOPPER_ECS_CLUSTER_NAME'] || 'unknown'
    end

    def source_name
      ENV['DYNO'] || ENV['HOSTNAME'] || 'unknown'
    end

    def app_name
      ENV['STATSD_APP_NAME'] || ENV['HEROKU_APP_NAME'] || hopper_app_name || 'unknown'
    end

    def hopper_app_name
      app_name = ENV['HOPPER_APP_NAME']
      cluster_name = ENV['HOPPER_ECS_CLUSTER_NAME']
      return unless app_name && cluster_name
      [app_name, cluster_name].join('-')
    end
  end

  def self.statsd
    RooOnRails::Statsd.instance.client
  end
end

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
roo_on_rails-2.2.2 lib/roo_on_rails/statsd.rb
roo_on_rails-2.2.1 lib/roo_on_rails/statsd.rb
roo_on_rails-2.2.0 lib/roo_on_rails/statsd.rb
roo_on_rails-2.1.2 lib/roo_on_rails/statsd.rb
roo_on_rails-2.1.0 lib/roo_on_rails/statsd.rb
roo_on_rails-2.0.0.pre.pre.2 lib/roo_on_rails/statsd.rb
roo_on_rails-2.0.0.pre.pre.1 lib/roo_on_rails/statsd.rb
roo_on_rails-1.22.0 lib/roo_on_rails/statsd.rb
roo_on_rails-1.21.0 lib/roo_on_rails/statsd.rb
roo_on_rails-1.20.0 lib/roo_on_rails/statsd.rb