Class: Maze::Schemas::TraceValidator

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

Overview

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

Constant Summary

Constants inherited from ValidatorBase

ValidatorBase::HEX_STRING_16, ValidatorBase::HEX_STRING_32, ValidatorBase::HOUR_TOLERANCE

Instance Attribute Summary

Attributes inherited from ValidatorBase

#errors, #success

Instance Method Summary collapse

Methods inherited from ValidatorBase

#each_element_contains, #each_element_contains_each, #each_element_exists, #each_event_contains, #each_event_contains_each, #element_a_greater_or_equal_element_b, #element_exists, #element_has_value, #element_int_in_range, #initialize, #regex_comparison, #validate_header, #validate_timestamp

Constructor Details

This class inherits a constructor from Maze::Schemas::ValidatorBase

Instance Method Details

#each_span_element_contains(container_path, attribute_path, key_value) ⇒ Object



108
109
110
111
112
113
114
115
116
117
118
# File 'lib/maze/schemas/trace_validator.rb', line 108

def each_span_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|
    span_element_contains("#{container_path}.#{index}.#{attribute_path}", key_value)
  end
end

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



87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/maze/schemas/trace_validator.rb', line 87

def span_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

#validateObject

Runs the validation against the trace given



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/maze/schemas/trace_validator.rb', line 16

def validate
  # The tests are being run
  @success = true
  verify_against_schema
  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]+$')
  each_span_element_contains('resourceSpans.0.scopeSpans.0.spans', 'attributes', 'bugsnag.sampling.p')
  span_element_contains('resourceSpans.0.resource.attributes', 'deployment.environment')
  span_element_contains('resourceSpans.0.resource.attributes', 'telemetry.sdk.name')
  span_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'
  )

  if Maze.config.client_mode_validation
    span_element_contains('resourceSpans.0.resource.attributes', 'device.id')
  end
end

#validate_headersObject

Checks that the required headers are present and correct



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
# File 'lib/maze/schemas/trace_validator.rb', line 52

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)
  unless Maze.config.unmanaged_traces_mode
    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
end

#verify_against_schemaObject



42
43
44
45
46
47
48
49
# File 'lib/maze/schemas/trace_validator.rb', line 42

def verify_against_schema
  if !@schema_errors.nil? && @schema_errors.size > 0
    @success = false
    @schema_errors.each do |error|
      @errors << "#{JSONSchemer::Errors.pretty(error)}"
    end
  end
end