Skip to content
This repository has been archived by the owner on Mar 13, 2024. It is now read-only.

Enable performance telemetry tracking for production deployments #9758

Merged
merged 4 commits into from
Feb 10, 2022
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
18 changes: 15 additions & 3 deletions actions/telemetry_actions.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
// See LICENSE.txt for license information.

import {Client4} from 'mattermost-redux/client';
import {getConfig} from 'mattermost-redux/selectors/entities/general';

import store from 'stores/redux_store.jsx';

import {isDevMode} from 'utils/utils';

Expand All @@ -14,6 +17,15 @@ const SUPPORTS_MEASURE_METHODS = isSupported([
performance.clearMeasures,
]);

export function isTelemetryEnabled(state) {
const config = getConfig(state);
return config.DiagnosticsEnabled === 'true';
}

export function shouldTrackPerformance(state = store.getState()) {
return isDevMode(state) || isTelemetryEnabled(state);
}

export function trackEvent(category, event, props) {
Client4.trackEvent(category, event, props);
if (isDevMode() && category === 'performance' && props) {
Expand All @@ -33,14 +45,14 @@ export function pageVisited(category, name) {
*
*/
export function clearMarks(names) {
if (!isDevMode() || !SUPPORTS_CLEAR_MARKS) {
if (!shouldTrackPerformance() || !SUPPORTS_CLEAR_MARKS) {
return;
}
names.forEach((name) => performance.clearMarks(name));
}

export function mark(name) {
if (!isDevMode() || !SUPPORTS_MARK) {
if (!shouldTrackPerformance() || !SUPPORTS_MARK) {
return;
}
performance.mark(name);
Expand All @@ -60,7 +72,7 @@ export function mark(name) {
*
*/
export function measure(name1, name2) {
if (!isDevMode() || !SUPPORTS_MEASURE_METHODS) {
if (!shouldTrackPerformance() || !SUPPORTS_MEASURE_METHODS) {
return [-1, ''];
}

Expand Down
48 changes: 48 additions & 0 deletions actions/telemetry_actions.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
// See LICENSE.txt for license information.

import {isTelemetryEnabled, shouldTrackPerformance} from 'actions/telemetry_actions';

describe('Actions.Telemetry', () => {
test('isTelemetryEnabled', async () => {
const state = {
entities: {
general: {
config: {
DiagnosticsEnabled: 'false',
},
},
},
};

expect(isTelemetryEnabled(state)).toBeFalsy();

state.entities.general.config.DiagnosticsEnabled = 'true';

expect(isTelemetryEnabled(state)).toBeTruthy();
});

test('shouldTrackPerformance', async () => {
const state = {
entities: {
general: {
config: {
DiagnosticsEnabled: 'false',
EnableDeveloper: 'false',
},
},
},
};

expect(shouldTrackPerformance(state)).toBeFalsy();

state.entities.general.config.DiagnosticsEnabled = 'true';

expect(shouldTrackPerformance(state)).toBeTruthy();

state.entities.general.config.DiagnosticsEnabled = 'false';
state.entities.general.config.EnableDeveloper = 'true';

expect(shouldTrackPerformance(state)).toBeTruthy();
});
});
4 changes: 2 additions & 2 deletions utils/utils.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -1669,8 +1669,8 @@ export function setCSRFFromCookie() {
/**
* Returns true if in dev mode, false otherwise.
*/
export function isDevMode() {
const config = getConfig(store.getState());
export function isDevMode(state = store.getState()) {
const config = getConfig(state);
return config.EnableDeveloper === 'true';
}

Expand Down