lib/dogstatsd/instrumentation/request.rb in dogstatsd-instrumentation-0.1.3 vs lib/dogstatsd/instrumentation/request.rb in dogstatsd-instrumentation-0.1.4
- old
+ new
@@ -1,46 +1,35 @@
# frozen_string_literal: true
require 'active_support'
require 'gem_config'
require 'datadog/statsd'
+require_relative 'client_provider'
module DogStatsd
module Instrumentation
module Request
include GemConfig::Base
with_configuration do
- has :enabled, classes: [TrueClass, FalseClass], default: true
- has :base_tags, classes: Hash, default: {}
-
- has :host, classes: String, default: Datadog::Statsd::DEFAULT_HOST
- has :port, classes: Integer, default: Datadog::Statsd::DEFAULT_PORT
- has :opts, classes: Hash, default: {}
- has :max_buffer_size, classes: Integer, default: 50
+ has :statsd, classes: Object
end
# From GemConfig::Base
def self.configure
super
- @@subscriber = @@subscriber.unsubscribe if @@subscriber
- @@subscriber = Subscriber.new(configuration) if configuration.enabled
+ @@subscriber.unsubscribe if @@subscriber
+
+ @@subscriber = Subscriber.new(configuration.statsd || ClientProvider.new)
end
@@subscriber = nil
class Subscriber
- def initialize(configuration)
- @base_tags = configuration.base_tags
+ def initialize(statsd)
+ @statsd = statsd
- @statsd = Datadog::Statsd.new(
- configuration.host,
- configuration.port,
- configuration.opts,
- configuration.max_buffer_size
- )
-
@subscriber = ActiveSupport::Notifications.subscribe /process_action.action_controller/ do |*args|
event = ActiveSupport::Notifications::Event.new(*args)
tags = {
controller: event.payload[:controller],
method: event.payload[:method],
@@ -56,10 +45,10 @@
end
end
end
def instrument(stat:, value:, tags:)
- @statsd.histogram stat, value, tags: Subscriber.tagify(@base_tags.merge(tags))
+ @statsd.histogram stat, value, tags: Subscriber.tagify(tags)
end
def self.tagify(hash)
hash.select { |_, value| value.present? }.map { |key, value| "#{key}:#{value}" }
end