forked from newrelic/node-newrelic
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test-naming-rules.js
executable file
·171 lines (147 loc) · 4.8 KB
/
test-naming-rules.js
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
#! /usr/bin/env node
'use strict'
/* eslint-disable no-console */
var fs = require('fs')
var path = require('path')
var readline = require('readline')
var urltils = require('../lib/util/urltils')
var MetricNormalizer = require('../lib/metrics/normalizer')
var cwd = process.cwd()
var opts = null
if (arrayContainsAny(process.argv, '-h', '-?', '--help')) {
printHelp()
} else if (process.argv.length === 3) {
opts = {rules: null, urls: path.resolve(cwd, process.argv[2])}
} else if (process.argv.length === 4) {
opts = {
rules: path.resolve(cwd, process.argv[2]),
urls: path.resolve(cwd, process.argv[3])
}
} else {
printHelp()
}
run(opts)
function printHelp() {
console.log('Usage:')
console.log(' newrelic-naming-rules (-h|--help|-?)')
console.log(' newrelic-naming-rules [<rules>] <urls>')
console.log('')
console.log('Runs the configured naming rules against the given set of URLs')
console.log('and prints out the resulting normalized URLs.')
console.log('')
console.log('Parameters:')
console.log(' <rules>')
console.log(' Optional. A JSON file containing additional rules to apply')
console.log(' to the URLs. Should be in the form of an array of arrays:')
console.log(' [["pattern", "replacement"], [/pattern2/, "replacement2"]]')
console.log('')
console.log(' <urls>')
console.log(' A file containing URLs to test against the naming rules.')
console.log(' Should contain one URL per line, and only contain the path')
console.log(' without host or protocol:')
console.log(' /foo/bar')
console.log(' /foo/biz')
console.log(' /fiz/bang')
console.log('')
console.log(' -h|--help|-?')
console.log(' Optional. Prints this help message and exits.')
process.exit(0)
}
function run(opts) {
var config = require('../lib/config').initialize()
var runtimeRules = opts.rules ? require(opts.rules) : null
var appliedRules = []
// responsible for handling default rules provided by the server
var defaultNormalizer = loadDefaultNormalizer(config, runtimeRules)
// rules defined by user in local configuration file
var userNormalizer = loadUserNormalizer(config, runtimeRules)
defaultNormalizer.on('appliedRule', onAppliedRule)
userNormalizer.on('appliedRule', onAppliedRule)
var urlsFile = fs.createReadStream(opts.urls, {encoding: 'utf-8'})
var reader = readline.createInterface({input: urlsFile, output: null})
reader.on('line', function onUrlLine(urlLine) {
appliedRules = []
var scrubbedUrl = urltils.scrub(urlLine)
var normalized = userNormalizer.normalize(scrubbedUrl)
if (!normalized.matched) {
normalized = defaultNormalizer.normalize(scrubbedUrl)
}
console.log(urlLine, ' => ', normalized.value)
if (appliedRules.length === 0) {
console.log('no rules matched')
} else {
for (var i = 0; i < appliedRules.length; i++) {
var match = appliedRules[i]
console.log(
' %s: %s => %s (rule %s)',
(i + 1), match.original, match.normalized, match.rule.pattern
)
}
}
console.log('')
})
urlsFile.on('end', function onUrlEnd() {
urlsFile.close()
reader.close()
})
function onAppliedRule(rule, newValue, oldValue) {
appliedRules.push({
rule: rule,
original: oldValue,
normalized: newValue
})
}
}
function loadDefaultNormalizer(config) {
// Load the normalizer.
var normalizer = new MetricNormalizer(config, 'URL')
// Add in the rules the collector would ship down.
normalizer.load([{
'match_expression':
'.*\\.(ace|arj|ini|txt|udl|plist|css|gif|ico|jpe?g|js|png|swf|woff|caf|' +
'aiff|m4v|mpe?g|mp3|mp4|mov)$',
'replacement': '/*.\\1',
'replace_all': false,
'each_segment': false,
'ignore': false,
'terminate_chain': true,
'eval_order': 1000
}, {
'match_expression': '^[0-9][0-9a-f_,.-]*$',
'replacement': '*',
'replace_all': false,
'each_segment': true,
'ignore': false,
'terminate_chain': false,
'eval_order': 1001
}, {
'match_expression': '^(.*)/[0-9][0-9a-f_,-]*\\.([0-9a-z][0-9a-z]*)$',
'replacement': '\\1/.*\\2',
'replace_all': false,
'each_segment': false,
'ignore': false,
'terminate_chain': false,
'eval_order': 1002
}])
return normalizer
}
function loadUserNormalizer(config, rules) {
// Load the normalizer.
var normalizer = new MetricNormalizer(config, 'user')
normalizer.loadFromConfig()
if (rules && rules.length) {
rules.forEach(function forEachRule(rule) {
// Add the rule like `API#addNamingRule` would.
normalizer.addSimple(rule[0], '/' + rule[1])
})
}
return normalizer
}
function arrayContainsAny(array) {
for (var i = 1; i < arguments.length; ++i) {
if (array.indexOf(arguments[i]) !== -1) {
return true
}
}
return false
}