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



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

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



109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/maze/schemas/trace_validator.rb', line 109

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

def validate
  # The tests are being run
  @success = true
  verify_against_schema
  validate_headers
  validate_fields
end

#validate_fieldsObject



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
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/maze/schemas/trace_validator.rb', line 69

def validate_fields
  resource_spans = Maze::Helper.read_key_path(@body, 'resourceSpans')

  # Loop through all resource spans
  (0...resource_spans.size).each do |rs|

    if Maze.config.client_mode_validation
      span_element_contains("resourceSpans.#{rs}.resource.attributes", 'device.id')
    end
    span_element_contains("resourceSpans.#{rs}.resource.attributes", 'deployment.environment')
    span_element_contains("resourceSpans.#{rs}.resource.attributes", 'telemetry.sdk.name')
    span_element_contains("resourceSpans.#{rs}.resource.attributes", 'telemetry.sdk.version')

    # Loop through all scope spans
    scope_spans = Maze::Helper.read_key_path(@body, "resourceSpans.#{rs}.scopeSpans")
    (0...scope_spans.size).each do |ss|

      each_span_element_contains("resourceSpans.#{rs}.scopeSpans.#{ss}.spans", 'attributes', 'bugsnag.sampling.p')

      # Loop through all spans
      spans = Maze::Helper.read_key_path(@body, "resourceSpans.#{rs}.scopeSpans.#{ss}.spans")
      (0...spans.size).each do |s|

        # Field validations
        regex_comparison("resourceSpans.#{rs}.scopeSpans.#{ss}.spans.#{s}.spanId", HEX_STRING_16)
        regex_comparison("resourceSpans.#{rs}.scopeSpans.#{ss}.spans.#{s}.traceId", HEX_STRING_32)
        element_int_in_range("resourceSpans.#{rs}.scopeSpans.#{ss}.spans.#{s}.kind", 0..5)
        regex_comparison("resourceSpans.#{rs}.scopeSpans.#{ss}.spans.#{s}.startTimeUnixNano", '^[0-9]+$')
        regex_comparison("resourceSpans.#{rs}.scopeSpans.#{ss}.spans.#{s}.endTimeUnixNano", '^[0-9]+$')
        validate_timestamp("resourceSpans.#{rs}.scopeSpans.#{ss}.spans.#{s}.startTimeUnixNano", HOUR_TOLERANCE)
        validate_timestamp("resourceSpans.#{rs}.scopeSpans.#{ss}.spans.#{s}.endTimeUnixNano", HOUR_TOLERANCE)
        element_a_greater_or_equal_element_b(
          "resourceSpans.#{rs}.scopeSpans.#{ss}.spans.#{s}.endTimeUnixNano",
          "resourceSpans.#{rs}.scopeSpans.#{ss}.spans.#{s}.startTimeUnixNano"
        )
      end
    end
  end
end

#validate_headersObject

Checks that the required headers are present and correct



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

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



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

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