-
Notifications
You must be signed in to change notification settings - Fork 867
/
core_usage_data_service.ts
307 lines (284 loc) · 11.2 KB
/
core_usage_data_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
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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
/*
* SPDX-License-Identifier: Apache-2.0
*
* The OpenSearch Contributors require contributions made to
* this file be licensed under the Apache-2.0 license or a
* compatible open source license.
*
* Any modifications Copyright OpenSearch Contributors. See
* GitHub history for details.
*/
/*
* Licensed to Elasticsearch B.V. under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch B.V. licenses this file to you under
* the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
import { Subject } from 'rxjs';
import { takeUntil } from 'rxjs/operators';
import { CoreService } from 'src/core/types';
import { SavedObjectsServiceStart } from 'src/core/server';
import { CoreContext } from '../core_context';
import { OpenSearchConfigType } from '../opensearch/opensearch_config';
import { HttpConfigType } from '../http';
import { LoggingConfigType } from '../logging';
import { SavedObjectsConfigType } from '../saved_objects/saved_objects_config';
import { CoreServicesUsageData, CoreUsageData, CoreUsageDataStart } from './types';
import { isConfigured } from './is_configured';
import { OpenSearchServiceStart } from '../opensearch';
import { OpenSearchDashboardsConfigType } from '../opensearch_dashboards_config';
import { MetricsServiceSetup, OpsMetrics } from '..';
export interface SetupDeps {
metrics: MetricsServiceSetup;
}
export interface StartDeps {
savedObjects: SavedObjectsServiceStart;
opensearch: OpenSearchServiceStart;
}
/**
* Because users can configure their Saved Object to any arbitrary index name,
* we need to map customized index names back to a "standard" index name.
*
* e.g. If a user configures `opensearchDashboards.index: .my_saved_objects` we want to the
* collected data to be grouped under `.kibana` not ".my_saved_objects".
*
* This is rather brittle, but the option to configure index names might go
* away completely anyway (see #60053).
*
* @param index The index name configured for this SO type
* @param opensearchDashboardsConfigIndex The default opensearch-dashboards index as configured by the user
* with `opensearchDashboards.index`
*/
const opensearchDashboardsOrTaskManagerIndex = (
index: string,
opensearchDashboardsConfigIndex: string
) => {
return index === opensearchDashboardsConfigIndex ? '.kibana' : '.kibana_task_manager';
};
export class CoreUsageDataService implements CoreService<void, CoreUsageDataStart> {
private opensearchConfig?: OpenSearchConfigType;
private configService: CoreContext['configService'];
private httpConfig?: HttpConfigType;
private loggingConfig?: LoggingConfigType;
private soConfig?: SavedObjectsConfigType;
private stop$: Subject<void>;
private opsMetrics?: OpsMetrics;
private opensearchDashboardsConfig?: OpenSearchDashboardsConfigType;
constructor(core: CoreContext) {
this.configService = core.configService;
this.stop$ = new Subject();
}
private async getSavedObjectIndicesUsageData(
savedObjects: SavedObjectsServiceStart,
opensearch: OpenSearchServiceStart
): Promise<CoreServicesUsageData['savedObjects']> {
const indices = await Promise.all(
Array.from(
savedObjects
.getTypeRegistry()
.getAllTypes()
.reduce((acc, type) => {
const index = type.indexPattern ?? this.opensearchDashboardsConfig!.index;
return index != null ? acc.add(index) : acc;
}, new Set<string>())
.values()
).map((index) => {
// The _cat/indices API returns the _index_ and doesn't return a way
// to map back from the index to the alias. So we have to make an API
// call for every alias
return opensearch.client.asInternalUser.cat
.indices<any[]>({
index,
format: 'JSON',
bytes: 'b',
})
.then(({ body }) => {
const stats = body[0];
return {
alias: opensearchDashboardsOrTaskManagerIndex(
index,
this.opensearchDashboardsConfig!.index
),
docsCount: stats['docs.count'] ? parseInt(stats['docs.count'], 10) : 0,
docsDeleted: stats['docs.deleted'] ? parseInt(stats['docs.deleted'], 10) : 0,
storeSizeBytes: stats['store.size'] ? parseInt(stats['store.size'], 10) : 0,
primaryStoreSizeBytes: stats['pri.store.size']
? parseInt(stats['pri.store.size'], 10)
: 0,
};
});
})
);
return {
indices,
};
}
private async getCoreUsageData(
savedObjects: SavedObjectsServiceStart,
opensearch: OpenSearchServiceStart
): Promise<CoreUsageData> {
if (
this.opensearchConfig == null ||
this.httpConfig == null ||
this.soConfig == null ||
this.opsMetrics == null
) {
throw new Error('Unable to read config valuopensearch. Ensure that setup() has completed.');
}
const opensearchConfig = this.opensearchConfig;
const soUsageData = await this.getSavedObjectIndicesUsageData(savedObjects, opensearch);
const http = this.httpConfig;
return {
config: {
opensearch: {
apiVersion: opensearchConfig.apiVersion,
sniffOnStart: opensearchConfig.sniffOnStart,
sniffIntervalMs:
opensearchConfig.sniffInterval !== false
? opensearchConfig.sniffInterval.asMilliseconds()
: -1,
sniffOnConnectionFault: opensearchConfig.sniffOnConnectionFault,
numberOfHostsConfigured: Array.isArray(opensearchConfig.hosts)
? opensearchConfig.hosts.length
: isConfigured.string(opensearchConfig.hosts)
? 1
: 0,
customHeadersConfigured: isConfigured.record(opensearchConfig.customHeaders),
healthCheckDelayMs: opensearchConfig.healthCheck.delay.asMilliseconds(),
logQueries: opensearchConfig.logQueries,
pingTimeoutMs: opensearchConfig.pingTimeout.asMilliseconds(),
requestHeadersWhitelistConfigured: isConfigured.stringOrArray(
opensearchConfig.requestHeadersWhitelist,
['authorization']
),
requestTimeoutMs: opensearchConfig.requestTimeout.asMilliseconds(),
shardTimeoutMs: opensearchConfig.shardTimeout.asMilliseconds(),
ssl: {
alwaysPresentCertificate: opensearchConfig.ssl.alwaysPresentCertificate,
certificateAuthoritiesConfigured: isConfigured.stringOrArray(
opensearchConfig.ssl.certificateAuthorities
),
certificateConfigured: isConfigured.string(opensearchConfig.ssl.certificate),
keyConfigured: isConfigured.string(opensearchConfig.ssl.key),
verificationMode: opensearchConfig.ssl.verificationMode,
truststoreConfigured: isConfigured.record(opensearchConfig.ssl.truststore),
keystoreConfigured: isConfigured.record(opensearchConfig.ssl.keystore),
},
},
http: {
basePathConfigured: isConfigured.string(http.basePath),
maxPayloadInBytes: http.maxPayload.getValueInBytes(),
rewriteBasePath: http.rewriteBasePath,
keepaliveTimeout: http.keepaliveTimeout,
socketTimeout: http.socketTimeout,
compression: {
enabled: http.compression.enabled,
referrerWhitelistConfigured: isConfigured.array(http.compression.referrerWhitelist),
},
xsrf: {
disableProtection: http.xsrf.disableProtection,
whitelistConfigured: isConfigured.array(http.xsrf.whitelist),
},
requestId: {
allowFromAnyIp: http.requestId.allowFromAnyIp,
ipAllowlistConfigured: isConfigured.array(http.requestId.ipAllowlist),
},
ssl: {
certificateAuthoritiesConfigured: isConfigured.stringOrArray(
http.ssl.certificateAuthorities
),
certificateConfigured: isConfigured.string(http.ssl.certificate),
cipherSuites: http.ssl.cipherSuites,
keyConfigured: isConfigured.string(http.ssl.key),
redirectHttpFromPortConfigured: isConfigured.number(http.ssl.redirectHttpFromPort),
supportedProtocols: http.ssl.supportedProtocols,
clientAuthentication: http.ssl.clientAuthentication,
keystoreConfigured: isConfigured.record(http.ssl.keystore),
truststoreConfigured: isConfigured.record(http.ssl.truststore),
},
},
logging: {
appendersTypesUsed: Array.from(
Array.from(this.loggingConfig?.appenders.values() ?? [])
.reduce((acc, a) => acc.add(a.kind), new Set<string>())
.values()
),
loggersConfiguredCount: this.loggingConfig?.loggers.length ?? 0,
},
savedObjects: {
maxImportPayloadBytes: this.soConfig.maxImportPayloadBytes.getValueInBytes(),
maxImportExportSizeBytes: this.soConfig.maxImportExportSize.getValueInBytes(),
},
},
environment: {
memory: {
heapSizeLimit: this.opsMetrics.process.memory.heap.size_limit,
heapTotalBytes: this.opsMetrics.process.memory.heap.total_in_bytes,
heapUsedBytes: this.opsMetrics.process.memory.heap.used_in_bytes,
},
},
services: {
savedObjects: soUsageData,
},
};
}
setup({ metrics }: SetupDeps) {
metrics
.getOpsMetrics$()
.pipe(takeUntil(this.stop$))
.subscribe((opsMetrics) => (this.opsMetrics = opsMetrics));
this.configService
.atPath<OpenSearchConfigType>('opensearch')
.pipe(takeUntil(this.stop$))
.subscribe((config) => {
this.opensearchConfig = config;
});
this.configService
.atPath<HttpConfigType>('server')
.pipe(takeUntil(this.stop$))
.subscribe((config) => {
this.httpConfig = config;
});
this.configService
.atPath<LoggingConfigType>('logging')
.pipe(takeUntil(this.stop$))
.subscribe((config) => {
this.loggingConfig = config;
});
this.configService
.atPath<SavedObjectsConfigType>('savedObjects')
.pipe(takeUntil(this.stop$))
.subscribe((config) => {
this.soConfig = config;
});
this.configService
.atPath<OpenSearchDashboardsConfigType>('opensearchDashboards')
.pipe(takeUntil(this.stop$))
.subscribe((config) => {
this.opensearchDashboardsConfig = config;
});
}
start({ savedObjects, opensearch }: StartDeps) {
return {
getCoreUsageData: () => {
return this.getCoreUsageData(savedObjects, opensearch);
},
};
}
stop() {
this.stop$.next();
this.stop$.complete();
}
}