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

Display error logs if write fails due to a bad socket #97

Conversation

abicky
Copy link
Contributor

@abicky abicky commented Jul 30, 2018

I want to display error logs so that I can notice that write fails, but currently no logs are displayed even if write fails due to a bad socket.
I confirmed the behavior using the following script:

require 'logger'
require 'socket'
require 'datadog/statsd'

socket_path = '/tmp/datadog/dsd.socket'
Thread.new do
  2.times do
    sock = Socket.new(:UNIX, :DGRAM, 0)
    sock.bind(Addrinfo.unix(socket_path))

    3.times do
      puts "Received: #{sock.recvfrom_nonblock(128).first}"
    rescue IO::WaitReadable
      sleep 0.1
      retry
    end
    puts "Close socket"
    sock.close
    File.unlink(socket_path)
    sleep 2
  end
end

logger = Logger.new($stdout)
logger.level = Logger::INFO
statsd = Datadog::Statsd.new(nil, nil, socket_path: socket_path, logger: logger)
while true
  print 'increment: '
  p statsd.increment("page.views")
  sleep 1
end

Before

$ ruby /path/to/script
increment: 14
Received: page.views:1|c
increment: 14
Received: page.views:1|c
increment: 14
Received: page.views:1|c
Close socket
increment: nil
increment: nil
increment: 14
Received: page.views:1|c

After

$ ruby /path/to/script
increment: 14
Received: page.views:1|c
increment: 14
Received: page.views:1|c
increment: 14
Received: page.views:1|c
Close socket
increment: E, [2018-07-30T18:58:03.582215 #41583] ERROR -- : Statsd: Datadog::Statsd::UDSConnection::BadSocketError Errno::ECONNRESET: Connection reset by peer - sendmsg(2)
nil
increment: E, [2018-07-30T18:58:04.583728 #41583] ERROR -- : Statsd: Datadog::Statsd::UDSConnection::BadSocketError Errno::ENOENT: No such file or directory - connect(2) for /tmp/datadog/dsd.socket
nil
increment: 14
Received: page.views:1|c

@albertvaka
Copy link
Contributor

albertvaka commented Jun 14, 2019

Thanks for the patch! This makes sense and the code looks better after splitting the Connection in two.

I had to fix a small conflict, but it's good to merge 😄

Sorry it took a year to review this... and thanks again!

@albertvaka albertvaka merged commit cbf2b46 into DataDog:master Jun 14, 2019
@abicky abicky deleted the display-error-logs-on-failures-due-to-bad-socket branch May 15, 2022 09:04
pudiva pushed a commit to pudiva/dogstatsd-ruby that referenced this pull request Jun 5, 2023
* Update rice scoring and templates

* rice-scoring: Fix liquid template

* Fix spelling
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants