atatus 1.3.0 → 1.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (147) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +6 -0
  3. data/Gemfile +72 -22
  4. data/LICENSE +1 -1
  5. data/atatus.gemspec +2 -2
  6. data/lib/atatus.rb +76 -16
  7. data/lib/atatus/agent.rb +78 -29
  8. data/lib/atatus/central_config.rb +72 -27
  9. data/lib/atatus/central_config/cache_control.rb +18 -1
  10. data/lib/atatus/child_durations.rb +64 -0
  11. data/lib/atatus/collector/base.rb +61 -29
  12. data/lib/atatus/collector/builder.rb +46 -2
  13. data/lib/atatus/collector/hist.rb +54 -0
  14. data/lib/atatus/collector/transport.rb +41 -11
  15. data/lib/atatus/config.rb +129 -28
  16. data/lib/atatus/config/bytes.rb +17 -0
  17. data/lib/atatus/config/duration.rb +17 -0
  18. data/lib/atatus/config/options.rb +29 -9
  19. data/lib/atatus/config/regexp_list.rb +17 -0
  20. data/lib/atatus/config/wildcard_pattern_list.rb +64 -0
  21. data/lib/atatus/context.rb +32 -1
  22. data/lib/atatus/context/request.rb +17 -0
  23. data/lib/atatus/context/request/socket.rb +18 -1
  24. data/lib/atatus/context/request/url.rb +17 -0
  25. data/lib/atatus/context/response.rb +27 -2
  26. data/lib/atatus/context/user.rb +17 -0
  27. data/lib/atatus/context_builder.rb +19 -4
  28. data/lib/atatus/deprecations.rb +17 -0
  29. data/lib/atatus/error.rb +27 -0
  30. data/lib/atatus/error/exception.rb +24 -0
  31. data/lib/atatus/error/log.rb +17 -0
  32. data/lib/atatus/error_builder.rb +17 -2
  33. data/lib/atatus/grape.rb +62 -0
  34. data/lib/atatus/graphql.rb +91 -0
  35. data/lib/atatus/grpc.rb +99 -0
  36. data/lib/atatus/instrumenter.rb +135 -30
  37. data/lib/atatus/internal_error.rb +17 -0
  38. data/lib/atatus/logging.rb +17 -2
  39. data/lib/atatus/metadata.rb +17 -0
  40. data/lib/atatus/metadata/process_info.rb +17 -0
  41. data/lib/atatus/metadata/service_info.rb +21 -6
  42. data/lib/atatus/metadata/system_info.rb +22 -3
  43. data/lib/atatus/metadata/system_info/container_info.rb +49 -10
  44. data/lib/atatus/metadata/system_info/hw_info.rb +1 -1
  45. data/lib/atatus/metrics.rb +69 -27
  46. data/lib/atatus/metrics/breakdown_set.rb +31 -0
  47. data/lib/atatus/metrics/{cpu_mem.rb → cpu_mem_set.rb} +110 -63
  48. data/lib/atatus/metrics/metric.rb +140 -0
  49. data/lib/atatus/metrics/set.rb +123 -0
  50. data/lib/atatus/metrics/span_scoped_set.rb +56 -0
  51. data/lib/atatus/metrics/transaction_set.rb +26 -0
  52. data/lib/atatus/metrics/vm_set.rb +58 -0
  53. data/lib/atatus/metricset.rb +48 -4
  54. data/lib/atatus/middleware.rb +28 -8
  55. data/lib/atatus/naively_hashable.rb +17 -0
  56. data/lib/atatus/normalizers.rb +23 -9
  57. data/lib/atatus/normalizers/grape.rb +22 -0
  58. data/lib/atatus/normalizers/grape/endpoint_run.rb +65 -0
  59. data/lib/atatus/normalizers/rails.rb +27 -0
  60. data/lib/atatus/normalizers/rails/action_controller.rb +44 -0
  61. data/lib/atatus/normalizers/rails/action_mailer.rb +43 -0
  62. data/lib/atatus/normalizers/{action_view.rb → rails/action_view.rb} +17 -0
  63. data/lib/atatus/normalizers/rails/active_record.rb +80 -0
  64. data/lib/atatus/opentracing.rb +75 -42
  65. data/lib/atatus/rails.rb +29 -13
  66. data/lib/atatus/railtie.rb +19 -6
  67. data/lib/atatus/resque.rb +29 -0
  68. data/lib/atatus/sinatra.rb +53 -0
  69. data/lib/atatus/span.rb +44 -15
  70. data/lib/atatus/span/context.rb +43 -28
  71. data/lib/atatus/span/context/db.rb +43 -0
  72. data/lib/atatus/span/context/destination.rb +77 -0
  73. data/lib/atatus/span/context/http.rb +43 -0
  74. data/lib/atatus/span_helpers.rb +18 -1
  75. data/lib/atatus/spies.rb +33 -15
  76. data/lib/atatus/spies/action_dispatch.rb +27 -6
  77. data/lib/atatus/spies/delayed_job.rb +26 -5
  78. data/lib/atatus/spies/dynamo_db.rb +62 -0
  79. data/lib/atatus/spies/elasticsearch.rb +53 -7
  80. data/lib/atatus/spies/faraday.rb +54 -20
  81. data/lib/atatus/spies/http.rb +36 -6
  82. data/lib/atatus/spies/json.rb +18 -0
  83. data/lib/atatus/spies/mongo.rb +41 -10
  84. data/lib/atatus/spies/net_http.rb +52 -11
  85. data/lib/atatus/spies/rake.rb +42 -23
  86. data/lib/atatus/spies/redis.rb +17 -0
  87. data/lib/atatus/spies/resque.rb +57 -0
  88. data/lib/atatus/spies/sequel.rb +54 -17
  89. data/lib/atatus/spies/shoryuken.rb +69 -0
  90. data/lib/atatus/spies/sidekiq.rb +46 -25
  91. data/lib/atatus/spies/sinatra.rb +20 -4
  92. data/lib/atatus/spies/sneakers.rb +74 -0
  93. data/lib/atatus/spies/sucker_punch.rb +58 -0
  94. data/lib/atatus/spies/tilt.rb +20 -1
  95. data/lib/atatus/sql.rb +36 -0
  96. data/lib/atatus/sql/signature.rb +169 -0
  97. data/lib/atatus/sql/tokenizer.rb +264 -0
  98. data/lib/atatus/sql/tokens.rb +63 -0
  99. data/lib/atatus/sql_summarizer.rb +24 -6
  100. data/lib/atatus/stacktrace.rb +17 -0
  101. data/lib/atatus/stacktrace/frame.rb +17 -3
  102. data/lib/atatus/stacktrace_builder.rb +23 -3
  103. data/lib/atatus/subscriber.rb +23 -4
  104. data/lib/atatus/trace_context.rb +84 -51
  105. data/lib/atatus/trace_context/traceparent.rb +111 -0
  106. data/lib/atatus/trace_context/tracestate.rb +148 -0
  107. data/lib/atatus/transaction.rb +74 -18
  108. data/lib/atatus/transport/base.rb +44 -27
  109. data/lib/atatus/transport/connection.rb +28 -72
  110. data/lib/atatus/transport/connection/http.rb +58 -35
  111. data/lib/atatus/transport/connection/proxy_pipe.rb +24 -5
  112. data/lib/atatus/transport/filters.rb +18 -1
  113. data/lib/atatus/transport/filters/hash_sanitizer.rb +77 -0
  114. data/lib/atatus/transport/filters/secrets_filter.rb +30 -55
  115. data/lib/atatus/transport/headers.rb +83 -0
  116. data/lib/atatus/transport/serializers.rb +17 -5
  117. data/lib/atatus/transport/serializers/context_serializer.rb +30 -3
  118. data/lib/atatus/transport/serializers/error_serializer.rb +17 -2
  119. data/lib/atatus/transport/serializers/metadata_serializer.rb +44 -22
  120. data/lib/atatus/transport/serializers/metricset_serializer.rb +34 -6
  121. data/lib/atatus/transport/serializers/span_serializer.rb +47 -12
  122. data/lib/atatus/transport/serializers/transaction_serializer.rb +18 -2
  123. data/lib/atatus/transport/user_agent.rb +48 -0
  124. data/lib/atatus/transport/worker.rb +31 -7
  125. data/lib/atatus/util.rb +18 -1
  126. data/lib/atatus/util/inflector.rb +17 -0
  127. data/lib/atatus/util/lru_cache.rb +17 -0
  128. data/lib/atatus/util/throttle.rb +17 -0
  129. data/lib/atatus/version.rb +19 -1
  130. metadata +46 -26
  131. data/Rakefile +0 -19
  132. data/bench/.gitignore +0 -2
  133. data/bench/app.rb +0 -53
  134. data/bench/benchmark.rb +0 -36
  135. data/bench/report.rb +0 -55
  136. data/bench/rubyprof.rb +0 -39
  137. data/bench/stackprof.rb +0 -23
  138. data/bin/build_docs +0 -5
  139. data/bin/console +0 -15
  140. data/bin/setup +0 -8
  141. data/bin/with_framework +0 -7
  142. data/lib/atatus/metrics/vm.rb +0 -60
  143. data/lib/atatus/normalizers/action_controller.rb +0 -27
  144. data/lib/atatus/normalizers/action_mailer.rb +0 -26
  145. data/lib/atatus/normalizers/active_record.rb +0 -45
  146. data/lib/atatus/util/prefixed_logger.rb +0 -18
  147. data/vendor/.gitkeep +0 -0
