instana 1.12.0 → 1.193.3.pre1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (131) hide show
  1. checksums.yaml +4 -4
  2. data/.circleci/config.yml +194 -133
  3. data/.editorconfig +10 -0
  4. data/.gitignore +2 -0
  5. data/.rubocop.yml +5 -1147
  6. data/.rubocop_todo.yml +1140 -0
  7. data/Appraisals +96 -0
  8. data/Gemfile +11 -32
  9. data/Rakefile +15 -49
  10. data/bin/console +5 -4
  11. data/gemfiles/.bundle/config +1 -2
  12. data/gemfiles/cuba_30.gemfile +14 -0
  13. data/gemfiles/dalli_20.gemfile +14 -0
  14. data/gemfiles/excon_02.gemfile +14 -0
  15. data/gemfiles/graphql_10.gemfile +14 -0
  16. data/gemfiles/grpc_10.gemfile +14 -0
  17. data/gemfiles/net_http_01.gemfile +13 -0
  18. data/gemfiles/rack_16.gemfile +14 -0
  19. data/gemfiles/rack_20.gemfile +14 -0
  20. data/gemfiles/rails_42.gemfile +16 -0
  21. data/gemfiles/rails_50.gemfile +16 -0
  22. data/gemfiles/rails_52.gemfile +16 -0
  23. data/gemfiles/rails_60.gemfile +16 -0
  24. data/gemfiles/redis_40.gemfile +14 -0
  25. data/gemfiles/resque_122.gemfile +15 -0
  26. data/gemfiles/resque_20.gemfile +14 -0
  27. data/gemfiles/rest_client_16.gemfile +14 -0
  28. data/gemfiles/rest_client_20.gemfile +14 -0
  29. data/gemfiles/roda_20.gemfile +14 -0
  30. data/gemfiles/roda_30.gemfile +14 -0
  31. data/gemfiles/sidekiq_42.gemfile +14 -0
  32. data/gemfiles/sidekiq_50.gemfile +14 -0
  33. data/gemfiles/sinatra_14.gemfile +14 -0
  34. data/instana.gemspec +1 -7
  35. data/lib/instana/activator.rb +41 -0
  36. data/lib/instana/activators/cuba.rb +18 -0
  37. data/lib/instana/activators/dalli.rb +20 -0
  38. data/lib/instana/activators/excon.rb +17 -0
  39. data/lib/instana/activators/graphql.rb +19 -0
  40. data/lib/instana/activators/grpc_client.rb +17 -0
  41. data/lib/instana/activators/grpc_server.rb +17 -0
  42. data/lib/instana/activators/net_http.rb +17 -0
  43. data/lib/instana/activators/rack.rb +13 -0
  44. data/lib/instana/activators/rails.rb +15 -0
  45. data/lib/instana/activators/redis.rb +17 -0
  46. data/lib/instana/activators/resque_client.rb +18 -0
  47. data/lib/instana/activators/resque_worker.rb +30 -0
  48. data/lib/instana/activators/rest_client.rb +17 -0
  49. data/lib/instana/activators/roda.rb +18 -0
  50. data/lib/instana/activators/sidekiq_client.rb +21 -0
  51. data/lib/instana/activators/sidekiq_worker.rb +21 -0
  52. data/lib/instana/activators/sinatra.rb +18 -0
  53. data/lib/instana/agent.rb +6 -0
  54. data/lib/instana/base.rb +2 -0
  55. data/lib/instana/config.rb +11 -0
  56. data/lib/instana/frameworks/cuba.rb +6 -13
  57. data/lib/instana/frameworks/instrumentation/abstract_mysql_adapter.rb +5 -7
  58. data/lib/instana/frameworks/instrumentation/action_view.rb +6 -10
  59. data/lib/instana/frameworks/instrumentation/active_record.rb +4 -4
  60. data/lib/instana/frameworks/instrumentation/mysql2_adapter.rb +17 -15
  61. data/lib/instana/frameworks/instrumentation/mysql_adapter.rb +11 -7
  62. data/lib/instana/frameworks/instrumentation/postgresql_adapter.rb +33 -19
  63. data/lib/instana/frameworks/rails.rb +0 -1
  64. data/lib/instana/frameworks/roda.rb +7 -13
  65. data/lib/instana/frameworks/sinatra.rb +1 -8
  66. data/lib/instana/instrumentation/dalli.rb +7 -18
  67. data/lib/instana/instrumentation/excon.rb +51 -57
  68. data/lib/instana/instrumentation/graphql.rb +75 -0
  69. data/lib/instana/instrumentation/grpc.rb +70 -66
  70. data/lib/instana/instrumentation/instrumented_request.rb +69 -0
  71. data/lib/instana/instrumentation/net-http.rb +43 -45
  72. data/lib/instana/instrumentation/rack.rb +14 -53
  73. data/lib/instana/instrumentation/redis.rb +10 -18
  74. data/lib/instana/instrumentation/resque.rb +14 -50
  75. data/lib/instana/instrumentation/rest-client.rb +2 -17
  76. data/lib/instana/instrumentation/sidekiq-client.rb +0 -9
  77. data/lib/instana/instrumentation/sidekiq-worker.rb +0 -9
  78. data/lib/instana/secrets.rb +42 -0
  79. data/lib/instana/setup.rb +4 -1
  80. data/lib/instana/tracer.rb +6 -0
  81. data/lib/instana/tracing/span.rb +14 -10
  82. data/lib/instana/util.rb +15 -69
  83. data/lib/instana/version.rb +1 -1
  84. data/test/activator_test.rb +27 -0
  85. data/test/config_test.rb +1 -17
  86. data/test/frameworks/cuba_test.rb +38 -40
  87. data/test/frameworks/rails/activerecord_test.rb +86 -34
  88. data/test/frameworks/rails_test.rb +15 -0
  89. data/test/frameworks/roda_test.rb +39 -41
  90. data/test/frameworks/sinatra_test.rb +61 -64
  91. data/test/instrumentation/excon_test.rb +1 -2
  92. data/test/instrumentation/graphql_test.rb +116 -0
  93. data/test/instrumentation/grpc_test.rb +2 -1
  94. data/test/instrumentation/{net-http_test.rb → net_http_test.rb} +1 -0
  95. data/test/instrumentation/rack_instrumented_request_test.rb +84 -0
  96. data/test/{frameworks → instrumentation}/rack_test.rb +32 -20
  97. data/test/instrumentation/redis_test.rb +10 -0
  98. data/test/instrumentation/resque_test.rb +3 -11
  99. data/test/instrumentation/{rest-client_test.rb → rest_client_test.rb} +6 -0
  100. data/test/instrumentation/sidekiq-client_test.rb +1 -0
  101. data/test/instrumentation/sidekiq-worker_test.rb +1 -0
  102. data/test/secrets_test.rb +73 -0
  103. data/test/{servers/grpc_50051.rb → support/apps/grpc/boot.rb} +1 -1
  104. data/test/{apps → support/apps/grpc}/grpc_server.rb +0 -0
  105. data/test/{servers/rackapp_6511.rb → support/apps/http_endpoint/boot.rb} +0 -0
  106. data/test/{servers/rails_3205.rb → support/apps/rails/boot.rb} +8 -14
  107. data/test/{models → support/apps/rails/models}/block.rb +1 -1
  108. data/test/{models → support/apps/rails/models}/block6.rb +0 -0
  109. data/test/support/apps/resque/boot.rb +2 -0
  110. data/test/{jobs → support/apps/resque/jobs}/resque_error_job.rb +0 -0
  111. data/test/{jobs → support/apps/resque/jobs}/resque_fast_job.rb +0 -0
  112. data/test/support/apps/sidekiq/boot.rb +21 -0
  113. data/test/{jobs → support/apps/sidekiq/jobs}/sidekiq_job_1.rb +0 -0
  114. data/test/{jobs → support/apps/sidekiq/jobs}/sidekiq_job_2.rb +0 -0
  115. data/test/{servers → support/apps}/sidekiq/worker.rb +2 -2
  116. data/test/support/helpers.rb +94 -0
  117. data/test/test_helper.rb +24 -144
  118. data/test/tracing/id_management_test.rb +4 -66
  119. metadata +98 -80
  120. data/gemfiles/libraries.gemfile +0 -71
  121. data/gemfiles/rails32.gemfile +0 -51
  122. data/gemfiles/rails42.gemfile +0 -50
  123. data/gemfiles/rails50.gemfile +0 -52
  124. data/gemfiles/rails52.gemfile +0 -52
  125. data/gemfiles/rails60.gemfile +0 -72
  126. data/lib/instana/instrumentation.rb +0 -25
  127. data/lib/instana/test.rb +0 -43
  128. data/test/apps/cuba.rb +0 -19
  129. data/test/apps/roda.rb +0 -13
  130. data/test/apps/sinatra.rb +0 -9
  131. data/test/servers/helpers/sidekiq_worker_initializer.rb +0 -27
