Skip to content

Commit 0202164

Browse files
authored
[APM] Lint rule for explicit return types (elastic#124771)
1 parent d720235 commit 0202164

File tree

9 files changed

+160
-80
lines changed

9 files changed

+160
-80
lines changed

.eslintrc.js

+12
Original file line numberDiff line numberDiff line change
@@ -886,6 +886,18 @@ module.exports = {
886886
],
887887
},
888888
},
889+
{
890+
// require explicit return types in route handlers for performance reasons
891+
files: ['x-pack/plugins/apm/server/**/route.ts'],
892+
rules: {
893+
'@typescript-eslint/explicit-function-return-type': [
894+
'error',
895+
{
896+
allowTypedFunctionExpressions: false,
897+
},
898+
],
899+
},
900+
},
889901

890902
/**
891903
* Fleet overrides

x-pack/plugins/apm/server/routes/backends/route.ts

+21-12
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import { getTopBackends } from './get_top_backends';
2121
import { getUpstreamServicesForBackend } from './get_upstream_services_for_backend';
2222
import { getThroughputChartsForBackend } from './get_throughput_charts_for_backend';
2323
import { getErrorRateChartsForBackend } from './get_error_rate_charts_for_backend';
24+
import { ConnectionStatsItemWithImpact } from './../../../common/connections';
2425

2526
const topBackendsRoute = createApmServerRoute({
2627
endpoint: 'GET /internal/apm/backends/top_backends',
@@ -105,10 +106,11 @@ const topBackendsRoute = createApmServerRoute({
105106
]);
106107

107108
return {
109+
// eslint-disable-next-line @typescript-eslint/explicit-function-return-type
108110
backends: currentBackends.map((backend) => {
109111
const { stats, ...rest } = backend;
110112
const prev = previousBackends.find(
111-
(item) => item.location.id === backend.location.id
113+
(item): boolean => item.location.id === backend.location.id
112114
);
113115
return {
114116
...rest,
@@ -221,17 +223,24 @@ const upstreamServicesForBackendRoute = createApmServerRoute({
221223
]);
222224

223225
return {
224-
services: currentServices.map((service) => {
225-
const { stats, ...rest } = service;
226-
const prev = previousServices.find(
227-
(item) => item.location.id === service.location.id
228-
);
229-
return {
230-
...rest,
231-
currentStats: stats,
232-
previousStats: prev?.stats ?? null,
233-
};
234-
}),
226+
services: currentServices.map(
227+
(
228+
service
229+
): Omit<ConnectionStatsItemWithImpact, 'stats'> & {
230+
currentStats: ConnectionStatsItemWithImpact['stats'];
231+
previousStats: ConnectionStatsItemWithImpact['stats'] | null;
232+
} => {
233+
const { stats, ...rest } = service;
234+
const prev = previousServices.find(
235+
(item): boolean => item.location.id === service.location.id
236+
);
237+
return {
238+
...rest,
239+
currentStats: stats,
240+
previousStats: prev?.stats ?? null,
241+
};
242+
}
243+
),
235244
};
236245
},
237246
});

x-pack/plugins/apm/server/routes/correlations/route.ts

+20-6
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,13 @@ import { withApmSpan } from '../../utils/with_apm_span';
2727

2828
import { createApmServerRoute } from '../apm_routes/create_apm_server_route';
2929
import { environmentRt, kueryRt, rangeRt } from '../default_api_types';
30+
import { LatencyCorrelation } from './../../../common/correlations/latency_correlations/types';
31+
import {
32+
FieldStats,
33+
TopValuesStats,
34+
} from './../../../common/correlations/field_stats_types';
35+
import { FieldValuePair } from './../../../common/correlations/types';
36+
import { FailedTransactionsCorrelation } from './../../../common/correlations/failed_transactions_correlations/types';
3037

3138
const INVALID_LICENSE = i18n.translate('xpack.apm.correlations.license.text', {
3239
defaultMessage:
@@ -59,7 +66,7 @@ const fieldCandidatesRoute = createApmServerRoute({
5966

6067
return withApmSpan(
6168
'get_correlations_field_candidates',
62-
async () =>
69+
async (): Promise<{ fieldCandidates: string[] }> =>
6370
await fetchTransactionDurationFieldCandidates(esClient, {
6471
...resources.params.query,
6572
index: indices.transaction,
@@ -106,7 +113,7 @@ const fieldStatsRoute = createApmServerRoute({
106113

107114
return withApmSpan(
108115
'get_correlations_field_stats',
109-
async () =>
116+
async (): Promise<{ stats: FieldStats[]; errors: any[] }> =>
110117
await fetchFieldsStats(
111118
esClient,
112119
{
@@ -155,7 +162,7 @@ const fieldValueStatsRoute = createApmServerRoute({
155162

156163
return withApmSpan(
157164
'get_correlations_field_value_stats',
158-
async () =>
165+
async (): Promise<TopValuesStats> =>
159166
await fetchFieldValueFieldStats(
160167
esClient,
161168
{
@@ -206,7 +213,7 @@ const fieldValuePairsRoute = createApmServerRoute({
206213

207214
return withApmSpan(
208215
'get_correlations_field_value_pairs',
209-
async () =>
216+
async (): Promise<{ errors: any[]; fieldValuePairs: FieldValuePair[] }> =>
210217
await fetchTransactionDurationFieldValuePairs(
211218
esClient,
212219
{
@@ -268,7 +275,11 @@ const significantCorrelationsRoute = createApmServerRoute({
268275

269276
return withApmSpan(
270277
'get_significant_correlations',
271-
async () =>
278+
async (): Promise<{
279+
latencyCorrelations: LatencyCorrelation[];
280+
ccsWarning: boolean;
281+
totalDocCount: number;
282+
}> =>
272283
await fetchSignificantCorrelations(
273284
esClient,
274285
paramsWithIndex,
@@ -321,7 +332,10 @@ const pValuesRoute = createApmServerRoute({
321332

322333
return withApmSpan(
323334
'get_p_values',
324-
async () => await fetchPValues(esClient, paramsWithIndex, fieldCandidates)
335+
async (): Promise<{
336+
failedTransactionsCorrelations: FailedTransactionsCorrelation[];
337+
ccsWarning: boolean;
338+
}> => await fetchPValues(esClient, paramsWithIndex, fieldCandidates)
325339
);
326340
},
327341
});

x-pack/plugins/apm/server/routes/data_view/route.ts

+5-1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import { createStaticDataView } from './create_static_data_view';
99
import { setupRequest } from '../../lib/helpers/setup_request';
1010
import { getDynamicDataView } from './get_dynamic_data_view';
1111
import { createApmServerRoute } from '../apm_routes/create_apm_server_route';
12+
import { ISavedObjectsRepository } from '../../../../../../src/core/server';
1213

1314
const staticDataViewRoute = createApmServerRoute({
1415
endpoint: 'POST /internal/apm/data_view/static',
@@ -24,7 +25,10 @@ const staticDataViewRoute = createApmServerRoute({
2425
const setupPromise = setupRequest(resources);
2526
const clientPromise = core
2627
.start()
27-
.then((coreStart) => coreStart.savedObjects.createInternalRepository());
28+
.then(
29+
(coreStart): ISavedObjectsRepository =>
30+
coreStart.savedObjects.createInternalRepository()
31+
);
2832

2933
const setup = await setupPromise;
3034
const savedObjectsClient = await clientPromise;

x-pack/plugins/apm/server/routes/fleet/route.ts

+19-10
Original file line numberDiff line numberDiff line change
@@ -105,16 +105,25 @@ const fleetAgentsRoute = createApmServerRoute({
105105
return {
106106
cloudStandaloneSetup,
107107
isFleetEnabled: true,
108-
fleetAgents: fleetAgents.map((agent) => {
109-
const packagePolicy = policiesGroupedById[agent.id];
110-
const packagePolicyVars = packagePolicy.inputs[0]?.vars;
111-
return {
112-
id: agent.id,
113-
name: agent.name,
114-
apmServerUrl: packagePolicyVars?.url?.value,
115-
secretToken: packagePolicyVars?.secret_token?.value,
116-
};
117-
}),
108+
fleetAgents: fleetAgents.map(
109+
(
110+
agent
111+
): {
112+
id: string;
113+
name: string;
114+
apmServerUrl: string | undefined;
115+
secretToken: string | undefined;
116+
} => {
117+
const packagePolicy = policiesGroupedById[agent.id];
118+
const packagePolicyVars = packagePolicy.inputs[0]?.vars;
119+
return {
120+
id: agent.id,
121+
name: agent.name,
122+
apmServerUrl: packagePolicyVars?.url?.value,
123+
secretToken: packagePolicyVars?.secret_token?.value,
124+
};
125+
}
126+
),
118127
};
119128
},
120129
});

x-pack/plugins/apm/server/routes/observability_overview/route.ts

+30-19
Original file line numberDiff line numberDiff line change
@@ -58,25 +58,36 @@ const observabilityOverviewRoute = createApmServerRoute({
5858
kuery: '',
5959
});
6060

61-
return withApmSpan('observability_overview', async () => {
62-
const [serviceCount, transactionPerMinute] = await Promise.all([
63-
getServiceCount({
64-
setup,
65-
searchAggregatedTransactions,
66-
start,
67-
end,
68-
}),
69-
getTransactionsPerMinute({
70-
setup,
71-
bucketSize,
72-
searchAggregatedTransactions,
73-
start,
74-
end,
75-
intervalString,
76-
}),
77-
]);
78-
return { serviceCount, transactionPerMinute };
79-
});
61+
return withApmSpan(
62+
'observability_overview',
63+
async (): Promise<{
64+
serviceCount: number;
65+
transactionPerMinute:
66+
| { value: undefined; timeseries: never[] }
67+
| {
68+
value: number;
69+
timeseries: Array<{ x: number; y: number | null }>;
70+
};
71+
}> => {
72+
const [serviceCount, transactionPerMinute] = await Promise.all([
73+
getServiceCount({
74+
setup,
75+
searchAggregatedTransactions,
76+
start,
77+
end,
78+
}),
79+
getTransactionsPerMinute({
80+
setup,
81+
bucketSize,
82+
searchAggregatedTransactions,
83+
start,
84+
end,
85+
intervalString,
86+
}),
87+
]);
88+
return { serviceCount, transactionPerMinute };
89+
}
90+
);
8091
},
8192
});
8293

x-pack/plugins/apm/server/routes/rum_client/route.ts

+1
Original file line numberDiff line numberDiff line change
@@ -407,6 +407,7 @@ function decodeUiFilters(
407407
}
408408
}
409409

410+
// eslint-disable-next-line @typescript-eslint/explicit-function-return-type
410411
async function setupUXRequest<TParams extends SetupUXRequestParams>(
411412
resources: APMRouteHandlerResources & { params: TParams }
412413
) {

x-pack/plugins/apm/server/routes/services/route.ts

+46-30
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,9 @@ import {
4949
} from '../../../../ml/server';
5050
import { getServiceInstancesDetailedStatisticsPeriods } from './get_service_instances/detailed_statistics';
5151
import { ML_ERRORS } from '../../../common/anomaly_detection';
52+
import { ScopedAnnotationsClient } from '../../../../observability/server';
53+
import { Annotation } from './../../../../observability/common/annotations';
54+
import { ConnectionStatsItemWithImpact } from './../../../common/connections';
5255

5356
const servicesRoute = createApmServerRoute({
5457
endpoint: 'GET /internal/apm/services',
@@ -373,8 +376,10 @@ const serviceAnnotationsRoute = createApmServerRoute({
373376
const [annotationsClient, searchAggregatedTransactions] = await Promise.all(
374377
[
375378
observability
376-
? withApmSpan('get_scoped_annotations_client', () =>
377-
observability.setup.getScopedAnnotationsClient(context, request)
379+
? withApmSpan(
380+
'get_scoped_annotations_client',
381+
(): Promise<undefined | ScopedAnnotationsClient> =>
382+
observability.setup.getScopedAnnotationsClient(context, request)
378383
)
379384
: undefined,
380385
getSearchAggregatedTransactions({
@@ -443,8 +448,10 @@ const serviceAnnotationsCreateRoute = createApmServerRoute({
443448
} = resources;
444449

445450
const annotationsClient = observability
446-
? await withApmSpan('get_scoped_annotations_client', () =>
447-
observability.setup.getScopedAnnotationsClient(context, request)
451+
? await withApmSpan(
452+
'get_scoped_annotations_client',
453+
(): Promise<undefined | ScopedAnnotationsClient> =>
454+
observability.setup.getScopedAnnotationsClient(context, request)
448455
)
449456
: undefined;
450457

@@ -454,20 +461,22 @@ const serviceAnnotationsCreateRoute = createApmServerRoute({
454461

455462
const { body, path } = params;
456463

457-
return withApmSpan('create_annotation', () =>
458-
annotationsClient.create({
459-
message: body.service.version,
460-
...body,
461-
'@timestamp': new Date(body['@timestamp']).toISOString(),
462-
annotation: {
463-
type: 'deployment',
464-
},
465-
service: {
466-
...body.service,
467-
name: path.serviceName,
468-
},
469-
tags: uniq(['apm'].concat(body.tags ?? [])),
470-
})
464+
return withApmSpan(
465+
'create_annotation',
466+
(): Promise<{ _id: string; _index: string; _source: Annotation }> =>
467+
annotationsClient.create({
468+
message: body.service.version,
469+
...body,
470+
'@timestamp': new Date(body['@timestamp']).toISOString(),
471+
annotation: {
472+
type: 'deployment',
473+
},
474+
service: {
475+
...body.service,
476+
name: path.serviceName,
477+
},
478+
tags: uniq(['apm'].concat(body.tags ?? [])),
479+
})
471480
);
472481
},
473482
});
@@ -925,18 +934,25 @@ export const serviceDependenciesRoute = createApmServerRoute({
925934
]);
926935

927936
return {
928-
serviceDependencies: currentPeriod.map((item) => {
929-
const { stats, ...rest } = item;
930-
const previousPeriodItem = previousPeriod.find(
931-
(prevItem) => item.location.id === prevItem.location.id
932-
);
933-
934-
return {
935-
...rest,
936-
currentStats: stats,
937-
previousStats: previousPeriodItem?.stats || null,
938-
};
939-
}),
937+
serviceDependencies: currentPeriod.map(
938+
(
939+
item
940+
): Omit<ConnectionStatsItemWithImpact, 'stats'> & {
941+
currentStats: ConnectionStatsItemWithImpact['stats'];
942+
previousStats: ConnectionStatsItemWithImpact['stats'] | null;
943+
} => {
944+
const { stats, ...rest } = item;
945+
const previousPeriodItem = previousPeriod.find(
946+
(prevItem): boolean => item.location.id === prevItem.location.id
947+
);
948+
949+
return {
950+
...rest,
951+
currentStats: stats,
952+
previousStats: previousPeriodItem?.stats || null,
953+
};
954+
}
955+
),
940956
};
941957
},
942958
});

0 commit comments

Comments
 (0)