@@ -1,3 +1,20 @@
1
+ # Licensed to Elasticsearch B.V. under one or more contributor
2
+ # license agreements. See the NOTICE file distributed with
3
+ # this work for additional information regarding copyright
4
+ # ownership. Elasticsearch B.V. licenses this file to you under
5
+ # the Apache License, Version 2.0 (the "License"); you may
6
+ # not use this file except in compliance with the License.
7
+ # You may obtain a copy of the License at
8
+ #
9
+ # http://www.apache.org/licenses/LICENSE-2.0
10
+ #
11
+ # Unless required by applicable law or agreed to in writing,
12
+ # software distributed under the License is distributed on an
13
+ # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14
+ # KIND, either express or implied. See the License for the
15
+ # specific language governing permissions and limitations
16
+ # under the License.
17
+
1
18
  # frozen_string_literal: true
2
19
 
3
20
  module Atatus
@@ -1,6 +1,21 @@
1
- # frozen_string_literal: true
1
+ # Licensed to Elasticsearch B.V. under one or more contributor
2
+ # license agreements. See the NOTICE file distributed with
3
+ # this work for additional information regarding copyright
4
+ # ownership. Elasticsearch B.V. licenses this file to you under
5
+ # the Apache License, Version 2.0 (the "License"); you may
6
+ # not use this file except in compliance with the License.
7
+ # You may obtain a copy of the License at
8
+ #
9
+ # http://www.apache.org/licenses/LICENSE-2.0
10
+ #
11
+ # Unless required by applicable law or agreed to in writing,
12
+ # software distributed under the License is distributed on an
13
+ # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14
+ # KIND, either express or implied. See the License for the
15
+ # specific language governing permissions and limitations
16
+ # under the License.
2
17
 