@@ -1,7 +1,6 @@
1
1
  require "instana/rack"
2
2
 
3
3
  if defined?(::Rails)
4
-
5
4
  if ::Rails::VERSION::MAJOR < 3
6
5
  ::Rails.configuration.after_initialize do
7
6
  # In Rails, let's use the Rails logger
@@ -4,11 +4,11 @@ module Instana
4
4
  module RodaPathTemplateExtractor
5
5
  module RequestMethods
6
6
  TERM = defined?(::Roda) ? ::Roda::RodaPlugins::Base::RequestMethods::TERM : Object
7
-
7
+
8
8
  def if_match(args, &blk)
9
9
  path = @remaining_path
10
10
  captures = @captures.clear
11
-
11
+
12
12
  if match_all(args)
13
13
  (env['INSTANA_PATH_TEMPLATE_FRAGMENTS'] ||= []).concat(named_args(args, blk))
14
14
  block_result(blk.(*captures))
@@ -21,10 +21,10 @@ module Instana
21
21
  false
22
22
  end
23
23
  end
24
-
25
- def named_args(args, blk)
26
- parameters = blk.parameters
27
- args.map do |a|
24
+
25
+ def named_args(args, blk)
26
+ parameters = blk.parameters
27
+ args.map do |a|
28
28
  case a
