graftcode-instrumentation 1.0.5

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 (26) hide show
  1. checksums.yaml +7 -0
  2. data/README.md +76 -0
  3. data/lib/graftcode/instrumentation/body_capture/command_writer.rb +62 -0
  4. data/lib/graftcode/instrumentation/body_capture/constants.rb +14 -0
  5. data/lib/graftcode/instrumentation/body_capture/string_writer.rb +39 -0
  6. data/lib/graftcode/instrumentation/body_capture/value_writer.rb +62 -0
  7. data/lib/graftcode/instrumentation/body_capture/write_context.rb +37 -0
  8. data/lib/graftcode/instrumentation/body_capture.rb +28 -0
  9. data/lib/graftcode/instrumentation/call_span/attributes.rb +35 -0
  10. data/lib/graftcode/instrumentation/call_span/constants.rb +30 -0
  11. data/lib/graftcode/instrumentation/call_span/factory.rb +65 -0
  12. data/lib/graftcode/instrumentation/call_span/trace_propagation.rb +69 -0
  13. data/lib/graftcode/instrumentation/call_span.rb +97 -0
  14. data/lib/graftcode/instrumentation/graftcode_instrumentation/log_recording.rb +81 -0
  15. data/lib/graftcode/instrumentation/graftcode_instrumentation.rb +103 -0
  16. data/lib/graftcode/instrumentation/hypertube_sdk.rb +28 -0
  17. data/lib/graftcode/instrumentation/library_catalog/command_tree.rb +26 -0
  18. data/lib/graftcode/instrumentation/library_catalog/gemspec_version.rb +40 -0
  19. data/lib/graftcode/instrumentation/library_catalog/path_helpers.rb +36 -0
  20. data/lib/graftcode/instrumentation/library_catalog/resolution.rb +67 -0
  21. data/lib/graftcode/instrumentation/library_catalog.rb +87 -0
  22. data/lib/graftcode/instrumentation/operation_name.rb +116 -0
  23. data/lib/graftcode/instrumentation/telemetry_settings.rb +62 -0
  24. data/lib/graftcode/instrumentation/version.rb +7 -0
  25. data/lib/graftcode/instrumentation.rb +21 -0
  26. metadata +191 -0