3
- require 'logger'
18
+ # frozen_string_literal: true
4
19
 
5
20
  module Atatus
6
21
  # @api private
@@ -1,3 +1,20 @@
1
+ # Licensed to Elasticsearch B.V. under one or more contributor
2
+ # license agreements. See the NOTICE file distributed with
3
+ # this work for additional information regarding copyright
4
+ # ownership. Elasticsearch B.V. licenses this file to you under
5
+ # the Apache License, Version 2.0 (the "License"); you may
6
+ # not use this file except in compliance with the License.
7
+ # You may obtain a copy of the License at
8
+ #
9
+ # http://www.apache.org/licenses/LICENSE-2.0
10
+ #
11
+ # Unless required by applicable law or agreed to in writing,
12
+ # software distributed under the License is distributed on an
13
+ # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14
+ # KIND, either express or implied. See the License for the
15
+ # specific language governing permissions and limitations
16
+ # under the License.
17
+
1
18
  # frozen_string_literal: true
2
19
 
3
20
  module Atatus
@@ -1,3 +1,20 @@
1
+ # Licensed to Elasticsearch B.V. under one or more contributor
2
+ # license agreements. See the NOTICE file distributed with
3
+ # this work for additional information regarding copyright
4
+ # ownership. Elasticsearch B.V. licenses this file to you under
5
+ # the Apache License, Version 2.0 (the "License"); you may
6
+ # not use this file except in compliance with the License.
7
+ # You may obtain a copy of the License at
8
+ #
9
+ # http://www.apache.org/licenses/LICENSE-2.0
10
+ #
11
+ # Unless required by applicable law or agreed to in writing,
12
+ # software distributed under the License is distributed on an
13
+ # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14
+ # KIND, either express or implied. See the License for the
15
+ # specific language governing permissions and limitations
16
+ # under the License.
17
+
1
18
  # frozen_string_literal: true
