-
Notifications
You must be signed in to change notification settings - Fork 8k
/
fleet_usage_sender.ts
170 lines (149 loc) · 5.32 KB
/
fleet_usage_sender.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
/*
* 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 type {
ConcreteTaskInstance,
TaskManagerStartContract,
TaskManagerSetupContract,
} from '@kbn/task-manager-plugin/server';
import { throwUnrecoverableError } from '@kbn/task-manager-plugin/server';
import type { CoreSetup } from '@kbn/core/server';
import { withSpan } from '@kbn/apm-utils';
import type { FleetUsage } from '../../collectors/register';
import { appContextService } from '../app_context';
import { fleetAgentsSchema, fleetUsagesSchema } from './fleet_usages_schema';
const FLEET_USAGES_EVENT_TYPE = 'fleet_usage';
const FLEET_AGENTS_EVENT_TYPE = 'fleet_agents';
export class FleetUsageSender {
private taskManager?: TaskManagerStartContract;
private taskVersion = '1.1.4';
private taskType = 'Fleet-Usage-Sender';
private wasStarted: boolean = false;
private interval = '1h';
private timeout = '1m';
private abortController = new AbortController();
constructor(
taskManager: TaskManagerSetupContract,
core: CoreSetup,
fetchUsage: (abortController: AbortController) => Promise<FleetUsage | undefined>
) {
taskManager.registerTaskDefinitions({
[this.taskType]: {
title: 'Fleet Usage Sender',
timeout: this.timeout,
maxAttempts: 1,
createTaskRunner: ({ taskInstance }: { taskInstance: ConcreteTaskInstance }) => {
return {
run: async () => {
return withSpan({ name: this.taskType, type: 'telemetry' }, () =>
this.runTask(taskInstance, core, () => fetchUsage(this.abortController))
);
},
cancel: async () => {
this.abortController.abort('task timed out');
},
};
},
},
});
this.registerTelemetryEventType(core);
}
private runTask = async (
taskInstance: ConcreteTaskInstance,
core: CoreSetup,
fetchUsage: () => Promise<FleetUsage | undefined>
) => {
if (!this.wasStarted) {
appContextService.getLogger().debug('[runTask()] Aborted. Task not started yet');
return;
}
// Check that this task is current
if (taskInstance.id !== this.taskId) {
throwUnrecoverableError(new Error('Outdated task version for task: ' + taskInstance.id));
return;
}
appContextService.getLogger().info('Running Fleet Usage telemetry send task');
try {
const usageData = await fetchUsage();
if (!usageData) {
return;
}
const {
agents_per_version: agentsPerVersion,
agents_per_output_type: agentsPerOutputType,
upgrade_details: upgradeDetails,
...fleetUsageData
} = usageData;
appContextService
.getLogger()
.debug('Fleet usage telemetry: ' + JSON.stringify(fleetUsageData));
core.analytics.reportEvent(FLEET_USAGES_EVENT_TYPE, fleetUsageData);
appContextService
.getLogger()
.debug('Agents per version telemetry: ' + JSON.stringify(agentsPerVersion));
agentsPerVersion.forEach((byVersion) => {
core.analytics.reportEvent(FLEET_AGENTS_EVENT_TYPE, { agents_per_version: byVersion });
});
appContextService
.getLogger()
.debug('Agents per output type telemetry: ' + JSON.stringify(agentsPerOutputType));
agentsPerOutputType.forEach((byOutputType) => {
core.analytics.reportEvent(FLEET_AGENTS_EVENT_TYPE, {
agents_per_output_type: byOutputType,
});
});
appContextService
.getLogger()
.debug('Agents upgrade details telemetry: ' + JSON.stringify(upgradeDetails));
upgradeDetails.forEach((upgradeDetailsObj) => {
core.analytics.reportEvent(FLEET_AGENTS_EVENT_TYPE, { upgrade_details: upgradeDetailsObj });
});
} catch (error) {
appContextService
.getLogger()
.error('Error occurred while sending Fleet Usage telemetry: ' + error);
}
};
private get taskId() {
return `${this.taskType}-${this.taskVersion}`;
}
public async start(taskManager: TaskManagerStartContract) {
this.taskManager = taskManager;
if (!taskManager) {
appContextService.getLogger().error('missing required service during start');
return;
}
this.wasStarted = true;
try {
appContextService.getLogger().info(`Task ${this.taskId} scheduled with interval 1h`);
await this.taskManager.ensureScheduled({
id: this.taskId,
taskType: this.taskType,
schedule: {
interval: this.interval,
},
scope: ['fleet'],
state: {},
params: {},
});
} catch (e) {
appContextService.getLogger().error(`Error scheduling task, received error: ${e}`);
}
}
/**
* took schema from [here](https://github.com/elastic/kibana/blob/main/x-pack/plugins/fleet/server/collectors/register.ts#L53) and adapted to EBT format
*/
private registerTelemetryEventType(core: CoreSetup): void {
core.analytics.registerEventType({
eventType: FLEET_USAGES_EVENT_TYPE,
schema: fleetUsagesSchema,
});
core.analytics.registerEventType({
eventType: FLEET_AGENTS_EVENT_TYPE,
schema: fleetAgentsSchema,
});
}
}