Skip to content
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

feat(rum): categorize transactions based on current url #827

Merged
merged 4 commits into from
Jul 3, 2020
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
98 changes: 98 additions & 0 deletions packages/rum-core/src/common/slugify.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
/**
* MIT License
*
* Copyright (c) 2017-present, Elasticsearch BV
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*
*/

import Url from './url'

/**
* Converts URL path tree in to slug based on tree depth
*/
export default function slugifyUrl(urlStr, depth = 2) {
const parsedUrl = new Url(urlStr)
const { query, path } = parsedUrl
const pathParts = path.substring(1).split('/')

const redactString = ':id'
const end = '**'
const specialCharsRegex = /\W|_/g
const digitsRegex = /[0-9]/g
const lowerCaseRegex = /[a-z]/g
const upperCaseRegex = /[A-Z]/g

const redactedParts = []
let redactedBefore = false

for (let index = 0; index < pathParts.length; index++) {
const part = pathParts[index]

if (redactedBefore || index > depth - 1) {
if (part) {
redactedParts.push(end)
}
break
}

const numberOfSpecialChars = (part.match(specialCharsRegex) || []).length
if (numberOfSpecialChars >= 2) {
redactedParts.push(redactString)
redactedBefore = true
continue
}

const numberOfDigits = (part.match(digitsRegex) || []).length
if (
numberOfDigits > 3 ||
(part.length > 3 && numberOfDigits / part.length >= 0.3)
) {
redactedParts.push(redactString)
redactedBefore = true
continue
}

const numberofUpperCase = (part.match(upperCaseRegex) || []).length
vigneshshanmugam marked this conversation as resolved.
Show resolved Hide resolved
const numberofLowerCase = (part.match(lowerCaseRegex) || []).length
const lowerCaseRate = numberofLowerCase / part.length
const upperCaseRate = numberofUpperCase / part.length
if (
part.length > 5 &&
((upperCaseRate > 0.3 && upperCaseRate < 0.6) ||
(lowerCaseRate > 0.3 && lowerCaseRate < 0.6))
) {
redactedParts.push(redactString)
redactedBefore = true
continue
}

part && redactedParts.push(part)
}

const redacted =
'/' +
(redactedParts.length >= 2
? redactedParts.join('/')
: redactedParts.join('')) +
(query ? '?{query}' : '')

return redacted
}
4 changes: 1 addition & 3 deletions packages/rum-core/src/common/url.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ const RULES = [
]
const PROTOCOL_REGEX = /^([a-z][a-z0-9.+-]*:)?(\/\/)?([\S\s]*)/i

class Url {
export default class Url {
constructor(url) {
let { protocol, address, slashes } = this.extractProtocol(url || '')
const relative = !protocol && !slashes
Expand Down Expand Up @@ -215,5 +215,3 @@ class Url {
}
}
}

export default Url
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ import {
} from '../common/constants'
import { addTransactionContext } from '../common/context'
import { __DEV__, state } from '../state'
import slugifyUrl from '../common/slugify'

class TransactionService {
constructor(logger, config) {
Expand Down Expand Up @@ -240,7 +241,7 @@ class TransactionService {
if (lastHiddenStart >= tr._start) {
if (__DEV__) {
this._logger.debug(
`transaction(${tr.id}, ${tr.name}, ${tr.type}) was discarded! The page was hidden during the transaction!`
`transaction(${tr.id}, ${name}, ${type}) was discarded! The page was hidden during the transaction!`
)
}
return
Expand Down Expand Up @@ -274,6 +275,13 @@ class TransactionService {
tr.spans.push(createTotalBlockingTimeSpan(metrics.tbt))
}
}
/**
* Categorize the transaction based on the current location
*/
if (tr.name === NAME_UNKNOWN) {
tr.name = slugifyUrl(window.location.href)
}
vigneshshanmugam marked this conversation as resolved.
Show resolved Hide resolved

captureNavigation(tr)

/**
Expand Down
68 changes: 68 additions & 0 deletions packages/rum-core/test/common/slugify.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/**
* MIT License
*
* Copyright (c) 2017-present, Elasticsearch BV
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*
*/

import slugifyUrl from '../../src/common/slugify'

describe('Slug URL', () => {
const validate = (before, after, depth) => {
expect(slugifyUrl(before, depth)).toBe(after)
}

it('accept depth parameter', () => {
validate('/a/b/c/d', '/a/b/**')
validate('/a/b/c/d', '/a/**', 1)
})

it('handle trailing slash', () => {
validate('/a/', '/a')
validate('/a', '/a')
validate('/', '/')
})

it('handle query param', () => {
validate('/a/b/?id=hello', '/a/b?{query}')
validate('/?foo=bar&bar=baz', '/?{query}')
vigneshshanmugam marked this conversation as resolved.
Show resolved Hide resolved
})

it('redact digits', () => {
validate('/a/123', '/a/123')
validate('/a/12312bcdd23', '/a/:id')
validate('/a/B00I8BIC9E', '/a/:id')
// uuid
validate('/a/786c1883-dc0b-495f-a16b-6c53fb20b272', '/a/:id')
})

it('redact special characters', () => {
validate('/a-b-c-d', '/:id')
validate('/a~b-c', '/:id')
validate('/a~b-c/d/e/f', '/:id/**')
validate('/b%c%d-ef', '/:id')
})

it('react mix of lower-uppercase characters', () => {
validate('/abcDEF', '/:id')
validate('/a1W9FtW5DnkyP3Bucng4aLvqT/edit', '/:id/**')
})
})