29
29
  when String
30
30
  a
@@ -35,13 +35,7 @@ module Instana
35
35
  "{#{name}}"
36
36
  end
37
37
  end.compact
38
- end
38
+ end
39
39
  end
40
40
  end
41
41
  end
42
-
43
- if defined?(::Roda)
44
- ::Instana.logger.debug "Instrumenting Roda"
45
- Roda.use ::Instana::Rack
46
- Roda.plugin ::Instana::RodaPathTemplateExtractor
47
- end
@@ -6,10 +6,9 @@ require "instana/rack"
6
6
  module Instana
7
7
  module SinatraPathTemplateExtractor
8
8
  def self.extended(base)
9
- ::Instana.logger.debug "#{base} extended #{self}"
10
9
  base.store_path_template
11
10
  end
12
-
11
+
13
12
  def store_path_template
14
13
  after do
15
14
  @env["INSTANA_HTTP_PATH_TEMPLATE"] = @env["sinatra.route"]
@@ -18,9 +17,3 @@ module Instana
18
17
  end
19
18
  end
20
19
  end
21
-
22
- if defined?(::Sinatra)
23
- ::Instana.logger.debug "Instrumenting Sinatra"
24
- ::Sinatra::Base.use ::Instana::Rack
25
- ::Sinatra::Base.register ::Instana::SinatraPathTemplateExtractor
26
- end
@@ -1,15 +1,10 @@
1
1
  module Instana
2
2
  module Instrumentation
3
3
  module Dalli
4
- def self.included(klass)
5
- ::Instana::Util.method_alias(klass, :perform)
6
- ::Instana::Util.method_alias(klass, :get_multi)
7
- end
8
-
9
- def perform_with_instana(*args, &blk)
4
+ def perform(*args, &blk)
10
5
  if !::Instana.tracer.tracing? || ::Instana.tracer.tracing_span?(:memcache)
11
6
  do_skip = true
12
- return perform_without_instana(*args, &blk)
7
+ return super(*args, &blk)
13
8
  end
14
9
 
15
10
  op, key, *_opts = args
@@ -22,7 +17,7 @@ module Instana
22
17
  ::Instana.tracer.log_entry(:memcache, entry_payload)
23
18
  exit_payload = { :memcache => {} }
24
19
 
25
- result = perform_without_instana(*args, &blk)
20
+ result = super(*args, &blk)
26
21
 
27
22
  if op == :get
28
23
  exit_payload[:memcache][:hit] = result ? 1 : 0
@@ -36,7 +31,7 @@ module Instana
36
31
  ::Instana.tracer.log_exit(:memcache, exit_payload) unless do_skip
37
32
  end
