-
Notifications
You must be signed in to change notification settings - Fork 8.3k
/
Copy pathplugin.ts
108 lines (86 loc) · 3.33 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
/*
* 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 { Observable } from 'rxjs';
import { first } from 'rxjs/operators';
import {
CoreSetup,
CoreStart,
Logger,
Plugin as CorePlugin,
PluginInitializerContext,
ClusterClient,
SharedGlobalConfig,
} from 'src/core/server';
import { IEventLogConfig, IEventLogService, IEventLogger, IEventLogConfig$ } from './types';
import { EventLogService } from './event_log_service';
import { createEsContext, EsContext } from './es';
export type PluginClusterClient = Pick<ClusterClient, 'callAsInternalUser' | 'asScoped'>;
const PROVIDER = 'eventLog';
const ACTIONS = {
starting: 'starting',
stopping: 'stopping',
};
export class Plugin implements CorePlugin<IEventLogService> {
private readonly config$: IEventLogConfig$;
private systemLogger: Logger;
private eventLogService?: IEventLogService;
private esContext?: EsContext;
private eventLogger?: IEventLogger;
private globalConfig$: Observable<SharedGlobalConfig>;
constructor(private readonly context: PluginInitializerContext) {
this.systemLogger = this.context.logger.get();
this.config$ = this.context.config.create<IEventLogConfig>();
this.globalConfig$ = this.context.config.legacy.globalConfig$;
}
async setup(core: CoreSetup): Promise<IEventLogService> {
const globalConfig = await this.globalConfig$.pipe(first()).toPromise();
const kibanaIndex = globalConfig.kibana.index;
this.systemLogger.debug('setting up plugin');
const config = await this.config$.pipe(first()).toPromise();
this.esContext = createEsContext({
logger: this.systemLogger,
// TODO: get index prefix from config.get(kibana.index)
indexNameRoot: kibanaIndex,
clusterClient: core.elasticsearch.adminClient,
});
this.eventLogService = new EventLogService({
config,
esContext: this.esContext,
systemLogger: this.systemLogger,
kibanaUUID: core.uuid.getInstanceUuid(),
});
this.eventLogService.registerProviderActions(PROVIDER, Object.values(ACTIONS));
this.eventLogger = this.eventLogService.getLogger({
event: { provider: PROVIDER },
});
return this.eventLogService;
}
async start(core: CoreStart) {
this.systemLogger.debug('starting plugin');
if (!this.esContext) throw new Error('esContext not initialized');
if (!this.eventLogger) throw new Error('eventLogger not initialized');
if (!this.eventLogService) throw new Error('eventLogService not initialized');
// launches initialization async
if (this.eventLogService.isIndexingEntries()) {
this.esContext.initialize();
}
// will log the event after initialization
this.eventLogger.logEvent({
event: { action: ACTIONS.starting },
message: 'eventLog starting',
});
}
stop() {
this.systemLogger.debug('stopping plugin');
if (!this.eventLogger) throw new Error('eventLogger not initialized');
// note that it's unlikely this event would ever be written,
// when Kibana is actuaelly stopping, as it's written asynchronously
this.eventLogger.logEvent({
event: { action: ACTIONS.stopping },
message: 'eventLog stopping',
});
}
}