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

Fix Default Logger Settings and Add Console Log Level Support #1326

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
15 changes: 7 additions & 8 deletions src/agent/agentLoader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -259,10 +259,10 @@ export class AgentLoader {
distroInstance = require.resolve(`${process.cwd()}/node_modules/@azure/monitor-opentelemetry`);
}
/**
* If loaded instance is in Azure machine home path do not attach the SDK, this means customer already instrumented their app.
* Linux App Service doesn't append the full cwd to the require.resolve, so we need to check for the relative path we expect
* if application insights is being imported in the user app code.
*/
* If loaded instance is in Azure machine home path do not attach the SDK, this means customer already instrumented their app.
* Linux App Service doesn't append the full cwd to the require.resolve, so we need to check for the relative path we expect
* if application insights is being imported in the user app code.
*/
if (
shimInstance.indexOf("home") > -1 || distroInstance.indexOf("home") > -1 ||
(shimInstance === LINUX_USER_APPLICATION_INSIGHTS_PATH && this._isLinux)
Expand All @@ -274,10 +274,9 @@ export class AgentLoader {
this._diagnosticLogger.logMessage(diagnosticLog);
return true;
}
else {
// ApplicationInsights or Azure Monitor Distro could be loaded outside of customer application, attach in this case
return false;
}
// ApplicationInsights or Azure Monitor Distro could be loaded outside of customer application, attach in this case
return false;


} catch (e) {
// crashed while trying to resolve "applicationinsights", so SDK does not exist. Attach appinsights
Expand Down
7 changes: 7 additions & 0 deletions src/agent/aksLoader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { DiagnosticLogger } from './diagnostics/diagnosticLogger';
import { FileWriter } from "./diagnostics/writers/fileWriter";
import { StatusLogger } from "./diagnostics/statusLogger";
import { AgentLoader } from "./agentLoader";
import { InstrumentationOptions } from '../types';

export class AKSLoader extends AgentLoader {

Expand All @@ -18,6 +19,12 @@ export class AKSLoader extends AgentLoader {
// Add OTLP if env variable is present
enabled: process.env["OTEL_EXPORTER_OTLP_METRICS_ENDPOINT"] ? true : false
};
(this._options.instrumentationOptions as InstrumentationOptions) = {
...this._options.instrumentationOptions,
console: { enabled: true },
bunyan: { enabled: true },
winston: { enabled: true },
}

let statusLogDir = '/var/log/applicationinsights/';
if (this._isWindows) {
Expand Down
2 changes: 1 addition & 1 deletion src/logs/diagnostic-channel/console.sub.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ let logger: Logger;
let logSendingLevel: SeverityNumber;

const subscriber = (event: IStandardEvent<consolePub.IConsoleData>) => {
const severity = (event.data.message as string | Error) instanceof Error ? SeverityNumber.ERROR : (event.data.stderr
const severity = event.data.message.indexOf("Error:") > -1 ? SeverityNumber.ERROR : (event.data.stderr
? SeverityNumber.WARN
: SeverityNumber.INFO);
if (logSendingLevel <= severity) {
Expand Down
18 changes: 16 additions & 2 deletions src/shared/configuration/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ import {
} from "@opentelemetry/resources";
import { JsonConfig } from "./jsonConfig";
import { AzureMonitorOpenTelemetryOptions, OTLPExporterConfig, InstrumentationOptions } from "../../types";
import { logLevelParser } from "../util/logLevelParser";

const loggingLevel = "APPLICATIONINSIGHTS_INSTRUMENTATION_LOGGING_LEVEL";


export class ApplicationInsightsConfig {
Expand Down Expand Up @@ -75,9 +78,7 @@ export class ApplicationInsightsConfig {
this._resource = this._getDefaultResource();

// Merge JSON configuration file if available
this._mergeConfig();
// Check for explicitly passed options when instantiating client
// This will take precedence over other settings
if (options) {
if (typeof(options.enableAutoCollectExceptions) === "boolean") {
this.enableAutoCollectExceptions = options.enableAutoCollectExceptions;
Expand Down Expand Up @@ -109,6 +110,19 @@ export class ApplicationInsightsConfig {
);
this.resource = Object.assign(this.resource, options.resource);
this.samplingRatio = options.samplingRatio || this.samplingRatio;

// Set console logging level from env var
if (process.env[loggingLevel]) {
this.instrumentationOptions = {
...this.instrumentationOptions,
console: {
...this.instrumentationOptions.console,
logSendingLevel: logLevelParser(process.env[loggingLevel]),
},
}
}

this._mergeConfig();
JacksonWeber marked this conversation as resolved.
Show resolved Hide resolved
}
}

Expand Down
17 changes: 17 additions & 0 deletions src/shared/util/logLevelParser.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.

import { SeverityNumber } from "@opentelemetry/api-logs";

export function logLevelParser(level: string): number {
switch (level) {
case "ERROR":
return SeverityNumber.ERROR;
case "WARN":
return SeverityNumber.WARN;
case "INFO":
return SeverityNumber.INFO;
default:
return SeverityNumber.UNSPECIFIED;
}
}
2 changes: 1 addition & 1 deletion src/shim/shim-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ class Config implements IConfig {
};
(options.instrumentationOptions as InstrumentationOptions) = {
...options.instrumentationOptions,
console: { enabled: true },
console: { enabled: false },
winston: { enabled: true },
};
if (this.samplingPercentage) {
Expand Down
2 changes: 2 additions & 0 deletions test/unitTests/agent/agentLoader.tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import * as sinon from "sinon";
import { AgentLoader } from "../../../src/agent/agentLoader";
import * as azureMonitor from "@azure/monitor-opentelemetry";
import { DiagnosticMessageId } from "../../../src/agent/types";
import { dispose } from "../../../src/logs/diagnostic-channel/winston.sub";

describe("agent/agentLoader", () => {
let originalEnv: NodeJS.ProcessEnv;
Expand Down Expand Up @@ -53,6 +54,7 @@ describe("agent/agentLoader", () => {
});

afterEach(() => {
dispose();
process.env = originalEnv;
sandbox.restore();
});
Expand Down
4 changes: 4 additions & 0 deletions test/unitTests/agent/aksLoader.tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import { logs } from "@opentelemetry/api-logs";
import { AKSLoader } from "../../../src/agent/aksLoader";
import { DiagnosticLogger } from "../../../src/agent/diagnostics/diagnosticLogger";
import { FileWriter } from "../../../src/agent/diagnostics/writers/fileWriter";
import { dispose as disposeConsole } from "../../../src/logs/diagnostic-channel/console.sub";
import { dispose as disposeWinston } from "../../../src/logs/diagnostic-channel/winston.sub";

describe("agent/AKSLoader", () => {
let originalEnv: NodeJS.ProcessEnv;
Expand All @@ -20,6 +22,8 @@ describe("agent/AKSLoader", () => {
});

afterEach(() => {
disposeConsole();
disposeWinston();
process.env = originalEnv;
sandbox.restore();
});
Expand Down
2 changes: 1 addition & 1 deletion test/unitTests/logs/console.tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ describe("AutoCollection/Console", () => {
});
const dummyError = new Error("test error");
const errorEvent: console.IConsoleData = {
message: dummyError as any,
message: dummyError.toString(),
stderr: false,
};
channel.publish("console", errorEvent);
Expand Down
4 changes: 2 additions & 2 deletions test/unitTests/shim/config.tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ describe("shim/configuration/config", () => {
redis4: { enabled: false },
postgreSql: { enabled: false },
bunyan: { enabled: true },
console: { enabled: true },
console: { enabled: false },
winston: { enabled: true },
}));
});
Expand Down Expand Up @@ -189,7 +189,7 @@ describe("shim/configuration/config", () => {
"redis4": { "enabled": true },
"postgreSql": { "enabled": true },
"bunyan": { "enabled": false },
"console": { "enabled": true },
"console": { "enabled": false },
"winston": { "enabled": false }
}));
});
Expand Down
Loading