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

load apm-rum agent lazily #78760

Merged
merged 5 commits into from
Sep 30, 2020
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
75 changes: 75 additions & 0 deletions src/core/public/apm_system.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
/*
* Licensed to Elasticsearch B.V. under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch B.V. licenses this file to you under
* the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

/**
* This is the entry point used to boot the frontend when serving a application
* that lives in the Kibana Platform.
*
* Any changes to this file should be kept in sync with
* src/legacy/ui/ui_bundles/app_entry_template.js
*/
Comment on lines +20 to +26
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe this comment can be deleted

import type { InternalApplicationStart } from './application';

interface ApmConfig {
// AgentConfigOptions is not exported from @elastic/apm-rum
globalLabels?: Record<string, string>;
}

interface StartDeps {
application: InternalApplicationStart;
}

export class ApmSystem {
private readonly enabled: boolean;
/**
* `apmConfig` would be populated with relevant APM RUM agent
* configuration if server is started with `ELASTIC_APM_ACTIVE=true`
*/
constructor(private readonly apmConfig?: ApmConfig) {
this.enabled = process.env.IS_KIBANA_DISTRIBUTABLE !== 'true' && apmConfig != null;
}

async setup() {
if (!this.enabled) return;
const { init, apm } = await import('@elastic/apm-rum');
const { globalLabels, ...apmConfig } = this.apmConfig!;
if (globalLabels) {
apm.addLabels(globalLabels);
}

init(apmConfig);
}

async start(start?: StartDeps) {
if (!this.enabled || !start) return;
/**
* Register listeners for navigation changes and capture them as
* route-change transactions after Kibana app is bootstrapped
*/
start.application.currentAppId$.subscribe((appId) => {
const apmInstance = (window as any).elasticApm;
if (appId && apmInstance && typeof apmInstance.startTransaction === 'function') {
apmInstance.startTransaction(`/app/${appId}`, 'route-change', {
managed: true,
canReuse: true,
});
}
});
}
}
50 changes: 50 additions & 0 deletions src/core/public/kbn_bootstrap.test.mocks.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/*
* Licensed to Elasticsearch B.V. under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch B.V. licenses this file to you under
* the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

import { applicationServiceMock } from './application/application_service.mock';
import { fatalErrorsServiceMock } from './fatal_errors/fatal_errors_service.mock';
export const fatalErrorMock = fatalErrorsServiceMock.createSetupContract();
export const coreSystemMock = {
setup: jest.fn().mockResolvedValue({
fatalErrors: fatalErrorMock,
}),
start: jest.fn().mockResolvedValue({
application: applicationServiceMock.createInternalStartContract(),
}),
};
jest.doMock('./core_system', () => ({
CoreSystem: jest.fn().mockImplementation(() => coreSystemMock),
}));

export const apmSystem = {
setup: jest.fn().mockResolvedValue(undefined),
start: jest.fn().mockResolvedValue(undefined),
};
export const ApmSystemConstructor = jest.fn().mockImplementation(() => apmSystem);
jest.doMock('./apm_system', () => ({
ApmSystem: ApmSystemConstructor,
}));

export const i18nLoad = jest.fn().mockResolvedValue(undefined);
jest.doMock('@kbn/i18n', () => ({
i18n: {
...jest.requireActual('@kbn/i18n').i18n,
load: i18nLoad,
},
}));
54 changes: 54 additions & 0 deletions src/core/public/kbn_bootstrap.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/*
* Licensed to Elasticsearch B.V. under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch B.V. licenses this file to you under
* the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
import { apmSystem, fatalErrorMock, i18nLoad } from './kbn_bootstrap.test.mocks';
import { __kbnBootstrap__ } from './';

describe('kbn_bootstrap', () => {
beforeAll(() => {
const metadata = {
i18n: { translationsUrl: 'http://localhost' },
vars: { apmConfig: null },
};
// eslint-disable-next-line no-unsanitized/property
document.body.innerHTML = `<kbn-injected-metadata data=${JSON.stringify(metadata)}>
</kbn-injected-metadata>`;
});

beforeEach(() => {
jest.clearAllMocks();
});

it('does not report a fatal error if apm load fails', async () => {
apmSystem.setup.mockRejectedValueOnce(new Error('reason'));
const consoleSpy = jest.spyOn(console, 'warn').mockImplementationOnce(() => undefined);

await __kbnBootstrap__();

expect(fatalErrorMock.add).toHaveBeenCalledTimes(0);
expect(consoleSpy).toHaveBeenCalledTimes(1);
});

it('reports a fatal error if i18n load fails', async () => {
i18nLoad.mockRejectedValueOnce(new Error('reason'));

await __kbnBootstrap__();

expect(fatalErrorMock.add).toHaveBeenCalledTimes(1);
});
});
80 changes: 24 additions & 56 deletions src/core/public/kbn_bootstrap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,70 +17,38 @@
* under the License.
*/

