Skip to content

Commit

Permalink
fix: remove invalid chars in span tags and marks (#34)
Browse files Browse the repository at this point in the history
  • Loading branch information
vigneshshanmugam authored and jahtalab committed Jan 28, 2019
1 parent 2000ee2 commit 9bdc575
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 17 deletions.
9 changes: 7 additions & 2 deletions src/common/utils.js
Expand Up @@ -148,7 +148,7 @@ function sanitizeString (value, limit, required, placeholder) {

function setTag (key, value, obj) {
if (!obj || !key) return
var skey = key.replace(/[.*]/g, '_')
var skey = removeInvalidChars(key)
obj[skey] = sanitizeString(value, constants.serverStringLimit)
return obj
}
Expand Down Expand Up @@ -324,6 +324,10 @@ function find (array, predicate, thisArg) {
return undefined
}

function removeInvalidChars (key) {
return key.replace(/[.*"]/g, '_')
}

module.exports = {
extend: function extend (dst) {
return baseExtend(dst, slice.call(arguments, 1), false)
Expand Down Expand Up @@ -445,5 +449,6 @@ module.exports = {
sanitizeObjectStrings,
setTag,
stripQueryStringFromUrl,
find
find,
removeInvalidChars
}
10 changes: 5 additions & 5 deletions src/performance-monitoring/span-base.js
Expand Up @@ -23,15 +23,15 @@
*
*/

const utils = require('../common/utils')
const { isUndefined, generateRandomId, setTag, merge } = require('../common/utils')
class SpanBase {
// context

constructor (name, type, options) {
this.options = options || {}
this.name = name || this.options.name || 'Unknown'
this.type = type || this.options.type || 'custom'
this.id = this.options.id || utils.generateRandomId(16)
this.id = this.options.id || generateRandomId(16)
this.traceId = this.options.traceId
this.sampled = this.options.sampled
this.timestamp = this.options.timestamp || Date.now()
Expand All @@ -55,14 +55,14 @@ class SpanBase {
}
var keys = Object.keys(tags)
keys.forEach(function (k) {
utils.setTag(k, tags[k], ctx.tags)
setTag(k, tags[k], ctx.tags)
})
}

addContext (context) {
if (!context) return
this.ensureContext()
utils.merge(this.context, context)
merge(this.context, context)
}

end () {
Expand All @@ -82,7 +82,7 @@ class SpanBase {
}

duration () {
if (utils.isUndefined(this._end) || utils.isUndefined(this._start)) {
if (isUndefined(this._end) || isUndefined(this._start)) {
return null
}

Expand Down
27 changes: 17 additions & 10 deletions src/performance-monitoring/transaction.js
Expand Up @@ -25,13 +25,20 @@

const Span = require('./span')
const SpanBase = require('./span-base')

var utils = require('../common/utils')
const {
generateRandomId,
getNavigationTimingMarks,
getPaintTimingMarks,
merge,
extend,
getPageMetadata,
removeInvalidChars
} = require('../common/utils')

class Transaction extends SpanBase {
constructor (name, type, options) {
super(name, type, options)
this.traceId = utils.generateRandomId()
this.traceId = generateRandomId()
this.marks = undefined

this.spans = []
Expand All @@ -47,8 +54,8 @@ class Transaction extends SpanBase {
}

addNavigationTimingMarks () {
var marks = utils.getNavigationTimingMarks()
var paintMarks = utils.getPaintTimingMarks()
var marks = getNavigationTimingMarks()
var paintMarks = getPaintTimingMarks()
if (marks) {
var agent = {
timeToFirstByte: marks.responseStart,
Expand All @@ -63,15 +70,15 @@ class Transaction extends SpanBase {
}

addMarks (obj) {
this.marks = utils.merge(this.marks || {}, obj)
this.marks = merge(this.marks || {}, obj)
}

mark (key) {
var skey = key.replace(/[.*]/g, '_')
var skey = removeInvalidChars(key)
var now = window.performance.now() - this._start
var custom = {}
custom[skey] = now
this.addMarks({ custom: custom })
this.addMarks({ custom })
}

redefine (name, type, options) {
Expand All @@ -85,7 +92,7 @@ class Transaction extends SpanBase {
return
}
var transaction = this
var opts = utils.extend({}, options)
var opts = extend({}, options)

opts.onEnd = function (trc) {
transaction._onSpanEnd(trc)
Expand Down Expand Up @@ -125,7 +132,7 @@ class Transaction extends SpanBase {
span.end()
}

var metadata = utils.getPageMetadata()
var metadata = getPageMetadata()
this.addContext(metadata)

this._adjustStartToEarliestSpan()
Expand Down
6 changes: 6 additions & 0 deletions test/common/utils.spec.js
Expand Up @@ -286,4 +286,10 @@ describe('lib/utils', function () {
result = utils.find(2, function () {})
expect(result).toBe(undefined)
})

it('should remove invalid characters', () => {
expect(utils.removeInvalidChars('invalid*')).toEqual('invalid_')
expect(utils.removeInvalidChars('invalid1.invalid2')).toEqual('invalid1_invalid2')
expect(utils.removeInvalidChars('invalid"')).toEqual('invalid_')
})
})

0 comments on commit 9bdc575

Please sign in to comment.