oneapm_rpm 1.3.7 → 1.4.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (74) hide show
  1. checksums.yaml +5 -13
  2. data/.gitignore +3 -2
  3. data/Gemfile +1 -1
  4. data/lib/one_apm/agent.rb +17 -10
  5. data/lib/one_apm/agent/agent/connect.rb +9 -3
  6. data/lib/one_apm/agent/agent/container_data_manager.rb +20 -3
  7. data/lib/one_apm/agent/agent/cross_app_samples_sender.rb +22 -0
  8. data/lib/one_apm/agent/agent/rule_fetcher.rb +15 -0
  9. data/lib/one_apm/agent/agent/start.rb +15 -3
  10. data/lib/one_apm/agent/agent/start_worker_thread.rb +8 -2
  11. data/lib/one_apm/agent/cross_app/cross_app_monitor.rb +7 -6
  12. data/lib/one_apm/agent/cross_app/cross_app_tracing.rb +45 -9
  13. data/lib/one_apm/agent/cross_app/cross_app_tracing_message.rb +130 -0
  14. data/lib/one_apm/agent/database/active_record_helper.rb +104 -50
  15. data/lib/one_apm/agent/datastore.rb +8 -0
  16. data/lib/one_apm/agent/datastore/metric_helper.rb +37 -6
  17. data/lib/one_apm/agent/synthetics_monitor.rb +1 -1
  18. data/lib/one_apm/collector/collector/helper.rb +5 -1
  19. data/lib/one_apm/collector/collector/http_connection.rb +16 -8
  20. data/lib/one_apm/collector/collector_service.rb +6 -4
  21. data/lib/one_apm/collector/containers/transaction_event_aggregator.rb +52 -75
  22. data/lib/one_apm/collector/containers/transaction_sampler.rb +6 -2
  23. data/lib/one_apm/configuration.rb +8 -4
  24. data/lib/one_apm/configuration/default_source.rb +24 -3
  25. data/lib/one_apm/configuration/server_source.rb +1 -1
  26. data/lib/one_apm/inst/http_clients/curb.rb +4 -1
  27. data/lib/one_apm/inst/http_clients/thrift.rb +57 -20
  28. data/lib/one_apm/inst/nosql/memcache.rb +21 -2
  29. data/lib/one_apm/inst/nosql/mongo2.rb +11 -8
  30. data/lib/one_apm/inst/nosql/redis.rb +8 -3
  31. data/lib/one_apm/inst/orm/active_record.rb +13 -7
  32. data/lib/one_apm/inst/rails4/active_record_subscriber.rb +16 -13
  33. data/lib/one_apm/inst/transaction_base.rb +0 -1
  34. data/lib/one_apm/logger/audit_logger.rb +19 -2
  35. data/lib/one_apm/manager.rb +1 -0
  36. data/lib/one_apm/probe/instance_methods.rb +0 -1
  37. data/lib/one_apm/rack/middleware_tracing.rb +1 -0
  38. data/lib/one_apm/support/event_buffer/sampled_buffer.rb +4 -0
  39. data/lib/one_apm/support/helper.rb +16 -0
  40. data/lib/one_apm/support/http_clients/curb_wrappers.rb +20 -0
  41. data/lib/one_apm/support/http_clients/excon_wrappers.rb +25 -0
  42. data/lib/one_apm/support/http_clients/httpclient_wrappers.rb +20 -0
  43. data/lib/one_apm/support/http_clients/net_http_wrappers.rb +20 -0
  44. data/lib/one_apm/support/http_clients/thrift_tracer.rb +64 -0
  45. data/lib/one_apm/support/http_clients/typhoeus_wrappers.rb +21 -1
  46. data/lib/one_apm/support/http_clients/uri_util.rb +1 -0
  47. data/lib/one_apm/support/ip.rb +15 -0
  48. data/lib/one_apm/support/rename_rules_engine.rb +39 -0
  49. data/lib/one_apm/support/rename_rules_engine/external_rename_rule.rb +84 -0
  50. data/lib/one_apm/support/rename_rules_engine/match_expression/base.rb +46 -0
  51. data/lib/one_apm/support/rename_rules_engine/match_expression/engine.rb +35 -0
  52. data/lib/one_apm/support/rename_rules_engine/match_expression/method.rb +20 -0
  53. data/lib/one_apm/support/rename_rules_engine/match_expression/split_object.rb +19 -0
  54. data/lib/one_apm/support/rename_rules_engine/match_expression/url.rb +19 -0
  55. data/lib/one_apm/support/rename_rules_engine/rename/base.rb +25 -0
  56. data/lib/one_apm/support/rename_rules_engine/rename/engine.rb +36 -0
  57. data/lib/one_apm/support/rename_rules_engine/rename/header.rb +17 -0
  58. data/lib/one_apm/support/rename_rules_engine/rename/parameter.rb +18 -0
  59. data/lib/one_apm/support/rename_rules_engine/rename/segment.rb +41 -0
  60. data/lib/one_apm/transaction.rb +11 -2
  61. data/lib/one_apm/transaction/class_methods.rb +3 -3
  62. data/lib/one_apm/transaction/event_analytic_data.rb +51 -0
  63. data/lib/one_apm/transaction/event_analytic_sample.rb +35 -0
  64. data/lib/one_apm/transaction/instance_helpers.rb +1 -1
  65. data/lib/one_apm/transaction/sample_buffer/cross_sample_buffer.rb +47 -0
  66. data/lib/one_apm/transaction/segment.rb +2 -1
  67. data/lib/one_apm/transaction/transaction_finish_append.rb +4 -0
  68. data/lib/one_apm/transaction/transaction_name.rb +1 -1
  69. data/lib/one_apm/transaction/transaction_sample.rb +12 -7
  70. data/lib/one_apm/version.rb +2 -2
  71. data/lib/sequel/extensions/oneapm_instrumentation.rb +16 -9
  72. data/lib/sequel/plugins/oneapm_instrumentation.rb +3 -2
  73. data/oneapm_rpm.gemspec +2 -1
  74. metadata +41 -21