38
33
 
39
- def get_multi_with_instana(*keys)
34
+ def get_multi(*keys)
40
35
  entry_payload = { :memcache => {} }
41
36
  entry_payload[:memcache][:namespace] = @options[:namespace] if @options.key?(:namespace)
42
37
  entry_payload[:memcache][:command] = :get_multi
@@ -45,7 +40,7 @@ module Instana
45
40
  ::Instana.tracer.log_entry(:memcache, entry_payload)
46
41
  exit_payload = { :memcache => {} }
47
42
 
48
- result = get_multi_without_instana(*keys)
43
+ result = super(*keys)
49
44
 
50
45
  exit_payload[:memcache][:hits] = result.length
51
46
  result
@@ -63,20 +58,14 @@ module Instana
63
58
  ::Instana::Util.method_alias(klass, :request)
64
59
  end
65
60
 
66
- def request_with_instana(op, *args)
61
+ def request(op, *args)
67
62
  if ::Instana.tracer.tracing? || ::Instana.tracer.tracing_span?(:memcache)
68
63
  info_payload = { :memcache => {} }
69
64
  info_payload[:memcache][:server] = "#{@hostname}:#{@port}"
70
65
  ::Instana.tracer.log_info(info_payload)
71
66
  end
72
- request_without_instana(op, *args)
67
+ super(op, *args)
73
68
  end
74
69
  end
75
70
  end
76
71
  end
77
-
78
- if defined?(::Dalli) && ::Instana.config[:dalli][:enabled]
79
- ::Instana.logger.debug "Instrumenting Dalli"
80
- ::Dalli::Client.send(:include, ::Instana::Instrumentation::Dalli)
81
- ::Dalli::Server.send(:include, ::Instana::Instrumentation::DalliServer)
82
- end
@@ -1,74 +1,68 @@
1
- if defined?(::Excon) && ::Instana.config[:excon][:enabled]
2
- module Instana
3
- module Instrumentation
4
- class Excon < ::Excon::Middleware::Base
5
- def request_call(datum)
6
- return @stack.request_call(datum) unless ::Instana.tracer.tracing?
1
+ module Instana
2
+ module Instrumentation
3
+ class Excon < ::Excon::Middleware::Base
4
+ def request_call(datum)
5
+ return @stack.request_call(datum) unless ::Instana.tracer.tracing?
7
6
 
8
- payload = { :http => {} }
9
- path = datum[:path].split('?').first
10
- payload[:http][:url] = "#{datum[:connection].instance_variable_get(:@socket_key)}#{path}"
11
- payload[:http][:method] = datum[:method] if datum.key?(:method)
7
+ payload = { :http => {} }
8
+ path = datum[:path].split('?').first
9
+ payload[:http][:url] = ::Instana.secrets.remove_from_query("#{datum[:connection].instance_variable_get(:@socket_key)}#{path}")
10
+ payload[:http][:method] = datum[:method] if datum.key?(:method)
12
11
 
13
- if datum[:pipeline] == true
14
- # Pass the context along in the datum so we get back on response
15
- # and can close out the async span
16
- datum[:instana_span] = ::Instana.tracer.log_async_entry(:excon, payload)
17
- t_context = datum[:instana_span].context
18
- else
19
- ::Instana.tracer.log_entry(:excon, payload)
20
- t_context = ::Instana.tracer.context
21
- end
12
+ if datum[:pipeline] == true
13
+ # Pass the context along in the datum so we get back on response
14
+ # and can close out the async span
15
+ datum[:instana_span] = ::Instana.tracer.log_async_entry(:excon, payload)
16
+ t_context = datum[:instana_span].context
17
+ else
18
+ ::Instana.tracer.log_entry(:excon, payload)
19
+ t_context = ::Instana.tracer.context
20
+ end
22
21
 
23
- # Set request headers; encode IDs as hexadecimal strings
24
- datum[:headers]['X-Instana-T'] = t_context.trace_id_header
25
- datum[:headers]['X-Instana-S'] = t_context.span_id_header
22
+ # Set request headers; encode IDs as hexadecimal strings
23
+ datum[:headers]['X-Instana-T'] = t_context.trace_id_header
24
+ datum[:headers]['X-Instana-S'] = t_context.span_id_header
26
25
 
27
- @stack.request_call(datum)
28
- end
26
+ @stack.request_call(datum)
27
+ end
29
28
 