2
19
 
3
20
  module Atatus
@@ -1,3 +1,20 @@
1
+ # Licensed to Elasticsearch B.V. under one or more contributor
2
+ # license agreements. See the NOTICE file distributed with
3
+ # this work for additional information regarding copyright
4
+ # ownership. Elasticsearch B.V. licenses this file to you under
5
+ # the Apache License, Version 2.0 (the "License"); you may
6
+ # not use this file except in compliance with the License.
7
+ # You may obtain a copy of the License at
8
+ #
9
+ # http://www.apache.org/licenses/LICENSE-2.0
10
+ #
11
+ # Unless required by applicable law or agreed to in writing,
12
+ # software distributed under the License is distributed on an
13
+ # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14
+ # KIND, either express or implied. See the License for the
15
+ # specific language governing permissions and limitations
16
+ # under the License.
17
+
1
18
  # frozen_string_literal: true
2
19
 
3
20
  module Atatus
@@ -13,16 +30,17 @@ module Atatus
13
30
 
14
31
  attr_reader :name, :version
15
32
  end
33
+
16
34
  class Agent < Versioned; end
17
35
  class Framework < Versioned; end
18
36
  class Language < Versioned; end
19
37
  class Runtime < Versioned; end
20
38
 
21
- # rubocop:disable Metrics/MethodLength
22
39
  def initialize(config)
23
40
  @config = config
24
41
 
25
42
  @name = @config.service_name
43
+ @node_name = @config.service_node_name
26
44
  @environment = @config.environment
27
45
  @agent = Agent.new(name: 'ruby', version: VERSION)
28
46
  @framework = Framework.new(
@@ -33,14 +51,12 @@ module Atatus
33
51
  @runtime = lookup_runtime
34
52
  @version = @config.service_version || Util.git_sha
35
53
  end
36
- # rubocop:enable Metrics/MethodLength
37
54
 
38
- attr_reader :name, :environment, :agent, :framework, :language, :runtime,
39
- :version
55
+ attr_reader :name, :node_name, :environment, :agent, :framework, :language,
56
+ :runtime, :version
40
57
 
41
58
  private
42
59
 
43
- # rubocop:disable Metrics/MethodLength
44
60
  def lookup_runtime
45
61
  case RUBY_ENGINE
46
62
  when 'ruby'
@@ -55,7 +71,6 @@ module Atatus
55
71
  )
56
72
  end
57
73
  end
58
- # rubocop:enable Metrics/MethodLength
59
74
  end
60
75
  end
61
76
  end
