nexus_semantic_logger 1.12 → 1.12.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 70e0537b45679788fc9dc6ce2c5e6ae6698e840735039bfc8d55ac324e03d062
4
- data.tar.gz: 6a489153ff7db8a3d2bca5696a3ceec917014f9e10416f3a04f5446114a0514b
3
+ metadata.gz: 027f845d9e52d9f2b489fdee4322de8da733d3d8b6b61164a26b7e65752cbd80
4
+ data.tar.gz: f3bab4633d763ea390e36347171b003bc046c452ffaa3c9bf2cec0c1d1ed0cdd
5
5
  SHA512:
6
- metadata.gz: 25f789b5449465c5466d74c780cfb6401e4c5f4df493b40d616f55de5d986366d3d677b69ea1ce51f20e7c94ce71c7e896ec3c61470969db96c213a5c8ad26d8
7
- data.tar.gz: a28d97cf7fa44c87511388145e38f2132e1787f711995523226ce388c5c4260ae59c5a975e52daa072bc11273f5fb6698b10ee5ebec3e21b43479b3f59bbfd33
6
+ metadata.gz: 888523a95588e73193a0e25bf788a4c1345815f67ad2815de793579394834be50ec3630a89d848a8b4b1d150deefdaad23b8be2d487863e97935c7f9f9b06e68
7
+ data.tar.gz: 13b686749756c64fa5011e5260da75ddacd991c38b1d944104ae3843ef606347e8b05300f485a62b3512922e5b8bd212f8917bcd1ede6b610ffaf5d100fb1a06
@@ -7,7 +7,7 @@ module NexusSemanticLogger
7
7
  # own statsd instance.
8
8
  class DatadogSingleton
9
9
  include Singleton
10
- attr_accessor :statsd, :global_tags
10
+ attr_accessor :statsd
11
11
 
12
12
  def flush
13
13
  statsd&.flush(sync: Rails.env.development?) # Force flush sync in development, speed up checks.
@@ -58,10 +58,9 @@ module NexusSemanticLogger
58
58
 
59
59
  private
60
60
 
61
- # Safely combine the global tags with the supplied tags.
61
+ # Safely combine the supplied tags.
62
62
  def combine_tags(tags)
63
63
  final_tags = []
64
- final_tags += global_tags unless global_tags.nil?
65
64
  final_tags += tags unless tags.nil?
66
65
  final_tags
67
66
  end
@@ -7,6 +7,22 @@ module NexusSemanticLogger
7
7
  def initialize(service)
8
8
  Datadog.configure do |c|
9
9
  if ENV['DD_AGENT_HOST'].present?
10
+
11
+ # Container and pod names should be set as env vars via the helm chart. Tagging metrics from the app
12
+ # with these values helps correlation with metrics from the kubernetes cluster.
13
+ container_name = ENV.fetch('CONTAINER_NAME') { '' }
14
+ pod_name = ENV.fetch('POD_NAME') { '' }
15
+
16
+ # Configure tags to be sent on all metrics.
17
+ # Note that 'env' is NOT sent- that is set as the default on the agent e.g. staging, canary, production.
18
+ # It does not necessarily align with the Rails env, and we do not want to double tag the env.
19
+ global_tags = [
20
+ "railsenv:#{Rails.env}",
21
+ "service:#{service}",
22
+ "container_name:#{container_name}",
23
+ "pod_name:#{pod_name}",
24
+ ]
25
+
10
26
  # To enable runtime metrics collection, set `true`. Defaults to `false`
11
27
  # You can also set DD_RUNTIME_METRICS_ENABLED=true to configure this.
12
28
  c.runtime_metrics.enabled = true
@@ -16,20 +32,19 @@ module NexusSemanticLogger
16
32
  datadog_singleton = DatadogSingleton.instance
17
33
  datadog_statsd_socket_path = ENV.fetch('DD_STATSD_SOCKET_PATH') { '' }
18
34
  datadog_singleton.statsd = if datadog_statsd_socket_path.to_s.strip.empty?
19
- Datadog::Statsd.new(ENV['DD_AGENT_HOST'], 8125)
35
+ Datadog::Statsd.new(ENV['DD_AGENT_HOST'], 8125, tags: global_tags)
20
36
  else