@@ -0,0 +1,17 @@
1
+ # encoding: utf-8
2
+
3
+ module OneApm
4
+ module Support
5
+ class RenameRulesEngine
6
+ module Rename
7
+ class Header < Base
8
+
9
+ def segment(request)
10
+ return '' if !state
11
+ request[value].nil?? request[value.to_sym] : request[value]
12
+ end
13
+ end
14
+ end
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,18 @@
1
+ # encoding: utf-8
2
+
3
+ module OneApm
4
+ module Support
5
+ class RenameRulesEngine
6
+ module Rename
7
+ class Parameter < Base
8
+
9
+ def segment(request)
10
+ return '' if !state
11
+ parameters = request.params
12
+ parameters[value].try(&:first)
13
+ end
14
+ end
15
+ end
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,41 @@
1
+ # encoding: utf-8
2
+
3
+ module OneApm
4
+ module Support
5
+ class RenameRulesEngine
6
+ module Rename
7
+ class Segment < Base
8
+
9
+ OA_RENAME_SEM_FIRST_MARK = 'first_'.freeze
10
+ OA_RENAME_SEM_LAST_MARK = 'last_'.freeze
11
+ OA_RENAME_SEM_VALUE_SEPARATOR = ','.freeze
12
+ OA_RENAME_SEM_REGEX = /\d+,*/
13
+ OA_RENAME_SEPARATOR = '/'.freeze
14
+
15
+ def segment(request)
16
+ return '' if !state || value.nil?
17
+ separated_path = request.uri.path.split(OA_RENAME_SEPARATOR)
18
+ separated_path.delete('')
19
+ if value.start_with?(OA_RENAME_SEM_FIRST_MARK)
20
+
21
+ to = (value.gsub(OA_RENAME_SEM_FIRST_MARK,'').to_i - 1)
22
+ separated_path[0..to].join(OA_RENAME_SEPARATOR)
23
+
24
+ elsif value.start_with?(OA_RENAME_SEM_LAST_MARK)
25
+ from = (value.gsub(OA_RENAME_SEM_LAST_MARK,'').to_i)
26
+ from = separated_path.size if from > separated_path.size
27
+ from = -from
28
+ separated_path[from..-1].join(OA_RENAME_SEPARATOR)
29
+
30
+ elsif OA_RENAME_SEM_REGEX.match(value)
31
+
32
+ value_at = value.split(OA_RENAME_SEM_VALUE_SEPARATOR)
33
+ value_at.map{|at| separated_path.at(at.to_i-1)}.join(OA_RENAME_SEPARATOR)
34
+
35
+ end
36
+ end
37
+ end
38
+ end
39
+ end
40
+ end
41
+ end
@@ -39,6 +39,7 @@ module OneApm
39
39
 