@@ -1,3 +1,20 @@
1
+ # Licensed to Elasticsearch B.V. under one or more contributor
2
+ # license agreements. See the NOTICE file distributed with
3
+ # this work for additional information regarding copyright
4
+ # ownership. Elasticsearch B.V. licenses this file to you under
5
+ # the Apache License, Version 2.0 (the "License"); you may
6
+ # not use this file except in compliance with the License.
7
+ # You may obtain a copy of the License at
8
+ #
9
+ # http://www.apache.org/licenses/LICENSE-2.0
10
+ #
11
+ # Unless required by applicable law or agreed to in writing,
12
+ # software distributed under the License is distributed on an
13
+ # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14
+ # KIND, either express or implied. See the License for the
15
+ # specific language governing permissions and limitations
16
+ # under the License.
17
+
1
18
  # frozen_string_literal: true
2
19
 
3
20
  module Atatus
@@ -7,7 +24,7 @@ module Atatus
7
24
  def initialize(config)
8
25
  @config = config
9
26
 
10
- @hostname = @config.hostname || `hostname`.chomp
27
+ @hostname = @config.hostname || self.class.system_hostname
11
28
  @architecture = gem_platform.cpu
12
29
  @platform = gem_platform.os
13
30
 
@@ -21,11 +38,13 @@ module Atatus
21
38
 
22
39
  attr_reader :hostname, :architecture, :platform, :container, :kubernetes, :hwinfo, :osinfo
23
40
 
24
- private
25
-
26
41
  def gem_platform
27
42
  @gem_platform ||= Gem::Platform.local
28
43
  end
44
+
45
+ def self.system_hostname
46
+ @system_hostname ||= `hostname`.chomp
47
+ end
29
48
  end
30
49
  end
31
50
  end
@@ -1,3 +1,20 @@
1
+ # Licensed to Elasticsearch B.V. under one or more contributor
2
+ # license agreements. See the NOTICE file distributed with
3
+ # this work for additional information regarding copyright
4
+ # ownership. Elasticsearch B.V. licenses this file to you under
5
+ # the Apache License, Version 2.0 (the "License"); you may
6
+ # not use this file except in compliance with the License.
7
+ # You may obtain a copy of the License at
8
+ #
9
+ # http://www.apache.org/licenses/LICENSE-2.0
10
+ #
11
+ # Unless required by applicable law or agreed to in writing,
12
+ # software distributed under the License is distributed on an
13
+ # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14
+ # KIND, either express or implied. See the License for the
15
+ # specific language governing permissions and limitations
16
+ # under the License.
17
+
1
18
  # frozen_string_literal: true
2
19
 
3
20
  module Atatus
@@ -34,7 +51,6 @@ module Atatus
34
51
  end
35
52
  end
36
53
 
37
- # rubocop:disable Metrics/MethodLength
38
54
  def kubernetes
39
55
  @kubernetes =
40
56
  begin
@@ -51,7 +67,6 @@ module Atatus
51
67
  kubernetes
52
68
  end
53
69
  end
54
- # rubocop:enable Metrics/MethodLength
55
70
 
56
71
  private
57
72
 
@@ -66,12 +81,18 @@ module Atatus
66
81
  ENV.fetch('KUBERNETES_POD_UID', kubernetes_pod_uid)
67
82
  end
68
83
 
69
- CONTAINER_ID_REGEX = /^[0-9A-Fa-f]{64}$/.freeze
70
- KUBEPODS_REGEX = %r{(?:^/kubepods/[^/]+/pod([^/]+)$)|(?:^/kubepods\.slice/kubepods-[^/]+\.slice/kubepods-[^/]+-pod([^/]+)\.slice$)}.freeze # rubocop:disable Metrics/LineLength
84
+ CONTAINER_ID_REGEXES = [
85
+ %r{^[[:xdigit:]]{64}$},
86
+ %r{^[[:xdigit:]]{8}-[[:xdigit:]]{4}-[[:xdigit:]]{4}-[[:xdigit:]]{4}-[[:xdigit:]]{4,}$}
87
+ ]
88
+ KUBEPODS_REGEXES = [
89
+ %r{(?:^/kubepods[^\s]*/pod([^/]+)$)},
90
+ %r{(?:^/kubepods\.slice/kubepods-[^/]+\.slice/kubepods-[^/]+-pod([^/]+)\.slice$)}
91
+ ]
71
92
  SYSTEMD_SCOPE_SUFFIX = '.scope'
