Skip to content

Commit 6ec65d2

Browse files
committed
refactoring
1 parent 6e49da5 commit 6ec65d2

File tree

7 files changed

+79
-52
lines changed

7 files changed

+79
-52
lines changed
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
/*
2+
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
3+
* or more contributor license agreements. Licensed under the Elastic License;
4+
* you may not use this file except in compliance with the Elastic License.
5+
*/
6+
import { i18n } from '@kbn/i18n';
7+
8+
export const INVALID_LICENSE = i18n.translate(
9+
'xpack.apm.settings.customizeUI.customLink.license.text',
10+
{
11+
defaultMessage:
12+
"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.",
13+
}
14+
);

x-pack/plugins/apm/public/components/app/Settings/CustomizeUI/CustomLink/index.tsx

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
import { EuiPanel, EuiSpacer, EuiFlexGroup, EuiFlexItem } from '@elastic/eui';
88
import { isEmpty } from 'lodash';
99
import React, { useEffect, useState } from 'react';
10-
import { i18n } from '@kbn/i18n';
10+
import { INVALID_LICENSE } from '../../../../../../common/custom_link';
1111
import { CustomLink } from '../../../../../../common/custom_link/custom_link_types';
1212
import { useLicense } from '../../../../../hooks/useLicense';
1313
import { useFetcher, FETCH_STATUS } from '../../../../../hooks/useFetcher';
@@ -94,15 +94,7 @@ export function CustomLinkOverview() {
9494
/>
9595
)
9696
) : (
97-
<LicensePrompt
98-
text={i18n.translate(
99-
'xpack.apm.settings.customizeUI.customLink.license.text',
100-
{
101-
defaultMessage:
102-
"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.",
103-
}
104-
)}
105-
/>
97+
<LicensePrompt text={INVALID_LICENSE} />
10698
)}
10799
</EuiPanel>
108100
</>

x-pack/plugins/apm/server/feature.ts

Lines changed: 43 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,12 @@
55
*/
66

77
import { i18n } from '@kbn/i18n';
8+
import { LicenseType } from '../../licensing/common/types';
89
import { AlertType } from '../common/alert_types';
10+
import {
11+
LicensingPluginSetup,
12+
LicensingRequestHandlerContext,
13+
} from '../../licensing/server';
914

1015
export const APM_FEATURE = {
1116
id: 'apm',
@@ -58,11 +63,43 @@ export const APM_FEATURE = {
5863
},
5964
};
6065

61-
export const APM_SERVICE_MAPS_FEATURE_NAME = 'APM service maps';
62-
export const APM_SERVICE_MAPS_LICENSE_TYPE = 'platinum';
66+
interface Feature {
67+
name: string;
68+
license: LicenseType;
69+
}
70+
type FeatureName = 'serviceMaps' | 'ml' | 'customLinks';
71+
export const features: Record<FeatureName, Feature> = {
72+
serviceMaps: {
73+
name: 'APM service maps',
74+
license: 'platinum',
75+
},
76+
ml: {
77+
name: 'APM machine learning',
78+
license: 'platinum',
79+
},
80+
customLinks: {
81+
name: 'APM custom links',
82+
license: 'gold',
83+
},
84+
};
6385

64-
export const APM_ML_FEATURE_NAME = 'APM machine learning';
65-
export const APM_ML_LICENSE_TYPE = 'platinum';
86+
export function registerFeaturesUsage({
87+
licensingPlugin,
88+
}: {
89+
licensingPlugin: LicensingPluginSetup;
90+
}) {
91+
Object.values(features).forEach(({ name, license }) => {
92+
licensingPlugin.featureUsage.register(name, license);
93+
});
94+
}
6695

67-
export const APM_CUSTOM_LINKS_FEATURE_NAME = 'APM custom links';
68-
export const APM_CUSTOM_LINKS_LICENSE_TYPE = 'gold';
96+
export function notifyFeatureUsage({
97+
licensingPlugin,
98+
featureName,
99+
}: {
100+
licensingPlugin: LicensingRequestHandlerContext;
101+
featureName: FeatureName;
102+
}) {
103+
const feature = features[featureName];
104+
licensingPlugin.featureUsage.notifyUsage(feature.name);
105+
}

x-pack/plugins/apm/server/plugin.ts

Lines changed: 3 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -26,15 +26,7 @@ import { MlPluginSetup } from '../../ml/server';
2626
import { ObservabilityPluginSetup } from '../../observability/server';
2727
import { SecurityPluginSetup } from '../../security/server';
2828
import { TaskManagerSetupContract } from '../../task_manager/server';
29-
import {
30-
APM_CUSTOM_LINKS_FEATURE_NAME,
31-
APM_CUSTOM_LINKS_LICENSE_TYPE,
32-
APM_FEATURE,
33-
APM_ML_FEATURE_NAME,
34-
APM_ML_LICENSE_TYPE,
35-
APM_SERVICE_MAPS_FEATURE_NAME,
36-
APM_SERVICE_MAPS_LICENSE_TYPE,
37-
} from './feature';
29+
import { APM_FEATURE, registerFeaturesUsage } from './feature';
3830
import { registerApmAlerts } from './lib/alerts/register_apm_alerts';
3931
import { createApmTelemetry } from './lib/apm_telemetry';
4032
import { getInternalSavedObjectsClient } from './lib/helpers/get_internal_saved_objects_client';
@@ -132,18 +124,8 @@ export class APMPlugin implements Plugin<APMPluginSetup> {
132124
});
133125

