Skip to content
This repository has been archived by the owner on Jul 10, 2023. It is now read-only.

Commit

Permalink
Set HTTP span as erroneous when the response code is 4XX 5xx Node.js
Browse files Browse the repository at this point in the history
  • Loading branch information
fatihaydilek committed Aug 7, 2019
1 parent c61ba93 commit 546e0a0
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 1 deletion.
5 changes: 5 additions & 0 deletions src/plugins/integrations/HttpIntegration.ts
Expand Up @@ -9,6 +9,7 @@ import Utils from '../utils/Utils';
import * as url from 'url';
import ThundraLogger from '../../ThundraLogger';
import InvocationSupport from '../support/InvocationSupport';
import HttpError from '../error/HttpError';

const shimmer = require('shimmer');
const has = require('lodash.has');
Expand Down Expand Up @@ -136,6 +137,10 @@ class HttpIntegration implements Integration {
const resourceName: string = res.headers[TriggerHeaderTags.RESOURCE_NAME];
span._setOperationName(resourceName);
}
const statusCode = res.statusCode.toString();
if (statusCode.startsWith('4') || statusCode.startsWith('5')) {
span.setErrorTag(new HttpError(res.statusMessage));
}
span.setTag(HttpTags.HTTP_STATUS, res.statusCode);
});

Expand Down
35 changes: 34 additions & 1 deletion test/integration/http.integration.test.js
Expand Up @@ -16,7 +16,7 @@ describe('HTTP integration', () => {
return Http.get(sdk).then(() => {
const span = tracer.getRecorder().spanList[0];

expect(span.operationName).toBe('jsonplaceholder.typicode.com/users/1')
expect(span.operationName).toBe('jsonplaceholder.typicode.com/users/1');
expect(span.className).toBe('HTTP');
expect(span.domainName).toBe('API');

Expand All @@ -35,6 +35,39 @@ describe('HTTP integration', () => {
});
});

test('should set 4XX 5XX errors on HTTP calls', () => {
const integration = new HttpIntegration({
httpPathDepth: 2,
});
const sdk = require('http');

const tracer = new ThundraTracer();
InvocationSupport.setFunctionName('functionName');

return Http.getError(sdk).then(() => {
const span = tracer.getRecorder().spanList[0];

expect(span.operationName).toBe('httpstat.us/404');
expect(span.className).toBe('HTTP');
expect(span.domainName).toBe('API');

expect(span.tags['operation.type']).toBe('GET');
expect(span.tags['http.method']).toBe('GET');
expect(span.tags['http.host']).toBe('httpstat.us');
expect(span.tags['http.path']).toBe('/404');
expect(span.tags['http.url']).toBe('httpstat.us/404');
expect(span.tags['http.status_code']).toBe(404);
expect(span.tags['error']).toBe(true);
expect(span.tags['error.kind']).toBe('HttpError');
expect(span.tags['error.message']).toBe('Not Found');
expect(span.tags['topology.vertex']).toEqual(true);
expect(span.tags['trigger.domainName']).toEqual('API');
expect(span.tags['trigger.className']).toEqual('AWS-Lambda');
expect(span.tags['trigger.operationNames']).toEqual(['functionName']);
expect(span.tags['http.body']).not.toBeTruthy();
});
});

test('should instrument HTTPS POST calls', () => {
const integration = new HttpIntegration({
httpPathDepth: 0,
Expand Down
18 changes: 18 additions & 0 deletions test/integration/utils/http.integration.utils.js
Expand Up @@ -16,6 +16,24 @@ module.exports.get = (http) => {
});
};

module.exports.getError = (http) => {
return new Promise((resolve) => {
const url = 'http://httpstat.us/404';
http.get(url, (resp) => {
let data = '';
resp.on('data', (chunk) => {
data += chunk;
});
resp.on('end', () => {
resolve(data);
});
}).on('error', (err) => {
// We resolve with error.
resolve(err);
});
});
};

module.exports.post = (https) => {
return new Promise((resolve) => {
const data = JSON.stringify({
Expand Down

0 comments on commit 546e0a0

Please sign in to comment.