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(workspace): update the logging guard to handle ConnectionString for Application Insights #13792

Merged
merged 5 commits into from
Oct 15, 2024
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
46 changes: 46 additions & 0 deletions frontend/packages/shared/src/contexts/LoggerContext.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import React from 'react';
import { render } from '@testing-library/react';
import { LoggerContextProvider, type LoggerContextProviderProps } from './LoggerContext';
import { ApplicationInsights } from '@microsoft/applicationinsights-web';

jest.mock('@microsoft/applicationinsights-web', () => {
const mockTrackException = jest.fn();
return {
ApplicationInsights: jest.fn().mockImplementation(() => ({
loadAppInsights: jest.fn(),
trackException: mockTrackException,
})),
mockTrackException,
};
});

describe('LoggerContextProvider', () => {
afterEach(() => {
jest.clearAllMocks();
});

test('does not initialize ApplicationInsights without connectionString', () => {
const emptyConfig = {};
renderLoggerContext({ config: emptyConfig });

expect(ApplicationInsights).not.toHaveBeenCalled();
});

test('does initialize ApplicationInsights when connectionString is provided', () => {
renderLoggerContext({
config: {
connectionString: 'my-unit-test-connection-string',
},
});

expect(ApplicationInsights).toHaveBeenCalled();
});
});

const renderLoggerContext = (props?: Pick<LoggerContextProviderProps, 'config'>): void => {
render(
<LoggerContextProvider config={{ ...props.config }}>
<div>child</div>
</LoggerContextProvider>,
);
};
6 changes: 3 additions & 3 deletions frontend/packages/shared/src/contexts/LoggerContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ export type LoggerConfig = IConfiguration & IConfig;

const LoggerContext = createContext<ApplicationInsights | null>(null);

type LoggerContextProviderProps = {
export type LoggerContextProviderProps = {
config: LoggerConfig;
children: ReactNode;
};
Expand All @@ -19,8 +19,8 @@ export const LoggerContextProvider = ({
const reactPlugin = useMemo(() => new ReactPlugin(), []);

const applicationInsights = useMemo(() => {
// check if we have a instrumentationKey, if not, don't initialize app insights (we do not want AI to run on localhost)
if (!config.instrumentationKey) return null;
// check if we have a connectionString, if not, don't initialize app insights (we do not want AI to run on localhost)
if (!config.connectionString) return null;

const insights = new ApplicationInsights({
config: {
Expand Down
Loading