-
Notifications
You must be signed in to change notification settings - Fork 8.3k
/
Copy pathml.ts
123 lines (115 loc) · 2.87 KB
/
ml.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
/*
* 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 { HttpSetup } from 'kibana/public';
import {
PROCESSOR_EVENT,
SERVICE_NAME,
TRANSACTION_TYPE,
} from '../../../common/elasticsearch_fieldnames';
import {
APM_ML_JOB_GROUP_NAME,
getMlJobId,
getMlPrefix,
encodeForMlApi,
} from '../../../common/ml_job_constants';
import { callApi } from './callApi';
import { ESFilter } from '../../../typings/elasticsearch';
import { callApmApi } from './createCallApmApi';
interface MlResponseItem {
id: string;
success: boolean;
error?: {
msg: string;
body: string;
path: string;
response: string;
statusCode: number;
};
}
interface StartedMLJobApiResponse {
datafeeds: MlResponseItem[];
jobs: MlResponseItem[];
}
async function getTransactionIndices() {
const indices = await callApmApi({
method: 'GET',
pathname: `/api/apm/settings/apm-indices`,
});
return indices['apm_oss.transactionIndices'];
}
export async function startMLJob({
serviceName,
transactionType,
http,
}: {
serviceName: string;
transactionType: string;
http: HttpSetup;
}) {
const transactionIndices = await getTransactionIndices();
const groups = [
APM_ML_JOB_GROUP_NAME,
encodeForMlApi(serviceName),
encodeForMlApi(transactionType),
];
const filter: ESFilter[] = [
{ term: { [SERVICE_NAME]: serviceName } },
{ term: { [PROCESSOR_EVENT]: 'transaction' } },
{ term: { [TRANSACTION_TYPE]: transactionType } },
];
return callApi<StartedMLJobApiResponse>(http, {
method: 'POST',
pathname: `/api/ml/modules/setup/apm_transaction`,
body: {
prefix: getMlPrefix(serviceName, transactionType),
groups,
indexPatternName: transactionIndices,
startDatafeed: true,
query: {
bool: {
filter,
},
},
},
});
}
// https://www.elastic.co/guide/en/elasticsearch/reference/6.5/ml-get-job.html
export interface MLJobApiResponse {
count: number;
jobs: Array<{
job_id: string;
}>;
}
export type MLError = Error & { body?: { message?: string } };
export async function getHasMLJob({
serviceName,
transactionType,
http,
}: {
serviceName: string;
transactionType: string;
http: HttpSetup;
}) {
try {
await callApi<MLJobApiResponse>(http, {
method: 'GET',
pathname: `/api/ml/anomaly_detectors/${getMlJobId(
serviceName,
transactionType
)}`,
});
return true;
} catch (error) {
if (
error?.body?.statusCode === 404 &&
error?.body?.attributes?.body?.error?.type ===
'resource_not_found_exception'
) {
return false; // false only if ML api responds with resource_not_found_exception
}
throw error;
}
}