openvidu-pro: Index browser logs based on finalUserId

pull/623/head
cruizba 2021-04-29 18:45:26 +02:00
parent 7c494b0476
commit a8917c17ac
3 changed files with 29 additions and 28 deletions

View File

@ -1323,12 +1323,12 @@ export class Session extends EventDispatcher {
reject(error); reject(error);
} else { } else {
// Configure JSNLogs
OpenViduLogger.configureJSNLog(this.openvidu, this.sessionId, response.id, token);
// Process join room response // Process join room response
this.processJoinRoomResponse(response); this.processJoinRoomResponse(response);
// Configure JSNLogs
OpenViduLogger.configureJSNLog(this.openvidu, token);
// Initialize local Connection object with values returned by openvidu-server // Initialize local Connection object with values returned by openvidu-server
this.connection = new Connection(this, response); this.connection = new Connection(this, response);

View File

@ -12,29 +12,36 @@ export class OpenViduLogger {
private MAX_MSECONDS_BATCH_MESSAGES: number = 5000; private MAX_MSECONDS_BATCH_MESSAGES: number = 5000;
private logger: Console = window.console; private logger: Console = window.console;
private loggingSessionId: string;
private LOG_FNS = [this.logger.log, this.logger.debug, this.logger.info, this.logger.warn, this.logger.error]; private LOG_FNS = [this.logger.log, this.logger.debug, this.logger.info, this.logger.warn, this.logger.error];
private currentAppender: any; private currentAppender: any;
private isProdMode = false; private isProdMode = false;
private isJSNLogSetup = false; private isJSNLogSetup = false;
// This two variables are used to restart JSNLog
// on different sessions and different userIds
private loggingSessionId: string | undefined;
private loggingFinalUserId: string | undefined;
private constructor() {} private constructor() {}
static configureJSNLog(openVidu: OpenVidu, sessionId: string, connectionId: string, token: string) { static configureJSNLog(openVidu: OpenVidu, token: string) {
// If instance is created is OpenVidu Pro // If instance is created is OpenVidu Pro
if (this.instance && openVidu.webrtcStatsInterval > -1 if (this.instance && openVidu.webrtcStatsInterval > -1
// If logs are enabled // If logs are enabled
&& openVidu.sendBrowserLogs === OpenViduLoggerConfiguration.debug && openVidu.sendBrowserLogs === OpenViduLoggerConfiguration.debug
// If diferent session or first session // Only reconfigure it if session or finalUserId has changed
&& sessionId !== this.instance.loggingSessionId) { && this.instance.canConfigureJSNLog(openVidu, this.instance)) {
try { try {
// isJSNLogSetup will not be true until completed setup // isJSNLogSetup will not be true until completed setup
this.instance.isJSNLogSetup = false; this.instance.isJSNLogSetup = false;
this.instance.info("Configuring JSNLogs."); this.instance.info("Configuring JSNLogs.");
const finalUserId = openVidu.finalUserId;
const sessionId = openVidu.session.sessionId;
const beforeSendCallback = (xhr) => { const beforeSendCallback = (xhr) => {
// If 401 or 403 or 404 modify ready and status so JSNLog don't retry to send logs // If 401 or 403 or 404 modify ready and status so JSNLog don't retry to send logs
// https://github.com/mperdeck/jsnlog.js/blob/v2.30.0/jsnlog.ts#L805-L818 // https://github.com/mperdeck/jsnlog.js/blob/v2.30.0/jsnlog.ts#L805-L818
@ -48,16 +55,16 @@ export class OpenViduLogger {
} }
// Headers to identify and authenticate logs // Headers to identify and authenticate logs
xhr.setRequestHeader('Authorization', "Basic " + btoa(`${connectionId}%/%${sessionId}` + ":" + token)); xhr.setRequestHeader('Authorization', "Basic " + btoa(`${finalUserId}%/%${sessionId}` + ":" + token));
xhr.setRequestHeader('X-Requested-With', 'XMLHttpRequest') xhr.setRequestHeader('X-Requested-With', 'XMLHttpRequest')
// Additional headers for OpenVidu // Additional headers for OpenVidu
xhr.setRequestHeader('OV-Connection-Id', connectionId); xhr.setRequestHeader('OV-Final-User-Id', finalUserId);
xhr.setRequestHeader('OV-Session-Id', sessionId); xhr.setRequestHeader('OV-Session-Id', sessionId);
xhr.setRequestHeader('OV-Token', token); xhr.setRequestHeader('OV-Token', token);
} }
// Creation of the appender. // Creation of the appender.
this.instance.currentAppender = JL.createAjaxAppender("appender-" + connectionId); this.instance.currentAppender = JL.createAjaxAppender(`appender-${finalUserId}-${sessionId}`);
this.instance.currentAppender.setOptions({ this.instance.currentAppender.setOptions({
beforeSend: beforeSendCallback, beforeSend: beforeSendCallback,
maxBatchSize: 1000, maxBatchSize: 1000,
@ -70,7 +77,7 @@ export class OpenViduLogger {
const getCircularReplacer = () => { const getCircularReplacer = () => {
const seen = new WeakSet(); const seen = new WeakSet();
return (key, value) => { return (key, value) => {
if (typeof value === "object" && value !== null) { if (typeof value === "object" && value != null) {
if (seen.has(value)) { if (seen.has(value)) {
return; return;
} }
@ -93,11 +100,15 @@ export class OpenViduLogger {
this.instance.isJSNLogSetup = true; this.instance.isJSNLogSetup = true;
this.instance.loggingSessionId = sessionId; this.instance.loggingSessionId = sessionId;
this.instance.loggingFinalUserId = finalUserId;
this.instance.info("JSNLog configured."); this.instance.info("JSNLog configured.");
} catch (e) { } catch (e) {
console.error("Error configuring JSNLog: "); console.error("Error configuring JSNLog: ");
console.error(e); console.error(e);
this.instance.isJSNLogSetup = false; this.instance.isJSNLogSetup = false;
this.instance.loggingSessionId = undefined;
this.instance.loggingFinalUserId = undefined;
this.instance.currentAppender = undefined;
} }
} }
} }
@ -166,4 +177,8 @@ export class OpenViduLogger {
return this.isJSNLogSetup; return this.isJSNLogSetup;
} }
private canConfigureJSNLog(openVidu: OpenVidu, logger: OpenViduLogger): boolean {
return openVidu.session.sessionId != logger.loggingSessionId || openVidu.finalUserId != logger.loggingFinalUserId
}
} }

View File

@ -50,11 +50,11 @@ public class TokenRegister {
* O(1) * O(1)
* Check if the current token string was registered in an active session * Check if the current token string was registered in an active session
* @param token Token string to check if it is registered * @param token Token string to check if it is registered
* @param connectionId Id of the connection to check * @param finalUserId userId of browser to check
* @param sessionId Id of session to check * @param sessionId Id of session to check
* @return <code>true</code> if token was registered. <code>false</code> otherwise * @return <code>true</code> if token was registered. <code>false</code> otherwise
*/ */
public boolean isTokenRegistered(String token, String connectionId, String sessionId) { public boolean isTokenRegistered(String token, String finalUserId, String sessionId) {
if (!this.tokensRegistered.containsKey(token)) { if (!this.tokensRegistered.containsKey(token)) {
// False because token is not registered // False because token is not registered
return false; return false;
@ -77,23 +77,9 @@ public class TokenRegister {
// In this final state, if connectionId is equal to participant public id and session Id is the same, // In this final state, if connectionId is equal to participant public id and session Id is the same,
// the token is registered correctly in the specific connectionId and sessionId // the token is registered correctly in the specific connectionId and sessionId
return participantsByTokens.get(token).getParticipantPublicId().equals(connectionId) return participantsByTokens.get(token).getFinalUserId().equals(finalUserId)
&& participantsByTokens.get(token).getSessionId().equals(sessionId); && participantsByTokens.get(token).getSessionId().equals(sessionId);
} }
/**
* O(1)
* Get registered token from token string
* @param tokenKey string key which represents the token
* @return
*/
public Token getRegisteredToken(String tokenKey) {
Token token = this.tokensRegistered.get(tokenKey);
if (token != null) {
return token;
}
return null;
}
} }