Module: TraceView::XTrace

Defined in:
lib/traceview/xtrace.rb

Overview

Methods to act on, manipulate or investigate an X-Trace value

Class Method Summary collapse

Class Method Details

.continue_service_context(start, finish) ⇒ Object

continue_service_context

In the case of service calls such as external HTTP requests, we pass along X-Trace headers so that request context can be maintained across servers and applications.

Remote requests can return a X-Trace header in which case we want to pickup on and continue the context in most cases.

if that be the case)



77
78
79
80
81
82
83
84
85
86
87
# File 'lib/traceview/xtrace.rb', line 77

def continue_service_context(start, finish)
  if TraceView::XTrace.valid?(finish) && TraceView.tracing?

    # Assure that we received back a valid X-Trace with the same task_id
    if TraceView::XTrace.task_id(start) == TraceView::XTrace.task_id(finish)
      TraceView::Context.fromString(finish)
    else
      TraceView.logger.debug "Mismatched returned X-Trace ID: #{finish}"
    end
  end
end

.edge_id(xtrace) ⇒ Object

TraceView::XTrace.edge_id

Extract and return the edge_id portion of an X-Trace ID



52
53
54
55
56
57
58
59
60
# File 'lib/traceview/xtrace.rb', line 52

def edge_id(xtrace)
  return nil unless TraceView::XTrace.valid?(xtrace)

  xtrace[42..57]
rescue StandardError => e
  TraceView.logger.debug e.message
  TraceView.logger.debug e.backtrace
  return nil
end

.task_id(xtrace) ⇒ Object

TraceView::XTrace.task_id

Extract and return the task_id portion of an X-Trace ID



37
38
39
40
41
42
43
44
45
# File 'lib/traceview/xtrace.rb', line 37

def task_id(xtrace)
  return nil unless TraceView::XTrace.valid?(xtrace)

  xtrace[2..41]
rescue StandardError => e
  TraceView.logger.debug e.message
  TraceView.logger.debug e.backtrace
  return nil
end

.valid?(xtrace) ⇒ Boolean

TraceView::XTrace.valid?

Perform basic validation on a potential X-Trace ID

Returns:

  • (Boolean)


15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/traceview/xtrace.rb', line 15

def valid?(xtrace)
  # Shouldn't be nil
  return false unless xtrace

  # The X-Trace ID shouldn't be an initialized empty ID
  return false if (xtrace =~ /^1b0000000/i) == 0

  # Valid X-Trace IDs have a length of 58 bytes and start with '1b'
  return false unless xtrace.length == 58 && (xtrace =~ /^1b/i) == 0

  true
rescue StandardError => e
  TraceView.logger.debug e.message
  TraceView.logger.debug e.backtrace
  false
end