Skip to content

Commit

Permalink
di
Browse files Browse the repository at this point in the history
  • Loading branch information
p committed Jun 3, 2024
1 parent 81de455 commit dae9e0c
Show file tree
Hide file tree
Showing 5 changed files with 96 additions and 10 deletions.
2 changes: 1 addition & 1 deletion lib/datadog/core/remote/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ def payload # rubocop:disable Metrics/MethodLength

client_tracer = {
runtime_id: Core::Environment::Identity.id,
language: 'python',#Core::Environment::Identity.lang,
language: Core::Environment::Identity.lang,
tracer_version: tracer_version,
service: service_name,
env: Datadog.configuration.env,
Expand Down
2 changes: 1 addition & 1 deletion lib/datadog/debugging/hook.rb
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ module Hook
TRACEPOINT_MUTEX.synchronize do
$tracepoint ||= TracePoint.new(:line) do |tp|
#puts '******* tracepoint invoked ************'
on_line_tracepoint(tp)
on_line_tracepoint(tp, &block)
end

$tracepoint.enable
Expand Down
7 changes: 6 additions & 1 deletion lib/datadog/debugging/probe.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ def self.from_remote_config(config)
type: config.fetch('type'),
file: config['where']&.[]('sourceFile'),
# Sometimes lines are received as an array of nil
line_nos: config['where']&.[]('lines').compact.map(&:to_i),
line_nos: config['where']&.[]('lines')&.compact&.map(&:to_i),
template: config['template'],
)
end
Expand All @@ -33,6 +33,11 @@ def initialize(id:, type:,
attr_reader :line_nos
attr_reader :module_name
attr_reader :function_name
attr_reader :template

def line?
line_nos && !line_nos.empty?
end
end
end
end
79 changes: 76 additions & 3 deletions lib/datadog/debugging/probe_notifier.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,54 @@ module ProbeNotifier
end

module_function def notify_executed(probe, tracepoint)
puts 'executing'
puts '------------ executing -------------------'
notify_snapshot(probe)
end

module_function def notify_snapshot(probe)
timestamp = timestamp_now
payload = {
service: Datadog.configuration.service,
'debugger.snapshot': {
id: SecureRandom.uuid,
timestamp: timestamp,
evaluationErrors: [],
probe: {
id: probe.id,
version: 0,
location: {
file: probe.file,
lines: probe.line_nos,
},
},
language: 'ruby',
stack: [],
captures: nil,
duration: nil,
},
host: nil,
logger: {
name: probe.file,
method: 'fixme',
thread_name: 'thread name',
thread_id: 'thread id',
version: 2,
},
'dd.trace_id': 423,
'dd.span_id': 4234,
ddsource: 'dd_debugger',
message: "hello world: #{probe.template}",
timestamp: timestamp,
}

# TODO also send query string parameters
send_json_payload('/debugger/v1/input', [payload])
end

module_function def notify(probe, message:, status:)
payload = {
service: Datadog.configuration.service,
timestamp: (Time.now.to_f * 1000).to_i,
timestamp: timestamp_now,
message: message,
ddsource: 'dd_debugger',
debugger: {
Expand All @@ -45,6 +86,10 @@ module ProbeNotifier
},
}

send_payload('/debugger/v1/diagnostics', payload)
end

module_function def send_payload(path, payload)
#uri = URI("http://localhost:8126/debugger/v1/diagnostics")
#http = Net::HTTP.new(uri.host, uri.port)
http = Datadog::Core::Transport::HTTP::Adapters::Net.new(agent_settings)
Expand All @@ -56,7 +101,7 @@ module ProbeNotifier
epayload = Datadog::Core::Vendor::Multipart::Post::UploadIO.new(
StringIO.new(JSON.dump(payload)), 'application/json', 'event.json')
env = OpenStruct.new(
path: '/debugger/v1/diagnostics',
path: path,
form: {'event' => epayload},
headers: {},
)
Expand All @@ -69,6 +114,34 @@ module ProbeNotifier
p response
end

module_function def send_json_payload(path, payload)
uri = URI("http://localhost:8126/debugger/v1/input")
http = Net::HTTP.new(uri.host, uri.port)
#http = Datadog::Core::Transport::HTTP::Adapters::Net.new(agent_settings)
headers =
{
'content-type' => 'application/json',
}

env = OpenStruct.new(
path: path,
form: payload,
headers: headers,
)

puts '-- notifying:'
pp payload

#response = http.post(env)
response = http.post(uri.path, JSON.dump(payload), headers)

p response
end

module_function def timestamp_now
(Time.now.to_f * 1000).to_i
end

module_function def agent_settings
settings = Datadog.configuration
Core::Configuration::AgentSettingsResolver.call(settings, logger: @logger)
Expand Down
16 changes: 12 additions & 4 deletions lib/datadog/debugging/remote.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ module Debugging
module Remote
class ReadError < StandardError; end

INSTALLED_PROBES = Concurrent::Map.new

class << self
PRODUCT = 'LIVE_DEBUGGING'

Expand All @@ -24,10 +26,16 @@ def process_config(config, content)
probe = Probe.from_remote_config(config)
ProbeNotifier.notify_received(probe)

Hook.hook_line(probe.file, probe.line_nos.first) do |tp|
puts '*** probe executed ***'
ProbeNotifier.notify_emitting(probe)
ProbeNotifier.notify_executed(probe, tp)
if probe.line?
Hook.hook_line(probe.file, probe.line_nos.first) do |tp|
puts '*** probe executed ***'
ProbeNotifier.notify_emitting(probe)
ProbeNotifier.notify_executed(probe, tp)
end

INSTALLED_PROBES[probe.id] = probe
else
puts "Not a line probe"
end

ProbeNotifier.notify_installed(probe)
Expand Down

0 comments on commit dae9e0c

Please sign in to comment.