forked from vectordotdev/vector
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcheck-events
More file actions
executable file
·189 lines (158 loc) · 6.01 KB
/
check-events
File metadata and controls
executable file
·189 lines (158 loc) · 6.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
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
95
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
#!/usr/bin/env ruby
# coding: utf-8
require 'find'
# These members/tags are common to multiple events
BYTE_SIZE_COUNT = ['byte_size', 'count']
def hash_array_add(hash, key, item)
arr = hash.fetch(key, Array::new)
arr.append(item)
hash[key] = arr
end
# A class to hold error reports and common functionality
class Event
attr_accessor :path
attr_reader :name, :reports
attr_writer :members
def initialize(name)
@path = nil
@name = name
@reports = []
@members = []
@counters = {}
@logs = []
end
def add_counter(name, tags)
@counters[name] = tags
end
def add_log(type, message, parameters)
@logs.append([type, message, parameters])
end
def valid?
@reports.clear
# Check BytesReceived events (for sources)
if @name.end_with? 'BytesReceived'
members_must_include(['byte_size'])
counters_must_include('component_received_bytes_total', ['protocol'] + @members - ['byte_size'])
end
# Check EventsReceived events (common)
if @name.end_with? 'EventsReceived'
members_must_include(BYTE_SIZE_COUNT)
counters_must_include('component_received_events_total', @members - BYTE_SIZE_COUNT)
counters_must_include('component_received_event_bytes_total', @members - BYTE_SIZE_COUNT)
end
# Check EventsSent events (common)
if @name.end_with? 'EventsSent'
members_must_include(BYTE_SIZE_COUNT)
counters_must_include('component_sent_events_total', @members - BYTE_SIZE_COUNT)
counters_must_include('component_sent_event_bytes_total', @members - BYTE_SIZE_COUNT)
end
# Check BytesSent events (for sinks)
if @name.end_with? 'BytesSent'
members_must_include(['byte_size'])
counters_must_include('component_sent_bytes_total', ['protocol'] + @members - ['byte_size'])
end
has_errors = @logs.one? { |type, _, _| type == 'error' }
# Make sure Error events output an error
if has_errors or @name.end_with? 'Error'
append('Error events MUST be named "___Error".') unless @name.end_with? 'Error'
counters_must_include('component_errors_total', ['error_type', 'stage'] + @members - ['body', 'error'])
end
# Make sure error events contain the right parameters
@logs.each do |type, message, parameters|
if type == 'error'
['error', 'stage'].each do |parameter|
unless parameters.include? parameter
@reports.append("Error log MUST include parameter \"#{parameter}\".")
end
end
end
end
@reports.empty?
end
private
def append(report)
@reports.append(report)
end
def generic_must_contain(array, names, prefix, suffix)
names.each do |name|
unless array.include? name
@reports.append("#{prefix} MUST #{suffix} \"#{name}\".")
end
end
end
def counters_must_include(name, required_tags)
unless @counters.include? name
@reports.append("This event MUST increment counter \"#{name}\".")
else
tags = @counters[name]
required_tags.each do |tag|
unless tags.include? tag
@reports.append("Counter \"#{name}\" MUST include tag \"#{tag}\".")
end
end
end
end
def members_must_include(names)
generic_must_contain(@members, names, 'This event', 'have a member named')
end
end
$all_events = Hash::new { |hash, key| hash[key] = Event::new(key) }
error_count = 0
# Scan sources and build internal structures
Find.find('src') do |path|
if path.end_with? '.rs'
text = File.read(path)
# Check log message texts for correct formatting. See below for the
# full regex
text.scan(/(trace|debug|info|warn|error)!\(\s*(message\s*=\s*)?"([^({)][^("]+)"/) do
|type, has_message_prefix, message|
reports = []
reports.append('Message must start with a capital.') unless message.match(/^[[:upper:]]/)
reports.append('Message must end with a period.') unless message.match(/\.$/)
unless reports.empty?
puts "#{path}: Errors in message \"#{message}\":"
reports.each { |report| puts " #{report}" }
error_count += 1
end
end
if path.start_with? 'src/internal_events/' and !text.match?(/## skip check-events ##/i)
# Scan internal event structs for member names
text.scan(/[\n ]struct (\S+?)(?:<.+?>)?(?: {\n(.+?)\n\s*}|;)\n/m) do |struct_name, members|
$all_events[struct_name].path = path
if members
member_names = members.scan(/ ([A-Za-z0-9_]+): /).map { |member,| member }
$all_events[struct_name].members = member_names
end
end
# Scan internal event implementation blocks for logs and metrics
text.scan(/^(\s*)impl(?:<.+?>)? InternalEvent for ([A-Za-z0-9_]+)(?:<.+?>)? {\n(.+?)\n\1}$/m) do |_space, event_name, block|
# Scan for counter names and tags
block.scan(/ counter!\((?:\n\s+)?"([^"]+)",(.+?)\)[;\n]/m) do |name, tags|
tags = tags.scan(/"([^"]+)" => /).map { |tag,| tag }
$all_events[event_name].add_counter(name, tags)
end
# Scan for log outputs and their parameters
block.scan(/
(trace|debug|info|warn|error)! # The log type
\(\s*(?:message\s*=\s*)? # Skip any leading "message =" bit
"([^({)][^("]+)" # The log message text
([^;]*?) # Match the parameter list
\)(?:;|\n\s*}) # Normally would end with simply ");", but some are missing the semicolon
/mx) do |type, message, parameters|
parameters = parameters.scan(/([a-z0-9_]+) *= .|[?%]([a-z0-9_.]+)/) \
.map { |assignment, simple| assignment or simple }
$all_events[event_name].add_log(type, message, parameters)
end
end
end
end
end
$all_events.each_value do |event|
unless event.valid?
puts "#{event.path}: Errors in event #{event.name}:"
event.reports.each { |report| puts " #{report}" }
error_count += 1
end
end
puts "#{error_count} error(s)"
exit 1 if error_count > 0