/**
* This is the entry point used to boot the frontend when serving a application
* that lives in the Kibana Platform.
*
* Any changes to this file should be kept in sync with
* src/legacy/ui/ui_bundles/app_entry_template.js
*/

import { i18n } from '@kbn/i18n';
import { CoreSystem } from './core_system';
import { ApmSystem } from './apm_system';

/** @internal */
export function __kbnBootstrap__() {
export async function __kbnBootstrap__() {
const injectedMetadata = JSON.parse(
document.querySelector('kbn-injected-metadata')!.getAttribute('data')!
);

/**
* `apmConfig` would be populated with relavant APM RUM agent
* configuration if server is started with `ELASTIC_APM_ACTIVE=true`
*/
const apmConfig = injectedMetadata.vars.apmConfig;
const APM_ENABLED = process.env.IS_KIBANA_DISTRIBUTABLE !== 'true' && apmConfig != null;

if (APM_ENABLED) {
// eslint-disable-next-line @typescript-eslint/no-var-requires
const { init, apm } = require('@elastic/apm-rum');
if (apmConfig.globalLabels) {
apm.addLabels(apmConfig.globalLabels);
}
init(apmConfig);
let i18nError: Error | undefined;
const apmSystem = new ApmSystem(injectedMetadata.vars.apmConfig);

await Promise.all([
// eslint-disable-next-line no-console
apmSystem.setup().catch(console.warn),
i18n.load(injectedMetadata.i18n.translationsUrl).catch((error) => {
i18nError = error;
}),
]);

const coreSystem = new CoreSystem({
injectedMetadata,
rootDomElement: document.body,
browserSupportsCsp: !(window as any).__kbnCspNotEnforced__,
});

const setup = await coreSystem.setup();
if (i18nError && setup) {
setup.fatalErrors.add(i18nError);
}

i18n
.load(injectedMetadata.i18n.translationsUrl)
.catch((e) => e)
.then(async (i18nError) => {
const coreSystem = new CoreSystem({
injectedMetadata,
rootDomElement: document.body,
browserSupportsCsp: !(window as any).__kbnCspNotEnforced__,
});

const setup = await coreSystem.setup();
if (i18nError && setup) {
setup.fatalErrors.add(i18nError);
}

const start = await coreSystem.start();

if (APM_ENABLED && start) {
/**
* Register listeners for navigation changes and capture them as
* route-change transactions after Kibana app is bootstrapped
*/
start.application.currentAppId$.subscribe((appId) => {
const apmInstance = (window as any).elasticApm;
if (appId && apmInstance && typeof apmInstance.startTransaction === 'function') {
apmInstance.startTransaction(`/app/${appId}`, 'route-change', {
managed: true,
canReuse: true,
});
}
});
}
});
const start = await coreSystem.start();
await apmSystem.start(start);
}
2 changes: 1 addition & 1 deletion src/core/public/public.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ import { UnregisterCallback } from 'history';
import { UserProvidedValues as UserProvidedValues_2 } from 'src/core/server/types';

// @internal (undocumented)
export function __kbnBootstrap__(): void;
export function __kbnBootstrap__(): Promise<void>;

// @public (undocumented)
export interface App<HistoryLocationState = unknown> {
Expand Down