Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Fleet] Upgrade details telemetry #173356

Merged
merged 7 commits into from
Dec 18, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions x-pack/plugins/fleet/server/collectors/agent_collectors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,13 +75,19 @@ export interface AgentData {
version: string;
count: number;
}>;
upgrade_details: Array<{
target_version: string;
state: string;
error_msg: string;
}>;
}

const DEFAULT_AGENT_DATA = {
agent_checkin_status: { error: 0, degraded: 0 },
agents_per_policy: [],
agents_per_version: [],
agents_per_os: [],
upgrade_details: [],
};

export const getAgentData = async (
Expand Down Expand Up @@ -135,6 +141,22 @@ export const getAgentData = async (
],
},
},
upgrade_details: {
multi_terms: {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@juliaElastic this will return only the first 10 items, it is what we want?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good catch! probably not, I'll increase this.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

increased to 1000

terms: [
{
field: 'upgrade_details.target_version.keyword',
},
{
field: 'upgrade_details.state',
},
{
field: 'upgrade_details.metadata.error_msg.keyword',
missing: '',
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this ensures that documents are included where error_msg is missing, and uses empty string instead: https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-bucket-multi-terms-aggregation.html#_missing_value_3

},
],
},
},
},
},
{ signal: abortController.signal }
Expand Down Expand Up @@ -190,11 +212,20 @@ export const getAgentData = async (
count: bucket.doc_count,
}));

const upgradeDetails = ((response?.aggregations?.upgrade_details as any).buckets ?? []).map(
(bucket: any) => ({
target_version: bucket.key[0],
state: bucket.key[1],
error_msg: bucket.key[2],
})
);

return {
agent_checkin_status: statuses,
agents_per_policy: agentsPerPolicy,
agents_per_version: agentsPerVersion,
agents_per_os: agentsPerOS,
upgrade_details: upgradeDetails,
};
} catch (error) {
if (error.statusCode === 404) {
Expand Down
4 changes: 3 additions & 1 deletion x-pack/plugins/fleet/server/collectors/agents_per_output.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,9 @@ export async function getAgentsPerOutput(
if (!outputTypeSupportPresets(output.type)) {
return;
}

if (!outputTypes[output.type]) {
return;
}
const outputTelemetryRecord = outputTypes[output.type];

if (!outputTelemetryRecord.preset_counts) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,13 @@ describe('fleet usage telemetry', () => {
status: 'HEALTHY',
},
],
upgrade_details: {
target_version: '8.12.0',
state: 'UPG_FAILED',
metadata: {
error_msg: 'Download failed',
},
},
},
{
create: {
Expand Down Expand Up @@ -176,6 +183,13 @@ describe('fleet usage telemetry', () => {
status: 'HEALTHY',
},
],
upgrade_details: {
target_version: '8.12.0',
state: 'UPG_FAILED',
metadata: {
error_msg: 'Agent crash detected',
},
},
},
{
create: {
Expand Down Expand Up @@ -220,6 +234,11 @@ describe('fleet usage telemetry', () => {
last_checkin: new Date(Date.now() - 1000 * 60 * 6).toISOString(),
active: true,
policy_id: 'policy2',
upgrade_details: {
target_version: '8.11.0',
state: 'UPG_ROLLBACK',
metadata: {},
},
},
{
create: {
Expand Down Expand Up @@ -557,5 +576,21 @@ describe('fleet usage telemetry', () => {
fleet_server_logs_top_errors: ['failed to unenroll offline agents'],
})
);
expect(usage?.upgrade_details.length).toBe(3);
expect(usage?.upgrade_details).toContainEqual({
target_version: '8.12.0',
state: 'UPG_FAILED',
error_msg: 'Download failed',
});
expect(usage?.upgrade_details).toContainEqual({
target_version: '8.12.0',
state: 'UPG_FAILED',
error_msg: 'Agent crash detected',
});
expect(usage?.upgrade_details).toContainEqual({
target_version: '8.11.0',
state: 'UPG_ROLLBACK',
error_msg: '',
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ const FLEET_AGENTS_EVENT_TYPE = 'fleet_agents';

export class FleetUsageSender {
private taskManager?: TaskManagerStartContract;
private taskVersion = '1.1.3';
private taskVersion = '1.1.4';
private taskType = 'Fleet-Usage-Sender';
private wasStarted: boolean = false;
private interval = '1h';
Expand Down Expand Up @@ -83,6 +83,7 @@ export class FleetUsageSender {
const {
agents_per_version: agentsPerVersion,
agents_per_output_type: agentsPerOutputType,
upgrade_details: upgradeDetails,
...fleetUsageData
} = usageData;
appContextService
Expand All @@ -106,6 +107,13 @@ export class FleetUsageSender {
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()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,9 +76,10 @@ export const fleetAgentsSchema: RootSchema<any> = {
description: 'Output type used by agent',
},
},
presets_counts: {
preset_counts: {
_meta: {
description: 'Count of agents per preset',
optional: true,
},
properties: {
balanced: {
Expand Down Expand Up @@ -117,6 +118,7 @@ export const fleetAgentsSchema: RootSchema<any> = {
type: 'keyword',
_meta: {
description: 'Output preset used by agent, if applicable',
optional: true,
},
},
count_as_data: {
Expand All @@ -133,6 +135,32 @@ export const fleetAgentsSchema: RootSchema<any> = {
},
},
},
upgrade_details: {
_meta: {
description: 'Agent upgrade details telemetry',
optional: true,
},
properties: {
target_version: {
type: 'keyword',
_meta: {
description: 'Target version of the agent upgrade',
},
},
state: {
type: 'keyword',
_meta: {
description: 'State of the agent upgrade',
},
},
error_msg: {
type: 'keyword',
_meta: {
description: 'Error message of the agent upgrade if failed',
},
},
},
},
};

export const fleetUsagesSchema: RootSchema<any> = {
Expand Down