Skip to content

Commit

Permalink
[APM] Track usage of Gold+ features (#77630)
Browse files Browse the repository at this point in the history
* adding license check

* fixing api test

* refactoring
  • Loading branch information
cauemarcondes authored Sep 18, 2020
1 parent 89ef12e commit e7b0e28
Show file tree
Hide file tree
Showing 11 changed files with 271 additions and 163 deletions.
14 changes: 14 additions & 0 deletions x-pack/plugins/apm/common/custom_link/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/*
* 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 { i18n } from '@kbn/i18n';

export const INVALID_LICENSE = i18n.translate(
'xpack.apm.settings.customizeUI.customLink.license.text',
{
defaultMessage:
"To create custom links, you must be subscribed to an Elastic Gold license or above. With it, you'll have the ability to create custom links to improve your workflow when analyzing your services.",
}
);
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import { EuiPanel, EuiSpacer, EuiFlexGroup, EuiFlexItem } from '@elastic/eui';
import { isEmpty } from 'lodash';
import React, { useEffect, useState } from 'react';
import { i18n } from '@kbn/i18n';
import { INVALID_LICENSE } from '../../../../../../common/custom_link';
import { CustomLink } from '../../../../../../common/custom_link/custom_link_types';
import { useLicense } from '../../../../../hooks/useLicense';
import { useFetcher, FETCH_STATUS } from '../../../../../hooks/useFetcher';
Expand Down Expand Up @@ -94,15 +94,7 @@ export function CustomLinkOverview() {
/>
)
) : (
<LicensePrompt
text={i18n.translate(
'xpack.apm.settings.customizeUI.customLink.license.text',
{
defaultMessage:
"To create custom links, you must be subscribed to an Elastic Gold license or above. With it, you'll have the ability to create custom links to improve your workflow when analyzing your services.",
}
)}
/>
<LicensePrompt text={INVALID_LICENSE} />
)}
</EuiPanel>
</>
Expand Down
47 changes: 45 additions & 2 deletions x-pack/plugins/apm/server/feature.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,12 @@
*/

import { i18n } from '@kbn/i18n';
import { LicenseType } from '../../licensing/common/types';
import { AlertType } from '../common/alert_types';
import {
LicensingPluginSetup,
LicensingRequestHandlerContext,
} from '../../licensing/server';

export const APM_FEATURE = {
id: 'apm',
Expand Down Expand Up @@ -58,5 +63,43 @@ export const APM_FEATURE = {
},
};

export const APM_SERVICE_MAPS_FEATURE_NAME = 'APM service maps';
export const APM_SERVICE_MAPS_LICENSE_TYPE = 'platinum';
interface Feature {
name: string;
license: LicenseType;
}
type FeatureName = 'serviceMaps' | 'ml' | 'customLinks';
export const features: Record<FeatureName, Feature> = {
serviceMaps: {
name: 'APM service maps',
license: 'platinum',
},
ml: {
name: 'APM machine learning',
license: 'platinum',
},
customLinks: {
name: 'APM custom links',
license: 'gold',
},
};

export function registerFeaturesUsage({
licensingPlugin,
}: {
licensingPlugin: LicensingPluginSetup;
}) {
Object.values(features).forEach(({ name, license }) => {
licensingPlugin.featureUsage.register(name, license);
});
}

export function notifyFeatureUsage({
licensingPlugin,
featureName,
}: {
licensingPlugin: LicensingRequestHandlerContext;
featureName: FeatureName;
}) {
const feature = features[featureName];
licensingPlugin.featureUsage.notifyUsage(feature.name);
}
12 changes: 3 additions & 9 deletions x-pack/plugins/apm/server/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,7 @@ import { MlPluginSetup } from '../../ml/server';
import { ObservabilityPluginSetup } from '../../observability/server';
import { SecurityPluginSetup } from '../../security/server';
import { TaskManagerSetupContract } from '../../task_manager/server';
import {
APM_FEATURE,
APM_SERVICE_MAPS_FEATURE_NAME,
APM_SERVICE_MAPS_LICENSE_TYPE,
} from './feature';
import { APM_FEATURE, registerFeaturesUsage } from './feature';
import { registerApmAlerts } from './lib/alerts/register_apm_alerts';
import { createApmTelemetry } from './lib/apm_telemetry';
import { getInternalSavedObjectsClient } from './lib/helpers/get_internal_saved_objects_client';
Expand Down Expand Up @@ -128,10 +124,8 @@ export class APMPlugin implements Plugin<APMPluginSetup> {
});

plugins.features.registerKibanaFeature(APM_FEATURE);
plugins.licensing.featureUsage.register(
APM_SERVICE_MAPS_FEATURE_NAME,
APM_SERVICE_MAPS_LICENSE_TYPE
);

registerFeaturesUsage({ licensingPlugin: plugins.licensing });

