Skip to content

Commit

Permalink
[APM] Adds support to install latest prerelease APM package version (#…
Browse files Browse the repository at this point in the history
…118836) (#120114)

* [APM] Adds support to install latest prerelease APM package version (#117975)

* fixes some type errors

* fixes linting failures

* PR feedback adding shared isPrereleaseVersion function

Co-authored-by: Oliver Gupte <[email protected]>
  • Loading branch information
kibanamachine and ogupte authored Dec 1, 2021
1 parent 62556d8 commit f0462e0
Show file tree
Hide file tree
Showing 17 changed files with 104 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export function Instruction({
variantId,
isCloudEnabled,
}) {
const { tutorialService, http, uiSettings, getBasePath } = getServices();
const { tutorialService, http, uiSettings, getBasePath, kibanaVersion } = getServices();

let pre;
if (textPre) {
Expand Down Expand Up @@ -82,6 +82,7 @@ export function Instruction({
http={http}
variantId={variantId}
isCloudEnabled={isCloudEnabled}
kibanaVersion={kibanaVersion}
/>
</EuiErrorBoundary>
</Suspense>
Expand Down
5 changes: 5 additions & 0 deletions x-pack/plugins/apm/common/fleet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,12 @@
* 2.0; you may not use this file except in compliance with the Elastic License
* 2.0.
*/
import semverParse from 'semver/functions/parse';

export const POLICY_ELASTIC_AGENT_ON_CLOUD = 'policy-elastic-agent-on-cloud';

export const SUPPORTED_APM_PACKAGE_VERSION = '7.16.0';

export function isPrereleaseVersion(version: string) {
return semverParse(version)?.prerelease?.length ?? 0 > 0;
}
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ function Wrapper({
basePath="http://localhost:5601"
isCloudEnabled={!onPrem}
variantId={apmAgent}
kibanaVersion="8.0.0"
/>
);
}
Expand Down
10 changes: 10 additions & 0 deletions x-pack/plugins/apm/public/tutorial/config_agent/index.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ describe('TutorialConfigAgent', () => {
}
basePath="http://localhost:5601"
isCloudEnabled
kibanaVersion="8.0.0"
/>
);
expect(component.getByTestId('loading')).toBeInTheDocument();
Expand All @@ -76,6 +77,7 @@ describe('TutorialConfigAgent', () => {
}
basePath="http://localhost:5601"
isCloudEnabled={false}
kibanaVersion="8.0.0"
/>
);
expect(
Expand Down Expand Up @@ -123,6 +125,7 @@ describe('TutorialConfigAgent', () => {
}
basePath="http://localhost:5601"
isCloudEnabled={false}
kibanaVersion="8.0.0"
/>
);
expect(
Expand Down Expand Up @@ -158,6 +161,7 @@ describe('TutorialConfigAgent', () => {
}
basePath="http://localhost:5601"
isCloudEnabled
kibanaVersion="8.0.0"
/>
);
expect(
Expand Down Expand Up @@ -199,6 +203,7 @@ describe('TutorialConfigAgent', () => {
}
basePath="http://localhost:5601"
isCloudEnabled
kibanaVersion="8.0.0"
/>
);
expect(
Expand Down Expand Up @@ -237,6 +242,7 @@ describe('TutorialConfigAgent', () => {
}
basePath="http://localhost:5601"
isCloudEnabled
kibanaVersion="8.0.0"
/>
);
expect(
Expand Down Expand Up @@ -271,6 +277,7 @@ describe('TutorialConfigAgent', () => {
}
basePath="http://localhost:5601"
isCloudEnabled
kibanaVersion="8.0.0"
/>
);
expect(
Expand Down Expand Up @@ -306,6 +313,7 @@ describe('TutorialConfigAgent', () => {
}
basePath="http://localhost:5601"
isCloudEnabled
kibanaVersion="8.0.0"
/>
);

Expand All @@ -329,6 +337,7 @@ describe('TutorialConfigAgent', () => {
}
basePath="http://localhost:5601"
isCloudEnabled
kibanaVersion="8.0.0"
/>
);
expect(
Expand Down Expand Up @@ -367,6 +376,7 @@ describe('TutorialConfigAgent', () => {
}
basePath="http://localhost:5601"
isCloudEnabled
kibanaVersion="8.0.0"
/>
);
expect(
Expand Down
14 changes: 12 additions & 2 deletions x-pack/plugins/apm/public/tutorial/config_agent/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,10 @@ import { i18n } from '@kbn/i18n';
import { HttpStart } from 'kibana/public';
import React, { useEffect, useMemo, useState } from 'react';
import styled from 'styled-components';
import { SUPPORTED_APM_PACKAGE_VERSION } from '../../../common/fleet';
import {
isPrereleaseVersion,
SUPPORTED_APM_PACKAGE_VERSION,
} from '../../../common/fleet';
import { APIReturnType } from '../../services/rest/createCallApmApi';
import { getCommands } from './commands/get_commands';
import { getPolicyOptions, PolicyOption } from './get_policy_options';
Expand Down Expand Up @@ -38,6 +41,7 @@ interface Props {
http: HttpStart;
basePath: string;
isCloudEnabled: boolean;
kibanaVersion: string;
}

const INITIAL_STATE = {
Expand All @@ -50,10 +54,12 @@ function getFleetLink({
isFleetEnabled,
hasFleetAgents,
basePath,
kibanaVersion,
}: {
isFleetEnabled: boolean;
hasFleetAgents: boolean;
basePath: string;
kibanaVersion: string;
}) {
if (!isFleetEnabled) {
return;
Expand All @@ -66,7 +72,9 @@ function getFleetLink({
}
: {
label: GET_STARTED_WITH_FLEET_LABEL,
href: `${basePath}/app/integrations#/detail/apm-${SUPPORTED_APM_PACKAGE_VERSION}/overview`,
href: isPrereleaseVersion(kibanaVersion)
? `${basePath}/app/integrations#/detail/apm/overview`
: `${basePath}/app/integrations#/detail/apm-${SUPPORTED_APM_PACKAGE_VERSION}/overview`,
};
}

Expand All @@ -75,6 +83,7 @@ function TutorialConfigAgent({
http,
basePath,
isCloudEnabled,
kibanaVersion,
}: Props) {
const [data, setData] = useState<APIResponseType>(INITIAL_STATE);
const [isLoading, setIsLoading] = useState(true);
Expand Down Expand Up @@ -138,6 +147,7 @@ function TutorialConfigAgent({
isFleetEnabled: data.isFleetEnabled,
hasFleetAgents,
basePath,
kibanaVersion,
})}
/>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,22 @@ interface Props {
http: HttpStart;
basePath: string;
isCloudEnabled: boolean;
kibanaVersion: string;
}

function TutorialConfigAgentRumScript({
http,
basePath,
isCloudEnabled,
kibanaVersion,
}: Props) {
return (
<TutorialConfigAgent
variantId="js_script"
http={http}
basePath={basePath}
isCloudEnabled={isCloudEnabled}
kibanaVersion={kibanaVersion}
/>
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,17 @@ import { i18n } from '@kbn/i18n';
import { HttpStart } from 'kibana/public';
import React, { useEffect, useState } from 'react';
import styled from 'styled-components';
import { SUPPORTED_APM_PACKAGE_VERSION } from '../../../common/fleet';
import {
isPrereleaseVersion,
SUPPORTED_APM_PACKAGE_VERSION,
} from '../../../common/fleet';
import { APIReturnType } from '../../services/rest/createCallApmApi';

interface Props {
http: HttpStart;
basePath: string;
isDarkTheme: boolean;
kibanaVersion: string;
}

const CentralizedContainer = styled.div`
Expand All @@ -36,7 +40,12 @@ const CentralizedContainer = styled.div`

type APIResponseType = APIReturnType<'GET /internal/apm/fleet/migration_check'>;

function TutorialFleetInstructions({ http, basePath, isDarkTheme }: Props) {
function TutorialFleetInstructions({
http,
basePath,
isDarkTheme,
kibanaVersion,
}: Props) {
const [data, setData] = useState<APIResponseType | undefined>();
const [isLoading, setIsLoading] = useState(false);

Expand Down Expand Up @@ -69,6 +78,8 @@ function TutorialFleetInstructions({ http, basePath, isDarkTheme }: Props) {

const apmIntegrationHref = shouldLinkToMigration
? `${basePath}/app/apm/settings/schema`
: isPrereleaseVersion(kibanaVersion)
? `${basePath}/app/integrations#/detail/apm/overview`
: `${basePath}/app/integrations/detail/apm-${SUPPORTED_APM_PACKAGE_VERSION}/overview`;

if (isLoading) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ function Wrapper({ hasFleetPoliciesWithApmIntegration }: Args) {
http={http}
basePath="http://localhost:5601"
isDarkTheme={false}
kibanaVersion="8.0.0"
/>
);
}
Expand Down
2 changes: 2 additions & 0 deletions x-pack/plugins/apm/server/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,7 @@ export class APMPlugin
ruleDataClient,
plugins: resourcePlugins,
telemetryUsageCounter,
kibanaVersion: this.initContext.env.packageInfo.version,
});

if (plugins.alerting) {
Expand All @@ -193,6 +194,7 @@ export class APMPlugin
ruleDataClient,
config: currentConfig,
logger: this.logger,
kibanaVersion: this.initContext.env.packageInfo.version,
});

core.deprecations.registerDeprecations({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ export function registerRoutes({
config,
ruleDataClient,
telemetryUsageCounter,
kibanaVersion,
}: {
core: APMRouteHandlerResources['core'];
plugins: APMRouteHandlerResources['plugins'];
Expand All @@ -58,6 +59,7 @@ export function registerRoutes({
config: APMRouteHandlerResources['config'];
ruleDataClient: APMRouteHandlerResources['ruleDataClient'];
telemetryUsageCounter?: TelemetryUsageCounter;
kibanaVersion: string;
}) {
const routes = repository.getRoutes();

Expand Down Expand Up @@ -115,6 +117,7 @@ export function registerRoutes({
validatedParams
),
ruleDataClient,
kibanaVersion,
}).then((value) => {
return {
aborted: false,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,15 @@ export async function createCloudApmPackgePolicy({
esClient,
logger,
setup,
kibanaVersion,
}: {
cloudPluginSetup: APMPluginSetupDependencies['cloud'];
fleetPluginStart: NonNullable<APMPluginStartDependencies['fleet']>;
savedObjectsClient: SavedObjectsClientContract;
esClient: ElasticsearchClient;
logger: Logger;
setup: Setup;
kibanaVersion: string;
}): Promise<PackagePolicy> {
const { attributes } = await savedObjectsClient.get(
APM_SERVER_SCHEMA_SAVED_OBJECT_TYPE,
Expand All @@ -46,9 +48,11 @@ export async function createCloudApmPackgePolicy({
(attributes as { schemaJson: string }).schemaJson
);
// Merges agent config and source maps with the new APM cloud package policy
const apmPackagePolicyDefinition = getApmPackagePolicyDefinition({
const apmPackagePolicyDefinition = await getApmPackagePolicyDefinition({
apmServerSchema,
cloudPluginSetup,
fleetPluginStart,
kibanaVersion,
});
const mergedAPMPackagePolicy = await mergePackagePolicyWithApm({
setup,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,28 @@
*/

import {
isPrereleaseVersion,
POLICY_ELASTIC_AGENT_ON_CLOUD,
SUPPORTED_APM_PACKAGE_VERSION,
} from '../../../common/fleet';
import { APMPluginSetupDependencies } from '../../types';
import {
APMPluginSetupDependencies,
APMPluginStartDependencies,
} from '../../types';
import { APM_PACKAGE_NAME } from './get_cloud_apm_package_policy';

interface GetApmPackagePolicyDefinitionOptions {
apmServerSchema: Record<string, any>;
cloudPluginSetup: APMPluginSetupDependencies['cloud'];
fleetPluginStart: APMPluginStartDependencies['fleet'];
kibanaVersion: string;
}
export function getApmPackagePolicyDefinition(
export async function getApmPackagePolicyDefinition(
options: GetApmPackagePolicyDefinitionOptions
) {
const { apmServerSchema, cloudPluginSetup } = options;
const { apmServerSchema, cloudPluginSetup, fleetPluginStart, kibanaVersion } =
options;

return {
name: 'Elastic APM',
namespace: 'default',
Expand All @@ -33,18 +41,37 @@ export function getApmPackagePolicyDefinition(
streams: [],
vars: getApmPackageInputVars({
cloudPluginSetup,
fleetPluginStart,
apmServerSchema: preprocessLegacyFields({ apmServerSchema }),
kibanaVersion,
}),
},
],
package: {
name: APM_PACKAGE_NAME,
version: SUPPORTED_APM_PACKAGE_VERSION,
version: await getApmPackageVersion(fleetPluginStart, kibanaVersion),
title: 'Elastic APM',
},
};
}

async function getApmPackageVersion(
fleetPluginStart: APMPluginStartDependencies['fleet'],
kibanaVersion: string
) {
if (fleetPluginStart && isPrereleaseVersion(kibanaVersion)) {
try {
const latestApmPackage = await fleetPluginStart.fetchFindLatestPackage(
'apm'
);
return latestApmPackage.version;
} catch (error) {
return SUPPORTED_APM_PACKAGE_VERSION;
}
}
return SUPPORTED_APM_PACKAGE_VERSION;
}

export function preprocessLegacyFields({
apmServerSchema,
}: {
Expand Down
Loading

0 comments on commit f0462e0

Please sign in to comment.