|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +module Datadog |
| 4 | + module DI |
| 5 | + # Contains local and instance variables used when evaluating |
| 6 | + # expressions in DI Expression Language. |
| 7 | + # |
| 8 | + # @api private |
| 9 | + class Context |
| 10 | + def initialize(probe:, settings:, serializer:, locals: nil, |
| 11 | + # In Ruby everything is a method, therefore we should always have |
| 12 | + # a target self. However, if we are not capturing a snapshot, |
| 13 | + # there is no need to pass in the target self. |
| 14 | + target_self: nil, |
| 15 | + path: nil, caller_locations: nil, |
| 16 | + serialized_entry_args: nil, |
| 17 | + return_value: nil, duration: nil, exception: nil) |
| 18 | + @probe = probe |
| 19 | + @settings = settings |
| 20 | + @serializer = serializer |
| 21 | + @locals = locals |
| 22 | + @target_self = target_self |
| 23 | + @path = path |
| 24 | + @caller_locations = caller_locations |
| 25 | + @serialized_entry_args = serialized_entry_args |
| 26 | + @return_value = return_value |
| 27 | + @duration = duration |
| 28 | + @exception = exception |
| 29 | + end |
| 30 | + |
| 31 | + attr_reader :probe |
| 32 | + attr_reader :settings |
| 33 | + attr_reader :serializer |
| 34 | + attr_reader :locals |
| 35 | + attr_reader :target_self |
| 36 | + # Actual path of the instrumented file. |
| 37 | + attr_reader :path |
| 38 | + # TODO check how many stack frames we should be keeping/sending, |
| 39 | + # this should be all frames for enriched probes and no frames for |
| 40 | + # non-enriched probes? |
| 41 | + attr_reader :caller_locations |
| 42 | + attr_reader :serialized_entry_args |
| 43 | + # Return value for the method, for a method probe |
| 44 | + attr_reader :return_value |
| 45 | + # How long the method took to execute, for a method probe |
| 46 | + attr_reader :duration |
| 47 | + # Exception raised by the method, if any, for a method probe |
| 48 | + attr_reader :exception |
| 49 | + |
| 50 | + def serialized_locals |
| 51 | + # TODO cache? |
| 52 | + locals && serializer.serialize_vars(locals, |
| 53 | + depth: probe.max_capture_depth || settings.dynamic_instrumentation.max_capture_depth, |
| 54 | + attribute_count: probe.max_capture_attribute_count || settings.dynamic_instrumentation.max_capture_attribute_count,) |
| 55 | + end |
| 56 | + |
| 57 | + def fetch(var_name) |
| 58 | + unless locals |
| 59 | + # TODO return "undefined" instead? |
| 60 | + return nil |
| 61 | + end |
| 62 | + locals[var_name.to_sym] |
| 63 | + end |
| 64 | + |
| 65 | + def fetch_ivar(var_name) |
| 66 | + target_self.instance_variable_get(var_name) |
| 67 | + end |
| 68 | + end |
| 69 | + end |
| 70 | +end |
0 commit comments