mirror of
https://github.com/elastic/kibana.git
synced 2025-04-23 01:13:23 -04:00
[Fullstory] add version and appId to reports (#111952)
* add version and appId to reports * tests * code review * cr 2 * manual parsing + todo
This commit is contained in:
parent
bc9ad13190
commit
7195317381
4 changed files with 97 additions and 8 deletions
|
@ -14,8 +14,11 @@ export interface FullStoryDeps {
|
|||
packageInfo: PackageInfo;
|
||||
}
|
||||
|
||||
export type FullstoryUserVars = Record<string, any>;
|
||||
|
||||
export interface FullStoryApi {
|
||||
identify(userId: string, userVars?: Record<string, any>): void;
|
||||
identify(userId: string, userVars?: FullstoryUserVars): void;
|
||||
setUserVars(userVars?: FullstoryUserVars): void;
|
||||
event(eventName: string, eventProperties: Record<string, any>): void;
|
||||
}
|
||||
|
||||
|
|
|
@ -10,6 +10,7 @@ import type { FullStoryDeps, FullStoryApi, FullStoryService } from './fullstory'
|
|||
|
||||
export const fullStoryApiMock: jest.Mocked<FullStoryApi> = {
|
||||
event: jest.fn(),
|
||||
setUserVars: jest.fn(),
|
||||
identify: jest.fn(),
|
||||
};
|
||||
export const initializeFullStoryMock = jest.fn<FullStoryService, [FullStoryDeps]>(() => ({
|
||||
|
|
|
@ -11,6 +11,7 @@ import { homePluginMock } from 'src/plugins/home/public/mocks';
|
|||
import { securityMock } from '../../security/public/mocks';
|
||||
import { fullStoryApiMock, initializeFullStoryMock } from './plugin.test.mocks';
|
||||
import { CloudPlugin, CloudConfigType, loadFullStoryUserId } from './plugin';
|
||||
import { Observable, Subject } from 'rxjs';
|
||||
|
||||
describe('Cloud Plugin', () => {
|
||||
describe('#setup', () => {
|
||||
|
@ -23,10 +24,12 @@ describe('Cloud Plugin', () => {
|
|||
config = {},
|
||||
securityEnabled = true,
|
||||
currentUserProps = {},
|
||||
currentAppId$ = undefined,
|
||||
}: {
|
||||
config?: Partial<CloudConfigType>;
|
||||
securityEnabled?: boolean;
|
||||
currentUserProps?: Record<string, any>;
|
||||
currentAppId$?: Observable<string | undefined>;
|
||||
}) => {
|
||||
const initContext = coreMock.createPluginInitializerContext({
|
||||
id: 'cloudId',
|
||||
|
@ -39,9 +42,15 @@ describe('Cloud Plugin', () => {
|
|||
},
|
||||
...config,
|
||||
});
|
||||
|
||||
const plugin = new CloudPlugin(initContext);
|
||||
|
||||
const coreSetup = coreMock.createSetup();
|
||||
const coreStart = coreMock.createStart();
|
||||
if (currentAppId$) {
|
||||
coreStart.application.currentAppId$ = currentAppId$;
|
||||
}
|
||||
coreSetup.getStartServices.mockResolvedValue([coreStart, {}, undefined]);
|
||||
const securitySetup = securityMock.createSetup();
|
||||
securitySetup.authc.getCurrentUser.mockResolvedValue(
|
||||
securityMock.createMockAuthenticatedUser(currentUserProps)
|
||||
|
@ -78,10 +87,46 @@ describe('Cloud Plugin', () => {
|
|||
});
|
||||
|
||||
expect(fullStoryApiMock.identify).toHaveBeenCalledWith(
|
||||
'03ac674216f3e15c761ee1a5e255f067953623c8b388b4459e13f978d7c846f4'
|
||||
'03ac674216f3e15c761ee1a5e255f067953623c8b388b4459e13f978d7c846f4',
|
||||
{
|
||||
version_str: 'version',
|
||||
version_major_int: -1,
|
||||
version_minor_int: -1,
|
||||
version_patch_int: -1,
|
||||
}
|
||||
);
|
||||
});
|
||||
|
||||
it('calls FS.setUserVars everytime an app changes', async () => {
|
||||
const currentAppId$ = new Subject<string | undefined>();
|
||||
const { plugin } = await setupPlugin({
|
||||
config: { full_story: { enabled: true, org_id: 'foo' } },
|
||||
currentUserProps: {
|
||||
username: '1234',
|
||||
},
|
||||
currentAppId$,
|
||||
});
|
||||
|
||||
expect(fullStoryApiMock.setUserVars).not.toHaveBeenCalled();
|
||||
currentAppId$.next('App1');
|
||||
expect(fullStoryApiMock.setUserVars).toHaveBeenCalledWith({
|
||||
app_id_str: 'App1',
|
||||
});
|
||||
currentAppId$.next();
|
||||
expect(fullStoryApiMock.setUserVars).toHaveBeenCalledWith({
|
||||
app_id_str: 'unknown',
|
||||
});
|
||||
|
||||
currentAppId$.next('App2');
|
||||
expect(fullStoryApiMock.setUserVars).toHaveBeenCalledWith({
|
||||
app_id_str: 'App2',
|
||||
});
|
||||
|
||||
expect(currentAppId$.observers.length).toBe(1);
|
||||
plugin.stop();
|
||||
expect(currentAppId$.observers.length).toBe(0);
|
||||
});
|
||||
|
||||
it('does not call FS.identify when security is not available', async () => {
|
||||
await setupPlugin({
|
||||
config: { full_story: { enabled: true, org_id: 'foo' } },
|
||||
|
|
|
@ -12,8 +12,10 @@ import {
|
|||
PluginInitializerContext,
|
||||
HttpStart,
|
||||
IBasePath,
|
||||
ApplicationStart,
|
||||
} from 'src/core/public';
|
||||
import { i18n } from '@kbn/i18n';
|
||||
import { Subscription } from 'rxjs';
|
||||
import type {
|
||||
AuthenticatedUser,
|
||||
SecurityPluginSetup,
|
||||
|
@ -58,9 +60,15 @@ export interface CloudSetup {
|
|||
isCloudEnabled: boolean;
|
||||
}
|
||||
|
||||
interface SetupFullstoryDeps extends CloudSetupDependencies {
|
||||
application?: Promise<ApplicationStart>;
|
||||
basePath: IBasePath;
|
||||
}
|
||||
|
||||
export class CloudPlugin implements Plugin<CloudSetup> {
|
||||
private config!: CloudConfigType;
|
||||
private isCloudEnabled: boolean;
|
||||
private appSubscription?: Subscription;
|
||||
|
||||
constructor(private readonly initializerContext: PluginInitializerContext) {
|
||||
this.config = this.initializerContext.config.get<CloudConfigType>();
|
||||
|
@ -68,7 +76,10 @@ export class CloudPlugin implements Plugin<CloudSetup> {
|
|||
}
|
||||
|
||||
public setup(core: CoreSetup, { home, security }: CloudSetupDependencies) {
|
||||
this.setupFullstory({ basePath: core.http.basePath, security }).catch((e) =>
|
||||
const application = core.getStartServices().then(([coreStart]) => {
|
||||
return coreStart.application;
|
||||
});
|
||||
this.setupFullstory({ basePath: core.http.basePath, security, application }).catch((e) =>
|
||||
// eslint-disable-next-line no-console
|
||||
console.debug(`Error setting up FullStory: ${e.toString()}`)
|
||||
);
|
||||
|
@ -138,6 +149,10 @@ export class CloudPlugin implements Plugin<CloudSetup> {
|
|||
.catch(() => setLinks(true));
|
||||
}
|
||||
|
||||
public stop() {
|
||||
this.appSubscription?.unsubscribe();
|
||||
}
|
||||
|
||||
/**
|
||||
* Determines if the current user should see links back to Cloud.
|
||||
* This isn't a true authorization check, but rather a heuristic to
|
||||
|
@ -164,10 +179,7 @@ export class CloudPlugin implements Plugin<CloudSetup> {
|
|||
return user?.roles.includes('superuser') ?? true;
|
||||
}
|
||||
|
||||
private async setupFullstory({
|
||||
basePath,
|
||||
security,
|
||||
}: CloudSetupDependencies & { basePath: IBasePath }) {
|
||||
private async setupFullstory({ basePath, security, application }: SetupFullstoryDeps) {
|
||||
const { enabled, org_id: orgId } = this.config.full_story;
|
||||
if (!enabled || !orgId) {
|
||||
return; // do not load any fullstory code in the browser if not enabled
|
||||
|
@ -198,7 +210,35 @@ export class CloudPlugin implements Plugin<CloudSetup> {
|
|||
if (userId) {
|
||||
// Do the hashing here to keep it at clear as possible in our source code that we do not send literal user IDs
|
||||
const hashedId = sha256(userId.toString());
|
||||
fullStory.identify(hashedId);
|
||||
application
|
||||
?.then(async () => {
|
||||
const appStart = await application;
|
||||
this.appSubscription = appStart.currentAppId$.subscribe((appId) => {
|
||||
// Update the current application every time it changes
|
||||
fullStory.setUserVars({
|
||||
app_id_str: appId ?? 'unknown',
|
||||
});
|
||||
});
|
||||
})
|
||||
.catch((e) => {
|
||||
// eslint-disable-next-line no-console
|
||||
console.error(
|
||||
`[cloud.full_story] Could not retrieve application service due to error: ${e.toString()}`,
|
||||
e
|
||||
);
|
||||
});
|
||||
const kibanaVer = this.initializerContext.env.packageInfo.version;
|
||||
// TODO: use semver instead
|
||||
const parsedVer = (kibanaVer.indexOf('.') > -1 ? kibanaVer.split('.') : []).map((s) =>
|
||||
parseInt(s, 10)
|
||||
);
|
||||
// `str` suffix is required for evn vars, see docs: https://help.fullstory.com/hc/en-us/articles/360020623234
|
||||
fullStory.identify(hashedId, {
|
||||
version_str: kibanaVer,
|
||||
version_major_int: parsedVer[0] ?? -1,
|
||||
version_minor_int: parsedVer[1] ?? -1,
|
||||
version_patch_int: parsedVer[2] ?? -1,
|
||||
});
|
||||
}
|
||||
} catch (e) {
|
||||
// eslint-disable-next-line no-console
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue