Skip to content

Commit

Permalink
feat: use @azure/logger (#801)
Browse files Browse the repository at this point in the history
* feat: use @azure/logger

* fix tests

* add tags

* fix build

* update

* address comments

* revert error and debug method

Co-authored-by: Jeff Mealo <[email protected]>
Co-authored-by: Hector Hernandez <[email protected]>
  • Loading branch information
3 people authored Oct 28, 2021
1 parent 5d5e8ee commit 1e5ef31
Show file tree
Hide file tree
Showing 9 changed files with 31 additions and 33 deletions.
1 change: 0 additions & 1 deletion AutoCollection/Exceptions.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import http = require("http");

import Contracts = require("../Declarations/Contracts");
import Logging = require("../Library/Logging");
import TelemetryClient = require("../Library/TelemetryClient");
import Sender = require("../Library/Sender");
import Queue = require("../Library/Channel");
Expand Down
1 change: 0 additions & 1 deletion Library/Channel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import Sender = require("./Sender");
import Util = require("./Util");

class Channel {

protected _lastSend: number;
protected _timeoutHandle: any;

Expand Down
6 changes: 1 addition & 5 deletions Library/CorrelationIdManager.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
import https = require('https');
import http = require('http');
import url = require('url');

import Util = require("./Util");
import Logging = require("./Logging");
import Config = require("./Config");
import Logging = require("./Logging");

class CorrelationIdManager {
private static TAG = "CorrelationIdManager";
Expand Down
11 changes: 6 additions & 5 deletions Library/Logging.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,22 @@
import { createClientLogger, AzureLogger } from "@azure/logger";

class Logging {
public static enableDebug = false;
public static disableWarnings = false;
public static disableErrors = false;

private static TAG = "ApplicationInsights:";
public static logger = createClientLogger('ApplicationInsights') as AzureLogger;

public static info(message?: any, ...optionalParams: any[]) {
if(Logging.enableDebug) {
console.info(Logging.TAG + message, optionalParams);
this.logger.info(Logging.TAG + message, optionalParams);
}
}

public static warn(message?: any, ...optionalParams: any[]) {
if(!Logging.disableWarnings) {
console.warn(Logging.TAG + message, optionalParams);
this.logger.warning(Logging.TAG + message, optionalParams);
}
}
}

export = Logging;
export = Logging;
13 changes: 6 additions & 7 deletions Library/Sender.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,15 @@ import os = require("os");
import path = require("path");
import zlib = require("zlib");
import child_process = require("child_process");

import AuthorizationHandler = require("./AuthorizationHandler");
import Logging = require("./Logging");
import Config = require("./Config")
import Contracts = require("../Declarations/Contracts");
import Constants = require("../Declarations/Constants");
import AutoCollectHttpDependencies = require("../AutoCollection/HttpDependencies");
import Statsbeat = require("../AutoCollection/Statsbeat");
import Util = require("./Util");
import { URL } from "url";
import Logging = require("./Logging");


class Sender {
Expand Down Expand Up @@ -49,7 +48,7 @@ class Sender {
protected _resendInterval: number;
protected _maxBytesOnDisk: number;

constructor(config: Config, getAuthorizationHandler?: (config: Config) => AuthorizationHandler, onSuccess?: (response: string) => void, onError?: (error: Error) => void, statsbeat?: Statsbeat) {
constructor(config: Config, getAuthorizationHandler?: (config: Config) => AuthorizationHandler, onSuccess?: (response: string) => void, onError?: (error: Error) => void, statsbeat?: Statsbeat) {
this._config = config;
this._onSuccess = onSuccess;
this._onError = onError;
Expand Down Expand Up @@ -181,7 +180,7 @@ class Sender {
zlib.gzip(payload, (err, buffer) => {
var dataToSend = buffer;
if (err) {
Logging.warn(err);
Logging.warn(Sender.TAG, err);
dataToSend = payload; // something went wrong so send without gzip
options.headers["Content-Length"] = payload.length.toString();
} else {
Expand Down Expand Up @@ -303,7 +302,7 @@ class Sender {
Logging.warn(Sender.TAG, notice, Util.dumpObj(error));
} else {
let notice = "Transient failure to reach ingestion endpoint. This batch of telemetry items will be retried. Error:";
Logging.info(Sender.TAG, notice, Util.dumpObj(error))
Logging.info(Sender.TAG, notice, Util.dumpObj(error));
}
this._onErrorHelper(error);

Expand Down Expand Up @@ -632,7 +631,7 @@ class Sender {
this.send(envelopes);
}
catch (error) {
Logging.warn("Failed to read persisted file", error);
Logging.warn(Sender.TAG, "Failed to read persisted file", error);
}
} else {
this._onErrorHelper(error);
Expand Down Expand Up @@ -661,7 +660,7 @@ class Sender {
try {
return JSON.stringify(payload);
} catch (error) {
Logging.warn("Failed to serialize payload", error, payload);
Logging.warn(Sender.TAG, "Failed to serialize payload", error, payload);
}
}

Expand Down
3 changes: 2 additions & 1 deletion Tests/Library/Channel.tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import sinon = require("sinon");

import Channel = require("../../Library/Channel");
import Contracts = require("../../Declarations/Contracts");
import Logging = require("../../Library/Logging");

class ChannelMock extends Channel {
public getBuffer() {
Expand Down Expand Up @@ -66,7 +67,7 @@ describe("Library/Channel", () => {
});

it("should log warning if invalid input is passed", () => {
var warnStub = sinon.stub(console, "warn");
var warnStub = sinon.stub(Logging, "warn");
channel.send(undefined);
channel.send(null);
channel.send(<any>"");
Expand Down
22 changes: 11 additions & 11 deletions Tests/Library/Logging.tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,19 @@ describe("Library/Logging", () => {

describe("#info(message, ...optionalParams: any)", () => {
it("should do nothing if disabled", () => {
var originalSetting = Logging.enableDebug;
var originalSetting = Logging.disableWarnings;
Logging.enableDebug = false;
var infoStub = sinon.stub(console, "info");
var infoStub = sinon.stub(Logging.logger, "info");
Logging.info("test");
assert.ok(infoStub.notCalled);
infoStub.restore();
Logging.enableDebug = originalSetting;
});

it("should log 'info' if enabled", () => {
it("should log 'info' if called", () => {
var originalSetting = Logging.enableDebug;
Logging.enableDebug = true;
var infoStub = sinon.stub(console, "info");
var infoStub = sinon.stub(Logging.logger, "info");
Logging.info("test");
assert.ok(infoStub.calledOnce);
infoStub.restore();
Expand All @@ -29,19 +29,19 @@ describe("Library/Logging", () => {

describe("#warn(message, ...optionalParams: any)", () => {
it("should do nothing if disabled", () => {
var originalSetting = Logging.enableDebug;
Logging.enableDebug = false;
var warnStub = sinon.stub(console, "warn");
Logging.info("test");
var originalSetting = Logging.disableWarnings;
Logging.disableWarnings = true
var warnStub = sinon.stub(Logging.logger, "warning");
Logging.warn("test");
assert.ok(warnStub.notCalled);
warnStub.restore();
Logging.enableDebug = originalSetting;
});

it("should log 'warn' if enabled", () => {
var originalSetting = Logging.enableDebug;
Logging.enableDebug = true;
var warnStub = sinon.stub(console, "warn");
var originalSetting = Logging.disableWarnings;
Logging.disableWarnings = false;
var warnStub = sinon.stub(Logging.logger, "warning");
Logging.warn("test");
assert.ok(warnStub.calledOnce);
warnStub.restore();
Expand Down
4 changes: 3 additions & 1 deletion Tests/Library/Sender.tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import Contracts = require("../../Declarations/Contracts");
import AuthorizationHandler = require("../../Library/AuthorizationHandler");
import Util = require("../../Library/Util");
import Statsbeat = require("../../AutoCollection/Statsbeat");
import Logging = require("../../Library/Logging");

class SenderMock extends Sender {
public getResendInterval() {
Expand Down Expand Up @@ -66,9 +67,10 @@ describe("Library/Sender", () => {
it("should not crash JSON.stringify", () => {
var a = <any>{ b: null };
a.b = a;
var warnStub = sandbox.stub(console, "warn");
var warnStub = sandbox.stub(Logging, "warn");
assert.doesNotThrow(() => sender.send([a]));
assert.ok(warnStub.calledOnce);
warnStub.restore();
});

it("should try to send telemetry from disk when 200", (done) => {
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,11 @@
"typescript": "4.1.2"
},
"dependencies": {
"@azure/logger": "^1.0.1",
"@azure/core-http": "^2.2.1",
"@opentelemetry/api": "^1.0.3",
"@opentelemetry/core": "^0.23.0",
"@opentelemetry/semantic-conventions": "^0.24.0",
"@opentelemetry/core": "^0.23.0",
"@opentelemetry/tracing": "^0.23.0",
"cls-hooked": "^4.2.2",
"continuation-local-storage": "^3.2.1",
Expand Down

0 comments on commit 1e5ef31

Please sign in to comment.