dogstatsd-ruby 4.3.0 → 4.8.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,98 @@
1
+ # frozen_string_literal: true
2
+ require 'time'
3
+
4
+ module Datadog
5
+ class Statsd
6
+ class Telemetry
7
+ attr_reader :metrics
8
+ attr_reader :events
9
+ attr_reader :service_checks
10
+ attr_reader :bytes_sent
11
+ attr_reader :bytes_dropped
12
+ attr_reader :packets_sent
13
+ attr_reader :packets_dropped
14
+ attr_reader :estimate_max_size
15
+
16
+ def initialize(disabled, flush_interval, global_tags: [], transport_type: :udp)
17
+ @disabled = disabled
18
+ @flush_interval = flush_interval
19
+ @global_tags = global_tags
20
+ @transport_type = transport_type
21
+ reset
22
+
23
+ # TODO: Karim: I don't know why but telemetry tags are serialized
24
+ # before global tags so by refactoring this, I am keeping the same behavior
25
+ @serialized_tags = Serialization::TagSerializer.new(
26
+ client: 'ruby',
27
+ client_version: VERSION,
28
+ client_transport: transport_type,
29
+ ).format(global_tags)
30
+
31
+ # estimate_max_size is an estimation or the maximum size of the
32
+ # telemetry payload. Since we don't want our packet to go over
33
+ # 'max_buffer_bytes', we have to adjust with the size of the telemetry
34
+ # (and any tags used). The telemetry payload size will change depending
35
+ # on the actual value of metrics: metrics received, packet dropped,
36
+ # etc. This is why we add a 63bytes margin: 9 bytes for each of the 7
37
+ # telemetry metrics.
38
+ @estimate_max_size = disabled ? 0 : flush.length + 9 * 7
39
+ end
40
+
41
+ def reset
42
+ @metrics = 0
43
+ @events = 0
44
+ @service_checks = 0
45
+ @bytes_sent = 0
46
+ @bytes_dropped = 0
47
+ @packets_sent = 0
48
+ @packets_dropped = 0
49
+ @next_flush_time = now_in_s + @flush_interval
50
+ end
51
+
52
+ def sent(metrics: 0, events: 0, service_checks: 0, bytes: 0, packets: 0)
53
+ @metrics += metrics
54
+ @events += events
55
+ @service_checks += service_checks
56
+
57
+ @bytes_sent += bytes
58
+ @packets_sent += packets
59
+ end
60
+
61
+ def dropped(bytes: 0, packets: 0)
62
+ @bytes_dropped += bytes
63
+ @packets_dropped += packets
64
+ end
65
+
66
+ def flush?
67
+ @next_flush_time < now_in_s
68
+ end
69
+
70
+ def flush
71
+ return '' if @disabled
72
+
73
+ # using shorthand syntax to reduce the garbage collection
74
+ %Q(
75
+ datadog.dogstatsd.client.metrics:#{@metrics}|#{COUNTER_TYPE}|##{serialized_tags}
76
+ datadog.dogstatsd.client.events:#{@events}|#{COUNTER_TYPE}|##{serialized_tags}
77
+ datadog.dogstatsd.client.service_checks:#{@service_checks}|#{COUNTER_TYPE}|##{serialized_tags}
78
+ datadog.dogstatsd.client.bytes_sent:#{@bytes_sent}|#{COUNTER_TYPE}|##{serialized_tags}
79
+ datadog.dogstatsd.client.bytes_dropped:#{@bytes_dropped}|#{COUNTER_TYPE}|##{serialized_tags}
80
+ datadog.dogstatsd.client.packets_sent:#{@packets_sent}|#{COUNTER_TYPE}|##{serialized_tags}
81
+ datadog.dogstatsd.client.packets_dropped:#{@packets_dropped}|#{COUNTER_TYPE}|##{serialized_tags})
82
+ end
83
+
84
+ private
85
+ attr_reader :serialized_tags
86
+
87
+ if Kernel.const_defined?('Process') && Process.respond_to?(:clock_gettime)
88
+ def now_in_s
89
+ Process.clock_gettime(Process::CLOCK_MONOTONIC, :second)
90
+ end
91
+ else
92
+ def now_in_s
93
+ Time.now.to_i
94
+ end
95
+ end
96
+ end
97
+ end
98
+ end
@@ -0,0 +1,37 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'connection'
4
+
5
+ module Datadog
6
+ class Statsd
7
+ class UDPConnection < Connection
8
+ DEFAULT_HOST = '127.0.0.1'
9
+ DEFAULT_PORT = 8125
10
+
11
+ # StatsD host. Defaults to 127.0.0.1.
12
+ attr_reader :host
13
+
14
+ # StatsD port. Defaults to 8125.
15
+ attr_reader :port
16
+
17
+ def initialize(host, port, logger, telemetry)
18
+ super(telemetry)
19
+ @host = host || ENV.fetch('DD_AGENT_HOST', DEFAULT_HOST)
20
+ @port = port || ENV.fetch('DD_DOGSTATSD_PORT', DEFAULT_PORT).to_i
21
+ @logger = logger
22
+ end
23
+
24
+ private
25
+
26
+ def connect
27
+ UDPSocket.new.tap do |socket|
28
+ socket.connect(host, port)
29
+ end
30
+ end
31
+
32
+ def send_message(message)
33
+ socket.send(message, 0)
34
+ end
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,38 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'connection'
4
+
5
+ module Datadog
6
+ class Statsd
7
+ class UDSConnection < Connection
8
+ class BadSocketError < StandardError; end
9
+
10
+ # DogStatsd unix socket path
11
+ attr_reader :socket_path
12
+
13
+ def initialize(socket_path, logger, telemetry)
14
+ super(telemetry)
15
+ @socket_path = socket_path
16
+ @logger = logger
17
+ end
18
+
19
+ private
20
+
21
+ def connect
22
+ socket = Socket.new(Socket::AF_UNIX, Socket::SOCK_DGRAM)
23
+ socket.connect(Socket.pack_sockaddr_un(@socket_path))
24
+ socket
25
+ end
26
+
27
+ def send_message(message)
28
+ socket.sendmsg_nonblock(message)
29
+ rescue Errno::ECONNREFUSED, Errno::ECONNRESET, Errno::ENOENT => e
30
+ @socket = nil
31
+ # TODO: FIXME: This error should be considered as a retryable error in the
32
+ # Connection class. An even better solution would be to make BadSocketError inherit
33
+ # from a specific retryable error class in the Connection class.
34
+ raise BadSocketError, "#{e.class}: #{e}"
35
+ end
36
+ end
37
+ end
38
+ end
@@ -0,0 +1,9 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'connection'
4
+
5
+ module Datadog
6
+ class Statsd
7
+ VERSION = '4.8.0'
8
+ end
9
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: dogstatsd-ruby
3
3
  version: !ruby/object:Gem::Version
4
- version: 4.3.0
4
+ version: 4.8.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Rein Henrichs
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2019-06-24 00:00:00.000000000 Z
11
+ date: 2020-04-21 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: A Ruby DogStastd client
14
14
  email: code@datadoghq.com
@@ -21,10 +21,26 @@ files:
21
21
  - LICENSE.txt
22
22
  - README.md
23
23
  - lib/datadog/statsd.rb
24
- homepage: http://github.com/datadog/dogstatsd-ruby
24
+ - lib/datadog/statsd/batch.rb
25
+ - lib/datadog/statsd/connection.rb
26
+ - lib/datadog/statsd/serialization.rb
27
+ - lib/datadog/statsd/serialization/event_serializer.rb
28
+ - lib/datadog/statsd/serialization/serializer.rb
29
+ - lib/datadog/statsd/serialization/service_check_serializer.rb
30
+ - lib/datadog/statsd/serialization/stat_serializer.rb
31
+ - lib/datadog/statsd/serialization/tag_serializer.rb
32
+ - lib/datadog/statsd/telemetry.rb
33
+ - lib/datadog/statsd/udp_connection.rb
34
+ - lib/datadog/statsd/uds_connection.rb
35
+ - lib/datadog/statsd/version.rb
36
+ homepage: https://github.com/DataDog/dogstatsd-ruby
25
37
  licenses:
26
38
  - MIT
27
- metadata: {}
39
+ metadata:
40
+ bug_tracker_uri: https://github.com/DataDog/dogstatsd-ruby/issues
41
+ changelog_uri: https://github.com/DataDog/dogstatsd-ruby/blob/v4.8.0/CHANGELOG.md
42
+ documentation_uri: https://www.rubydoc.info/gems/dogstatsd-ruby/4.8.0
43
+ source_code_uri: https://github.com/DataDog/dogstatsd-ruby/tree/v4.8.0
28
44
  post_install_message:
29
45
  rdoc_options: []
30
46
  require_paths:
@@ -41,7 +57,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
41
57
  version: '0'
42
58
  requirements: []
43
59
  rubyforge_project:
44
- rubygems_version: 2.7.6
60
+ rubygems_version: 2.7.10
45
61
  signing_key:
46
62
  specification_version: 4
47
63
  summary: A Ruby DogStatsd client