@@ -0,0 +1,81 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Graftcode
4
+ module Instrumentation
5
+ class GraftcodeInstrumentation
6
+ module LogRecording
7
+ SEVERITY_NUMBER = {
8
+ Hypertube::Utils::Logging::LogsLevel::TRACE => 1,
9
+ Hypertube::Utils::Logging::LogsLevel::DEBUG => 5,
10
+ Hypertube::Utils::Logging::LogsLevel::INFO => 9,
11
+ Hypertube::Utils::Logging::LogsLevel::WARNING => 13,
12
+ Hypertube::Utils::Logging::LogsLevel::ERROR => 17
13
+ }.freeze
14
+
15
+ private
16
+
17
+ def emit_log_record(level, message, properties, exception)
18
+ @logger.on_emit(
19
+ timestamp: Time.now,
20
+ severity_number: SEVERITY_NUMBER.fetch(level, 0),
21
+ severity_text: severity_text(level),
22
+ body: message,
23
+ attributes: log_record_attributes(properties, exception),
24
+ context: OpenTelemetry::Context.current
25
+ )
26
+ end
27
+
28
+ def record_span_event(level, message, properties, exception)
29
+ span = current_span
30
+ return unless span.recording?
31
+
32
+ if exception.nil?
33
+ span.add_event(message, attributes: base_event_attributes(level, properties))
34
+ return
35
+ end
36
+
37
+ span.add_event('exception', attributes: exception_event_attributes(level, message, properties, exception))
38
+ end
39
+
40
+ def log_record_attributes(properties, exception)
41
+ attributes = properties.to_h.dup
42
+ merge_exception_attributes!(attributes, exception)
43
+ attributes
44
+ end
45
+
46
+ def base_event_attributes(level, properties)
47
+ attributes = { 'log.level' => severity_text(level) }
48
+ properties.each { |key, value| attributes[key] = value }
49
+ attributes
50
+ end
51
+
52
+ def exception_event_attributes(level, message, properties, exception)
53
+ attributes = base_event_attributes(level, properties)
54
+ attributes['log.message'] = message
55
+ merge_exception_attributes!(attributes, exception)
56
+ attributes
57
+ end
58
+
59
+ def merge_exception_attributes!(attributes, exception)
60
+ return if exception.nil?
61
+
62
+ attributes['exception.type'] = exception.class.name
63
+ set_if_present(attributes, 'exception.message', Hypertube::Utils::Logging::LogsRedactor.redact_value(exception.message))
64
+ set_if_present(attributes, 'exception.stacktrace', Hypertube::Utils::Logging::LogsRedactor.redact_value(stack_trace(exception)))
65
+ end
66
+
67
+ def severity_text(level)
68
+ level == Hypertube::Utils::Logging::LogsLevel::INFO ? 'Information' : Hypertube::Utils::Logging::LogsLevel.name_for(level)
69
+ end
70
+
71
+ def stack_trace(exception)
72
+ exception.full_message(highlight: false, order: :top)
73
+ end
74
+
75
+ def set_if_present(attributes, key, value)
76
+ attributes[key] = value unless value.nil?
77
+ end
78
+ end
79
+ end
80
+ end
81
+ end
@@ -0,0 +1,103 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'opentelemetry-api'
4
+ require_relative 'hypertube_sdk'
5
+ require_relative 'call_span'
6
+ require_relative 'graftcode_instrumentation/log_recording'
7
+
8
+ module Graftcode
9
+ module Instrumentation
10
+ class GraftcodeInstrumentation
11
+ include Hypertube::Utils::Abstractions::IHypertubeInvocation
12
+ include Hypertube::Utils::Abstractions::IHypertubeHandling
13
+ include Hypertube::Utils::Logging::IHypertubeLogging
14
+ include LogRecording
15
+
16
+ INSTRUMENTATION_NAME = 'Graftcode'
17
+ LOGGER_NAME = 'Hypertube'
18
+
19
+ def self.attach(tracer_provider, minimum_log_level: nil, logger_provider: nil)
20
+ new(tracer_provider, minimum_log_level, logger_provider)
21
+ end
22
+
23
+ def initialize(tracer_provider, minimum_log_level = nil, logger_provider = nil)
24
+ raise ArgumentError, 'tracer_provider' if tracer_provider.nil?
25
+
26
+ @tracer = tracer_provider.tracer(INSTRUMENTATION_NAME, VERSION)
27
+ @minimum_log_level = minimum_log_level
28
+ @logger = if minimum_log_level.nil? || logger_provider.nil?
29
+ nil
30
+ else
31
+ logger_provider.logger(name: LOGGER_NAME, version: VERSION)
32
+ end
33
+
34
+ Hypertube::Utils::Abstractions::HypertubeBroker.attach(self)
35
+ Hypertube::Utils::Logging::HypertubeLogger.attach(self)
36
+ end
37
+
38
+ def close
39
+ Hypertube::Utils::Abstractions::HypertubeBroker.detach(self)
40
+ Hypertube::Utils::Logging::HypertubeLogger.detach(self)
41
+ end
42
+
43
+ def on_execute(context, command, connection_data)
44
+ reported = Hypertube::Utils::Logging::LogsCommandResolver.resolve_for_invoke(command)
45
+ span = CallSpan.start_invoke(@tracer, reported, connection_data)
46
+ context.items << span unless span.nil?
47
+ end
48
+
49
+ def on_response(context, response)
50
+ CallSpan.complete_spans(context, response, nil)
51
+ end
52
+
53
+ def on_invocation_error(context, exception)
54
+ CallSpan.complete_spans(context, nil, exception)
55
+ end
56
+
57
+ def on_process(context, command)
58
+ headers = {}
59
+ reported = Hypertube::Utils::Logging::LogsCommandResolver.resolve_for_handle(command, headers)
60
+ span = CallSpan.start_handle(@tracer, reported, headers)
61
+ context.items << span unless span.nil?
62
+ end
63
+
64
+ def on_processed(context, response)
65
+ CallSpan.complete_spans(context, response, nil)
66
+ end
67
+
68
+ def on_handling_error(context, exception)
69
+ CallSpan.complete_spans(context, nil, exception)
70
+ end
71
+
72
+ def requested?
73
+ return current_span.recording? if @minimum_log_level.nil?
74
+
75
+ @minimum_log_level != Hypertube::Utils::Logging::LogsLevel::OFF
76
+ end
77
+
78
+ def on_log(level, message, properties, exception)
79
+ return unless log_enabled?(level)
80
+
81
+ if @logger.nil?
82
+ record_span_event(level, message, properties, exception)
83
+ else
84
+ emit_log_record(level, message, properties, exception)
85
+ end
86
+ end
87
+
88
+ def log_enabled?(level)
89
+ return current_span.recording? if @minimum_log_level.nil?
90
+ return false if level == Hypertube::Utils::Logging::LogsLevel::OFF
91
+ return false if @minimum_log_level == Hypertube::Utils::Logging::LogsLevel::OFF
92
+
93
+ Hypertube::Utils::Logging::LogsLevel.at_least?(level, @minimum_log_level)
94
+ end
95
+
96
+ private
97
+
98
+ def current_span
99
+ OpenTelemetry::Trace.current_span
100
+ end
101
+ end
102
+ end
103
+ end
@@ -0,0 +1,28 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Graftcode
4
+ module Instrumentation
5
+ module HypertubeSdk
6
+ FEATURES = %w[abstractions utils/connection_type utils/runtime_name_handler].freeze
7
+ LOADED_MARKER = '/utils/abstractions/hypertube_broker.rb'
8
+
9
+ module_function
10
+
11
+ # Graftcode Gateway loads the SDK from its Binaries folder by absolute path, not as a gem.
12
+ # Requiring the gem there would fail under Bundler or load a second copy of the broker and logger.
13
+ def require_features
14
+ root = loaded_root
15
+ FEATURES.each do |feature|
16
+ root.nil? ? require("hypertube-ruby-sdk/#{feature}") : require(File.join(root, feature))
17
+ end
18
+ end
19
+
20
+ def loaded_root
21
+ broker = $LOADED_FEATURES.find { |path| path.end_with?(LOADED_MARKER) }
22
+ broker && File.expand_path('../..', File.dirname(broker))
23
+ end
24
+ end
25
+
26
+ HypertubeSdk.require_features
27
+ end
28
+ end
@@ -0,0 +1,26 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative '../hypertube_sdk'
4
+
5
+ module Graftcode
6
+ module Instrumentation
7
+ module LibraryCatalog
8
+ module CommandTree
9
+ module_function
10
+
11
+ def find(command, command_type)
12
+ matches = []
13
+ collect(command, command_type, matches)
14
+ matches
15
+ end
16
+
17
+ def collect(command, command_type, matches)
18
+ matches << command if command.command_type == command_type
19
+ command.payload.each do |item|
20
+ collect(item, command_type, matches) if item.is_a?(Hypertube::Utils::Command)
21
+ end
22
+ end
23
+ end
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,40 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Graftcode
4
+ module Instrumentation
5
+ module LibraryCatalog
6
+ module GemspecVersion
7
+ @version_by_path = {}
8
+ @version_mutex = Mutex.new
9
+
10
+ module_function
11
+
12
+ def reset!
13
+ @version_mutex.synchronize { @version_by_path = {} }
14
+ end
15
+
16
+ def read_cached(path)
17
+ @version_mutex.synchronize do
18
+ @version_by_path.fetch(path) do
19
+ @version_by_path[path] = read(path)
20
+ end
21
+ end
22
+ end
23
+
24
+ def read(path)
25
+ gemspec = if path.end_with?('.gemspec')
26
+ path
27
+ else
28
+ Dir[File.join(File.dirname(path), '*.gemspec')].first
29
+ end
30
+ return '' if gemspec.nil? || !File.file?(gemspec)
31
+
32
+ spec = Gem::Specification.load(gemspec)
33
+ spec&.version.to_s
34
+ rescue StandardError
35
+ ''
36
+ end
37
+ end
38
+ end
39
+ end
40
+ end
@@ -0,0 +1,36 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative '../hypertube_sdk'
4
+ require_relative '../operation_name'
5
+
6
+ module Graftcode
7
+ module Instrumentation
8
+ module LibraryCatalog
9
+ module_function
10
+
11
+ def resolve_library_path(command)
12
+ command.payload.reverse_each do |item|
13
+ path = as_text(item)
14
+ return path unless OperationName.blank?(path)
15
+ end
16
+ nil
17
+ end
18
+
19
+ def as_text(payload_item)
20
+ if payload_item.is_a?(Hypertube::Utils::Command) &&
21
+ payload_item.command_type == Hypertube::Utils::CommandType::VALUE &&
22
+ payload_item.payload[0].is_a?(String)
23
+ return payload_item.payload[0]
24
+ end
25
+
26
+ payload_item.is_a?(String) ? payload_item : nil
27
+ end
28
+
29
+ def file_name_without_extension(path)
30
+ name = File.basename(path)
31
+ dot = name.rindex('.')
32
+ dot.nil? || dot.zero? ? name : name[0...dot]
33
+ end
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,67 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative '../hypertube_sdk'
4
+ require_relative '../operation_name'
5
+ require_relative 'command_tree'
6
+
7
+ module Graftcode
8
+ module Instrumentation
9
+ module LibraryCatalog
10
+ module_function
11
+
12
+ def resolve_library_name(command, connection_data)
13
+ name = library_name_from_load_commands(command)
14
+ return name if name
15
+
16
+ type_name = OperationName.resolve_target_type_name(command)
17
+ return nil if OperationName.blank?(type_name)
18
+
19
+ libraries = libraries_for(connection_data)
20
+ return root_namespace(type_name) if libraries.nil?
21
+
22
+ match_library_for_type(type_name, libraries) || root_namespace(type_name)
23
+ end
24
+
25
+ def library_name_from_load_commands(command)
26
+ CommandTree.find(command, Hypertube::Utils::CommandType::LOAD_LIBRARY).each do |load|
27
+ path = resolve_library_path(load)
28
+ next if OperationName.blank?(path)
29
+
30
+ name = file_name_without_extension(path)
31
+ return name unless OperationName.blank?(name)
32
+ end
33
+ nil
34
+ end
35
+
36
+ def libraries_for(connection_data)
37
+ @libraries_mutex.synchronize { @libraries_by_connection[connection_key(connection_data)] }
38
+ end
39
+
40
+ def match_library_for_type(type_name, libraries)
41
+ libraries.each_value.reduce(nil) do |best, library|
42
+ next best unless type_of_library?(type_name, library.name)
43
+
44
+ best.nil? || library.name.length > best.length ? library.name : best
45
+ end
46
+ end
47
+
48
+ def root_namespace(type_name)
49
+ separator = type_name.index(NAMESPACE_SEPARATOR)
50
+ separator.nil? ? type_name : type_name[0...separator]
51
+ end
52
+
53
+ def type_of_library?(type_name, library_name)
54
+ type = type_name.downcase
55
+ library = library_name.downcase
56
+ type == library || type.start_with?("#{library}.", "#{library}::")
57
+ end
58
+
59
+ def lookup_version(connection_data, library)
60
+ libraries = libraries_for(connection_data)
61
+ return nil if libraries.nil?
62
+
63
+ libraries[library.downcase]&.version
64
+ end
65
+ end
66
+ end
67
+ end
@@ -0,0 +1,87 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'hypertube_sdk'
4
+ require_relative 'operation_name'
5
+ require_relative 'library_catalog/command_tree'
6
+ require_relative 'library_catalog/gemspec_version'
7
+ require_relative 'library_catalog/path_helpers'
8
+ require_relative 'library_catalog/resolution'
9
+
10
+ module Graftcode
11
+ module Instrumentation
12
+ module LibraryCatalog
13
+ @libraries_by_connection = {}
14
+ @libraries_mutex = Mutex.new
15
+
16
+ Library = Struct.new(:name, :version, keyword_init: true)
17
+ NAMESPACE_SEPARATOR = /\.|::/.freeze
18
+
19
+ CONNECTION_TYPE_NAMES = {
20
+ Hypertube::Utils::ConnectionType::IN_MEMORY => 'IN_MEMORY',
21
+ Hypertube::Utils::ConnectionType::TCP => 'TCP',
22
+ Hypertube::Utils::ConnectionType::WEB_SOCKET => 'WEB_SOCKET',
23
+ Hypertube::Utils::ConnectionType::HTTP2 => 'HTTP2',
24
+ Hypertube::Utils::ConnectionType::TCP_TLS => 'TCP_TLS',
25
+ Hypertube::Utils::ConnectionType::PLUGIN => 'PLUGIN'
26
+ }.freeze
27
+
28
+ module_function
29
+
30
+ def reset!
31
+ @libraries_mutex.synchronize { @libraries_by_connection = {} }
32
+ GemspecVersion.reset!
33
+ end
34
+
35
+ def record(command, connection_data)
36
+ CommandTree.find(command, Hypertube::Utils::CommandType::LOAD_LIBRARY).each do |load|
37
+ record_load(load, connection_data)
38
+ end
39
+ end
40
+
41
+ def describe_target(command, connection_data)
42
+ channel = channel_name(connection_data)
43
+ library = resolve_library_name(command, connection_data)
44
+
45
+ return "#{runtime_label(command.runtime_name)} #{channel}" if OperationName.blank?(library)
46
+
47
+ version = lookup_version(connection_data, library)
48
+ if OperationName.blank?(version)
49
+ "#{library} #{channel}"
50
+ else
51
+ "#{library} #{version} #{channel}"
52
+ end
53
+ end
54
+
55
+ def channel_name(connection_data)
56
+ connection_type_name(connection_data.connection_type).tr('_', '').downcase
57
+ end
58
+
59
+ def connection_type_name(connection_type)
60
+ CONNECTION_TYPE_NAMES.fetch(connection_type, 'UNKNOWN')
61
+ end
62
+
63
+ def runtime_label(runtime_name)
64
+ Hypertube::Utils::RuntimeNameHandler.get_name(runtime_name).capitalize
65
+ end
66
+
67
+ def record_load(command, connection_data)
68
+ path = resolve_library_path(command)
69
+ return if OperationName.blank?(path)
70
+
71
+ name = file_name_without_extension(path)
72
+ return if OperationName.blank?(name)
73
+
74
+ @libraries_mutex.synchronize do
75
+ key = connection_key(connection_data)
76
+ @libraries_by_connection[key] ||= {}
77
+ version = GemspecVersion.read_cached(path)
78
+ @libraries_by_connection[key][name.downcase] = Library.new(name: name, version: version)
79
+ end
80
+ end
81
+
82
+ def connection_key(connection_data)
83
+ "#{connection_data.connection_type}|#{connection_data.hostname}"
84
+ end
85
+ end
86
+ end
87
+ end
@@ -0,0 +1,116 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'hypertube_sdk'
4
+
5
+ module Graftcode
6
+ module Instrumentation
7
+ module OperationName
8
+ MAX_DEPTH = 32
9
+
10
+ MEMBER_COMMANDS = [
11
+ Hypertube::Utils::CommandType::INVOKE_STATIC_METHOD,
12
+ Hypertube::Utils::CommandType::INVOKE_INSTANCE_METHOD,
13
+ Hypertube::Utils::CommandType::INVOKE_GENERIC_STATIC_METHOD,
14
+ Hypertube::Utils::CommandType::INVOKE_GENERIC_METHOD,
15
+ Hypertube::Utils::CommandType::GET_STATIC_FIELD,
16
+ Hypertube::Utils::CommandType::SET_STATIC_FIELD,
17
+ Hypertube::Utils::CommandType::GET_INSTANCE_FIELD,
18
+ Hypertube::Utils::CommandType::SET_INSTANCE_FIELD,
19
+ Hypertube::Utils::CommandType::GET_STATIC_METHOD_AS_DELEGATE,
20
+ Hypertube::Utils::CommandType::GET_INSTANCE_METHOD_AS_DELEGATE
21
+ ].freeze
22
+
23
+ TYPE_FROM_TARGET_COMMANDS = [
24
+ Hypertube::Utils::CommandType::CREATE_CLASS_INSTANCE,
25
+ Hypertube::Utils::CommandType::SET_INSTANCE_FIELD,
26
+ Hypertube::Utils::CommandType::INVOKE_STATIC_METHOD,
27
+ Hypertube::Utils::CommandType::INVOKE_INSTANCE_METHOD,
28
+ Hypertube::Utils::CommandType::GET_STATIC_FIELD,
29
+ Hypertube::Utils::CommandType::GET_INSTANCE_FIELD
30
+ ].freeze
31
+
32
+ module_function
33
+
34
+ def resolve(command)
35
+ case command.command_type
36
+ when *MEMBER_COMMANDS
37
+ qualified = qualify(
38
+ resolve_type_name_core(payload_at(command, 0), 0),
39
+ resolve_text(payload_at(command, 1), 0)
40
+ )
41
+ qualified || fallback(command)
42
+ when Hypertube::Utils::CommandType::CREATE_CLASS_INSTANCE
43
+ created_type = resolve_type_name_core(payload_at(command, 0), 0)
44
+ created_type ? "#{created_type}.ctor" : fallback(command)
45
+ when Hypertube::Utils::CommandType::INVOKE_GLOBAL_FUNCTION,
46
+ Hypertube::Utils::CommandType::GET_GLOBAL_FIELD
47
+ resolve_text(payload_at(command, 0), 0) || fallback(command)
48
+ else
49
+ fallback(command)
50
+ end
51
+ end
52
+
53
+ def resolve_target_type_name(command)
54
+ case command.command_type
55
+ when *MEMBER_COMMANDS, Hypertube::Utils::CommandType::CREATE_CLASS_INSTANCE
56
+ resolve_type_name_core(payload_at(command, 0), 0)
57
+ when Hypertube::Utils::CommandType::GET_TYPE
58
+ resolve_type_name_core(command, 0)
59
+ end
60
+ end
61
+
62
+ def blank?(value)
63
+ value.nil? || value.to_s.strip.empty?
64
+ end
65
+
66
+ def qualify(type_name, member_name)
67
+ return nil if blank?(member_name)
68
+
69
+ blank?(type_name) ? member_name : "#{type_name}.#{member_name}"
70
+ end
71
+
72
+ def fallback(command)
73
+ type_name = Hypertube::Utils::CommandType.get_name(command.command_type)
74
+ "hypertube.#{type_name}"
75
+ end
76
+
77
+ def payload_at(command, index)
78
+ command.payload[index]
79
+ end
80
+
81
+ def resolve_text(payload_item, depth)
82
+ return nil if depth > MAX_DEPTH
83
+ return payload_item.strip if payload_item.is_a?(String) && !payload_item.strip.empty?
84
+ return nil unless payload_item.is_a?(Hypertube::Utils::Command)
85
+ return nil unless payload_item.command_type == Hypertube::Utils::CommandType::VALUE
86
+
87
+ resolve_text(payload_at(payload_item, 0), depth + 1)
88
+ end
89
+
90
+ def resolve_type_name_core(target, depth)
91
+ return nil if depth > MAX_DEPTH || target.nil?
92
+ return target.strip if target.is_a?(String) && !target.strip.empty?
93
+ return target.name if target.is_a?(Module)
94
+ return resolve_type_name_from_command(target, depth) if target.is_a?(Hypertube::Utils::Command)
95
+
96
+ target.class.name
97
+ end
98
+
99
+ def resolve_type_name_from_command(command, depth)
100
+ case command.command_type
101
+ when Hypertube::Utils::CommandType::VALUE,
102
+ *TYPE_FROM_TARGET_COMMANDS
103
+ resolve_type_name_core(payload_at(command, 0), depth + 1)
104
+ when Hypertube::Utils::CommandType::GET_TYPE,
105
+ Hypertube::Utils::CommandType::CAST,
106
+ Hypertube::Utils::CommandType::CREATE_NULL
107
+ command.payload.each do |item|
108
+ name = resolve_text(item, depth + 1)
109
+ return name unless blank?(name)
110
+ end
111
+ nil
112
+ end
113
+ end
114
+ end
115
+ end
116
+ end
@@ -0,0 +1,62 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Graftcode
4
+ module Instrumentation
5
+ module TelemetrySettings
6
+ MINIMUM_BODY_MAX_CHARS = 256
7
+ DEFAULT_BODY_MAX_CHARS = 8192
8
+ MAXIMUM_BODY_MAX_CHARS = 32_768
9
+
10
+ module_function
11
+
12
+ def auto_inject_enabled?
13
+ @auto_inject = read_flag('HYPERTUBE_TELEMETRY_AUTO_INJECT', true) if @auto_inject.nil?
14
+ @auto_inject
15
+ end
16
+
17
+ def auto_inject_enabled=(value)
18
+ @auto_inject = value
19
+ end
20
+
21
+ def body_capture_enabled?
22
+ @capture_bodies = read_flag('HYPERTUBE_TELEMETRY_CAPTURE_BODIES', false) if @capture_bodies.nil?
23
+ @capture_bodies
24
+ end
25
+
26
+ def body_capture_enabled=(value)
27
+ @capture_bodies = value
28
+ end
29
+
30
+ def body_max_characters
31
+ @body_max_characters = read_body_max_chars if @body_max_characters.nil?
32
+ @body_max_characters
33
+ end
34
+
35
+ def body_max_characters=(value)
36
+ @body_max_characters = clamp_body_max_characters(value)
37
+ end
38
+
39
+ def clamp_body_max_characters(value)
40
+ return MINIMUM_BODY_MAX_CHARS if value < MINIMUM_BODY_MAX_CHARS
41
+
42
+ [value, MAXIMUM_BODY_MAX_CHARS].min
43
+ end
44
+
45
+ def read_flag(name, default_value)
46
+ value = ENV.fetch(name, nil)&.strip&.downcase
47
+ return default_value if value.nil? || value.empty?
48
+
49
+ value != 'false' && value != '0'
50
+ end
51
+
52
+ def read_body_max_chars
53
+ raw = ENV.fetch('HYPERTUBE_TELEMETRY_BODY_MAX_CHARS', nil)
54
+ return DEFAULT_BODY_MAX_CHARS if raw.nil? || raw.strip.empty?
55
+
56
+ clamp_body_max_characters(Integer(raw))
57
+ rescue ArgumentError
58
+ DEFAULT_BODY_MAX_CHARS
59
+ end
60
+ end
61
+ end
62
+ end
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Graftcode
4
+ module Instrumentation
5
+ VERSION = '1.0.0'
6
+ end
7
+ end
@@ -0,0 +1,21 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'instrumentation/version'
4
+ require_relative 'instrumentation/telemetry_settings'
5
+ require_relative 'instrumentation/operation_name'
6
+ require_relative 'instrumentation/library_catalog'
7
+ require_relative 'instrumentation/body_capture'
8
+ require_relative 'instrumentation/call_span'
9
+ require_relative 'instrumentation/graftcode_instrumentation'
10
+
11
+ module Graftcode
12
+ module Instrumentation
13
+ def self.attach(tracer_provider, minimum_log_level: nil, logger_provider: nil)
14
+ GraftcodeInstrumentation.attach(
15
+ tracer_provider,
16
+ minimum_log_level: minimum_log_level,
17
+ logger_provider: logger_provider
18
+ )
19
+ end
20
+ end
21
+ end