-
Notifications
You must be signed in to change notification settings - Fork 8.3k
/
agent_configuration.ts
183 lines (170 loc) · 5.9 KB
/
agent_configuration.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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
/*
* 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 t from 'io-ts';
import Boom from 'boom';
import { setupRequest } from '../../lib/helpers/setup_request';
import { getServiceNames } from '../../lib/settings/agent_configuration/get_service_names';
import { createOrUpdateConfiguration } from '../../lib/settings/agent_configuration/create_or_update_configuration';
import { searchConfigurations } from '../../lib/settings/agent_configuration/search';
import { listConfigurations } from '../../lib/settings/agent_configuration/list_configurations';
import { getEnvironments } from '../../lib/settings/agent_configuration/get_environments';
import { deleteConfiguration } from '../../lib/settings/agent_configuration/delete_configuration';
import { createRoute } from '../create_route';
import { transactionSampleRateRt } from '../../../common/runtime_types/transaction_sample_rate_rt';
import { transactionMaxSpansRt } from '../../../common/runtime_types/transaction_max_spans_rt';
import { getAgentNameByService } from '../../lib/settings/agent_configuration/get_agent_name_by_service';
import { markAppliedByAgent } from '../../lib/settings/agent_configuration/mark_applied_by_agent';
// get list of configurations
export const agentConfigurationRoute = createRoute(core => ({
path: '/api/apm/settings/agent-configuration',
handler: async ({ context, request }) => {
const setup = await setupRequest(context, request);
return await listConfigurations({ setup });
}
}));
// delete configuration
export const deleteAgentConfigurationRoute = createRoute(() => ({
method: 'DELETE',
path: '/api/apm/settings/agent-configuration/{configurationId}',
options: {
tags: ['access:apm', 'access:apm_write']
},
params: {
path: t.type({
configurationId: t.string
})
},
handler: async ({ context, request }) => {
const setup = await setupRequest(context, request);
const { configurationId } = context.params.path;
return await deleteConfiguration({
configurationId,
setup
});
}
}));
// get list of services
export const listAgentConfigurationServicesRoute = createRoute(() => ({
method: 'GET',
path: '/api/apm/settings/agent-configuration/services',
handler: async ({ context, request }) => {
const setup = await setupRequest(context, request);
return await getServiceNames({
setup
});
}
}));
const agentPayloadRt = t.intersection([
t.partial({ agent_name: t.string }),
t.type({
service: t.intersection([
t.partial({ name: t.string }),
t.partial({ environment: t.string })
])
}),
t.type({
settings: t.intersection([
t.partial({ transaction_sample_rate: transactionSampleRateRt }),
t.partial({ capture_body: t.string }),
t.partial({ transaction_max_spans: transactionMaxSpansRt })
])
})
]);
// get environments for service
export const listAgentConfigurationEnvironmentsRoute = createRoute(() => ({
path: '/api/apm/settings/agent-configuration/environments',
params: {
query: t.partial({ serviceName: t.string })
},
handler: async ({ context, request }) => {
const setup = await setupRequest(context, request);
const { serviceName } = context.params.query;
return await getEnvironments({ serviceName, setup });
}
}));
// get agentName for service
export const agentConfigurationAgentNameRoute = createRoute(() => ({
path: '/api/apm/settings/agent-configuration/agent_name',
params: {
query: t.type({ serviceName: t.string })
},
handler: async ({ context, request }) => {
const setup = await setupRequest(context, request);
const { serviceName } = context.params.query;
const agentName = await getAgentNameByService({ serviceName, setup });
return agentName;
}
}));
export const createAgentConfigurationRoute = createRoute(() => ({
method: 'POST',
path: '/api/apm/settings/agent-configuration/new',
params: {
body: agentPayloadRt
},
options: {
tags: ['access:apm', 'access:apm_write']
},
handler: async ({ context, request }) => {
const setup = await setupRequest(context, request);
return await createOrUpdateConfiguration({
configuration: context.params.body,
setup
});
}
}));
export const updateAgentConfigurationRoute = createRoute(() => ({
method: 'PUT',
path: '/api/apm/settings/agent-configuration/{configurationId}',
options: {
tags: ['access:apm', 'access:apm_write']
},
params: {
path: t.type({
configurationId: t.string
}),
body: agentPayloadRt
},
handler: async ({ context, request }) => {
const setup = await setupRequest(context, request);
const { configurationId } = context.params.path;
return await createOrUpdateConfiguration({
configurationId,
configuration: context.params.body,
setup
});
}
}));
// Lookup single configuration (used by APM Server)
export const agentConfigurationSearchRoute = createRoute(core => ({
method: 'POST',
path: '/api/apm/settings/agent-configuration/search',
params: {
body: t.type({
service: t.intersection([
t.type({ name: t.string }),
t.partial({ environment: t.string })
]),
etag: t.string
})
},
handler: async ({ context, request }) => {
const setup = await setupRequest(context, request);
const { body } = context.params;
const config = await searchConfigurations({
serviceName: body.service.name,
environment: body.service.environment,
setup
});
if (!config) {
throw new Boom('Not found', { statusCode: 404 });
}
// update `applied_by_agent` field if etags match
if (body.etag === config._source.etag && !config._source.applied_by_agent) {
markAppliedByAgent({ id: config._id, body: config._source, setup });
}
return config;
}
}));