30
- def error_call(datum)
31
- return @stack.error_call(datum) unless ::Instana.tracer.tracing?
29
+ def error_call(datum)
30
+ return @stack.error_call(datum) unless ::Instana.tracer.tracing?
32
31
 
33
- if datum[:pipeline] == true
34
- ::Instana.tracer.log_async_error(datum[:error], datum[:instana_span])
35
- else
36
- ::Instana.tracer.log_error(datum[:error])
37
- end
38
- @stack.error_call(datum)
32
+ if datum[:pipeline] == true
33
+ ::Instana.tracer.log_async_error(datum[:error], datum[:instana_span])
34
+ else
35
+ ::Instana.tracer.log_error(datum[:error])
39
36
  end
37
+ @stack.error_call(datum)
38
+ end
40
39
 
41
- def response_call(datum)
42
- # FIXME: Will connect exceptions call a response?
43
- #
44
- return @stack.response_call(datum) unless ::Instana.tracer.tracing?
40
+ def response_call(datum)
41
+ # FIXME: Will connect exceptions call a response?
42
+ #
43
+ return @stack.response_call(datum) unless ::Instana.tracer.tracing?
45
44
 
46
- result = @stack.response_call(datum)
45
+ result = @stack.response_call(datum)
47
46
 
48
- status = datum[:status]
49
- if !status && datum.key?(:response) && datum[:response].is_a?(Hash)
50
- status = datum[:response][:status]
51
- end
47
+ status = datum[:status]
48
+ if !status && datum.key?(:response) && datum[:response].is_a?(Hash)
49
+ status = datum[:response][:status]
50
+ end
52
51
 
53
- if status.between?(500, 511)
54
- # Because of the 5xx response, we flag this span as errored but
55
- # without a backtrace (no exception)
56
- ::Instana.tracer.log_error(nil)
57
- end
52
+ if status.between?(500, 511)
53
+ # Because of the 5xx response, we flag this span as errored but
54
+ # without a backtrace (no exception)
55
+ ::Instana.tracer.log_error(nil)
56
+ end
58
57
 
59
- if datum[:pipeline] == true
60
- # Pickup context of this async span from datum[:instana_span]
61
- ::Instana.tracer.log_async_exit(:excon, { :http => {:status => status } }, datum[:instana_span])
62
- else
63
- ::Instana.tracer.log_exit(:excon, { :http => {:status => status } })
64
- end
65
- result
58
+ if datum[:pipeline] == true
59
+ # Pickup context of this async span from datum[:instana_span]
60
+ ::Instana.tracer.log_async_exit(:excon, { :http => {:status => status } }, datum[:instana_span])
61
+ else
62
+ ::Instana.tracer.log_exit(:excon, { :http => {:status => status } })
66
63
  end
64
+ result
67
65
  end
68
66
  end
69
67
  end
70
-
71
- ::Instana.logger.debug "Instrumenting Excon"
72
- ::Excon.defaults[:middlewares].unshift(::Instana::Instrumentation::Excon)
73
68
  end
