Module: OpenTrace::PayloadBuilder

Defined in:
lib/opentrace/payload_builder.rb

Overview

Materializes deferred log entries (frozen Arrays) into payload Hashes. All heavy work (context merge, timestamp formatting, Hash building) runs on the background dispatch thread, keeping the request thread fast.

Produces the v2 flat schema: top-level indexed fields, everything else in ‘body`.

Class Method Summary collapse

Class Method Details

.clean_backtrace(backtrace) ⇒ Object



299
300
301
302
303
304
305
# File 'lib/opentrace/payload_builder.rb', line 299

def clean_backtrace(backtrace)
  if defined?(::Rails) && ::Rails.respond_to?(:backtrace_cleaner)
    ::Rails.backtrace_cleaner.clean(backtrace)
  else
    backtrace.reject { |line| line.include?("/gems/") }
  end
end

.extract_source_location(backtrace) ⇒ Object

Extract source file and line number from the first app-relevant backtrace line. Format: “app/controllers/users_controller.rb:42:in ‘show’”



285
286
287
288
289
290
291
292
293
294
295
296
297
# File 'lib/opentrace/payload_builder.rb', line 285

def extract_source_location(backtrace)
  return [nil, nil] unless backtrace.is_a?(Array) && !backtrace.empty?

  line = backtrace.first.to_s
  parts = line.split(":", 3)
  return [nil, nil] if parts.length < 2

  file = parts[0]
  line_num = parts[1].to_i
  [file, line_num]
rescue StandardError
  [nil, nil]
end

.format_timestamp(ts) ⇒ Object



272
273
274
275
276
277
278
279
280
281
# File 'lib/opentrace/payload_builder.rb', line 272

def format_timestamp(ts)
  case ts
  when Float
    Time.at(ts).utc.strftime("%Y-%m-%dT%H:%M:%S.%6NZ")
  when Time
    ts.utc.strftime("%Y-%m-%dT%H:%M:%S.%6NZ")
  else
    Time.now.utc.strftime("%Y-%m-%dT%H:%M:%S.%6NZ")
  end
end

.materialize(entry, config) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
# File 'lib/opentrace/payload_builder.rb', line 12

def materialize(entry, config)
  if entry.is_a?(Array)
    entry[0] == :request ? materialize_request(entry, config) : materialize_log(entry, config)
  elsif entry.is_a?(Hash)
    entry # legacy direct payload
  end
rescue StandardError => e
  OpenTrace.stats.increment(:payload_build_errors) if OpenTrace.respond_to?(:stats)
  $stderr.puts "[OpenTrace] PayloadBuilder error: #{e.class}: #{e.message}" if OpenTrace.respond_to?(:config) && OpenTrace.config.respond_to?(:debug) && OpenTrace.config.debug
  nil
end

.materialize_log(entry, config) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/opentrace/payload_builder.rb', line 24

def materialize_log(entry, config)
  ts, level, message, , ctx, request_id, trace_id,
    span_id, parent_span_id, req_summary, event_type = entry

  meta = ctx.is_a?(Hash) ? ctx.dup : {}
  meta.merge!() if .is_a?(Hash)

  static_ctx = OpenTrace.send(:static_context)
  static_ctx.each { |k, v| meta[k] ||= v }

  # Extract trace_id from metadata if user provided it there
  meta_trace_id = meta.delete(:trace_id)
  effective_trace_id = meta_trace_id || trace_id

  # Promote indexed fields (remove from metadata to avoid duplication)
  commit_hash = meta.delete(:git_sha)
  effective_request_id = meta.delete(:request_id) || request_id
  error_class = meta.delete(:error_class)
  exception_message = meta.delete(:exception_message)
  meta.delete(:error_fingerprint) # server computes fingerprint
  backtrace = meta.delete(:backtrace)
  source_file, source_line = extract_source_location(backtrace)

  # Promote identity fields from metadata to top level
  user_id = meta.delete(:user_id)
  tenant_id = meta.delete(:tenant_id)
  session_id = meta.delete(:session_id)

  payload = {
    ts: format_timestamp(ts),
    level: level.to_s.downcase,
    service: config.service,
    env: config.environment,
    message: message.to_s
  }

  payload[:version] = commit_hash if commit_hash
  payload[:event_type] = event_type.to_s if event_type
  payload[:trace_id] = effective_trace_id.to_s if effective_trace_id
  payload[:span_id] = span_id if span_id
  payload[:parent_span_id] = parent_span_id if parent_span_id
  payload[:request_id] = effective_request_id.to_s if effective_request_id
  payload[:user_id] = user_id.to_s if user_id
  payload[:tenant_id] = tenant_id.to_s if tenant_id
  payload[:session_id] = session_id.to_s if session_id

  # Flatten request_summary fields to top level if present
  if req_summary.is_a?(Hash)
    payload[:method] = req_summary[:method] if req_summary[:method]
    payload[:path] = req_summary[:path] if req_summary[:path]
    payload[:status] = req_summary[:status] if req_summary[:status]
    payload[:duration_ms] = req_summary[:duration_ms].to_i if req_summary[:duration_ms]
    payload[:controller] = req_summary[:controller] if req_summary[:controller]
  end

  # Build body from remaining metadata + exception info
  body = {}
  body[:context] = meta.compact unless meta.empty?

  if error_class
    exc = { class: error_class }
    exc[:message] = exception_message&.slice(0, 500) if exception_message
    exc[:file] = source_file if source_file
    exc[:line] = source_line if source_line && source_line > 0
    exc[:backtrace] = backtrace.first(15) if backtrace.is_a?(Array) && !backtrace.empty?
    body[:exception] = exc
  end

  payload[:body] = body unless body.empty?
  payload
end

.materialize_request(entry, config) ⇒ Object



96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
# File 'lib/opentrace/payload_builder.rb', line 96

def materialize_request(entry, config)
  _, started, finished, controller, action, method, path, status,
    exc_class, exc_message, exc_backtrace, request_id, trace_id,
    span_id, parent_span_id, cached_ctx, collector, extra = entry

  duration_ms = (finished && started) ? (finished - started) * 1000.0 : 0.0

  meta = cached_ctx.is_a?(Hash) ? cached_ctx.dup : {}
  meta.merge!(extra) if extra.is_a?(Hash)

  static_ctx = OpenTrace.send(:static_context)
  static_ctx.each { |k, v| meta[k] ||= v }
  meta[:request_id] ||= request_id if request_id

  if cached_ctx.is_a?(Hash) && cached_ctx.key?(:user_id)
    meta[:user_id] = cached_ctx[:user_id]
  end

  exception_info = nil
  if exc_class
    cleaned_bt = exc_backtrace ? clean_backtrace(exc_backtrace) : nil
    source_file, source_line = extract_source_location(cleaned_bt)
    exception_info = { class: exc_class }
    exception_info[:message] = exc_message&.slice(0, 500) if exc_message
    exception_info[:file] = source_file if source_file
    exception_info[:line] = source_line if source_line && source_line > 0
    exception_info[:backtrace] = cleaned_bt.first(15) if cleaned_bt.is_a?(Array) && !cleaned_bt.empty?
  end

  # Run deferred EXPLAIN on background thread
  explain_results = nil
  if extra.is_a?(Hash) && extra[:pending_explains] && defined?(ActiveRecord::Base)
    explain_results = run_pending_explains(extra.delete(:pending_explains))
    explain_results = nil if explain_results.empty?
  end

  # Build collector summary data
  summary = collector&.summary
  db_ms = nil
  db_count = nil
  n_plus_one = nil
  slow_queries = nil
  dup_queries = nil
  view_ms = nil
  performance_body = nil
  queries_body = nil
  external_body = nil
  timeline_body = nil

  if collector && summary
    db_ms = summary[:sql_total_ms]&.round(1)
    db_count = summary[:sql_query_count]
    n_plus_one = summary[:n_plus_one_warning] || false
    slow_queries = summary[:sql_slowest_ms] ? 1 : 0
    dup_queries = 0 # placeholder; collector may provide this later

    view_ms = summary[:view_total_ms]&.round(1)

    # Build performance body section
    perf = {}
    perf[:view_ms] = view_ms if view_ms
    perf[:view_count] = summary[:view_render_count] if summary[:view_render_count]
    perf[:view_slowest_ms] = summary[:view_slowest_ms] if summary[:view_slowest_ms]
    perf[:view_slowest_template] = summary[:view_slowest_template] if summary[:view_slowest_template]
    perf[:memory_before_mb] = summary[:memory_before_mb] if summary[:memory_before_mb]
    perf[:memory_after_mb] = summary[:memory_after_mb] if summary[:memory_after_mb]
    perf[:memory_delta_mb] = summary[:memory_delta_mb] if summary[:memory_delta_mb]
    perf[:cache_reads] = summary[:cache_reads] if summary[:cache_reads]
    perf[:cache_hits] = summary[:cache_hits] if summary[:cache_hits]
    perf[:cache_writes] = summary[:cache_writes] if summary[:cache_writes]
    perf[:cache_hit_ratio] = summary[:cache_hit_ratio] if summary[:cache_hit_ratio]
    perf[:sql_slowest_ms] = summary[:sql_slowest_ms] if summary[:sql_slowest_ms]
    perf[:sql_slowest_name] = summary[:sql_slowest_name] if summary[:sql_slowest_name]
    perf[:http_external_count] = summary[:http_external_count] if summary[:http_external_count]
    perf[:http_external_total_ms] = summary[:http_external_total_ms] if summary[:http_external_total_ms]
    perf[:http_slowest_ms] = summary[:http_slowest_ms] if summary[:http_slowest_ms]
    perf[:http_slowest_host] = summary[:http_slowest_host] if summary[:http_slowest_host]

    # Compute time breakdown
    if duration_ms > 0
      sql_pct = [((collector.sql_total_ms / duration_ms) * 100).round(1), 100.0].min
      view_pct = [((collector.view_total_ms / duration_ms) * 100).round(1), 100.0].min
      http_pct = collector.http_count > 0 ? [((collector.http_total_ms / duration_ms) * 100).round(1), 100.0].min : 0.0
      other_pct = [100 - sql_pct - view_pct - http_pct, 0].max.round(1)
      perf[:time_breakdown] = {
        sql_pct: sql_pct,
        view_pct: view_pct,
        http_pct: http_pct,
        other_pct: other_pct
      }
    end

    performance_body = perf unless perf.empty?
    timeline_body = summary[:timeline] if summary[:timeline]
  end

  level = if exc_class
            "error"
          elsif status.to_i >= 500
            "error"
          elsif status.to_i >= 400
            "warn"
          else
            "info"
          end

  # Use custom transaction name if set
  transaction_name = meta.delete(:transaction_name)
  message = if transaction_name
              "#{transaction_name} #{status} #{duration_ms.round(1)}ms"
            else
              "#{method} #{path} #{status} #{duration_ms.round(1)}ms"
            end

  # Promote indexed fields (remove from metadata to avoid duplication)
  commit_hash = meta.delete(:git_sha)
  effective_request_id = meta.delete(:request_id) || request_id
  user_id = meta.delete(:user_id)
  tenant_id = meta.delete(:tenant_id)
  session_id = meta.delete(:session_id)

  # Remove fields that are now top-level or in body
  meta.delete(:exception_message)
  meta.delete(:error_class)
  meta.delete(:backtrace)
  meta.delete(:error_fingerprint)

  payload = {
    ts: format_timestamp(started),
    level: level,
    service: config.service,
    env: config.environment,
    message: message,
    event_type: "http.request"
  }

  payload[:version] = commit_hash if commit_hash
  payload[:trace_id] = trace_id.to_s if trace_id
  payload[:span_id] = span_id if span_id
  payload[:parent_span_id] = parent_span_id if parent_span_id
  payload[:request_id] = effective_request_id.to_s if effective_request_id
  payload[:user_id] = user_id.to_s if user_id
  payload[:tenant_id] = tenant_id.to_s if tenant_id
  payload[:session_id] = session_id.to_s if session_id

  # Flat request fields
  payload[:method] = method if method
  payload[:path] = path if path
  payload[:status] = status if status
  payload[:duration_ms] = duration_ms.round(0).to_i
  payload[:controller] = controller if controller

  # Flat DB fields
  payload[:db_ms] = db_ms.to_i if db_ms
  payload[:db_count] = db_count if db_count
  payload[:n_plus_one] = n_plus_one unless n_plus_one.nil?
  payload[:slow_queries] = slow_queries if slow_queries
  payload[:dup_queries] = dup_queries if dup_queries

  # Build body hash
  body = {}

  # Context: remaining metadata + transaction_name
  ctx = meta.compact
  ctx[:transaction_name] = transaction_name if transaction_name
  body[:context] = ctx unless ctx.empty?

  body[:performance] = performance_body if performance_body
  body[:queries] = explain_results if explain_results
  body[:timeline] = timeline_body if timeline_body
  body[:exception] = exception_info if exception_info

  payload[:body] = body unless body.empty?
  payload
end

.run_explain(sql) ⇒ Object



322
323
324
325
326
327
328
329
330
331
332
333
334
335
# File 'lib/opentrace/payload_builder.rb', line 322

def run_explain(sql)
  # Only EXPLAIN simple SELECTs -- reject anything suspicious
  normalized = sql.to_s.strip
  return nil unless normalized.match?(/\ASELECT\b/i)
  return nil if normalized.include?(";") # No multi-statement

  ActiveRecord::Base.connection_pool.with_connection do |conn|
    result = conn.execute("EXPLAIN #{normalized}")
    rows = result.respond_to?(:rows) ? result.rows : result.map(&:values)
    rows.flatten.join("\n").slice(0, 2000)
  end
rescue StandardError
  nil
end

.run_pending_explains(pending) ⇒ Object



307
308
309
310
311
312
313
314
315
316
317
318
319
320
# File 'lib/opentrace/payload_builder.rb', line 307

def run_pending_explains(pending)
  pending.filter_map do |entry|
    plan = run_explain(entry[:sql])
    next unless plan
    {
      sql: entry[:sql].to_s.slice(0, 500),
      duration_ms: entry[:duration_ms],
      name: entry[:name],
      explain_plan: plan
    }
  end
rescue StandardError
  []
end