21
- Datadog::Statsd.new(socket_path: datadog_statsd_socket_path)
37
+ Datadog::Statsd.new(socket_path: datadog_statsd_socket_path, tags: global_tags)
22
38
  end
23
39
  c.runtime_metrics.statsd = datadog_singleton.statsd
24
40
 
25
- # Configure tags to be sent on all metrics.
26
- # Note that 'env' is NOT sent- that is set as the default on the agent e.g. staging, canary, production.
27
- # It does not necessarily align with the Rails env, and we do not want to double tag the env.
28
- datadog_singleton.global_tags = ["railsenv:#{Rails.env}", "service:#{service}"]
29
41
  # Trace tags API is Hash<String,String>, see https://www.rubydoc.info/gems/ddtrace/Datadog/Tracing
42
+ # Should match the global tags, but as a Hash.
30
43
  c.tags = {
31
44
  railsenv: Rails.env,
32
45
  service: service,
46
+ container_name: container_name,
47
+ pod_name: pod_name,
33
48
  }
34
49
 
35
50
  # Tracer requires configuration to a datadog agent via DD_AGENT_HOST.
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
  module NexusSemanticLogger
3
- # Leave this as 1.12 in order for CI process to replace with the tagged version.
4
- VERSION = '1.12'
3
+ # Leave this as 1.12.2 in order for CI process to replace with the tagged version.
4
+ VERSION = '1.12.2'
5
5
  end
@@ -2,40 +2,12 @@
2
2
  require 'puma'
3
3
  require 'puma/plugin'
4
4
  require 'socket'
5
- require 'nexus_semantic_logger/datadog_singleton'
5
+ require 'nexus_semantic_logger'
6
6
 
7
7
  # Forked from puma-plugin-statsd.
8
8
  # Uses the same datadog settings as nexus_semantic_logger.
9
9
  # To use, add to puma.rb:
10
10
  # plugin :nexus_puma_statsd
11
- class StatsdConnector
12
- STATSD_TYPES = { count: 'c', gauge: 'g' }
13
- METRIC_DELIMETER = "."
14
-
15
- attr_reader :host, :port
16
-
17
- def initialize
18
- @host = ENV.fetch('DD_AGENT_HOST', '127.0.0.1')
19
- @port = ENV.fetch('DD_STATSD_PORT', 8125)
20
- @socket_path = ENV.fetch('DD_STATSD_SOCKET_PATH', '')
21
- end
22
-
23
- def send(metric_name:, value:, type:, tags: nil)
24
- data = "#{metric_name}:#{value}|#{STATSD_TYPES.fetch(type)}"
25
- data = "#{data}|##{tags}" unless tags.nil?
26
-
27
- if @socket_path.to_s.strip.empty?
28
- socket = UDPSocket.new
29
- socket.send(data, 0, host, port)
30
- else
31
- socket = Socket.new(Socket::AF_UNIX, Socket::SOCK_DGRAM)
32
- socket.connect(Socket.pack_sockaddr_un(@socket_path))
33
- socket.sendmsg_nonblock(data)
34
- end
35
- ensure
36
- socket.close
37
- end
38
- end
39
11
 
40
12
  # Wrap puma's stats in a safe API
41
13
  class PumaStats
@@ -111,14 +83,7 @@ Puma::Plugin.create do
111
83
  @launcher.events
112
84
  end
113
85
 
114
- @statsd = ::StatsdConnector.new
115
- @log_writer.debug("statsd: enabled (host: #{@statsd.host})")
116
-
117
- # Fetch global metric prefix from env variable
118
- @metric_prefix = ENV.fetch('DD_STATSD_METRIC_PREFIX', nil)
119
- if @metric_prefix && !@metric_prefix.end_with?(::StatsdConnector::METRIC_DELIMETER)
120
- @metric_prefix += ::StatsdConnector::METRIC_DELIMETER
121
- end
86
+ @log_writer.debug('statsd: enabled')
122
87
 
123
88
  register_hooks
124
89
  end
@@ -137,8 +102,6 @@ Puma::Plugin.create do
137
102
  # Examples: simple-tag-0 tag-key-1:tag-value-1
138
103
  #
139
104
  tags = []
140
- global_tags = NexusSemanticLogger::DatadogSingleton.instance.global_tags
141
- tags += global_tags unless global_tags.nil?
142
105
 
143
106
  if ENV.key?('HOSTNAME')
144
107
  tags << "pod_name:#{ENV['HOSTNAME']}"
@@ -179,15 +142,9 @@ Puma::Plugin.create do
179
142
  tags << "dd.internal.entity_id:#{ENV['DD_ENTITY_ID']}"
180
143
  end
181
144
 
182
- # Return nil if we have no environment variable tags. This way we don't
183
- # send an unnecessary '|' on the end of each stat
184
145
  return nil if tags.empty?
185
146
 
186
- tags.join(",")
187
- end
188
-
189
- def prefixed_metric_name(puma_metric)
190
- "#{@metric_prefix}#{puma_metric}"
147
+ tags
191
148
  end
192
149
 
193
150
  # Send data to statsd every few seconds
@@ -196,24 +153,19 @@ Puma::Plugin.create do
196
153
 
197
154
  sleep(5)
198
155
  loop do
199
- @log_writer.debug("statsd: notify statsd")
156
+ @log_writer.debug('statsd: notify statsd')
200
157
  begin
201
158
  stats = ::PumaStats.new(Puma.stats_hash)
202
- @statsd.send(metric_name: prefixed_metric_name("puma.workers"), value: stats.workers, type: :gauge, tags: tags)
203
- @statsd.send(metric_name: prefixed_metric_name("puma.booted_workers"), value: stats.booted_workers,
204
- type: :gauge, tags: tags)
205
- @statsd.send(metric_name: prefixed_metric_name("puma.old_workers"), value: stats.old_workers, type: :gauge,
206
- tags: tags)
207
- @statsd.send(metric_name: prefixed_metric_name("puma.running"), value: stats.running, type: :gauge, tags: tags)
208
- @statsd.send(metric_name: prefixed_metric_name("puma.backlog"), value: stats.backlog, type: :gauge, tags: tags)
209
- @statsd.send(metric_name: prefixed_metric_name("puma.pool_capacity"), value: stats.pool_capacity, type: :gauge,
210
- tags: tags)
211
- @statsd.send(metric_name: prefixed_metric_name("puma.max_threads"), value: stats.max_threads, type: :gauge,
212
- tags: tags)
213
- @statsd.send(metric_name: prefixed_metric_name("puma.requests_count"), value: stats.requests_count,
214
- type: :gauge, tags: tags)
159
+ NexusSemanticLogger.metrics.gauge('puma.workers', stats.workers, tags: tags)
160
+ NexusSemanticLogger.metrics.gauge('puma.booted_workers', stats.booted_workers, tags: tags)
161
+ NexusSemanticLogger.metrics.gauge('puma.old_workers', stats.old_workers, tags: tags)
162
+ NexusSemanticLogger.metrics.gauge('puma.running', stats.running, tags: tags)
163
+ NexusSemanticLogger.metrics.gauge('puma.backlog', stats.backlog, tags: tags)
164
+ NexusSemanticLogger.metrics.gauge('puma.pool_capacity', stats.pool_capacity, tags: tags)
165
+ NexusSemanticLogger.metrics.gauge('puma.max_threads', stats.max_threads, tags: tags)
166
+ NexusSemanticLogger.metrics.gauge('puma.requests_count', stats.requests_count, tags: tags)
215
167
  rescue StandardError => e
216
- @log_writer.unknown_error(e, nil, "! statsd: notify stats failed")
168
+ @log_writer.unknown_error(e, nil, '! statsd: notify stats failed')
217
169
  ensure
218
170
  sleep(2)
219
171
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: nexus_semantic_logger
3
3
  version: !ruby/object:Gem::Version
4
- version: '1.12'
4
+ version: 1.12.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Johnathon Harris
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2023-05-24 00:00:00.000000000 Z
11
+ date: 2023-07-19 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: amazing_print
@@ -137,7 +137,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
137
137
  - !ruby/object:Gem::Version
138
138
  version: '0'
139
139
  requirements: []
140
- rubygems_version: 3.4.13
140
+ rubygems_version: 3.4.17
141
141
  signing_key:
142
142
  specification_version: 4
143
143
  summary: semantic_logger usage for nexus