-
Notifications
You must be signed in to change notification settings - Fork 8.3k
/
Copy pathplugin.ts
205 lines (182 loc) · 7.31 KB
/
plugin.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
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0; you may not use this file except in compliance with the Elastic License
* 2.0.
*/
import { Server } from '@hapi/hapi';
import { schema, TypeOf } from '@kbn/config-schema';
import { i18n } from '@kbn/i18n';
import { Logger } from '@kbn/logging';
import {
CoreSetup,
PluginInitializerContext,
Plugin,
PluginConfigDescriptor,
} from 'src/core/server';
import { LOGS_FEATURE_ID, METRICS_FEATURE_ID } from '../common/constants';
import { InfraStaticSourceConfiguration } from '../common/source_configuration/source_configuration';
import { inventoryViewSavedObjectType } from '../common/saved_objects/inventory_view';
import { metricsExplorerViewSavedObjectType } from '../common/saved_objects/metrics_explorer_view';
import { LOGS_FEATURE, METRICS_FEATURE } from './features';
import { initInfraServer } from './infra_server';
import { FrameworkFieldsAdapter } from './lib/adapters/fields/framework_fields_adapter';
import { InfraServerPluginSetupDeps, InfraServerPluginStartDeps } from './lib/adapters/framework';
import { KibanaFramework } from './lib/adapters/framework/kibana_framework_adapter';
import { InfraKibanaLogEntriesAdapter } from './lib/adapters/log_entries/kibana_log_entries_adapter';
import { KibanaMetricsAdapter } from './lib/adapters/metrics/kibana_metrics_adapter';
import { InfraElasticsearchSourceStatusAdapter } from './lib/adapters/source_status';
import { registerAlertTypes } from './lib/alerting';
import { InfraFieldsDomain } from './lib/domains/fields_domain';
import { InfraLogEntriesDomain } from './lib/domains/log_entries_domain';
import { InfraMetricsDomain } from './lib/domains/metrics_domain';
import { InfraBackendLibs, InfraDomainLibs } from './lib/infra_types';
import { infraSourceConfigurationSavedObjectType, InfraSources } from './lib/sources';
import { InfraSourceStatus } from './lib/source_status';
import { LogEntriesService } from './services/log_entries';
import { InfraPluginRequestHandlerContext } from './types';
import { UsageCollector } from './usage/usage_collector';
import { createGetLogQueryFields } from './services/log_queries/get_log_query_fields';
import { handleEsError } from '../../../../src/plugins/es_ui_shared/server';
import { RulesService } from './services/rules';
export const config: PluginConfigDescriptor = {
schema: schema.object({
enabled: schema.boolean({ defaultValue: true }),
inventory: schema.object({
compositeSize: schema.number({ defaultValue: 2000 }),
}),
sources: schema.maybe(
schema.object({
default: schema.maybe(
schema.object({
logAlias: schema.maybe(schema.string()), // NOTE / TODO: Should be deprecated in 8.0.0
metricAlias: schema.maybe(schema.string()),
fields: schema.maybe(
schema.object({
timestamp: schema.maybe(schema.string()),
message: schema.maybe(schema.arrayOf(schema.string())),
tiebreaker: schema.maybe(schema.string()),
host: schema.maybe(schema.string()),
container: schema.maybe(schema.string()),
pod: schema.maybe(schema.string()),
})
),
})
),
})
),
}),
deprecations: ({ deprecate }) => [deprecate('enabled', '8.0.0')],
};
export type InfraConfig = TypeOf<typeof config.schema>;
export interface KbnServer extends Server {
usage: any;
}
const logsSampleDataLinkLabel = i18n.translate('xpack.infra.sampleDataLinkLabel', {
defaultMessage: 'Logs',
});
export interface InfraPluginSetup {
defineInternalSourceConfiguration: (
sourceId: string,
sourceProperties: InfraStaticSourceConfiguration
) => void;
}
export class InfraServerPlugin implements Plugin<InfraPluginSetup> {
public config: InfraConfig;
public libs: InfraBackendLibs | undefined;
public logger: Logger;
private logsRules: RulesService;
private metricsRules: RulesService;
constructor(context: PluginInitializerContext) {
this.config = context.config.get<InfraConfig>();
this.logger = context.logger.get();
this.logsRules = new RulesService(
LOGS_FEATURE_ID,
'observability.logs',
this.logger.get('logsRules')
);
this.metricsRules = new RulesService(
METRICS_FEATURE_ID,
'observability.metrics',
this.logger.get('metricsRules')
);
}
setup(core: CoreSetup<InfraServerPluginStartDeps>, plugins: InfraServerPluginSetupDeps) {
const framework = new KibanaFramework(core, this.config, plugins);
const sources = new InfraSources({
config: this.config,
});
const sourceStatus = new InfraSourceStatus(
new InfraElasticsearchSourceStatusAdapter(framework),
{
sources,
}
);
// register saved object types
core.savedObjects.registerType(infraSourceConfigurationSavedObjectType);
core.savedObjects.registerType(metricsExplorerViewSavedObjectType);
core.savedObjects.registerType(inventoryViewSavedObjectType);
// TODO: separate these out individually and do away with "domains" as a temporary group
// and make them available via the request context so we can do away with
// the wrapper classes
const domainLibs: InfraDomainLibs = {
fields: new InfraFieldsDomain(new FrameworkFieldsAdapter(framework), {
sources,
}),
logEntries: new InfraLogEntriesDomain(new InfraKibanaLogEntriesAdapter(framework), {
framework,
sources,
}),
metrics: new InfraMetricsDomain(new KibanaMetricsAdapter(framework)),
};
this.libs = {
configuration: this.config,
framework,
sources,
sourceStatus,
...domainLibs,
getLogQueryFields: createGetLogQueryFields(sources, framework),
handleEsError,
logsRules: this.logsRules.setup(core, plugins),
metricsRules: this.metricsRules.setup(core, plugins),
};
plugins.features.registerKibanaFeature(METRICS_FEATURE);
plugins.features.registerKibanaFeature(LOGS_FEATURE);
plugins.home.sampleData.addAppLinksToSampleDataset('logs', [
{
path: `/app/logs`,
label: logsSampleDataLinkLabel,
icon: 'logsApp',
},
]);
initInfraServer(this.libs);
registerAlertTypes(plugins.alerting, this.libs, plugins.ml);
core.http.registerRouteHandlerContext<InfraPluginRequestHandlerContext, 'infra'>(
'infra',
(context, request) => {
const mlSystem = plugins.ml?.mlSystemProvider(request, context.core.savedObjects.client);
const mlAnomalyDetectors = plugins.ml?.anomalyDetectorsProvider(
request,
context.core.savedObjects.client
);
const spaceId = plugins.spaces?.spacesService.getSpaceId(request) || 'default';
return {
mlAnomalyDetectors,
mlSystem,
spaceId,
};
}
);
// Telemetry
UsageCollector.registerUsageCollector(plugins.usageCollection);
const logEntriesService = new LogEntriesService();
logEntriesService.setup(core, { ...plugins, sources });
return {
defineInternalSourceConfiguration(sourceId, sourceProperties) {
sources.defineInternalSourceConfiguration(sourceId, sourceProperties);
},
} as InfraPluginSetup;
}
start() {}
stop() {}
}