createApmApi().init(core, {
config$: mergedConfig$,
Expand Down
8 changes: 6 additions & 2 deletions x-pack/plugins/apm/server/routes/service_map.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import { getServiceMap } from '../lib/service_map/get_service_map';
import { getServiceMapServiceNodeInfo } from '../lib/service_map/get_service_map_service_node_info';
import { createRoute } from './create_route';
import { rangeRt, uiFiltersRt } from './default_api_types';
import { APM_SERVICE_MAPS_FEATURE_NAME } from '../feature';
import { notifyFeatureUsage } from '../feature';
import { getSearchAggregatedTransactions } from '../lib/helpers/aggregated_transactions';
import { getParsedUiFilters } from '../lib/helpers/convert_ui_filters/get_parsed_ui_filters';

Expand All @@ -37,7 +37,11 @@ export const serviceMapRoute = createRoute(() => ({
if (!isActivePlatinumLicense(context.licensing.license)) {
throw Boom.forbidden(invalidLicenseMessage);
}
context.licensing.featureUsage.notifyUsage(APM_SERVICE_MAPS_FEATURE_NAME);

notifyFeatureUsage({
licensingPlugin: context.licensing,
featureName: 'serviceMaps',
});

const logger = context.logger;
const setup = await setupRequest(context, request);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import { setupRequest } from '../../lib/helpers/setup_request';
import { getAllEnvironments } from '../../lib/environments/get_all_environments';
import { hasLegacyJobs } from '../../lib/anomaly_detection/has_legacy_jobs';
import { getSearchAggregatedTransactions } from '../../lib/helpers/aggregated_transactions';
import { notifyFeatureUsage } from '../../feature';

// get ML anomaly detection jobs for each environment
export const anomalyDetectionJobsRoute = createRoute(() => ({
Expand Down Expand Up @@ -62,6 +63,10 @@ export const createAnomalyDetectionJobsRoute = createRoute(() => ({
}

await createAnomalyDetectionJobs(setup, environments, context.logger);
notifyFeatureUsage({
licensingPlugin: context.licensing,
featureName: 'ml',
});
},
}));

Expand Down
26 changes: 26 additions & 0 deletions x-pack/plugins/apm/server/routes/settings/custom_link.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,14 @@
* or more contributor license agreements. Licensed under the Elastic License;
* you may not use this file except in compliance with the Elastic License.
*/

import Boom from 'boom';
import * as t from 'io-ts';
import { pick } from 'lodash';
import { INVALID_LICENSE } from '../../../common/custom_link';
import { ILicense } from '../../../../licensing/common/types';
import { FILTER_OPTIONS } from '../../../common/custom_link/custom_link_filter_options';
import { notifyFeatureUsage } from '../../feature';
import { setupRequest } from '../../lib/helpers/setup_request';
import { createOrUpdateCustomLink } from '../../lib/settings/custom_link/create_or_update_custom_link';
import {
Expand All @@ -17,6 +22,10 @@ import { getTransaction } from '../../lib/settings/custom_link/get_transaction';
import { listCustomLinks } from '../../lib/settings/custom_link/list_custom_links';
import { createRoute } from '../create_route';

function isActiveGoldLicense(license: ILicense) {
return license.isActive && license.hasAtLeast('gold');
}

export const customLinkTransactionRoute = createRoute(() => ({
path: '/api/apm/settings/custom_links/transaction',
params: {
Expand All @@ -37,6 +46,9 @@ export const listCustomLinksRoute = createRoute(() => ({
query: filterOptionsRt,
},
handler: async ({ context, request }) => {
if (!isActiveGoldLicense(context.licensing.license)) {
throw Boom.forbidden(INVALID_LICENSE);
}
const setup = await setupRequest(context, request);
const { query } = context.params;
// picks only the items listed in FILTER_OPTIONS
Expand All @@ -55,9 +67,17 @@ export const createCustomLinkRoute = createRoute(() => ({
tags: ['access:apm', 'access:apm_write'],
},
handler: async ({ context, request }) => {
if (!isActiveGoldLicense(context.licensing.license)) {
throw Boom.forbidden(INVALID_LICENSE);
}
const setup = await setupRequest(context, request);
const customLink = context.params.body;
const res = await createOrUpdateCustomLink({ customLink, setup });

notifyFeatureUsage({
licensingPlugin: context.licensing,
featureName: 'customLinks',
});
return res;
},
}));
Expand All @@ -75,6 +95,9 @@ export const updateCustomLinkRoute = createRoute(() => ({
tags: ['access:apm', 'access:apm_write'],
},
handler: async ({ context, request }) => {
if (!isActiveGoldLicense(context.licensing.license)) {
throw Boom.forbidden(INVALID_LICENSE);
}
const setup = await setupRequest(context, request);
const { id } = context.params.path;
const customLink = context.params.body;
Expand All @@ -99,6 +122,9 @@ export const deleteCustomLinkRoute = createRoute(() => ({
tags: ['access:apm', 'access:apm_write'],
},
handler: async ({ context, request }) => {
if (!isActiveGoldLicense(context.licensing.license)) {
throw Boom.forbidden(INVALID_LICENSE);
}
const setup = await setupRequest(context, request);
const { id } = context.params.path;
const res = await deleteCustomLink({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -149,13 +149,6 @@ export default function featureControlsTests({ getService }: FtrProviderContext)
log.error(JSON.stringify(res, null, 2));
},
},
{
req: {
url: `/api/apm/settings/custom_links`,
},
expectForbidden: expect404,
expectResponse: expect200,
},
{
req: {
url: `/api/apm/settings/custom_links/transaction`,
Expand Down
Loading

0 comments on commit e7b0e28

Please sign in to comment.