74
-
@@ -0,0 +1,75 @@
1
+ module Instana
2
+ module Instrumentation
3
+ class GraphqlTracing < GraphQL::Tracing::PlatformTracing
4
+ self.platform_keys = {
5
+ 'lex' => 'lex.graphql',
6
+ 'parse' => 'parse.graphql',
7
+ 'validate' => 'validate.graphql',
8
+ 'analyze_query' => 'analyze.graphql',
9
+ 'analyze_multiplex' => 'analyze.graphql',
10
+ 'execute_multiplex' => 'execute.graphql',
11
+ 'execute_query' => 'execute.graphql',
12
+ 'execute_query_lazy' => 'execute.graphql',
13
+ }
14
+
15
+ def platform_trace(platform_key, key, data)
16
+ return yield unless key == 'execute_query'
17
+ operation = data[:query].selected_operation
18
+
19
+ arguments = []
20
+ fields = []
21
+
22
+ operation.selections.each do |field|
23
+ arguments.concat(walk_fields(field, :arguments))
24
+ fields.concat(walk_fields(field, :selections))
25
+ end
26
+
27
+ payload = {
28
+ operationName: data[:query].operation_name || 'anonymous',
29
+ operationType: operation.operation_type,
30
+ arguments: grouped_fields(arguments),
31
+ fields: grouped_fields(fields),
32
+ }
33
+
34
+ begin
35
+ ::Instana.tracer.log_entry(:'graphql.server')
36
+ yield
37
+ rescue Exception => e
38
+ ::Instana.tracer.log_error(e)
39
+ raise e
40
+ ensure
41
+ ::Instana.tracer.log_exit(:'graphql.server', {graphql: payload})
42
+ end
43
+ end
44
+
45
+ def platform_field_key(type, field)
46
+ "#{type.graphql_name}.#{field.graphql_name}"
47
+ end
48
+
49
+ def platform_authorized_key(type)
50
+ "#{type.graphql_name}.authorized.graphql"
51
+ end
52
+
53
+ def platform_resolve_type_key(type)
54
+ "#{type.graphql_name}.resolve_type.graphql"
55
+ end
56
+
57
+ private
58
+
59
+ def walk_fields(parent, method)
60
+ return [] unless parent.respond_to?(method)
61
+
62
+ parent.send(method).map do |field|
63
+ [{object: parent.name, field: field.name}] + walk_fields(field, method)
64
+ end.flatten
65
+ end
66
+
67
+ def grouped_fields(fields)
68
+ fields
69
+ .group_by { |p| p[:object] }
70
+ .map { |name, p| [name, p.map { |f| f[:field] }] }
71
+ .to_h
72
+ end
73
+ end
74
+ end
75
+ end
@@ -1,84 +1,88 @@
1
1
  call_types = [:request_response, :client_streamer, :server_streamer, :bidi_streamer]
2
2
 
3
- if defined?(GRPC::ActiveCall) && ::Instana.config[:grpc][:enabled]
4
- call_types.each do |call_type|
5
- GRPC::ClientStub.class_eval <<-RUBY, __FILE__, __LINE__ + 1
6
- def #{call_type}_with_instana(method, *others, **options)
7
- kvs = { rpc: {} }
3
+ module Instana
4
+ module Instrumentation
5
+ module GRPCCientInstrumentation
6
+ CALL_TYPES = [:request_response, :client_streamer, :server_streamer, :bidi_streamer]
8
7
 
9
- unless ::Instana.tracer.tracing?
10
- return #{call_type}_without_instana(method, *others, **options)
11
- end
8
+ CALL_TYPES.each do |call_type|
9
+ define_method(call_type) do |method, *others, **options|
10
+ begin
11
+ kvs = { rpc: {} }
12
12
 
13
- kvs[:rpc][:flavor] = :grpc
14
- kvs[:rpc][:host] = @host
15
- kvs[:rpc][:call] = method
16
- kvs[:rpc][:call_type] = :#{call_type}
13
+ unless ::Instana.tracer.tracing?
14
+ return super(method, *others, **options)
15
+ end
17
16
 
18
- ::Instana.tracer.log_entry(:'rpc-client', kvs)
17
+ kvs[:rpc][:flavor] = :grpc
18
+ kvs[:rpc][:host] = @host
19
+ kvs[:rpc][:call] = method
20
+ kvs[:rpc][:call_type] = call_type
19
21
 
20
- context = ::Instana.tracer.context
21
- if context
22
- options[:metadata] = (options[:metadata] || {}).merge(
23
- 'x-instana-t' => context.trace_id_header,
24
- 'x-instana-s' => context.span_id_header
25
- )
26
- end
22
+ ::Instana.tracer.log_entry(:'rpc-client', kvs)
27
23
 
28
- #{call_type}_without_instana(method, *others, **options)
29
- rescue => e
30
- kvs[:rpc][:error] = true
31
- ::Instana.tracer.log_info(kvs)
32
- ::Instana.tracer.log_error(e)
33
- raise
34
- ensure
35
- ::Instana.tracer.log_exit(:'rpc-client', {})
36
- end
24
+ context = ::Instana.tracer.context
25
+ if context
26
+ options[:metadata] = (options[:metadata] || {}).merge(
27
+ 'x-instana-t' => context.trace_id_header,
28
+ 'x-instana-s' => context.span_id_header
29
+ )
30
+ end
37
31
 
