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

[BUG] Multiple errors are getting thrown and swallowed during initialization when no instrumentation Key is provided #1691 #1695

Merged
merged 1 commit into from
Oct 20, 2021
Merged
Show file tree
Hide file tree
Changes from all 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
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,14 @@ export interface IDiagnosticLogger {
*/
warnToConsole(message: string): void;

/**
* This will write an error to the console if possible.
* Provided by the default DiagnosticLogger instance, and internally the SDK will fall back to warnToConsole, however,
* direct callers MUST check for its existence on the logger as you can provide your own IDiagnosticLogger instance.
* @param message {string} - The error message
*/
errorToConsole?(message: string): void;
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Adding new log to console as an error, so that critical errors are highlighted better.


/**
* Resets the internal message count
*/
Expand Down
12 changes: 5 additions & 7 deletions shared/AppInsightsCore/src/JavaScriptSDK/BaseCore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import { ICookieMgr } from "../JavaScriptSDK.Interfaces/ICookieMgr";
import { createCookieMgr } from "./CookieMgr";
import { arrForEach, isNullOrUndefined, toISOString, getSetValue, setValue, throwError, isNotTruthy, isFunction } from "./HelperFuncs";
import { strExtensionConfig, strIKey } from "./Constants";
import { DiagnosticLogger } from "./DiagnosticLogger";

const validationError = "Extensions must provide callback to initialize";

Expand Down Expand Up @@ -56,11 +57,8 @@ export class BaseCore implements IAppInsightsCore {
dynamicProto(BaseCore, this, (_self) => {
_self._extensions = new Array<IPlugin>();
_channelController = new ChannelController();
_self.logger = objCreateFn({
throwInternal: (severity: LoggingSeverity, msgId: _InternalMessageId, msg: string, properties?: Object, isUserAct = false) => { },
warnToConsole: (message: string) => { },
resetInternalMessageCount: () => { }
});
// Use a default logger so initialization errors are not dropped on the floor with full logging
_self.logger = new DiagnosticLogger({ loggingLevelConsole: LoggingSeverity.CRITICAL });

_eventQueue = [];
_self.isInitialized = () => _isInitialized;
Expand All @@ -74,7 +72,7 @@ export class BaseCore implements IAppInsightsCore {
if (!config || isNullOrUndefined(config.instrumentationKey)) {
throwError("Please provide instrumentation key");
}

_notificationManager = notificationManager;

// For backward compatibility only
Expand All @@ -95,7 +93,7 @@ export class BaseCore implements IAppInsightsCore {
if (logger) {
_self.logger = logger;
}

// Concat all available extensions
let allExtensions = [];
allExtensions.push(...extensions, ...config.extensions);
Expand Down
55 changes: 40 additions & 15 deletions shared/AppInsightsCore/src/JavaScriptSDK/DiagnosticLogger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ const AiUserActionablePrefix = "AI: ";
*/
const AIInternalMessagePrefix = "AITR_";

const strErrorToConsole = "errorToConsole";
const strWarnToConsole = "warnToConsole";

function _sanitizeDiagnosticText(text: string) {
if (text) {
Expand All @@ -33,6 +35,20 @@ function _sanitizeDiagnosticText(text: string) {
return "";
}

function _logToConsole(func: string, message: string) {
let theConsole = getConsole();
if (!!theConsole) {
let logFunc = "log";
if (theConsole[func]) {
logFunc = "func";
}

if (isFunction(theConsole[logFunc])) {
theConsole[logFunc](message);
}
}
}

export class _InternalLogMessage{
public static dataType: string = "MessageData";

Expand Down Expand Up @@ -107,20 +123,23 @@ export class DiagnosticLogger implements IDiagnosticLogger {
if (_self.enableDebugExceptions()) {
throw message;
} else {
// Get the logging function and fallback to warnToConsole of for some reason errorToConsole doesn't exist
let logFunc = severity === LoggingSeverity.CRITICAL ? strErrorToConsole : strWarnToConsole;

if (!isUndefined(message.message)) {
const logLevel = _self.consoleLoggingLevel();
if (isUserAct) {
// check if this message type was already logged to console for this page view and if so, don't log it again
const messageKey: number = +message.messageId;

if (!_messageLogged[messageKey] && logLevel >= LoggingSeverity.WARNING) {
_self.warnToConsole(message.message);
if (!_messageLogged[messageKey] && logLevel >= severity) {
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was a bug as the "logLevel" is documented as 0 - zero == disabled, 1 - critical only and 2 - everything.

The code as previously was treating 0 and 1 as disabled

_self[logFunc](message.message);
_messageLogged[messageKey] = true;
}
} else {
// don't log internal AI traces in the console, unless the verbose logging is enabled
if (logLevel >= LoggingSeverity.WARNING) {
_self.warnToConsole(message.message);
// Only log traces if the console Logging Level is >= the throwInternal severity level
if (logLevel >= severity) {
_self[logFunc](message.message);
}
}

Expand All @@ -134,17 +153,15 @@ export class DiagnosticLogger implements IDiagnosticLogger {
* @param message {string} - The warning message
*/
_self.warnToConsole = (message: string) => {
let theConsole = getConsole();
if (!!theConsole) {
let logFunc = "log";
if (theConsole.warn) {
logFunc = "warn";
}
_logToConsole("warn", message);
}

if (isFunction(theConsole[logFunc])) {
theConsole[logFunc](message);
}
}
/**
* This will write an error to the console if possible
* @param message {string} - The error message
*/
_self.errorToConsole = (message: string) => {
_logToConsole("error", message);
}

/**
Expand Down Expand Up @@ -261,6 +278,14 @@ export class DiagnosticLogger implements IDiagnosticLogger {
// @DynamicProtoStub -- DO NOT add any code as this will be removed during packaging
}

/**
* This will write an error to the console if possible
* @param message {string} - The warning message
*/
public errorToConsole(message: string) {
// @DynamicProtoStub -- DO NOT add any code as this will be removed during packaging
}

/**
* Resets the internal message count
*/
Expand Down