72
93
 
73
- # rubocop:disable Metrics/MethodLength, Metrics/PerceivedComplexity
74
- # rubocop:disable Metrics/CyclomaticComplexity, Metrics/AbcSize
94
+ # rubocop:disable Metrics/PerceivedComplexity
95
+ # rubocop:disable Metrics/CyclomaticComplexity
75
96
  def read_from_cgroup!
76
97
  return unless File.exist?(cgroup_path)
77
98
  IO.readlines(cgroup_path).each do |line|
@@ -103,18 +124,36 @@ module Atatus
103
124
  end
104
125
  end
105
126
 
106
- if (kubepods_match = KUBEPODS_REGEX.match(directory))
127
+ if (kubepods_match = match_kubepods(directory))
107
128
  pod_id = kubepods_match[1] || kubepods_match[2]
108
129
 
109
130
  self.container_id = container_id
110
131
  self.kubernetes_pod_uid = pod_id
111
- elsif CONTAINER_ID_REGEX.match(container_id)
132
+ elsif match_container(container_id)
112
133
  self.container_id = container_id
113
134
  end
114
135
  end
115
136
  end
116
- # rubocop:enable Metrics/MethodLength, Metrics/PerceivedComplexity
117
- # rubocop:enable Metrics/CyclomaticComplexity, Metrics/AbcSize
137
+ # rubocop:enable Metrics/PerceivedComplexity
138
+ # rubocop:enable Metrics/CyclomaticComplexity
139
+
140
+ def match_kubepods(directory)
141
+ KUBEPODS_REGEXES.each do |r|
142
+ next unless (match = r.match(directory))
143
+ return match
144
+ end
145
+
146
+ nil
147
+ end
148
+
149
+ def match_container(container_id)
150
+ CONTAINER_ID_REGEXES.each do |r|
151
+ next unless (match = r.match(container_id))
152
+ return match
153
+ end
154
+
155
+ nil
156
+ end
118
157
  end
119
158
  end
120
159
  end
@@ -66,7 +66,7 @@ module Atatus
66
66
  end
67
67
 
68
68
  LINUX_CPUINFO_PATH = '/proc/cpuinfo'.freeze
69
- PROCESSOR_COUNT_REGEX = /^processor\s*:/.freeze
69
+ PROCESSOR_COUNT_REGEX = /processor\s+:\s+([0-9.]+)/.freeze
70
70
  MODEL_NAME_REGEX = /model name.+:(.+)/.freeze
71
71
  CPU_MHZ_REGEX = /cpu MHz\s+:\s+([0-9.]+)/.freeze
72
72
 
@@ -1,14 +1,27 @@
1
- # frozen_string_literal: true
1
+ # Licensed to Elasticsearch B.V. under one or more contributor
2
+ # license agreements. See the NOTICE file distributed with
3
+ # this work for additional information regarding copyright
4
+ # ownership. Elasticsearch B.V. licenses this file to you under
5
+ # the Apache License, Version 2.0 (the "License"); you may
6
+ # not use this file except in compliance with the License.
7
+ # You may obtain a copy of the License at
8
+ #
9
+ # http://www.apache.org/licenses/LICENSE-2.0
10
+ #
11
+ # Unless required by applicable law or agreed to in writing,
12
+ # software distributed under the License is distributed on an
13
+ # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14
+ # KIND, either express or implied. See the License for the
15
+ # specific language governing permissions and limitations
16
+ # under the License.
2
17
 
3
- require 'atatus/metricset'
18
+ # frozen_string_literal: true
4
19
 
5
20
  module Atatus
6
21
  # @api private
7
22
  module Metrics
8
- MUTEX = Mutex.new
9
-
10
23
  def self.new(config, &block)
11
- Collector.new(config, &block)
24
+ Registry.new(config, &block)
12
25
  end
13
26
 
14
27
  def self.platform
