-
Notifications
You must be signed in to change notification settings - Fork 13.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Extend metrics for HTTP Request node #3282
Merged
ivov
merged 7 commits into
n8n-3496-discoverability-in-app-nodes-flow
from
n8n-3371-extend-telemetry-2
May 19, 2022
Merged
Changes from 2 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
f8642c8
:zap: Extend metrics
ivov a4efca1
:test_tube: Add tests
ivov 6694513
:twisted_rightwards_arrows: Merge parent branch
ivov 03b7939
:twisted_rightwards_arrows: Merge parent branch
ivov e5f0ddd
:twisted_rightwards_arrows: Merge parent branch
ivov 7148228
:zap: Update param names
ivov e172f0a
:twisted_rightwards_arrows: Merge parent branch
ivov File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -60,6 +60,74 @@ function areOverlapping( | |
); | ||
} | ||
|
||
const URL_PARTS_REGEX = /(?<protocolPlusDomain>.*?\..*?)(?<pathnamePlusQs>\/.*)/; | ||
|
||
export function getDomainBase(raw: string, urlParts = URL_PARTS_REGEX): string { | ||
try { | ||
const url = new URL(raw); | ||
|
||
return [url.protocol, url.hostname].join('//'); | ||
} catch (_) { | ||
const match = urlParts.exec(raw); | ||
|
||
if (!match?.groups?.protocolPlusDomain) return ''; | ||
|
||
return match.groups.protocolPlusDomain; | ||
} | ||
} | ||
|
||
function isSensitive(segment: string) { | ||
return /%40/.test(segment) || /^\d+$/.test(segment) || /^[0-9A-F]{8}/i.test(segment); | ||
} | ||
|
||
export const ANONYMIZATION_CHARACTER = '*'; | ||
|
||
function sanitizeRoute(raw: string, check = isSensitive, char = ANONYMIZATION_CHARACTER) { | ||
return raw | ||
.split('/') | ||
.map((segment) => (check(segment) ? char.repeat(segment.length) : segment)) | ||
.join('/'); | ||
} | ||
|
||
function sanitizeQuery(raw: string, check = isSensitive, char = ANONYMIZATION_CHARACTER) { | ||
return raw | ||
.split('&') | ||
.map((segment) => { | ||
const [key, value] = segment.split('='); | ||
return [key, check(value) ? char.repeat(value.length) : value].join('='); | ||
}) | ||
.join('&'); | ||
} | ||
|
||
function sanitizeUrl(raw: string) { | ||
if (/\?/.test(raw)) { | ||
const [route, query] = raw.split('?'); | ||
|
||
return [sanitizeRoute(route), sanitizeQuery(query)].join('?'); | ||
} | ||
|
||
return sanitizeRoute(raw); | ||
} | ||
|
||
/** | ||
* Return pathname plus query string from URL, anonymizing IDs in route and query params. | ||
*/ | ||
export function getDomainPath(raw: string, urlParts = URL_PARTS_REGEX): string { | ||
try { | ||
const url = new URL(raw); | ||
|
||
if (!url.hostname) throw new Error('Malformed URL'); | ||
|
||
return sanitizeUrl(url.pathname + url.search); | ||
} catch (_) { | ||
const match = urlParts.exec(raw); | ||
|
||
if (!match?.groups?.pathnamePlusQs) return ''; | ||
|
||
return sanitizeUrl(match.groups.pathnamePlusQs); | ||
} | ||
} | ||
|
||
export function generateNodesGraph( | ||
workflow: IWorkflowBase, | ||
nodeTypes: INodeTypes, | ||
|
@@ -100,12 +168,29 @@ export function generateNodesGraph( | |
position: node.position, | ||
}; | ||
|
||
if (node.type === 'n8n-nodes-base.httpRequest') { | ||
if (node.type === 'n8n-nodes-base.httpRequest' && node.typeVersion === 1) { | ||
try { | ||
nodeItem.domain = new URL(node.parameters.url as string).hostname; | ||
} catch (e) { | ||
nodeItem.domain = node.parameters.url as string; | ||
} catch (_) { | ||
nodeItem.domain = getDomainBase(node.parameters.url as string); | ||
} | ||
} else if (node.type === 'n8n-nodes-base.httpRequest' && node.typeVersion === 2) { | ||
const { authenticateWith } = node.parameters as { authenticateWith: string }; | ||
|
||
nodeItem.credential_type = { | ||
none: 'none', | ||
genericAuth: node.parameters.genericAuthType as string, | ||
nodeCredential: node.parameters.nodeCredentialType as string, | ||
}[authenticateWith]; | ||
|
||
nodeItem.credential_set = node.credentials | ||
? Object.keys(node.credentials).length > 0 | ||
: false; | ||
|
||
const { url } = node.parameters as { url: string }; | ||
|
||
nodeItem.domain_base = getDomainBase(url); | ||
nodeItem.domain_path = getDomainPath(url); | ||
Comment on lines
+192
to
+193
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. FYI namings chosen by Product. |
||
} else { | ||
const nodeType = nodeTypes.getByNameAndVersion(node.type); | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,176 @@ | ||
import { v5 as uuidv5, v3 as uuidv3, v4 as uuidv4, v1 as uuidv1 } from 'uuid'; | ||
import { | ||
ANONYMIZATION_CHARACTER as CHAR, | ||
getDomainBase, | ||
getDomainPath, | ||
} from '../src/TelemetryHelpers'; | ||
|
||
describe('getDomainBase should return protocol plus domain', () => { | ||
test('in valid URLs', () => { | ||
for (const url of validUrls(numericId)) { | ||
const { full, protocolPlusDomain } = url; | ||
expect(getDomainBase(full)).toBe(protocolPlusDomain); | ||
} | ||
}); | ||
|
||
test('in malformed URLs', () => { | ||
for (const url of malformedUrls(numericId)) { | ||
const { full, protocolPlusDomain } = url; | ||
expect(getDomainBase(full)).toBe(protocolPlusDomain); | ||
} | ||
}); | ||
}); | ||
|
||
describe('getDomainPath should return pathname plus query string', () => { | ||
describe('anonymizing numeric IDs', () => { | ||
test('in valid URLs', () => { | ||
for (const url of validUrls(numericId)) { | ||
const { full, pathnamePlusQs } = url; | ||
expect(getDomainPath(full)).toBe(pathnamePlusQs); | ||
} | ||
}); | ||
|
||
test('in malformed URLs', () => { | ||
for (const url of malformedUrls(numericId)) { | ||
const { full, pathnamePlusQs } = url; | ||
expect(getDomainPath(full)).toBe(pathnamePlusQs); | ||
} | ||
}); | ||
}); | ||
|
||
describe('anonymizing UUIDs', () => { | ||
test('in valid URLs', () => { | ||
for (const url of uuidUrls(validUrls)) { | ||
const { full, pathnamePlusQs } = url; | ||
expect(getDomainPath(full)).toBe(pathnamePlusQs); | ||
} | ||
}); | ||
|
||
test('in malformed URLs', () => { | ||
for (const url of uuidUrls(malformedUrls)) { | ||
const { full, pathnamePlusQs } = url; | ||
expect(getDomainPath(full)).toBe(pathnamePlusQs); | ||
} | ||
}); | ||
}); | ||
|
||
describe('anonymizing emails', () => { | ||
test('in valid URLs', () => { | ||
for (const url of validUrls(email)) { | ||
const { full, pathnamePlusQs } = url; | ||
expect(getDomainPath(full)).toBe(pathnamePlusQs); | ||
} | ||
}); | ||
|
||
test('in malformed URLs', () => { | ||
for (const url of malformedUrls(email)) { | ||
const { full, pathnamePlusQs } = url; | ||
expect(getDomainPath(full)).toBe(pathnamePlusQs); | ||
} | ||
}); | ||
}); | ||
}); | ||
|
||
function validUrls(idMaker: typeof numericId | typeof email, char = CHAR) { | ||
const firstId = idMaker(); | ||
const secondId = idMaker(); | ||
const firstIdObscured = char.repeat(firstId.length); | ||
const secondIdObscured = char.repeat(secondId.length); | ||
|
||
return [ | ||
{ | ||
full: `https://test.com/api/v1/users/${firstId}`, | ||
protocolPlusDomain: 'https://test.com', | ||
pathnamePlusQs: `/api/v1/users/${firstIdObscured}`, | ||
}, | ||
{ | ||
full: `https://test.com/api/v1/users/${firstId}/`, | ||
protocolPlusDomain: 'https://test.com', | ||
pathnamePlusQs: `/api/v1/users/${firstIdObscured}/`, | ||
}, | ||
{ | ||
full: `https://test.com/api/v1/users/${firstId}/posts/${secondId}`, | ||
protocolPlusDomain: 'https://test.com', | ||
pathnamePlusQs: `/api/v1/users/${firstIdObscured}/posts/${secondIdObscured}`, | ||
}, | ||
{ | ||
full: `https://test.com/api/v1/users/${firstId}/posts/${secondId}/`, | ||
protocolPlusDomain: 'https://test.com', | ||
pathnamePlusQs: `/api/v1/users/${firstIdObscured}/posts/${secondIdObscured}/`, | ||
}, | ||
{ | ||
full: `https://test.com/api/v1/users/${firstId}/posts/${secondId}/`, | ||
protocolPlusDomain: 'https://test.com', | ||
pathnamePlusQs: `/api/v1/users/${firstIdObscured}/posts/${secondIdObscured}/`, | ||
}, | ||
{ | ||
full: `https://test.com/api/v1/users?id=${firstId}`, | ||
protocolPlusDomain: 'https://test.com', | ||
pathnamePlusQs: `/api/v1/users?id=${firstIdObscured}`, | ||
}, | ||
{ | ||
full: `https://test.com/api/v1/users?id=${firstId}&post=${secondId}`, | ||
protocolPlusDomain: 'https://test.com', | ||
pathnamePlusQs: `/api/v1/users?id=${firstIdObscured}&post=${secondIdObscured}`, | ||
}, | ||
]; | ||
} | ||
|
||
function malformedUrls(idMaker: typeof numericId | typeof email, char = CHAR) { | ||
const firstId = idMaker(); | ||
const secondId = idMaker(); | ||
const firstIdObscured = char.repeat(firstId.length); | ||
const secondIdObscured = char.repeat(secondId.length); | ||
|
||
return [ | ||
{ | ||
full: `test.com/api/v1/users/${firstId}/posts/${secondId}/`, | ||
protocolPlusDomain: 'test.com', | ||
pathnamePlusQs: `/api/v1/users/${firstIdObscured}/posts/${secondIdObscured}/`, | ||
}, | ||
{ | ||
full: `htp://test.com/api/v1/users/${firstId}/posts/${secondId}/`, | ||
protocolPlusDomain: 'htp://test.com', | ||
pathnamePlusQs: `/api/v1/users/${firstIdObscured}/posts/${secondIdObscured}/`, | ||
}, | ||
{ | ||
full: `test.com/api/v1/users?id=${firstId}`, | ||
protocolPlusDomain: 'test.com', | ||
pathnamePlusQs: `/api/v1/users?id=${firstIdObscured}`, | ||
}, | ||
{ | ||
full: `test.com/api/v1/users?id=${firstId}&post=${secondId}`, | ||
protocolPlusDomain: 'test.com', | ||
pathnamePlusQs: `/api/v1/users?id=${firstIdObscured}&post=${secondIdObscured}`, | ||
}, | ||
]; | ||
} | ||
|
||
const email = () => encodeURIComponent('test@test.com'); | ||
|
||
function uuidUrls( | ||
urlsMaker: typeof validUrls | typeof malformedUrls, | ||
baseName = 'test', | ||
namespaceUuid = uuidv4(), | ||
) { | ||
return [ | ||
...urlsMaker(() => uuidv5(baseName, namespaceUuid)), | ||
...urlsMaker(uuidv4), | ||
...urlsMaker(() => uuidv3(baseName, namespaceUuid)), | ||
...urlsMaker(uuidv1), | ||
]; | ||
} | ||
|
||
function digit() { | ||
return Math.floor(Math.random() * 10); | ||
} | ||
|
||
function positiveDigit(): number { | ||
const d = digit(); | ||
|
||
return d === 0 ? positiveDigit() : d; | ||
} | ||
|
||
function numericId(length = positiveDigit()) { | ||
return Array.from({ length }, digit).join(''); | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
URL constructor may succeed and still misdetect hostname as part of pathname, e.g. when misspelling the protocol.