-
Notifications
You must be signed in to change notification settings - Fork 8.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Closes #42169 - Adds /api/infra/apm_metrics - Adds makeInternalRequest method to Framwork library - Adds scafolding for tests - Removes duplicate version of Boom - Adds tests
- Loading branch information
1 parent
fcd0d73
commit b57bee7
Showing
15 changed files
with
390 additions
and
31 deletions.
There are no files selected for viewing
97 changes: 97 additions & 0 deletions
97
x-pack/legacy/plugins/infra/common/http_api/apm_metrics_api.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
/* | ||
* 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 * as rt from 'io-ts'; | ||
import { InfraWrappableRequest } from '../../server/lib/adapters/framework'; | ||
import { InfraNodeTypeRT } from './common'; | ||
|
||
export const InfraApmMetricsRequestRT = rt.type({ | ||
timeRange: rt.type({ | ||
min: rt.number, | ||
max: rt.number, | ||
}), | ||
nodeId: rt.string, | ||
nodeType: InfraNodeTypeRT, | ||
sourceId: rt.string, | ||
}); | ||
|
||
export const InfraApmMetricsDataPointRT = rt.type({ | ||
timestamp: rt.number, | ||
value: rt.union([rt.number, rt.null]), | ||
}); | ||
|
||
export const InfraApmMetricsDataBucketRT = rt.type({ | ||
name: rt.string, | ||
data: rt.array(InfraApmMetricsDataPointRT), | ||
}); | ||
|
||
export const InfraApmMetricsServiceRT = rt.type({ | ||
name: rt.string, | ||
transactionsPerMinute: rt.array(InfraApmMetricsDataBucketRT), | ||
responseTimes: rt.array(InfraApmMetricsDataBucketRT), | ||
}); | ||
|
||
export const InfraApmMetricsRT = rt.type({ | ||
services: rt.array(InfraApmMetricsServiceRT), | ||
}); | ||
|
||
export const APMDataPointRT = rt.type({ | ||
x: rt.number, | ||
y: rt.union([rt.number, rt.null]), | ||
}); | ||
|
||
export const APMTpmBucketsRT = rt.type({ | ||
key: rt.string, | ||
dataPoints: rt.array(APMDataPointRT), | ||
}); | ||
|
||
export const APMChartResponseRT = rt.type({ | ||
apmTimeseries: rt.intersection([ | ||
rt.type({ | ||
responseTimes: rt.type({ | ||
avg: rt.array(APMDataPointRT), | ||
p95: rt.array(APMDataPointRT), | ||
p99: rt.array(APMDataPointRT), | ||
}), | ||
tpmBuckets: rt.array(APMTpmBucketsRT), | ||
}), | ||
rt.partial({ | ||
overallAvgDuration: rt.number, | ||
}), | ||
]), | ||
}); | ||
|
||
export const APMServiceResponseRT = rt.type({ | ||
hasHistoricalData: rt.boolean, | ||
hasLegacyData: rt.boolean, | ||
items: rt.array( | ||
rt.type({ | ||
agentName: rt.string, | ||
avgResponseTime: rt.number, | ||
environments: rt.array(rt.string), | ||
errorsPerMinute: rt.number, | ||
serviceName: rt.string, | ||
transactionsPerMinute: rt.number, | ||
}) | ||
), | ||
}); | ||
|
||
export type InfraApmMetricsRequest = rt.TypeOf<typeof InfraApmMetricsRequestRT>; | ||
|
||
export type InfraApmMetricsRequestWrapped = InfraWrappableRequest<InfraApmMetricsRequest>; | ||
|
||
export type InfraApmMetrics = rt.TypeOf<typeof InfraApmMetricsRT>; | ||
|
||
export type InfraApmMetricsService = rt.TypeOf<typeof InfraApmMetricsServiceRT>; | ||
|
||
export type InfraApmMetricsDataBucket = rt.TypeOf<typeof InfraApmMetricsDataBucketRT>; | ||
|
||
export type InfraApmMetricsDataPoint = rt.TypeOf<typeof InfraApmMetricsDataPointRT>; | ||
|
||
export type APMDataPoint = rt.TypeOf<typeof APMDataPointRT>; | ||
|
||
export type APMTpmBuckets = rt.TypeOf<typeof APMTpmBucketsRT>; | ||
|
||
export type APMChartResponse = rt.TypeOf<typeof APMChartResponseRT>; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 * as rt from 'io-ts'; | ||
|
||
export const InfraNodeTypeRT = rt.keyof({ | ||
host: null, | ||
container: null, | ||
pod: null, | ||
}); | ||
|
||
export type InfraNodeType = rt.TypeOf<typeof InfraNodeTypeRT>; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,7 +13,6 @@ | |
}, | ||
"dependencies": { | ||
"@types/color": "^3.0.0", | ||
"boom": "7.3.0", | ||
"lodash": "^4.17.13" | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
52 changes: 52 additions & 0 deletions
52
x-pack/legacy/plugins/infra/server/routes/apm_metrics/index.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
/* | ||
* 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 Boom from 'boom'; | ||
import { InfraBackendLibs } from '../../lib/infra_types'; | ||
import { throwErrors } from '../../../common/runtime_types'; | ||
import { | ||
InfraApmMetricsRequestRT, | ||
InfraApmMetricsRequestWrapped, | ||
InfraApmMetrics, | ||
InfraApmMetricsRT, | ||
} from '../../../common/http_api'; | ||
import { getApmServices } from './lib/get_apm_services'; | ||
import { getApmServiceData } from './lib/get_apm_service_data'; | ||
|
||
export const initApmMetricsRoute = (libs: InfraBackendLibs) => { | ||
const { framework, sources } = libs; | ||
|
||
framework.registerRoute<InfraApmMetricsRequestWrapped, Promise<InfraApmMetrics>>({ | ||
method: 'POST', | ||
path: '/api/infra/apm_metrics', | ||
handler: async req => { | ||
try { | ||
const { timeRange, nodeId, nodeType, sourceId } = InfraApmMetricsRequestRT.decode( | ||
req.payload | ||
).getOrElseL(throwErrors(Boom.badRequest)); | ||
const { configuration } = await sources.getSourceConfiguration(req, sourceId); | ||
const serviceNames = await getApmServices( | ||
framework, | ||
req, | ||
configuration, | ||
nodeId, | ||
nodeType, | ||
timeRange | ||
); | ||
const services = await Promise.all( | ||
serviceNames.map(name => | ||
getApmServiceData(framework, req, configuration, name, nodeId, nodeType, timeRange) | ||
) | ||
); | ||
return InfraApmMetricsRT.decode({ services }).getOrElseL( | ||
throwErrors(Boom.badImplementation) | ||
); | ||
} catch (error) { | ||
throw Boom.boomify(error); | ||
} | ||
}, | ||
}); | ||
}; |
72 changes: 72 additions & 0 deletions
72
x-pack/legacy/plugins/infra/server/routes/apm_metrics/lib/get_apm_service_data.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
/* | ||
* 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 moment from 'moment'; | ||
import { Legacy } from 'kibana'; | ||
import Boom from 'boom'; | ||
import { throwErrors } from '../../../../common/runtime_types'; | ||
import { InfraNodeType } from '../../../../common/http_api/common'; | ||
import { getIdFieldName } from '../../metadata/lib/get_id_field_name'; | ||
import { | ||
InfraApmMetricsService, | ||
APMChartResponseRT, | ||
APMDataPoint, | ||
APMTpmBuckets, | ||
} from '../../../../common/http_api'; | ||
import { | ||
InfraBackendFrameworkAdapter, | ||
InfraFrameworkRequest, | ||
} from '../../../lib/adapters/framework'; | ||
import { InfraSourceConfiguration } from '../../../lib/sources'; | ||
|
||
export const getApmServiceData = async ( | ||
framework: InfraBackendFrameworkAdapter, | ||
req: InfraFrameworkRequest, | ||
sourceConfiguration: InfraSourceConfiguration, | ||
service: string, | ||
nodeId: string, | ||
nodeType: InfraNodeType, | ||
timeRange: { min: number; max: number } | ||
): Promise<InfraApmMetricsService> => { | ||
const nodeField = getIdFieldName(sourceConfiguration, nodeType); | ||
const params = new URLSearchParams({ | ||
start: moment(timeRange.min).toISOString(), | ||
end: moment(timeRange.max).toISOString(), | ||
transactionType: 'request', | ||
uiFilters: JSON.stringify({ kuery: `${nodeField}: "${nodeId}"` }), | ||
}); | ||
const res = await framework.makeInternalRequest( | ||
req as InfraFrameworkRequest<Legacy.Request>, | ||
`/api/apm/services/${service}/transaction_groups/charts?${params.toString()}`, | ||
'GET' | ||
); | ||
if (res.statusCode !== 200) { | ||
throw res; | ||
} | ||
const result = APMChartResponseRT.decode(res.result).getOrElseL( | ||
throwErrors(message => Boom.badImplementation(`Request to APM Failed: ${message}`)) | ||
); | ||
const { responseTimes, tpmBuckets } = result.apmTimeseries; | ||
return { | ||
name: service, | ||
transactionsPerMinute: tpmBuckets.map(mapApmBucketToDataBucket), | ||
responseTimes: [ | ||
createApmBucket('avg', responseTimes.avg), | ||
createApmBucket('p95', responseTimes.p95), | ||
createApmBucket('p99', responseTimes.p99), | ||
], | ||
}; | ||
}; | ||
|
||
const createApmBucket = (name: string, data: APMDataPoint[]) => { | ||
return { | ||
name, | ||
data: data.map(p => ({ timestamp: p.x, value: p.y })), | ||
}; | ||
}; | ||
|
||
const mapApmBucketToDataBucket = (bucket: APMTpmBuckets) => | ||
createApmBucket(bucket.key, bucket.dataPoints); |
Oops, something went wrong.