@@ -16,24 +29,17 @@ module Atatus
16
29
  end
17
30
 
18
31
  # @api private
19
- class Collector
32
+ class Registry
20
33
  include Logging
21
34
 
22
35
  TIMEOUT_INTERVAL = 5 # seconds
23
36
 
24
- def initialize(config, labels: nil, &block)
37
+ def initialize(config, &block)
25
38
  @config = config
26
- @labels = labels
27
- @samplers = [CpuMem, VM].map do |kls|
28
- debug "Adding metrics collector '#{kls}'"
29
- kls.new(config)
30
- end
31
39
  @callback = block
32
40
  end
33
41
 
34
- attr_reader :config, :samplers, :callback, :labels
35
-
36
- # rubocop:disable Metrics/MethodLength
42
+ attr_reader :config, :sets, :callback
37
43
  def start
38
44
  unless config.collect_metrics?
39
45
  debug 'Skipping metrics'
@@ -42,6 +48,18 @@ module Atatus
42
48
 
43
49
  debug 'Starting metrics'
44
50
 
51
+ # Only set the @sets once, in case we stop
52
+ # and start again.
53
+ @sets ||= {
54
+ system: CpuMemSet,
55
+ # vm: VMSet,
56
+ breakdown: BreakdownSet,
57
+ transaction: TransactionSet
58
+ }.each_with_object({}) do |(key, kls), sets|
59
+ debug "Adding metrics collector '#{kls}'"
60
+ sets[key] = kls.new(config)
61
+ end
62
+
45
63
  @timer_task = Concurrent::TimerTask.execute(
46
64
  run_now: true,
47
65
  execution_interval: config.metrics_interval,
@@ -60,7 +78,6 @@ module Atatus
60
78
 
61
79
  @running = true
62
80
  end
63
- # rubocop:enable Metrics/MethodLength
64
81
 
65
82
  def stop
66
83
  return unless running?
@@ -75,24 +92,49 @@ module Atatus
75
92
  !!@running
76
93
  end
77
94
 
78
- def collect_and_send
79
- metricset = Metricset.new(labels: labels, **collect)
80
- return if metricset.empty?
95
+ def handle_forking!
96
+ # Note that ideally we would be able to check if the @timer_task died
97
+ # and restart it. You can't simply check @timer_task.running? because
98
+ # it will only return the state of the TimerTask, not whether the
99
+ # internal thread used to manage the execution interval has died.
100
+ # This is a limitation of the Concurrent::TimerTask object.
101
+ # Therefore, our only option when forked is to stop and start.
102
+ # ~estolfo
103
+ stop
104
+ start
105
+ end
81
106
 
82
- callback.call(metricset)
107
+ def get(key)
108
+ sets.fetch(key)
109
+ end
110
+
111
+ def collect_and_send
112
+ return unless @config.recording?
113
+ metricsets = collect
114
+ metricsets.compact!
115
+ metricsets.each do |m|
116
+ callback.call(m)
117
+ end
83
118
  end
84
119
 
85
120
  def collect
86
- MUTEX.synchronize do
87
- samplers.each_with_object({}) do |sampler, samples|
88
- next unless (sample = sampler.collect)
89
- samples.merge!(sample)
90
- end
121
+ sets.each_value.each_with_object([]) do |set, arr|
122
+ samples = set.collect
123
+ next unless samples
124
+ arr.concat(samples)
91
125
  end
92
126
  end
93
127
  end
94
128
  end
95
129
  end
96
130
 
97
- require 'atatus/metrics/cpu_mem'
98
- require 'atatus/metrics/vm'
131
+ require 'atatus/metricset'
132
+
133
+ require 'atatus/metrics/metric'
134
+ require 'atatus/metrics/set'
135
+
136
+ require 'atatus/metrics/cpu_mem_set'
137
+ require 'atatus/metrics/vm_set'
138
+ require 'atatus/metrics/span_scoped_set'
139
+ require 'atatus/metrics/transaction_set'
140
+ require 'atatus/metrics/breakdown_set'