Skip to content

Commit ffaf7d9

Browse files
p-datadogp
authored andcommitted
DI: lift Context out of EL (#4949)
Co-authored-by: Oleg Pudeyev <[email protected]>
1 parent 9631fc4 commit ffaf7d9

File tree

13 files changed

+174
-180
lines changed

13 files changed

+174
-180
lines changed

lib/datadog/di/boot.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
require_relative 'error'
66
require_relative 'code_tracker'
77
require_relative 'component'
8+
require_relative 'context'
89
require_relative 'instrumenter'
910
require_relative 'probe'
1011
require_relative 'probe_builder'

lib/datadog/di/context.rb

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
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

lib/datadog/di/el.rb

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,4 @@
22

33
require_relative 'el/expression'
44
require_relative 'el/compiler'
5-
require_relative 'el/context'
65
require_relative 'el/evaluator'

lib/datadog/di/el/context.rb

Lines changed: 0 additions & 74 deletions
This file was deleted.

lib/datadog/di/instrumenter.rb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ def hook_method(probe, &block)
123123
if condition = probe.condition
124124
begin
125125
# This context will be recreated later, unlike for line probes.
126-
context = EL::Context.new(
126+
context = Context.new(
127127
locals: serializer.combine_args(args, kwargs, self),
128128
target_self: self,
129129
probe: probe, settings: settings, serializer: serializer,
@@ -189,7 +189,7 @@ def hook_method(probe, &block)
189189
caller_locs = method_frame + caller_locations # steep:ignore
190190
# TODO capture arguments at exit
191191

192-
context = EL::Context.new(locals: nil, target_self: self,
192+
context = Context.new(locals: nil, target_self: self,
193193
probe: probe, settings: settings, serializer: serializer,
194194
serialized_entry_args: serialized_entry_args,
195195
caller_locations: caller_locs,
@@ -367,7 +367,7 @@ def hook_line(probe, &block)
367367

368368
if continue
369369
if condition = probe.condition
370-
context = EL::Context.new(
370+
context = Context.new(
371371
locals: Instrumenter.get_local_variables(tp),
372372
target_self: tp.self,
373373
probe: probe, settings: settings, serializer: serializer,
@@ -385,7 +385,7 @@ def hook_line(probe, &block)
385385
# want to run it if the callback won't be executed due to the
386386
# rate limit.
387387
# Thus the copy-paste of the creation call here.
388-
context ||= EL::Context.new(
388+
context ||= Context.new(
389389
locals: Instrumenter.get_local_variables(tp),
390390
target_self: tp.self,
391391
probe: probe, settings: settings, serializer: serializer,

sig/datadog/di/context.rbs

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
module Datadog
2+
module DI
3+
class Context
4+
@probe: untyped
5+
6+
@settings: untyped
7+
8+
@serializer: untyped
9+
10+
@locals: untyped
11+
12+
@target_self: untyped
13+
14+
@path: untyped
15+
16+
@caller_locations: untyped
17+
18+
@serialized_entry_args: untyped
19+
20+
@return_value: untyped
21+
22+
@duration: untyped
23+
24+
@exception: untyped
25+
26+
def initialize: (probe: untyped, settings: untyped, serializer: untyped, ?locals: untyped?, ?target_self: untyped?, ?path: untyped?, ?caller_locations: untyped?, ?serialized_entry_args: untyped?, ?return_value: untyped?, ?duration: untyped?, ?exception: untyped?) -> void
27+
28+
attr_reader probe: untyped
29+
30+
attr_reader settings: untyped
31+
32+
attr_reader serializer: untyped
33+
34+
attr_reader locals: untyped
35+
36+
attr_reader target_self: untyped
37+
attr_reader path: untyped
38+
attr_reader caller_locations: untyped
39+
40+
attr_reader serialized_entry_args: untyped
41+
attr_reader return_value: untyped
42+
attr_reader duration: untyped
43+
attr_reader exception: untyped
44+
45+
def serialized_locals: () -> untyped
46+
47+
def fetch: (untyped var_name) -> (nil | untyped)
48+
49+
def fetch_ivar: (untyped var_name) -> untyped
50+
end
51+
end
52+
end

sig/datadog/di/el/context.rbs

Lines changed: 0 additions & 54 deletions
This file was deleted.

sig/datadog/di/probe_manager.rbs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ module Datadog
5757

5858
public
5959
def install_pending_line_probes: (untyped path) -> untyped
60-
def probe_executed_callback: (EL::Context context) -> untyped
60+
def probe_executed_callback: (Context context) -> untyped
6161
attr_reader definition_trace_point: untyped
6262
end
6363
end

sig/datadog/di/probe_notification_builder.rbs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,15 @@ module Datadog
1919

2020
def build_errored: (Probe probe, Exception exception) -> Hash[Symbol,untyped]
2121

22-
def build_executed: (EL::Context context) -> Hash[Symbol,untyped]
22+
def build_executed: (Context context) -> Hash[Symbol,untyped]
2323

24-
def build_snapshot: (EL::Context context) -> Hash[Symbol,untyped]
24+
def build_snapshot: (Context context) -> Hash[Symbol,untyped]
2525

2626
def build_status: (Probe probe, message: untyped, status: untyped) -> Hash[Symbol,untyped]
2727

2828
def format_caller_locations: (Array[untyped] callers) -> Array[Hash[Symbol,untyped]]
2929

30-
def evaluate_template: (untyped template, EL::Context context) -> [String, Array[String]]
30+
def evaluate_template: (untyped template, Context context) -> [String, Array[String]]
3131

3232
def timestamp_now: () -> Integer
3333

spec/datadog/di/el/integration_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ class ELTestIvarClass
4646
end
4747

4848
let(:context) do
49-
Datadog::DI::EL::Context.new(locals: locals, target_self: target,
49+
Datadog::DI::Context.new(locals: locals, target_self: target,
5050
probe: nil, settings: nil, serializer: nil)
5151
end
5252

0 commit comments

Comments
 (0)