-
Notifications
You must be signed in to change notification settings - Fork 8.3k
/
get_service.ts
82 lines (74 loc) · 1.97 KB
/
get_service.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
/*
* 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 { BucketAgg } from 'elasticsearch';
import { ESFilter } from 'elasticsearch';
import { idx } from 'x-pack/plugins/apm/common/idx';
import {
PROCESSOR_EVENT,
SERVICE_AGENT_NAME,
SERVICE_NAME,
TRANSACTION_TYPE
} from '../../../common/elasticsearch_fieldnames';
import { rangeFilter } from '../helpers/range_filter';
import { Setup } from '../helpers/setup_request';
export interface ServiceAPIResponse {
serviceName: string;
types: string[];
agentName?: string;
}
export async function getService(
serviceName: string,
setup: Setup
): Promise<ServiceAPIResponse> {
const { start, end, esFilterQuery, client, config } = setup;
const filter: ESFilter[] = [
{ term: { [SERVICE_NAME]: serviceName } },
{ terms: { [PROCESSOR_EVENT]: ['error', 'transaction'] } },
{ range: rangeFilter(start, end) }
];
if (esFilterQuery) {
filter.push(esFilterQuery);
}
const params = {
index: [
config.get<string>('apm_oss.errorIndices'),
config.get<string>('apm_oss.transactionIndices')
],
body: {
size: 0,
query: {
bool: {
filter
}
},
aggs: {
types: {
terms: { field: TRANSACTION_TYPE, size: 100 }
},
agents: {
terms: { field: SERVICE_AGENT_NAME, size: 1 }
}
}
}
};
interface Aggs {
types: {
buckets: BucketAgg[];
};
agents: {
buckets: BucketAgg[];
};
}
const { aggregations } = await client<void, Aggs>('search', params);
const buckets = idx(aggregations, _ => _.types.buckets) || [];
const types = buckets.map(bucket => bucket.key);
const agentName = idx(aggregations, _ => _.agents.buckets[0].key);
return {
serviceName,
types,
agentName
};
}