134126
plugins.features.registerKibanaFeature(APM_FEATURE);
135-
plugins.licensing.featureUsage.register(
136-
APM_SERVICE_MAPS_FEATURE_NAME,
137-
APM_SERVICE_MAPS_LICENSE_TYPE
138-
);
139-
plugins.licensing.featureUsage.register(
140-
APM_ML_FEATURE_NAME,
141-
APM_ML_LICENSE_TYPE
142-
);
143-
plugins.licensing.featureUsage.register(
144-
APM_CUSTOM_LINKS_FEATURE_NAME,
145-
APM_CUSTOM_LINKS_LICENSE_TYPE
146-
);
127+
128+
registerFeaturesUsage({ licensingPlugin: plugins.licensing });
147129

148130
createApmApi().init(core, {
149131
config$: mergedConfig$,

x-pack/plugins/apm/server/routes/service_map.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import { getServiceMap } from '../lib/service_map/get_service_map';
1515
import { getServiceMapServiceNodeInfo } from '../lib/service_map/get_service_map_service_node_info';
1616
import { createRoute } from './create_route';
1717
import { rangeRt, uiFiltersRt } from './default_api_types';
18-
import { APM_SERVICE_MAPS_FEATURE_NAME } from '../feature';
18+
import { notifyFeatureUsage } from '../feature';
1919
import { getSearchAggregatedTransactions } from '../lib/helpers/aggregated_transactions';
2020
import { getParsedUiFilters } from '../lib/helpers/convert_ui_filters/get_parsed_ui_filters';
2121

@@ -37,7 +37,11 @@ export const serviceMapRoute = createRoute(() => ({
3737
if (!isActivePlatinumLicense(context.licensing.license)) {
3838
throw Boom.forbidden(invalidLicenseMessage);
3939
}
40-
context.licensing.featureUsage.notifyUsage(APM_SERVICE_MAPS_FEATURE_NAME);
40+
41+
notifyFeatureUsage({
42+
licensingPlugin: context.licensing,
43+
featureName: 'serviceMaps',
44+
});
4145

4246
const logger = context.logger;
4347
const setup = await setupRequest(context, request);

x-pack/plugins/apm/server/routes/settings/anomaly_detection.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import { setupRequest } from '../../lib/helpers/setup_request';
1515
import { getAllEnvironments } from '../../lib/environments/get_all_environments';
1616
import { hasLegacyJobs } from '../../lib/anomaly_detection/has_legacy_jobs';
1717
import { getSearchAggregatedTransactions } from '../../lib/helpers/aggregated_transactions';
18-
import { APM_ML_FEATURE_NAME } from '../../feature';
18+
import { notifyFeatureUsage } from '../../feature';
1919

2020
// get ML anomaly detection jobs for each environment
2121
export const anomalyDetectionJobsRoute = createRoute(() => ({
@@ -63,7 +63,10 @@ export const createAnomalyDetectionJobsRoute = createRoute(() => ({
6363
}
6464

6565
await createAnomalyDetectionJobs(setup, environments, context.logger);
66-
context.licensing.featureUsage.notifyUsage(APM_ML_FEATURE_NAME);
66+
notifyFeatureUsage({
67+
licensingPlugin: context.licensing,
68+
featureName: 'ml',
69+
});
6770
},
6871
}));
6972

x-pack/plugins/apm/server/routes/settings/custom_link.ts

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,13 @@
44
* you may not use this file except in compliance with the Elastic License.
55
*/
66

7-
import { i18n } from '@kbn/i18n';
87
import Boom from 'boom';
98
import * as t from 'io-ts';
109
import { pick } from 'lodash';
10+
import { INVALID_LICENSE } from '../../../common/custom_link';
1111
import { ILicense } from '../../../../licensing/common/types';
1212
import { FILTER_OPTIONS } from '../../../common/custom_link/custom_link_filter_options';
13-
import { APM_CUSTOM_LINKS_FEATURE_NAME } from '../../feature';
13+
import { notifyFeatureUsage } from '../../feature';
1414
import { setupRequest } from '../../lib/helpers/setup_request';
1515
import { createOrUpdateCustomLink } from '../../lib/settings/custom_link/create_or_update_custom_link';
1616
import {
@@ -26,14 +26,6 @@ function isActiveGoldLicense(license: ILicense) {
2626
return license.isActive && license.hasAtLeast('gold');
2727
}
2828

29-
const INVALID_LICENSE = i18n.translate(
30-
'xpack.apm.settings.customizeUI.customLink.forbidden',
31-
{
32-
defaultMessage:
33-
"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.",
34-
}
35-
);
36-
3729
export const customLinkTransactionRoute = createRoute(() => ({
3830
path: '/api/apm/settings/custom_links/transaction',
3931
params: {
@@ -82,7 +74,10 @@ export const createCustomLinkRoute = createRoute(() => ({
8274
const customLink = context.params.body;
8375
const res = await createOrUpdateCustomLink({ customLink, setup });
8476

85-
context.licensing.featureUsage.notifyUsage(APM_CUSTOM_LINKS_FEATURE_NAME);
77+
notifyFeatureUsage({
78+
licensingPlugin: context.licensing,
79+
featureName: 'customLinks',
80+
});
8681
return res;
8782
},
8883
}));

0 commit comments

Comments
 (0)