Skip to content

Instantly share code, notes, and snippets.

@Drowze
Last active December 17, 2025 21:07
Show Gist options
  • Select an option

  • Save Drowze/aa3e5841ae8044c678a8c52188bd08e2 to your computer and use it in GitHub Desktop.

Select an option

Save Drowze/aa3e5841ae8044c678a8c52188bd08e2 to your computer and use it in GitHub Desktop.
Simple datadog trace parser/debugger
# frozen_string_literal: true
# Simple datadog trace parser/debugger
#
# To run, simply:
# $ rackup -p 8126
require 'roda'
require 'pry'
require 'msgpack'
require 'json'
class HumanizeId
def initialize(prefix)
@prefix = prefix
@last_id = 0
@ids = {}
end
def call(id)
return if Integer(id) == 0
@ids[id] ||= "#{@prefix}#{@last_id += 1}"
end
end
class App < Roda
plugin :json
@@raw_traces = []
@@parsed_traces = []
@@trace_humanizer = HumanizeId.new("t")
@@span_humanizer = HumanizeId.new("s")
RELEVANT_DD_META_TAGS = %w[water kafka karafka messag work consum produc].freeze
route do |r|
r.get 'info' do
{}
end
r.get 'raw_traces' do
@@raw_traces
end
r.get 'parsed_traces' do
spans = @@parsed_traces.dup
spans_by_parent_id = spans
.group_by { |span| span[:parent_id] }
.transform_values do |child_spans|
child_spans.sort_by { |span| span[:start] }
end
spans.each do |span|
span[:children] = spans_by_parent_id[span[:span_id]] || []
end
spans_by_parent_id.except(nil).select { |_, v| v.empty? }.each_pair do |parent_id, spans|
warn "No parent span (id: #{parent_id}) found. #{spans.size} spans are children of it"
end
spans_by_parent_id[nil] || []
end
r.post('v0.4/traces') do
trace_batches = MessagePack.unpack(request.body.read)
trace_batches.each do |trace_batch|
trace_batch.each do |trace|
@@raw_traces << trace
duration_ms = (Integer(trace["duration"]) * 1e-6).round(2)
start = Time.at(Integer(trace["start"]) * 1e-9)
@@parsed_traces << {
start: start.strftime("%F %T.%3N"),
duration: "#{format(duration_ms.to_s, "%.2f")}ms",
trace_id: @@trace_humanizer.(trace["trace_id"]),
span_id: @@span_humanizer.(trace["span_id"]),
parent_id: @@span_humanizer.(trace["parent_id"]),
operation_name: trace["name"],
resource_name: trace["resource"],
service: trace["service"],
meta: trace["meta"].select do |k, _|
RELEVANT_DD_META_TAGS.any? { |word| k.downcase.include?(word) }
end.transform_keys(&:to_sym),
span_links: trace["span_links"].map do |span_link|
{
trace_id: @@trace_humanizer.(span_link["trace_id"]),
span_id: @@span_humanizer.(span_link["span_id"]),
}
end
}
end
end
{success: true}
end
end
end
run App.freeze.app
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment