Class: Maze::Schemas::TraceValidator

Inherits:
Object
  • Object
show all
Defined in:
lib/maze/schemas/trace_validator.rb

Overview

Contains a set of pre-defined validations for ensuring traces are correct

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(request) ⇒ TraceValidator

Creates the validator

@param request [Hash] The trace request to validate


25
26
27
28
29
30
# File 'lib/maze/schemas/trace_validator.rb', line 25

def initialize(request)
  @headers = request[:request].header
  @body = request[:body]
  @success = nil
  @errors = []
end

Instance Attribute Details

#errorsObject (readonly)

Returns the value of attribute errors.



20
21
22
# File 'lib/maze/schemas/trace_validator.rb', line 20

def errors
  @errors
end

#successObject (readonly)

Whether the trace passed the validation, one of true, false, or nil (not run)

@returns [Boolean|nil] Whether the validation was successful


19
20
21
# File 'lib/maze/schemas/trace_validator.rb', line 19

def success
  @success
end

Instance Method Details

#each_element_contains(container_path, attribute_path, key_value) ⇒ Object



161
162
163
164
165
166
167
168
169
170
171
# File 'lib/maze/schemas/trace_validator.rb', line 161

def each_element_contains(container_path, attribute_path, key_value)
  container = Maze::Helper.read_key_path(@body, container_path)
  if container.nil? || !container.kind_of?(Array)
    @success = false
    @errors << "Element '#{container_path}' was expected to be an array, was '#{container}'"
    return
  end
  container.each_with_index do |_item, index|
    element_contains("#{container_path}.#{index}.#{attribute_path}", key_value)
  end
end

#element_a_greater_or_equal_element_b(path_a, path_b) ⇒ Object



173
174
175
176
177
178
179
180
# File 'lib/maze/schemas/trace_validator.rb', line 173

def element_a_greater_or_equal_element_b(path_a, path_b)
  element_a = Maze::Helper.read_key_path(@body, path_a)
  element_b = Maze::Helper.read_key_path(@body, path_b)
  unless element_a && element_b && element_a >= element_b
    @success = false
    @errors << "Element '#{path_a}':'#{element_a}' was expected to be greater than or equal to '#{path_b}':'#{element_b}'"
  end
end

#element_contains(path, key_value, value_type = nil, possible_values = nil) ⇒ Object



140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
# File 'lib/maze/schemas/trace_validator.rb', line 140

def element_contains(path, key_value, value_type=nil, possible_values=nil)
  container = Maze::Helper.read_key_path(@body, path)
  if container.nil? || !container.kind_of?(Array)
    @success = false
    @errors << "Element '#{path}' was expected to be an array, was '#{container}'"
    return
  end
  element = container.find { |value| value['key'].eql?(key_value) }
  unless element
    @success = false
    @errors << "Element '#{path}' did not contain a value with the key '#{key_value}'"
    return
  end
  if value_type && possible_values
    unless element['value'] && element['value'][value_type] && possible_values.include?(element['value'][value_type])
      @success = false
      @errors << "Element '#{path}':'#{element}' did not contain a value of '#{value_type}' from '#{possible_values}'"
    end
  end
end

#element_int_in_range(path, range) ⇒ Object



127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/maze/schemas/trace_validator.rb', line 127

def element_int_in_range(path, range)
  element_value = Maze::Helper.read_key_path(@body, path)
  if element_value.nil? || !element_value.kind_of?(Integer)
    @success = false
    @errors << "Element '#{path}' was expected to be an integer, was '#{element_value}'"
    return
  end
  unless range.include?(element_value)
    @success = false
    @errors << "Element '#{path}':'#{element_value}' was expected to be in the range '#{range}'"
  end
end

#regex_comparison(path, regex) ⇒ Object



118
119
120
121
122
123
124
125
# File 'lib/maze/schemas/trace_validator.rb', line 118

def regex_comparison(path, regex)
  element_value = Maze::Helper.read_key_path(@body, path)
  expected = Regexp.new(regex)
  unless expected.match(element_value)
    @success = false
    @errors << "Element '#{path}' was expected to match the regex '#{regex}', but was '#{element_value}'"
  end
end

#validateObject

Runs the validation against the trace given



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/maze/schemas/trace_validator.rb', line 33

