Skip to content

Commit

Permalink
always send upsert, even if there are no samples or the transform fai…
Browse files Browse the repository at this point in the history
…led (#181)
  • Loading branch information
jgraff2 authored and iamigo committed Mar 19, 2019
1 parent 8bc959a commit f10f974
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 6 deletions.
2 changes: 2 additions & 0 deletions src/constants.js
Expand Up @@ -53,4 +53,6 @@ module.exports = {
deadline: 30000,
},
},

maxMessageBodyLength: 4096,
};
5 changes: 5 additions & 0 deletions src/remoteCollection/errorSamples.js
Expand Up @@ -11,9 +11,14 @@
*/
const debug =
require('debug')('refocus-collector:remoteCollection:errorSamples');
const { maxMessageBodyLength } = require('../constants');

module.exports = (collectResponse, messageBody) => {
debug('errorSamples', collectResponse.name, messageBody);
if (messageBody.length > maxMessageBodyLength) {
messageBody = messageBody.slice(0, maxMessageBodyLength - 3) + '...';
}

const samples = [];
collectResponse.aspects.forEach((a) => {
collectResponse.subjects.forEach((s) => {
Expand Down
7 changes: 6 additions & 1 deletion src/remoteCollection/handleCollectResponse.js
Expand Up @@ -140,7 +140,12 @@ function generateSamples(collectRes) {
*/
const func = RefocusCollectorEval.getTransformFunction(tr, status);
if (func) {
return RefocusCollectorEval.safeTransform(func, args);
try {
return RefocusCollectorEval.safeTransform(func, args);
} catch (err) {
const errorMessage = `Transform error: ${err.message} (${collectRes.preparedUrl})`;
return errorSamples(collectRes, errorMessage);
}
}

// Default error samples
Expand Down
5 changes: 0 additions & 5 deletions src/utils/httpUtils.js
Expand Up @@ -138,11 +138,6 @@ function doBulkUpsert(url, userToken, intervalSecs, proxy, arr) {
return Promise.resolve(e);
}

// Don't bother sending a POST if the array is empty.
if (arr.length === 0) {
return Promise.resolve(true);
}

url += bulkUpsertEndpoint;
return new Promise((resolve) => {
debug('Bulk upserting %d samples to %s', arr.length, url);
Expand Down
56 changes: 56 additions & 0 deletions test/remoteCollection/errorSamples.js
@@ -0,0 +1,56 @@
/**
* Copyright (c) 2019, salesforce.com, inc.
* All rights reserved.
* Licensed under the BSD 3-Clause license.
* For full license text, see LICENSE.txt file in the repo root or
* https://opensource.org/licenses/BSD-3-Clause
*/

/**
* test/remoteCollection/errorSamples.js
*/
const expect = require('chai').expect;
const errorSamples = require('../../src/remoteCollection/errorSamples');

describe('test/remoteCollection/errorSamples.js >', () => {
const cr = {
aspects: [{ name: 'aspect1' }],
subjects: [{ absolutePath: 'subject1' }],
};

it('creates samples based on subjects/aspects and message', () => {
const messageBody = '1234567890';
expect(errorSamples(cr, messageBody)).to.deep.equal([
{
name: 'subject1|aspect1',
messageCode: 'ERROR',
messageBody: '1234567890',
value: 'ERROR',
},
]);
});

it('not over max length, not truncated', () => {
const messageBody = ''.padEnd(4096);
expect(errorSamples(cr, messageBody)).to.deep.equal([
{
name: 'subject1|aspect1',
messageCode: 'ERROR',
messageBody: ''.padEnd(4096),
value: 'ERROR',
},
]);
});

it('long message is truncated', () => {
const messageBody = ''.padEnd(4097);
expect(errorSamples(cr, messageBody)).to.deep.equal([
{
name: 'subject1|aspect1',
messageCode: 'ERROR',
messageBody: ''.padEnd(4093) + '...',
value: 'ERROR',
},
]);
});
});

0 comments on commit f10f974

Please sign in to comment.