This repository has been archived by the owner on Jun 13, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
d06a451
commit eb916eb
Showing
8 changed files
with
99 additions
and
83 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
86 changes: 31 additions & 55 deletions
86
packages/react/src/instrumentation/redirectInstrumentation.js
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 |
---|---|---|
@@ -1,62 +1,38 @@ | ||
/* eslint max-len: ["error", { "ignoreComments": true }] */ | ||
|
||
/* eslint no-param-reassign: ["error", { "props": true, "ignorePropertyModificationsFor": ["parentSpan"] }] */ | ||
const EpsagonUtils = require('@epsagon/web').EpsagonUtils; | ||
|
||
function addEpsSpanAttrs(span, parentSpan) { | ||
if(parentSpan.identifyFields){ | ||
const { userId, userName, userEmail, companyId, companyName } = parentSpan.identifyFields; | ||
if (userId) span.setAttribute('user.id', parentSpan.identifyFields.userId); | ||
if (userName) span.setAttribute('user.name', parentSpan.identifyFields.name); | ||
if (userEmail) span.setAttribute('user.email', parentSpan.identifyFields.email); | ||
if (companyId) span.setAttribute('company.id', parentSpan.identifyFields.companyId); | ||
if (companyName) span.setAttribute('company.name', parentSpan.identifyFields.companyName); | ||
} | ||
if(parentSpan.tags){ | ||
for(let key in parentSpan.tags){ | ||
span.setAttribute(key, parentSpan.tags[key]); | ||
} | ||
} | ||
} | ||
|
||
|
||
function ReactRedirectInstrumentation(history, tracer, parentSpan) { | ||
const getInitPathName = () => { | ||
if (history && history.location) { | ||
return history.location.pathname; | ||
} | ||
|
||
return undefined; | ||
}; | ||
|
||
let span; | ||
const startSpan = (originPath) => { | ||
span = tracer.startSpan('route_change', { | ||
attributes: { | ||
operation: 'route_change', | ||
type: 'browser', | ||
previousPage: originPath, | ||
}, | ||
|
||
const startSpan = (originPath, newPath) => { | ||
return tracer.startSpan('route_change', { | ||
attributes: { | ||
operation: 'route_change', | ||
type: 'browser', | ||
previousPage: originPath, | ||
path: newPath, | ||
}, | ||
}); | ||
}; | ||
|
||
history.listen((location, action) => { | ||
let currentPath = `${location.pathname}${window.location.hash}` | ||
if (action && (action.toLowerCase() === 'push' || action.toLowerCase() === 'pop') && (parentSpan.path != currentPath)) { | ||
let span = startSpan(parentSpan.path, location.pathname) | ||
parentSpan.currentSpan = span; | ||
if(location.hash && location.hash.length){span.setAttribute('hash', location.hash)}; | ||
if(location.search && location.search.length){span.setAttribute('search', location.search)}; | ||
if(location.state && location.state.length){span.setAttribute('history.state', location.state)}; | ||
span.setAttribute('action', action); | ||
span.setStatus({ code: 0 }); | ||
EpsagonUtils.addEpsSpanAttrs(span, parentSpan); | ||
span.end(); | ||
parentSpan.path = currentPath; | ||
} | ||
|
||
}); | ||
}; | ||
|
||
const initPathName = getInitPathName(); | ||
startSpan(initPathName); | ||
|
||
history.listen((location, action) => { | ||
if (action && (action.toLowerCase() === 'push' || action.toLowerCase() === 'pop')) { | ||
if (span) { | ||
parentSpan.currentSpan = span; | ||
span.setAttribute('path', location.pathname); | ||
span.setAttribute('hash', location.hash); | ||
span.setAttribute('search', location.search); | ||
span.setAttribute('history.state', location.state); | ||
span.setAttribute('action', action); | ||
span.setStatus({ code: 0 }); | ||
addEpsSpanAttrs(span, parentSpan); | ||
span.end(); | ||
startSpan(location.pathname); | ||
} | ||
} | ||
}); | ||
} | ||
|
||
} | ||
|
||
export default ReactRedirectInstrumentation; |
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 |
---|---|---|
@@ -1,6 +1,6 @@ | ||
{ | ||
"include": [ | ||
"src/**/*" | ||
"src/*" | ||
], | ||
"compilerOptions": { | ||
"allowJs": true, | ||
|
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
38 changes: 38 additions & 0 deletions
38
packages/web/src/instrumentation/redirectInstrumentation.js
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,38 @@ | ||
import EpsagonUtils from '../utils'; | ||
|
||
class EpsagonRedirectInstrumentation{ | ||
|
||
constructor(tracer, parentSpan, resetTimer) { | ||
this.parentSpan = parentSpan; | ||
this.parentSpan.path = `${window.location.pathname}${window.location.hash}`; | ||
this.tracer = tracer; | ||
setInterval(()=>{ | ||
let currentPath = `${window.location.pathname}${window.location.hash}`; | ||
if (this.parentSpan.path && currentPath != parentSpan.path){ | ||
this.createRouteChangeSpan(this.parentSpan.path, currentPath); | ||
this.parentSpan.path = currentPath; | ||
} | ||
}, resetTimer) | ||
} | ||
|
||
createRouteChangeSpan = (oldPath, newPath) => { | ||
let span = this.tracer.startSpan('route_change', { | ||
attributes: { | ||
"operation": "route_change", | ||
"type": "browser", | ||
"previousPage": oldPath, | ||
"path": newPath, | ||
"http.url": window.location.href | ||
} | ||
}); | ||
this.parentSpan.currentSpan = span; | ||
if(window.location.hash){ | ||
span.setAttribute('hash', window.location.hash); | ||
} | ||
EpsagonUtils.addEpsSpanAttrs(span, this.parentSpan); | ||
span.setStatus({ code: 0 }); | ||
span.end(); | ||
} | ||
} | ||
|
||
export default EpsagonRedirectInstrumentation; |
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