Skip to content

Commit

Permalink
suppress unintended logging when CSM is disabled (#175)
Browse files Browse the repository at this point in the history
issue: #171
  • Loading branch information
Spencer Lepine authored Sep 26, 2023
1 parent bb64c47 commit 337d7d5
Show file tree
Hide file tree
Showing 2 changed files with 112 additions and 1 deletion.
14 changes: 14 additions & 0 deletions src/service/csmService.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,10 @@ class CsmService {
csmConfig.widgetType : this.widgetType;
}

_hasCSMFailedToImport() {
return typeof csm === 'undefined';
}

getDefaultDimensions() {
return [
{
Expand All @@ -79,6 +83,8 @@ class CsmService {
}

addMetric(metric) {
if (this._hasCSMFailedToImport()) return;

// if csmService is never initialized, store the metrics in an array
if (!this.csmInitialized) {
if (this.metricsToBePublished) {
Expand All @@ -101,6 +107,8 @@ class CsmService {
}

addLatencyMetric(method, timeDifference, category, otherDimensions = []) {
if (this._hasCSMFailedToImport()) return;

try {
const latencyMetric = new csm.Metric(
method,
Expand Down Expand Up @@ -135,6 +143,8 @@ class CsmService {
}

addCountAndErrorMetric(method, category, error, otherDimensions = []) {
if (this._hasCSMFailedToImport()) return;

try {
const dimensions = [
...this.getDefaultDimensions(),
Expand Down Expand Up @@ -170,6 +180,8 @@ class CsmService {
}

addCountMetric(method, category, otherDimensions = []) {
if (this._hasCSMFailedToImport()) return;

try {
const dimensions = [
...this.getDefaultDimensions(),
Expand All @@ -193,6 +205,8 @@ class CsmService {
}

addAgentCountMetric(metricName, count) {
if (this._hasCSMFailedToImport()) return;

try {
const _self = this;
if (csm && csm.API.addCount && metricName) {
Expand Down
99 changes: 98 additions & 1 deletion src/service/csmService.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
} from "../config/csmConfig";
import * as CsmConfig from "../config/csmConfig";
import { GlobalConfig } from "../globalConfig";
import { ChatSessionObject } from "../core/chatSession";

jest.useFakeTimers();
jest.spyOn(global, 'setTimeout');
Expand Down Expand Up @@ -190,4 +191,100 @@ describe("Common csmService tests", () => {
expect(csm.API.addCount).toHaveBeenCalledWith("test", 1);
});
});
});
});

describe("Disabling CSM", () => {
let backupGlobalCSM = global.csm;

const chatSessionCreateInput = {
loggerConfig: { useDefaultLogger: true },
// disableCSM: false, // default false
chatDetails: {
ContactId: 'abc',
ParticipantId: 'abc',
ParticipantToken: 'abc',
},
type: "CUSTOMER" // <-- IMPORTANT
};

beforeEach(() => {
jest.resetAllMocks();
jest.spyOn(csmService, 'addLatencyMetricWithStartTime');
jest.spyOn(csmService, 'addMetric');
jest.spyOn(csmService.logger, 'error');
jest.spyOn(csmService.logger, 'info');

// Mock these for `lib/connect-csm-worker.js`
global.URL.createObjectURL = jest.fn(() => 'mock_sharedWorkerBlobUrl');
global.SharedWorker = jest.fn(() => ({
port: {
start: jest.fn(),
postMessage: jest.fn(),
},
}));

// Remove existing "csm" value from other test files
delete global.csm;
});

afterEach(() => {
global.csm = backupGlobalCSM;
});

it("should not execute addMetric when CSM has been disabled", () => {
let backup_loadCsmScriptAndExecute = csmService.loadCsmScriptAndExecute;
let backup_initializeCSM = csmService.initializeCSM;

csmService.loadCsmScriptAndExecute = jest.fn();
csmService.initializeCSM = jest.fn();

// Initialize chat session with CSM disabled
ChatSessionObject.create({
...chatSessionCreateInput,
disableCSM: true, // <-- IMPORTANT
});
expect(csmService.loadCsmScriptAndExecute).not.toHaveBeenCalled();
expect(csmService.initializeCSM).not.toHaveBeenCalled();
expect(csmService.csmInitialized).toBe(false);

// Attempt to publish a metric
csmService.addLatencyMetricWithStartTime("test-method", 0, "test-category");
csmService.addCountAndErrorMetric("test-method", "test-category", "test-error");
csmService.addCountMetric("test-method", "test-category");
csmService.addAgentCountMetric("test-name", 0);

// Assert expected behavior
expect(csmService.addMetric).not.toHaveBeenCalled();
expect(csmService.logger.error).not.toHaveBeenCalled(); // Should be silent and not output any ERROR logs
expect(csmService.logger.info).not.toHaveBeenCalled(); // Should be silent and not output any INFO logs

// Cleanup
csmService.loadCsmScriptAndExecute = backup_loadCsmScriptAndExecute;
csmService.initializeCSM = backup_initializeCSM;
});

it("should properly execute addMetric when CSM is enabled", () => {
jest.spyOn(CsmConfig, 'getLdasEndpointUrl').mockReturnValue(mockLdasEndpoint);

// Initialize chat session with CSM disabled
expect(csmService.csmInitialized).toBe(false);
ChatSessionObject.create({
...chatSessionCreateInput,
disableCSM: false, // <-- default "true"
});
expect(csmService.logger.error).not.toHaveBeenCalled(); // Should not trigger "catch" block
expect(csmService.csmInitialized).toBe(true);

// Attempt to publish a metric
csmService.addLatencyMetricWithStartTime("test-method", 0, "test-category");
csmService.addCountAndErrorMetric("test-method", "test-category", "test-error");
csmService.addCountMetric("test-method", "test-category");
csmService.addAgentCountMetric("test-name", 0);

// Assert expected behavior
expect(csmService.addLatencyMetricWithStartTime).toHaveBeenCalled();
expect(csmService.logger.error).not.toHaveBeenCalled(); // Should not trigger "catch" block
expect(csmService.addMetric).toHaveBeenCalled();
expect(csmService.logger.error).not.toHaveBeenCalled(); // Should not trigger "catch" block
});
});

0 comments on commit 337d7d5

Please sign in to comment.