Skip to content

Commit

Permalink
Fix up undefined handling in EP Meta telem task (#106269)
Browse files Browse the repository at this point in the history
* Fix up undefined handling in EP Meta telem task

* Check for null and undef.

* inverse the ternary op.
  • Loading branch information
pjhampton committed Jul 20, 2021
1 parent 1f798aa commit 34e0983
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,33 @@ describe('test', () => {
.createTaskRunner;
const taskRunner = createTaskRunner({ taskInstance: mockTaskInstance });
await taskRunner.run();
expect(mockSender.fetchDiagnosticAlerts).not.toHaveBeenCalled();
expect(mockSender.fetchEndpointMetrics).not.toHaveBeenCalled();
expect(mockSender.fetchEndpointPolicyResponses).not.toHaveBeenCalled();
});

test('endpoint task should run when opted in', async () => {
const mockSender = createMockTelemetryEventsSender(true);
const mockTaskManager = taskManagerMock.createSetup();
const telemetryEpMetaTask = new MockTelemetryEndpointTask(logger, mockTaskManager, mockSender);

const mockTaskInstance = {
id: TelemetryEndpointTaskConstants.TYPE,
runAt: new Date(),
attempts: 0,
ownerId: '',
status: TaskStatus.Running,
startedAt: new Date(),
scheduledAt: new Date(),
retryAt: new Date(),
params: {},
state: {},
taskType: TelemetryEndpointTaskConstants.TYPE,
};
const createTaskRunner =
mockTaskManager.registerTaskDefinitions.mock.calls[0][0][TelemetryEndpointTaskConstants.TYPE]
.createTaskRunner;
const taskRunner = createTaskRunner({ taskInstance: mockTaskInstance });
await taskRunner.run();
expect(telemetryEpMetaTask.runTask).toHaveBeenCalled();
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,11 @@ export class TelemetryEndpointTask {
body: EndpointMetricsAggregation;
};

if (endpointMetricsResponse.aggregations === undefined) {
this.logger.debug(`no endpoint metrics to report`);
return 0;
}

const endpointMetrics = endpointMetricsResponse.aggregations.endpoint_agents.buckets.map(
(epMetrics) => {
return {
Expand All @@ -188,8 +193,10 @@ export class TelemetryEndpointTask {
*/
const agentsResponse = endpointData.fleetAgentsResponse;
if (agentsResponse === undefined) {
this.logger.debug('no fleet agent information available');
return 0;
}

const fleetAgents = agentsResponse.agents.reduce((cache, agent) => {
if (agent.id === DefaultEndpointPolicyIdToIgnore) {
return cache;
Expand Down Expand Up @@ -241,14 +248,18 @@ export class TelemetryEndpointTask {
const { body: failedPolicyResponses } = (endpointData.epPolicyResponse as unknown) as {
body: EndpointPolicyResponseAggregation;
};
const policyResponses = failedPolicyResponses.aggregations.policy_responses.buckets.reduce(
(cache, endpointAgentId) => {
const doc = endpointAgentId.latest_response.hits.hits[0];
cache.set(endpointAgentId.key, doc);
return cache;
},
new Map<string, EndpointPolicyResponseDocument>()
);

// If there is no policy responses in the 24h > now then we will continue
const policyResponses = failedPolicyResponses.aggregations
? failedPolicyResponses.aggregations.policy_responses.buckets.reduce(
(cache, endpointAgentId) => {
const doc = endpointAgentId.latest_response.hits.hits[0];
cache.set(endpointAgentId.key, doc);
return cache;
},
new Map<string, EndpointPolicyResponseDocument>()
)
: new Map<string, EndpointPolicyResponseDocument>();

/** STAGE 4 - Create the telemetry log records
*
Expand All @@ -267,7 +278,7 @@ export class TelemetryEndpointTask {

const policyInformation = fleetAgents.get(fleetAgentId);
if (policyInformation) {
policyConfig = endpointPolicyCache.get(policyInformation);
policyConfig = endpointPolicyCache.get(policyInformation) || null;

if (policyConfig) {
failedPolicy = policyResponses.get(policyConfig?.id);
Expand Down Expand Up @@ -319,7 +330,7 @@ export class TelemetryEndpointTask {
);
return telemetryPayloads.length;
} catch (err) {
this.logger.error('Could not send endpoint alert telemetry');
this.logger.warn('could not complete endpoint alert telemetry task');
return 0;
}
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ export const createMockTelemetryEventsSender = (
start: jest.fn(),
stop: jest.fn(),
fetchDiagnosticAlerts: jest.fn(),
fetchEndpointMetrics: jest.fn(),
fetchEndpointPolicyResponses: jest.fn(),
queueTelemetryEvents: jest.fn(),
processEvents: jest.fn(),
isTelemetryOptedIn: jest.fn().mockReturnValue(enableTelemtry ?? jest.fn()),
Expand Down

0 comments on commit 34e0983

Please sign in to comment.