Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

make logger an instance var #90

Merged
merged 1 commit into from
Jun 6, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
### Breaking changes

* port / host / tags / namespace can no longer be set on the instance to allow thread-safety [#87][] by [@grosser][]
* replace global logger with instance option [#90][] by [@grosser][]

## 3.3.0 / 2018.02.04

Expand Down
12 changes: 6 additions & 6 deletions lib/datadog/statsd.rb
Original file line number Diff line number Diff line change
Expand Up @@ -78,10 +78,8 @@ class Statsd
# Maximum number of metrics in the buffer before it is flushed
attr_reader :max_buffer_size

class << self
# Set to a standard logger instance to enable debug logging.
attr_accessor :logger
end
# Logger
attr_reader :logger

# Return the current version of the library.
# deprecated, but cannot be removed since uses might use it to check the version against older releases
Expand All @@ -93,10 +91,12 @@ def self.VERSION
# @param [Integer] port your statsd port
# @option opts [String] :namespace set a namespace to be prepended to every metric name
# @option opts [Array<String>] :tags tags to be added to every metric
# @option opts [Loger] :logger for debugging
# @option opts [Integer] :max_buffer_size max messages to buffer
def initialize(host = DEFAULT_HOST, port = DEFAULT_PORT, opts = EMPTY_OPTIONS)
@host = host || DEFAULT_HOST
@port = port || DEFAULT_PORT
@logger = opts[:logger]

@socket_path = opts[:socket_path]
@socket = connect_to_socket if @socket_path.nil?
Expand Down Expand Up @@ -461,7 +461,7 @@ def sock
end

def send_to_socket(message)
self.class.logger.debug { "Statsd: #{message}" } if self.class.logger
@logger.debug { "Statsd: #{message}" } if @logger
if @socket_path.nil?
sock.send(message, 0)
else
Expand All @@ -485,7 +485,7 @@ def send_to_socket(message)
end
end

self.class.logger.error { "Statsd: #{boom.class} #{boom}" } if self.class.logger
@logger.error { "Statsd: #{boom.class} #{boom}" } if @logger
nil
end
end
Expand Down
14 changes: 6 additions & 8 deletions spec/statsd_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ class Datadog::Statsd
before do
@statsd = Datadog::Statsd.new('localhost', 1234, namespace: namespace)
@statsd.socket = FakeUDPSocket.new
Datadog::Statsd.logger = nil
end

describe ".VERSION" do
Expand Down Expand Up @@ -369,18 +368,18 @@ def helper_time_return

describe "with logging" do
require 'stringio'
before { Datadog::Statsd.logger = Logger.new(@log = StringIO.new) }
before { @statsd.instance_variable_set(:@logger, Logger.new(@log = StringIO.new)) }

it "should write to the log in debug" do
Datadog::Statsd.logger.level = Logger::DEBUG
@statsd.logger.level = Logger::DEBUG

@statsd.increment('foobar')

@log.string.must_match "Statsd: foobar:1|c"
end

it "should not write to the log unless debug" do
Datadog::Statsd.logger.level = Logger::INFO
@statsd.logger.level = Logger::INFO

@statsd.increment('foobar')

Expand Down Expand Up @@ -428,7 +427,7 @@ class Datadog::Statsd::SomeClass; end

describe "handling socket errors" do
before do
Datadog::Statsd.logger = Logger.new(@log = StringIO.new)
@statsd.instance_variable_set(:@logger, Logger.new(@log = StringIO.new))
@statsd.socket.instance_eval { def send(*) raise SocketError end }
end

Expand All @@ -442,14 +441,14 @@ class Datadog::Statsd::SomeClass; end
end

it "works without a logger" do
Datadog::Statsd.logger = nil
@statsd.instance_variable_set(:@logger, nil)
@statsd.increment('foobar')
end
end

describe "handling closed socket" do
before do
Datadog::Statsd.logger = Logger.new(@log = StringIO.new)
@statsd.instance_variable_set(:@logger, Logger.new(@log = StringIO.new))
end

it "should try once to reconnect" do
Expand Down Expand Up @@ -500,7 +499,6 @@ def send(*)
describe "UDS error handling" do
before do
@statsd = Datadog::Statsd.new('localhost', 1234, {:socket_path => '/tmp/socket'})
Datadog::Statsd.logger = Logger.new(@log = StringIO.new)
end

describe "when socket throws connection reset error" do
Expand Down