38
- alias #{call_type}_without_instana #{call_type}
39
- alias #{call_type} #{call_type}_with_instana
40
- RUBY
32
+ super(method, *others, **options)
33
+ rescue => e
34
+ kvs[:rpc][:error] = true
35
+ ::Instana.tracer.log_info(kvs)
36
+ ::Instana.tracer.log_error(e)
37
+ raise
38
+ ensure
39
+ ::Instana.tracer.log_exit(:'rpc-client', {})
40
+ end
41
+ end
42
+ end
43
+ end
41
44
  end
42
- ::Instana.logger.debug 'Instrumenting GRPC client'
43
45
  end
44
46
 
45
- if defined?(GRPC::RpcDesc) && ::Instana.config[:grpc][:enabled]
46
- call_types.each do |call_type|
47
- GRPC::RpcDesc.class_eval <<-RUBY, __FILE__, __LINE__ + 1
48
- def handle_#{call_type}_with_instana(active_call, mth, *others)
49
- kvs = { rpc: {} }
50
- metadata = active_call.metadata
47
+ module Instana
48
+ module Instrumentation
49
+ module GRPCServerInstrumentation
50
+ CALL_TYPES = [:request_response, :client_streamer, :server_streamer, :bidi_streamer]
51
51
 
52
- incoming_context = {}
53
- if metadata.key?('x-instana-t')
54
- incoming_context[:trace_id] = ::Instana::Util.header_to_id(metadata['x-instana-t'])
55
- incoming_context[:span_id] = ::Instana::Util.header_to_id(metadata['x-instana-s']) if metadata.key?('x-instana-s')
56
- incoming_context[:level] = metadata['x-instana-l'] if metadata.key?('x-instana-l')
57
- end
52
+ CALL_TYPES.each do |call_type|
53
+ define_method(:"handle_#{call_type}") do |active_call, mth, *others|
54
+ begin
55
+ kvs = { rpc: {} }
56
+ metadata = active_call.metadata
58
57
 
59
- kvs[:rpc][:flavor] = :grpc
60
- kvs[:rpc][:host] = Socket.gethostname
61
- kvs[:rpc][:call] = "/\#{mth.owner.service_name}/\#{name}"
62
- kvs[:rpc][:call_type] = :#{call_type}
63
- kvs[:rpc][:peer] = { address: active_call.peer }
58
+ incoming_context = {}
59
+ if metadata.key?('x-instana-t')
60
+ incoming_context[:trace_id] = ::Instana::Util.header_to_id(metadata['x-instana-t'])
61
+ incoming_context[:span_id] = ::Instana::Util.header_to_id(metadata['x-instana-s']) if metadata.key?('x-instana-s')
62
+ incoming_context[:level] = metadata['x-instana-l'] if metadata.key?('x-instana-l')
63
+ end
64
64
 
65
- ::Instana.tracer.log_start_or_continue(
66
- :'rpc-server', kvs, incoming_context
67
- )
65
+ kvs[:rpc][:flavor] = :grpc
66
+ kvs[:rpc][:host] = Socket.gethostname
67
+ kvs[:rpc][:call] = "/#{mth.owner.service_name}/#{name}"
68
+ kvs[:rpc][:call_type] = call_type
69
+ kvs[:rpc][:peer] = { address: active_call.peer }
68
70
 
69
- handle_#{call_type}_without_instana(active_call, mth, *others)
70
- rescue => e
71
- kvs[:rpc][:error] = true
72
- ::Instana.tracer.log_info(kvs)
73
- ::Instana.tracer.log_error(e)
74
- raise
75
- ensure
76
- ::Instana.tracer.log_end(:'rpc-server', {}) if ::Instana.tracer.tracing?
77
- end
71
+ ::Instana.tracer.log_start_or_continue(
72
+ :'rpc-server', kvs, incoming_context
73
+ )
78
74
 
79
- alias handle_#{call_type}_without_instana handle_#{call_type}
80
- alias handle_#{call_type} handle_#{call_type}_with_instana
81
- RUBY
75
+ super(active_call, mth, *others)
76
+ rescue => e
77
+ kvs[:rpc][:error] = true
78
+ ::Instana.tracer.log_info(kvs)
79
+ ::Instana.tracer.log_error(e)
80
+ raise
81
+ ensure
82
+ ::Instana.tracer.log_end(:'rpc-server', {}) if ::Instana.tracer.tracing?
83
+ end
84
+ end
85
+ end
86
+ end
82
87
  end
83
- ::Instana.logger.debug 'Instrumenting GRPC server'
84
88
  end