40
40
  attr_reader :database_metric_name,
41
41
  :guid,
42
+ :frozen_name,
42
43
  :metrics,
43
44
  :gc_start_snapshot,
44
45
  :category,
@@ -64,7 +65,7 @@ module OneApm
64
65
  @request = options[:request]
65
66
  @exceptions = {}
66
67
  @metrics = TransactionMetrics.new
67
- @guid = generate_guid
68
+ @guid = OneApm::Helper.generate_guid
68
69
  @cat_path_hashes = nil
69
70
 
70
71
  @ignore_this_transaction = false
@@ -147,17 +148,25 @@ module OneApm
147
148
  :start_timestamp => start_time.to_f,
148
149
  :duration => duration,
149
150
  :metrics => @metrics,
150
- :custom_params => custom_parameters
151
+ :custom_params => custom_parameters,
152
+ :guid => guid,
153
+ :request_url => reqest_url
151
154
  }
155
+
152
156
  append_cat_info(state, duration, payload)
153
157
  append_apdex_perf_zone(duration, payload)
154
158
  append_synthetics_to(state, payload)
155
159
  append_referring_transaction_guid_to(state, payload)
156
160
  append_http_response_code(payload)
161
+ append_metric_ids_to(payload)
157
162
 
158
163
  agent.events.notify(:transaction_finished, payload)
159
164
  end
160
165
 
166
+ def reqest_url
167
+ request.url rescue ''
168
+ end
169
+
161
170
  # Call this to ensure that the current transaction is not saved
162
171
  def abort_transaction!(state)
163
172
  transaction_sampler.ignore_transaction(state)
@@ -111,13 +111,13 @@ module OneApm
111
111
  # Make a safe attempt to get the URI, without the host and query string.
112
112
  def uri_from_request(req)
113
113
  approximate_uri = case
114
- when req.respond_to?(:fullpath ) then req.fullpath
115
- when req.respond_to?(:path ) then req.path
114
+ # when req.respond_to?(:fullpath ) then req.fullpath
115
+ # when req.respond_to?(:path ) then req.path
116
116
  when req.respond_to?(:request_uri) then req.request_uri
117
117
  when req.respond_to?(:uri ) then req.uri
118
118
  when req.respond_to?(:url ) then req.url
119
119
  end
