-
Notifications
You must be signed in to change notification settings - Fork 8.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Telemetry for Dyanmic Actions (Drilldowns) (#84580)
* feat: ๐ธ set up telemetry for UiActions * feat: ๐ธ improve ui_actions_enhanced collector * feat: ๐ธ namespace ui actions telemetry stats * refactor: ๐ก improve dynamic actions collector setup * feat: ๐ธ add tests for dynamicActionsCollector * feat: ๐ธ collect dynamic action trigger statistics * refactor: ๐ก standartize metric naming * feat: ๐ธ aggregate action x trigger counts * test: ๐ add tests for factory stats * docs: โ๏ธ add ui actions enhanced telemetry docs * fix: ๐ revert type change * refactor: ๐ก make dynamic action stats global * refactor: ๐ก use global telemetry stats in action factories
- Loading branch information
Showing
7 changed files
with
536 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
124 changes: 124 additions & 0 deletions
124
...k/plugins/ui_actions_enhanced/server/telemetry/dynamic_action_factories_collector.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,124 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License; | ||
* you may not use this file except in compliance with the Elastic License. | ||
*/ | ||
|
||
/* eslint-disable @typescript-eslint/naming-convention */ | ||
|
||
import { dynamicActionFactoriesCollector } from './dynamic_action_factories_collector'; | ||
import { DynamicActionsState } from '../../common'; | ||
import { ActionFactory } from '../types'; | ||
|
||
type GetActionFactory = (id: string) => undefined | ActionFactory; | ||
|
||
const factories: Record<string, ActionFactory> = { | ||
FACTORY_ID_1: ({ | ||
id: 'FACTORY_ID_1', | ||
telemetry: jest.fn((state: DynamicActionsState, stats: Record<string, any>) => { | ||
stats.myStat_1 = 1; | ||
stats.myStat_2 = 123; | ||
return stats; | ||
}), | ||
} as unknown) as ActionFactory, | ||
FACTORY_ID_2: ({ | ||
id: 'FACTORY_ID_2', | ||
telemetry: jest.fn((state: DynamicActionsState, stats: Record<string, any>) => stats), | ||
} as unknown) as ActionFactory, | ||
FACTORY_ID_3: ({ | ||
id: 'FACTORY_ID_3', | ||
telemetry: jest.fn((state: DynamicActionsState, stats: Record<string, any>) => { | ||
stats.myStat_1 = 2; | ||
stats.stringStat = 'abc'; | ||
return stats; | ||
}), | ||
} as unknown) as ActionFactory, | ||
}; | ||
|
||
const getActionFactory: GetActionFactory = (id: string) => factories[id]; | ||
|
||
const state: DynamicActionsState = { | ||
events: [ | ||
{ | ||
eventId: 'eventId-1', | ||
triggers: ['TRIGGER_1'], | ||
action: { | ||
factoryId: 'FACTORY_ID_1', | ||
name: 'Click me!', | ||
config: {}, | ||
}, | ||
}, | ||
{ | ||
eventId: 'eventId-2', | ||
triggers: ['TRIGGER_2', 'TRIGGER_3'], | ||
action: { | ||
factoryId: 'FACTORY_ID_2', | ||
name: 'Click me, too!', | ||
config: { | ||
doCleanup: true, | ||
}, | ||
}, | ||
}, | ||
{ | ||
eventId: 'eventId-3', | ||
triggers: ['TRIGGER_4', 'TRIGGER_1'], | ||
action: { | ||
factoryId: 'FACTORY_ID_3', | ||
name: 'Go to documentation', | ||
config: { | ||
url: 'http://google.com', | ||
iamFeelingLucky: true, | ||
}, | ||
}, | ||
}, | ||
], | ||
}; | ||
|
||
beforeEach(() => { | ||
Object.values(factories).forEach((factory) => { | ||
((factory.telemetry as unknown) as jest.SpyInstance).mockClear(); | ||
}); | ||
}); | ||
|
||
describe('dynamicActionFactoriesCollector', () => { | ||
test('returns empty stats when there are not dynamic actions', () => { | ||
const stats = dynamicActionFactoriesCollector( | ||
getActionFactory, | ||
{ | ||
events: [], | ||
}, | ||
{} | ||
); | ||
|
||
expect(stats).toEqual({}); | ||
}); | ||
|
||
test('calls .telemetry() method of a supplied factory', () => { | ||
const currentState = { | ||
events: [state.events[0]], | ||
}; | ||
dynamicActionFactoriesCollector(getActionFactory, currentState, {}); | ||
|
||
const spy1 = (factories.FACTORY_ID_1.telemetry as unknown) as jest.SpyInstance; | ||
const spy2 = (factories.FACTORY_ID_2.telemetry as unknown) as jest.SpyInstance; | ||
|
||
expect(spy1).toHaveBeenCalledTimes(1); | ||
expect(spy2).toHaveBeenCalledTimes(0); | ||
|
||
expect(spy1.mock.calls[0][0]).toEqual(currentState.events[0]); | ||
expect(typeof spy1.mock.calls[0][1]).toBe('object'); | ||
expect(!!spy1.mock.calls[0][1]).toBe(true); | ||
}); | ||
|
||
test('returns stats received from factory', () => { | ||
const currentState = { | ||
events: [state.events[0]], | ||
}; | ||
const stats = dynamicActionFactoriesCollector(getActionFactory, currentState, {}); | ||
|
||
expect(stats).toEqual({ | ||
myStat_1: 1, | ||
myStat_2: 123, | ||
}); | ||
}); | ||
}); |
24 changes: 24 additions & 0 deletions
24
x-pack/plugins/ui_actions_enhanced/server/telemetry/dynamic_action_factories_collector.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License; | ||
* you may not use this file except in compliance with the Elastic License. | ||
*/ | ||
|
||
import { DynamicActionsState } from '../../common'; | ||
import { ActionFactory } from '../types'; | ||
|
||
export const dynamicActionFactoriesCollector = ( | ||
getActionFactory: (id: string) => undefined | ActionFactory, | ||
state: DynamicActionsState, | ||
stats: Record<string, any> | ||
): Record<string, any> => { | ||
for (const event of state.events) { | ||
const factory = getActionFactory(event.action.factoryId); | ||
|
||
if (factory) { | ||
stats = factory.telemetry(event, stats); | ||
} | ||
} | ||
|
||
return stats; | ||
}; |
Oops, something went wrong.