def validate
  @success = true

  validate_headers
  regex_comparison('resourceSpans.0.scopeSpans.0.spans.0.spanId', HEX_STRING_16)
  regex_comparison('resourceSpans.0.scopeSpans.0.spans.0.traceId', HEX_STRING_32)
  element_int_in_range('resourceSpans.0.scopeSpans.0.spans.0.kind', 0..5)
  regex_comparison('resourceSpans.0.scopeSpans.0.spans.0.startTimeUnixNano', '^[0-9]+$')
  regex_comparison('resourceSpans.0.scopeSpans.0.spans.0.endTimeUnixNano', '^[0-9]+$')
  element_contains('resourceSpans.0.resource.attributes', 'device.id')
  each_element_contains('resourceSpans.0.scopeSpans.0.spans', 'attributes', 'bugsnag.sampling.p')
  element_contains('resourceSpans.0.resource.attributes', 'deployment.environment')
  element_contains('resourceSpans.0.resource.attributes', 'telemetry.sdk.name')
  element_contains('resourceSpans.0.resource.attributes', 'telemetry.sdk.version')
  validate_timestamp('resourceSpans.0.scopeSpans.0.spans.0.startTimeUnixNano', HOUR_TOLERANCE)
  validate_timestamp('resourceSpans.0.scopeSpans.0.spans.0.endTimeUnixNano', HOUR_TOLERANCE)
  element_a_greater_or_equal_element_b(
    'resourceSpans.0.scopeSpans.0.spans.0.endTimeUnixNano',
    'resourceSpans.0.scopeSpans.0.spans.0.startTimeUnixNano'
  )
end

#validate_header(name) ⇒ Object



75
76
77
78
79
80
81
82
# File 'lib/maze/schemas/trace_validator.rb', line 75

def validate_header(name)
  value = @headers[name]
  if value.nil? || value.size > 1
    @errors << "Expected exactly one value for header #{name}, received #{value || 'nil'}"
  else
    yield value[0]
  end
end

#validate_headersObject

Checks that the required headers are present and correct



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/maze/schemas/trace_validator.rb', line 85

def validate_headers
  # API key
  validate_header('bugsnag-api-key') do |api_key|
    expected = Regexp.new(HEX_STRING_32)
    unless expected.match(api_key)
      @success = false
      @errors << "bugsnag-api-key header was expected to match the regex '#{HEX_STRING_32}', but was '#{api_key}'"
    end
  end

  # Bugsnag-Sent-at
  validate_header('bugsnag-sent-at') do |date|
    begin
      Date.iso8601(date)
    rescue Date::Error
      @success = false
      @errors << "bugsnag-sent-at header was expected to be an IOS 8601 date, but was '#{date}'"
    end
  end

  # Bugsnag-Span-Sampling
  # of the format x:y where x is a decimal between 0 and 1 (inclusive) and y is the number of spans in the batch (if possible at this stage - we could weaken this if necessary)
  validate_header('bugsnag-span-sampling') do |sampling|
    begin
      expected = Regexp.new(SAMPLING_HEADER)
      unless expected.match(sampling)
        @success = false
        @errors << "bugsnag-span-sampling header was expected to match the regex '#{SAMPLING_HEADER}', but was '#{sampling}'"
      end
    end
  end
end

#validate_timestamp(path, tolerance) ⇒ Object



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/maze/schemas/trace_validator.rb', line 55

def validate_timestamp(path, tolerance)
  timestamp = Maze::Helper.read_key_path(@body, path)
  unless timestamp.kind_of?(String)
    @success = false
    @errors << "Timestamp was expected to be a string, was '#{timestamp.class.name}'"
    return
  end
  parsed_timestamp = timestamp.to_i
  unless parsed_timestamp > 0
    @success = false
    @errors << "Timestamp was expected to be a positive integer, was '#{parsed_timestamp}'"
    return
  end
  time_in_nanos = Time.now.to_i * 1000000000
  unless (time_in_nanos - parsed_timestamp).abs < tolerance
    @success = false
    @errors << "Timestamp was expected to be within #{tolerance} nanoseconds of the current time (#{time_in_nanos}), was '#{parsed_timestamp}'"
  end
end