Skip to content

Commit cbd15ec

Browse files
authored
Fix Default Logger Settings and Add Console Log Level Support (#1326)
* Fix default logger settings and add console log level support. * Clean up. * Update to disable bunyan and winston in non-AKS auto-attach.
1 parent 0a74f1a commit cbd15ec

File tree

10 files changed

+58
-15
lines changed

10 files changed

+58
-15
lines changed

src/agent/agentLoader.ts

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -259,10 +259,10 @@ export class AgentLoader {
259259
distroInstance = require.resolve(`${process.cwd()}/node_modules/@azure/monitor-opentelemetry`);
260260
}
261261
/**
262-
* If loaded instance is in Azure machine home path do not attach the SDK, this means customer already instrumented their app.
263-
* Linux App Service doesn't append the full cwd to the require.resolve, so we need to check for the relative path we expect
264-
* if application insights is being imported in the user app code.
265-
*/
262+
* If loaded instance is in Azure machine home path do not attach the SDK, this means customer already instrumented their app.
263+
* Linux App Service doesn't append the full cwd to the require.resolve, so we need to check for the relative path we expect
264+
* if application insights is being imported in the user app code.
265+
*/
266266
if (
267267
shimInstance.indexOf("home") > -1 || distroInstance.indexOf("home") > -1 ||
268268
(shimInstance === LINUX_USER_APPLICATION_INSIGHTS_PATH && this._isLinux)
@@ -274,10 +274,9 @@ export class AgentLoader {
274274
this._diagnosticLogger.logMessage(diagnosticLog);
275275
return true;
276276
}
277-
else {
278-
// ApplicationInsights or Azure Monitor Distro could be loaded outside of customer application, attach in this case
279-
return false;
280-
}
277+
// ApplicationInsights or Azure Monitor Distro could be loaded outside of customer application, attach in this case
278+
return false;
279+
281280

282281
} catch (e) {
283282
// crashed while trying to resolve "applicationinsights", so SDK does not exist. Attach appinsights

src/agent/aksLoader.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import { DiagnosticLogger } from './diagnostics/diagnosticLogger';
77
import { FileWriter } from "./diagnostics/writers/fileWriter";
88
import { StatusLogger } from "./diagnostics/statusLogger";
99
import { AgentLoader } from "./agentLoader";
10+
import { InstrumentationOptions } from '../types';
1011

1112
export class AKSLoader extends AgentLoader {
1213

@@ -18,6 +19,12 @@ export class AKSLoader extends AgentLoader {
1819
// Add OTLP if env variable is present
1920
enabled: process.env["OTEL_EXPORTER_OTLP_METRICS_ENDPOINT"] ? true : false
2021
};
22+
(this._options.instrumentationOptions as InstrumentationOptions) = {
23+
...this._options.instrumentationOptions,
24+
console: { enabled: true },
25+
bunyan: { enabled: true },
26+
winston: { enabled: true },
27+
}
2128

2229
let statusLogDir = '/var/log/applicationinsights/';
2330
if (this._isWindows) {

src/logs/diagnostic-channel/console.sub.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ let logger: Logger;
1111
let logSendingLevel: SeverityNumber;
1212

1313
const subscriber = (event: IStandardEvent<consolePub.IConsoleData>) => {
14-
const severity = (event.data.message as string | Error) instanceof Error ? SeverityNumber.ERROR : (event.data.stderr
14+
const severity = event.data.message.indexOf("Error:") > -1 ? SeverityNumber.ERROR : (event.data.stderr
1515
? SeverityNumber.WARN
1616
: SeverityNumber.INFO);
1717
if (logSendingLevel <= severity) {

src/shared/configuration/config.ts

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@ import {
1111
} from "@opentelemetry/resources";
1212
import { JsonConfig } from "./jsonConfig";
1313
import { AzureMonitorOpenTelemetryOptions, OTLPExporterConfig, InstrumentationOptions } from "../../types";
14+
import { logLevelParser } from "../util/logLevelParser";
15+
16+
const loggingLevel = "APPLICATIONINSIGHTS_INSTRUMENTATION_LOGGING_LEVEL";
1417

1518

1619
export class ApplicationInsightsConfig {
@@ -75,9 +78,7 @@ export class ApplicationInsightsConfig {
7578
this._resource = this._getDefaultResource();
7679

7780
// Merge JSON configuration file if available
78-
this._mergeConfig();
7981
// Check for explicitly passed options when instantiating client
80-
// This will take precedence over other settings
8182
if (options) {
8283
if (typeof(options.enableAutoCollectExceptions) === "boolean") {
8384
this.enableAutoCollectExceptions = options.enableAutoCollectExceptions;
@@ -109,6 +110,19 @@ export class ApplicationInsightsConfig {
109110
);
110111
this.resource = Object.assign(this.resource, options.resource);
111112
this.samplingRatio = options.samplingRatio || this.samplingRatio;
113+
114+
// Set console logging level from env var
115+
if (process.env[loggingLevel]) {
116+
this.instrumentationOptions = {
117+
...this.instrumentationOptions,
118+
console: {
119+
...this.instrumentationOptions.console,
120+
logSendingLevel: logLevelParser(process.env[loggingLevel]),
121+
},
122+
}
123+
}
124+
125+
this._mergeConfig();
112126
}
113127
}
114128

src/shared/util/logLevelParser.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// Copyright (c) Microsoft Corporation.
2+
// Licensed under the MIT license.
3+
4+
import { SeverityNumber } from "@opentelemetry/api-logs";
5+
6+
export function logLevelParser(level: string): number {
7+
switch (level) {
8+
case "ERROR":
9+
return SeverityNumber.ERROR;
10+
case "WARN":
11+
return SeverityNumber.WARN;
12+
case "INFO":
13+
return SeverityNumber.INFO;
14+
default:
15+
return SeverityNumber.UNSPECIFIED;
16+
}
17+
}

src/shim/shim-config.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ class Config implements IConfig {
148148
};
149149
(options.instrumentationOptions as InstrumentationOptions) = {
150150
...options.instrumentationOptions,
151-
console: { enabled: true },
151+
console: { enabled: false },
152152
winston: { enabled: true },
153153
};
154154
if (this.samplingPercentage) {

test/unitTests/agent/agentLoader.tests.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import * as sinon from "sinon";
77
import { AgentLoader } from "../../../src/agent/agentLoader";
88
import * as azureMonitor from "@azure/monitor-opentelemetry";
99
import { DiagnosticMessageId } from "../../../src/agent/types";
10+
import { dispose } from "../../../src/logs/diagnostic-channel/winston.sub";
1011

1112
describe("agent/agentLoader", () => {
1213
let originalEnv: NodeJS.ProcessEnv;
@@ -53,6 +54,7 @@ describe("agent/agentLoader", () => {
5354
});
5455

5556
afterEach(() => {
57+
dispose();
5658
process.env = originalEnv;
5759
sandbox.restore();
5860
});

test/unitTests/agent/aksLoader.tests.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ import { logs } from "@opentelemetry/api-logs";
66
import { AKSLoader } from "../../../src/agent/aksLoader";
77
import { DiagnosticLogger } from "../../../src/agent/diagnostics/diagnosticLogger";
88
import { FileWriter } from "../../../src/agent/diagnostics/writers/fileWriter";
9+
import { dispose as disposeConsole } from "../../../src/logs/diagnostic-channel/console.sub";
10+
import { dispose as disposeWinston } from "../../../src/logs/diagnostic-channel/winston.sub";
911

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

2224
afterEach(() => {
25+
disposeConsole();
26+
disposeWinston();
2327
process.env = originalEnv;
2428
sandbox.restore();
2529
});

test/unitTests/logs/console.tests.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ describe("AutoCollection/Console", () => {
4141
});
4242
const dummyError = new Error("test error");
4343
const errorEvent: console.IConsoleData = {
44-
message: dummyError as any,
44+
message: dummyError.toString(),
4545
stderr: false,
4646
};
4747
channel.publish("console", errorEvent);

test/unitTests/shim/config.tests.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ describe("shim/configuration/config", () => {
159159
redis4: { enabled: false },
160160
postgreSql: { enabled: false },
161161
bunyan: { enabled: true },
162-
console: { enabled: true },
162+
console: { enabled: false },
163163
winston: { enabled: true },
164164
}));
165165
});
@@ -189,7 +189,7 @@ describe("shim/configuration/config", () => {
189189
"redis4": { "enabled": true },
190190
"postgreSql": { "enabled": true },
191191
"bunyan": { "enabled": false },
192-
"console": { "enabled": true },
192+
"console": { "enabled": false },
193193
"winston": { "enabled": false }
194194
}));
195195
});

0 commit comments

Comments
 (0)