Submit
Path:
~
/
/
usr
/
share
/
grafana
/
public
/
app
/
features
/
alerting
/
unified
/
File Content:
Analytics.ts
import { dateTime } from '@grafana/data'; import { createMonitoringLogger, getBackendSrv } from '@grafana/runtime'; import { config, reportInteraction } from '@grafana/runtime/src'; import { contextSrv } from 'app/core/core'; import { RuleNamespace } from '../../../types/unified-alerting'; import { RulerRulesConfigDTO } from '../../../types/unified-alerting-dto'; export const USER_CREATION_MIN_DAYS = 7; export const LogMessages = { filterByLabel: 'filtering alert instances by label', loadedList: 'loaded Alert Rules list', leavingRuleGroupEdit: 'leaving rule group edit without saving', alertRuleFromPanel: 'creating alert rule from panel', alertRuleFromScratch: 'creating alert rule from scratch', recordingRuleFromScratch: 'creating recording rule from scratch', clickingAlertStateFilters: 'clicking alert state filters', cancelSavingAlertRule: 'user canceled alert rule creation', successSavingAlertRule: 'alert rule saved successfully', unknownMessageFromError: 'unknown messageFromError', }; const alertingLogger = createMonitoringLogger('features.alerting', { module: 'Alerting' }); export function logInfo(message: string, context?: Record<string, string>) { alertingLogger.logInfo(message, context); } export function logError(error: Error, context?: Record<string, string>) { alertingLogger.logError(error, context); } export function logWarning(message: string, context?: Record<string, string>) { alertingLogger.logWarning(message, context); } // eslint-disable-next-line @typescript-eslint/no-explicit-any export function withPerformanceLogging<TFunc extends (...args: any[]) => Promise<any>>( func: TFunc, message: string, context: Record<string, string> ): (...args: Parameters<TFunc>) => Promise<Awaited<ReturnType<TFunc>>> { return async function (...args) { const startLoadingTs = performance.now(); const response = await func(...args); logInfo(message, { loadTimeMs: (performance.now() - startLoadingTs).toFixed(0), ...context, }); return response; }; } // eslint-disable-next-line @typescript-eslint/no-explicit-any export function withPromRulesMetadataLogging<TFunc extends (...args: any[]) => Promise<RuleNamespace[]>>( func: TFunc, message: string, context: Record<string, string> ) { return async (...args: Parameters<TFunc>) => { const startLoadingTs = performance.now(); const response = await func(...args); const { namespacesCount, groupsCount, rulesCount } = getPromRulesMetadata(response); logInfo(message, { loadTimeMs: (performance.now() - startLoadingTs).toFixed(0), namespacesCount, groupsCount, rulesCount, ...context, }); return response; }; } function getPromRulesMetadata(promRules: RuleNamespace[]) { const namespacesCount = promRules.length; const groupsCount = promRules.flatMap((ns) => ns.groups).length; const rulesCount = promRules.flatMap((ns) => ns.groups).flatMap((g) => g.rules).length; const metadata = { namespacesCount: namespacesCount.toFixed(0), groupsCount: groupsCount.toFixed(0), rulesCount: rulesCount.toFixed(0), }; return metadata; } // eslint-disable-next-line @typescript-eslint/no-explicit-any export function withRulerRulesMetadataLogging<TFunc extends (...args: any[]) => Promise<RulerRulesConfigDTO>>( func: TFunc, message: string, context: Record<string, string> ) { return async (...args: Parameters<TFunc>) => { const startLoadingTs = performance.now(); const response = await func(...args); const { namespacesCount, groupsCount, rulesCount } = getRulerRulesMetadata(response); logInfo(message, { loadTimeMs: (performance.now() - startLoadingTs).toFixed(0), namespacesCount, groupsCount, rulesCount, ...context, }); return response; }; } function getRulerRulesMetadata(rulerRules: RulerRulesConfigDTO) { const namespacesCount = Object.keys(rulerRules).length; const groups = Object.values(rulerRules).flatMap((groups) => groups); const rules = groups.flatMap((group) => group.rules); return { namespacesCount: namespacesCount.toFixed(0), groupsCount: groups.length.toFixed(0), rulesCount: rules.length.toFixed(0), }; } export async function isNewUser() { try { const { createdAt } = await getBackendSrv().get(`/api/user`); const limitDateForNewUser = dateTime().subtract(USER_CREATION_MIN_DAYS, 'days'); const userCreationDate = dateTime(createdAt); const isNew = limitDateForNewUser.isBefore(userCreationDate); return isNew; } catch { return true; //if no date is returned, we assume the user is new to prevent tracking actions } } export const trackRuleListNavigation = async ( props: AlertRuleTrackingProps = { grafana_version: config.buildInfo.version, org_id: contextSrv.user.orgId, user_id: contextSrv.user.id, } ) => { const isNew = await isNewUser(); if (isNew) { return; } reportInteraction('grafana_alerting_navigation', props); }; export const trackNewAlerRuleFormSaved = async (props: AlertRuleTrackingProps) => { const isNew = await isNewUser(); if (isNew) { return; } reportInteraction('grafana_alerting_rule_creation', props); }; export const trackNewAlerRuleFormCancelled = async (props: AlertRuleTrackingProps) => { const isNew = await isNewUser(); if (isNew) { return; } reportInteraction('grafana_alerting_rule_aborted', props); }; export const trackNewAlerRuleFormError = async (props: AlertRuleTrackingProps & { error: string }) => { const isNew = await isNewUser(); if (isNew) { return; } reportInteraction('grafana_alerting_rule_form_error', props); }; export const trackInsightsFeedback = async (props: { useful: boolean; panel: string }) => { const defaults = { grafana_version: config.buildInfo.version, org_id: contextSrv.user.orgId, user_id: contextSrv.user.id, }; reportInteraction('grafana_alerting_insights', { ...defaults, ...props }); }; export type AlertRuleTrackingProps = { user_id: number; grafana_version?: string; org_id?: number; };
Submit
FILE
FOLDER
INFO
Name
Size
Permission
Action
__mocks__
---
0755
__snapshots__
---
0755
api
---
0755
components
---
0755
home
---
0755
hooks
---
0755
insights
---
0755
integration
---
0755
mocks
---
0755
search
---
0755
state
---
0755
styles
---
0755
testSetup
---
0755
types
---
0755
utils
---
0755
Admin.tsx
950 bytes
0644
AlertGroups.test.tsx
7318 bytes
0644
AlertGroups.tsx
4151 bytes
0644
AlertWarning.tsx
643 bytes
0644
AlertsFolderView.test.tsx
5384 bytes
0644
AlertsFolderView.tsx
6841 bytes
0644
Analytics.test.ts
1283 bytes
0644
Analytics.ts
6136 bytes
0644
CloneRuleEditor.test.tsx
13662 bytes
0644
CloneRuleEditor.tsx
2563 bytes
0644
ExistingRuleEditor.tsx
2070 bytes
0644
GrafanaRuleQueryViewer.test.tsx
2163 bytes
0644
GrafanaRuleQueryViewer.tsx
15211 bytes
0644
MuteTimings.test.tsx
8845 bytes
0644
MuteTimings.tsx
3375 bytes
0644
NotificationPolicies.test.tsx
28082 bytes
0644
NotificationPolicies.tsx
14112 bytes
0644
PanelAlertTab.tsx
634 bytes
0644
PanelAlertTabContent.test.tsx
10087 bytes
0644
PanelAlertTabContent.tsx
2915 bytes
0644
Receivers.tsx
2183 bytes
0644
RedirectToRuleViewer.test.tsx
6476 bytes
0644
RedirectToRuleViewer.tsx
5092 bytes
0644
RuleEditor.tsx
3860 bytes
0644
RuleEditorCloudOnlyAllowed.test.tsx
7164 bytes
0644
RuleEditorCloudRules.test.tsx
7118 bytes
0644
RuleEditorExisting.test.tsx
8764 bytes
0644
RuleEditorGrafanaRules.test.tsx
8187 bytes
0644
RuleEditorRecordingRule.test.tsx
7031 bytes
0644
RuleList.test.tsx
30652 bytes
0644
RuleList.tsx
7456 bytes
0644
RuleViewer.tsx
2939 bytes
0644
Silences.test.tsx
14125 bytes
0644
Silences.tsx
4613 bytes
0644
TESTING.md
1445 bytes
0644
TODO.md
1411 bytes
0644
createRouteGroupsMatcherWorker.ts
296 bytes
0644
initAlerting.tsx
1151 bytes
0644
mockApi.ts
13689 bytes
0644
mockGrafanaNotifiers.ts
66886 bytes
0644
mocks.ts
18877 bytes
0644
routeGroupsMatcher.ts
2098 bytes
0644
routeGroupsMatcher.worker.ts
338 bytes
0644
useRouteGroupsMatcher.test.tsx
1384 bytes
0644
useRouteGroupsMatcher.ts
3343 bytes
0644
N4ST4R_ID | Naxtarrr