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

upload data improvements #999

Merged
merged 5 commits into from
Dec 14, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
Original file line number Diff line number Diff line change
Expand Up @@ -578,6 +578,52 @@ describe('uploadDataChunk', () => {
expect(uploadDataChunkErr).not.toBe(undefined);
expect(postSpy).toHaveBeenCalledTimes(1);
});

it('should clean errors before throwing', async () => {
const context = createTestContext();
const job = generateSynchronizationJob();

const type = 'entities';
const batch = [];

const mockLogger = {
trace: jest.fn(),
debug: jest.fn(),
info: jest.fn(),
warn: jest.fn(),
error: jest.fn(),
fatal: jest.fn(),
};

jest.spyOn(context.apiClient, 'post').mockImplementation(() => {
const err = new Error('thing went bad');
Object.assign(err, {
config: {
data: 'Stuff',
headers: {
Authroization: 'some fake token',
'content-type': 'application/json',
},
},
});
throw err;
});

await expect(
uploadDataChunk({
logger: mockLogger as any,
apiClient: context.apiClient,
jobId: job.id,
type,
batch,
}),
).rejects.toBeInstanceOf(Error);
const firstInfoCall = mockLogger.info.mock.calls[0];
const args = firstInfoCall[0];
const axiosError = args['err'];
expect(axiosError.config.data).toBeUndefined();
expect(axiosError.config.headers.Authorization).toBeUndefined();
});
});

function createTestContext(
Expand All @@ -586,6 +632,7 @@ function createTestContext(
const apiClient = createApiClient({
apiBaseUrl: getApiBaseUrl(),
account: 'test-account',
accessToken: 'fake-token',
});

const logger = createIntegrationLogger({
Expand Down
42 changes: 30 additions & 12 deletions packages/integration-sdk-runtime/src/synchronization/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import {
SynchronizationJob,
} from '@jupiterone/integration-sdk-core';

import { AxiosError } from 'axios';

import { IntegrationLogger } from '../logger';

import { ExecuteIntegrationResult } from '../execution';
Expand Down Expand Up @@ -467,19 +469,25 @@ export async function uploadDataChunk<
'Uploading data...',
);
const startTime = Date.now();
await apiClient.post(
`/persister/synchronization/jobs/${jobId}/${type as string}`,
{
[type]: batch,
},
{
headers: {
// NOTE: Other headers that were applied when the client was created,
// are still maintained
[RequestHeaders.CorrelationId]: uploadCorrelationId,
try {
await apiClient.post(
`/persister/synchronization/jobs/${jobId}/${type as string}`,
{
[type]: batch,
},
},
);
{
headers: {
// NOTE: Other headers that were applied when the client was created,
// are still maintained
[RequestHeaders.CorrelationId]: uploadCorrelationId,
},
},
);
} catch (err) {
cleanAxiosError(err);
throw err;
}

const duration = Date.now() - startTime;
if (duration >= 10_000) {
logger.info(
Expand Down Expand Up @@ -590,3 +598,13 @@ export async function abortSynchronization({

return abortedJob;
}

function cleanAxiosError(err: AxiosError) {
Copy link
Contributor

Choose a reason for hiding this comment

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

I have seen logged Axios errors that are huge. Are we able to reduce logging to the valuable parts?

if (err.config?.headers?.Authorization) {
delete err.config.headers.Authorization;
}

if (err.config?.data) {
delete err.config.data;
}
}
Loading