120
- return approximate_uri[%r{^(https?://.*?)?(/[^?]*)}, 2] || '/' if approximate_uri
120
+ return approximate_uri[%r{^(https?://.*?)?(/[^?]*)}, 1] + approximate_uri[%r{^(https?://.*?)?(/[^?]*)}, 2] || '/' if approximate_uri
121
121
  end
122
122
 
123
123
  # Make a safe attempt to get the referer from a request object, generally successful when
@@ -0,0 +1,51 @@
1
+ # encoding: utf-8
2
+ module OneApm
3
+ class EventAnalyticData
4
+ include OneApm::Coerce
5
+ OA_ERROR_ALL_KEY = 'Errors/all'.freeze
6
+
7
+ attr_reader :start_time, :guid, :refer_guid, :trip_guid, :call_count, :duration
8
+ attr_reader :errors, :apdex, :request_url, :bytes_sent, :bytes_rec, :has_trace, :exec_time, :call_type
9
+
10
+ def initialize(payload = {})
11
+ @start_time = payload[:start_timestamp].to_f * 1000
12
+ @guid = payload[:guid]
13
+ @refer_guid = payload[:referring_transaction_guid]
14
+ @trip_guid = payload[:cat_trip_id] || @refer_guid || @guid
15
+ @call_count = payload[:call_count] || 1
16
+ @duration = payload[:duration]
17
+ @errors = error_times(payload[:metrics])
18
+ @apdex = payload[:apdex_perf_zone]
19
+ @request_url = payload[:request_url]
20
+ @bytes_sent = 0
21
+ @bytes_rec = 0
22
+ @has_trace = apdex != Transaction::OA_APDEX_S ? 1 : 0
23
+ @exec_time = payload[:duration]
24
+ @call_type = 0 # 0: syn , 1: asyn
25
+ end
26
+
27
+ def error_times(txn_metrics)
28
+ return txn_metrics[OA_ERROR_ALL_KEY].call_count if txn_metrics.has_key?(OA_ERROR_ALL_KEY) rescue 0
29
+ return 0
30
+ end
31
+
32
+ def to_collector_array(encoder = nil)
33
+ [
34
+ start_time,
35
+ guid,
36
+ refer_guid,
37
+ trip_guid,
38
+ call_count,
39
+ duration,
40
+ errors,
41
+ apdex,
42
+ request_url,
43
+ bytes_sent,
44
+ bytes_rec,
45
+ has_trace,
46
+ exec_time,
47
+ call_type
48
+ ]
49
+ end
50
+ end
51
+ end
@@ -0,0 +1,35 @@
1
+ # encoding: utf-8
2
+
3
+ module OneApm
4
+ class EventAnalyticSample
5
+ include OneApm::Coerce
6
+
7
+ attr_accessor :event_analytic_data, :guid
8
+ attr_reader :metric_id, :name, :scope
9
+
10
+ def initialize(payload = {})
11
+ @name = payload[:name]
12
+ @scope = payload[:scope] || ''
13
+ @metric_id = metrir_id_from(payload[:metric_ids] || {})
14
+ @guid = payload[:guid]
15
+ @event_analytic_data = [ OneApm::EventAnalyticData.new(payload) ]
16
+ end
17
+
18
+ def spec_name
19
+ return [metric_id] if metric_id
20
+ [-1, name, scope]
21
+ end
22
+
23
+ def metrir_id_from(metric_ids)
24
+ metric_spec = OneApm::MetricSpec.new(name, scope)
25
+ metric_ids[metric_spec]
26
+ end
27
+
28
+ def to_collector_array(encoder = nil)
29
+ [
30
+ spec_name,
31
+ event_analytic_data.map(&:to_collector_array)
32
+ ]
33
+ end
34
+ end
35
+ end
@@ -68,7 +68,7 @@ module OneApm
68
68
  HEX_DIGITS = (0..15).map{|i| i.to_s(16)}
69
69
  GUID_LENGTH = 16
70
70
 
71
- # generate a random 64 bit uuid
71
+ # generate a random 16 length uuid
72
72
  def generate_guid
73
73
  guid = ''
74
74
  GUID_LENGTH.times do |a|
@@ -0,0 +1,47 @@
1
+ # encoding: utf-8
2
+
3
+ require 'one_apm/transaction/sample_buffer/transaction_sample_buffer'
4
+
5
+ module OneApm
6
+ class Transaction
7
+ class CrossSampleBuffer < TransactionSampleBuffer
8
+
9
+ def initialize(events)
10
+ @counter = 0
11
+ @samples = []
12
+ @events = events
13
+ end
14
+
15
+ def capacity
16
+ Manager.config[:cross_sampler_count]
17
+ end
18
+
19
+ def reset_counter!
20
+ @counter = 0
21
+ end
22
+
23
+ def full?
24
+ @counter >= capacity
25
+ end
26
+
27
+ def allow_sample?(sample)
28
+ sample.params[:custom_params][:'bw.trip_id'] rescue false
29
+ end
30
+
31
+ private
32
+
33
+ # If a buffer needs to modify an added sample, override this method.
34
+ # Bounds checking, allowing samples and truncation belongs elsewhere.
35
+ def add_sample(sample)
36
+ if full?
37
+ OneApm::Manager.logger.debug "Cross sample buffer is full, counter: #{@counter} "
38
+ else
39
+ @counter += 1
40
+ OneApm::Manager.logger.debug "Cross sample buffer add sample, counter: #{@counter} "
41
+ @events.notify(:cross_sample_buffer_added, sample)
42
+ end
43
+ end
44
+
45
+ end
46
+ end
47
+ end
@@ -46,7 +46,8 @@ module OneApm
46
46
  OneApm::Helper.time_to_millis(@exit_timestamp),
47
47
  string(@metric_name),
48
48
  (@params || {}) ] +
49
- [ (@called_segments ? @called_segments.map{|s| s.to_array} : []) ]
49
+ [ (@called_segments ? @called_segments.map{|s| s.to_array} : []) ] #+
50
+ # [ '', '' ]
50
51
  end
51
52
 
52
53
  def path_string
@@ -58,6 +58,10 @@ module OneApm
58
58
  payload[:synthetics_monitor_id] = synthetics_monitor_id
59
59
  end
60
60
 
61
+ def append_metric_ids_to(payload)
62
+ payload[:metric_ids] = agent.service.metric_id_cache if agent.service
63
+ end
64
+
61
65
  def include_guid?(state, duration)
62
66
  state.is_cross_app? || is_synthetics_request?
63
67
  end
@@ -18,7 +18,7 @@ module OneApm
18
18
 
19
19
  def filter name
20
20
  encoded_name = Helper.correctly_encoded(name)
21
- encoded_name ? encoded_name.gsub("Controller", "WebTransaction") : nil
21
+ encoded_name ? encoded_name.gsub("Controller", "WebTransaction/Uri") : nil
22
22
  end
23
23
 
24
24
  def make_transaction_name(name, category = nil)
@@ -10,7 +10,6 @@ require 'one_apm/transaction/composite_segment'
10
10
  module OneApm
11
11
  # the number of segments that need to exist before we roll them up
12
12
  # into one segment with multiple executions
13
- COLLAPSE_SEGMENTS_THRESHOLD = 2
14
13
 
15
14
  class TransactionSample
16
15
  include TransactionAnalysis
@@ -59,15 +58,16 @@ module OneApm
59
58
  include OneApm::Coerce
60
59
 
61
60
  def to_array
62
- [ float(@start_time),
61
+ [ OneApm::Helper.time_to_millis(@start_time),
63
62
  @params[:request_params],
64
63
  @params[:custom_params],
65
- @root_segment.to_array ]
64
+ @root_segment.to_array]
66
65
  end
67
66
 
68
67
  def to_collector_array(encoder)
68
+ # trace_tree = OneApm::Helper.obfuscator.obfuscate(OneApm::JSONWrapper.dump([self.to_array]))
69
69
  trace_tree = encoder.encode(self.to_array)
70
- [ Helper.time_to_millis(@start_time),
70
+ trace_array = [ Helper.time_to_millis(@start_time),
71
71
  Helper.time_to_millis(duration),
72
72
  string(transaction_name),
73
73
  string(@params[:uri]),
@@ -75,9 +75,14 @@ module OneApm
75
75
  string(@guid),
76
76
  nil,
77
77
  forced?,
78
- int_or_nil(xray_session_id),
79
- string(synthetics_resource_id)
80
- ]
78
+ trip_guid
79
+ ]
80
+ return trace_array unless int_or_nil(xray_session_id)
81
+ trace_array + [ int_or_nil(xray_session_id) ]
82
+ end
83
+
84
+ def trip_guid
85
+ string(params[:custom_params][:'bw.trip_id'] || @guid) rescue string(@guid)
81
86
  end
82
87
 
83
88
  def path_string
@@ -4,8 +4,8 @@ module OneApm
4
4
  module VERSION
5
5
 
6
6
  MAJOR = 1
7
- MINOR = 3
8
- TINY = 7
7
+ MINOR = 4
8
+ TINY = 0
9
9
 
10
10
  STRING = [MAJOR, MINOR, TINY].compact.join('.')
11
11
 
@@ -29,16 +29,18 @@ module Sequel
29
29
  return rval
30
30
  end
31
31
 
32
+ def generate_metrics(operation, config = {})
33
+ @product ||= database_info(config)
34
+ metric_for(@product, operation).compact
35
+ end
36
+
32
37
  # Record metrics for the specified +sql+ and +args+ using the specified
33
38
  # +duration+.
34
39
  def record_metrics(sql, args, duration)
35
- primary_metric = primary_metric_for(sql, args)
36
- engine = OneApm::Manager.agent.stats_engine
37
-
38
- metrics = rollup_metrics_for(primary_metric)
39
- metrics << remote_service_metric(*self.opts.values_at(:adapter, :host)) if self.opts.key?(:adapter)
40
-
41
- engine.tl_record_scoped_and_unscoped_metrics(primary_metric, metrics, duration)
40
+ operation = operator_metric_for(sql, args)
41
+ engine = OneApm::Manager.agent.stats_engine
42
+ base, *other_metrics = generate_metrics(operation, self.opts)
43
+ engine.tl_record_scoped_and_unscoped_metrics(base, other_metrics, duration)
42
44
  end
43
45
 
44
46
  THREAD_SAFE_CONNECTION_POOL_CLASSES = [
@@ -48,7 +50,8 @@ module Sequel
48
50
  # Record the given +sql+ within a new frame, using the given +start+ and
49
51
  # +finish+ times.
50
52
  def notice_sql(state, sql, args, start, finish)
51
- metric = primary_metric_for(sql, args)
53
+ operation = operator_metric_for(sql, args)
54
+ base, *other_metrics = generate_metrics(operation, self.opts)
52
55
  agent = OneApm::Manager.agent
53
56
  duration = finish - start
54
57
  stack = state.traced_method_stack
@@ -64,7 +67,7 @@ module Sequel
64
67
  end
65
68
  end
66
69
  agent.transaction_sampler.notice_sql(sql, self.opts, duration, state, &explainer)
67
- agent.sql_sampler.notice_sql(sql, metric, self.opts, duration, state, &explainer)
70
+ agent.sql_sampler.notice_sql(sql, base, self.opts, duration, state, &explainer)
68
71
  ensure
69
72
  stack.pop_frame(state, frame, metric, finish)
70
73
  end
@@ -76,6 +79,10 @@ module Sequel
76
79
  return metric_for_sql(OneApm::Helper.correctly_encoded(sql))
77
80
  end
78
81
 
82
+ def operator_metric_for(sql, _)
83
+ return operator_for_sql(OneApm::Helper.correctly_encoded(sql))
84
+ end
85
+
79
86
  end # module OneApmInstrumentation
80
87
 
81
88
  OneApm::Manager.logger.debug "Registering the :oneapm_instrumentation extension."
@@ -12,8 +12,9 @@ module Sequel
12
12
  def make_tracer_method( opname, options )
13
13
  body = Proc.new do |*args, &block|
14
14
  classname = self.is_a?( Class ) ? self.name : self.class.name
15
- metric = "Database/%s/%s" % [ classname, opname ]
16
- trace_execution_scoped( metric, options ) do
15
+ op_name = OneApm::Agent::Instrumentation::ActiveRecordHelper.rename_for(opname)
16
+ metrics = OneApm::Agent::Instrumentation::ActiveRecordHelper.metric_for(classname, op_name).compact
17
+ trace_execution_scoped( metrics, options ) do
17
18
  super( *args, &block )
18
19
  end
19
20
  end
@@ -33,7 +33,8 @@ Gem::Specification.new do |s|
33
33
  s.add_development_dependency 'simplecov', '= 0.10.0'
34
34
  s.add_development_dependency 'mocha', '~> 0.13.0'
35
35
  s.add_development_dependency 'rails', '~> 3.2'
36
- s.add_development_dependency 'pry', '~> 0.9.12'
36
+ s.add_development_dependency 'pry', '~> 0.10.3'
37
+
37
38
  s.add_development_dependency 'hometown', '~> 0.2.5'
38
39
 
39
40
  if RUBY_VERSION >= '1.9.3'