Skip to content

Commit e9cc3e1

Browse files
watsonStephen Belanger
authored andcommitted
feat: support APM Server intake API version 2 (elastic#465)
Closes elastic#356
1 parent bd085fd commit e9cc3e1

File tree

4 files changed

+148
-0
lines changed

4 files changed

+148
-0
lines changed

lib/agent.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ var Instrumentation = require('./instrumentation')
1717
var parsers = require('./parsers')
1818
var stackman = require('./stackman')
1919
var symbols = require('./symbols')
20+
var truncate = require('./truncate')
2021

2122
var IncomingMessage = http.IncomingMessage
2223
var ServerResponse = http.ServerResponse
@@ -163,6 +164,8 @@ Agent.prototype.start = function (opts) {
163164

164165
this._instrumentation.start()
165166

167+
this._instrumentation.start()
168+
166169
Error.stackTraceLimit = this._conf.stackTraceLimit
167170
if (this._conf.captureExceptions) this.handleUncaughtExceptions()
168171

@@ -317,6 +320,11 @@ Agent.prototype.captureError = function (err, opts, cb) {
317320
return
318321
}
319322

323+
<<<<<<< HEAD
324+
=======
325+
truncate.error(error, agent._conf)
326+
327+
>>>>>>> feat: support APM Server intake API version 2 (#465)
320328
if (agent._apmServer) {
321329
agent.logger.info(`Sending error ${id} to Elastic APM`)
322330
agent._apmServer.sendError(error, function () {

lib/instrumentation/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ var hook = require('require-in-the-middle')
77
var semver = require('semver')
88

99
var Transaction = require('./transaction')
10+
var truncate = require('../truncate')
1011
var shimmer = require('./shimmer')
1112

1213
var MODULES = [

lib/truncate.js

Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
'use strict'
2+
3+
var truncate = require('unicode-byte-truncate')
4+
5+
var config = require('./config')
6+
7+
exports.transaction = truncTransaction
8+
exports.span = truncSpan
9+
exports.error = truncError
10+
11+
function truncTransaction (trans) {
12+
trans.name = truncate(String(trans.name), config.INTAKE_STRING_MAX_SIZE)
13+
trans.type = truncate(String(trans.type), config.INTAKE_STRING_MAX_SIZE)
14+
trans.result = truncate(String(trans.result), config.INTAKE_STRING_MAX_SIZE)
15+
16+
// Unless sampled, context will be null
17+
if (trans.sampled) truncContext(trans.context)
18+
}
19+
20+
function truncSpan (span) {
21+
span.name = truncate(String(span.name), config.INTAKE_STRING_MAX_SIZE)
22+
span.type = truncate(String(span.type), config.INTAKE_STRING_MAX_SIZE)
23+
if (span.stacktrace) span.stacktrace = truncFrames(span.stacktrace)
24+
}
25+
26+
function truncError (error, conf) {
27+
if (error.log) {
28+
if (error.log.level) {
29+
error.log.level = truncate(String(error.log.level), config.INTAKE_STRING_MAX_SIZE)
30+
}
31+
if (error.log.logger_name) {
32+
error.log.logger_name = truncate(String(error.log.logger_name), config.INTAKE_STRING_MAX_SIZE)
33+
}
34+
if (error.log.message && conf.errorMessageMaxLength >= 0) {
35+
error.log.message = truncate(String(error.log.message), conf.errorMessageMaxLength)
36+
}
37+
if (error.log.param_message) {
38+
error.log.param_message = truncate(String(error.log.param_message), config.INTAKE_STRING_MAX_SIZE)
39+
}
40+
if (error.log.stacktrace) {
41+
error.log.stacktrace = truncFrames(error.log.stacktrace)
42+
}
43+
}
44+
45+
if (error.exception) {
46+
if (error.exception.message && conf.errorMessageMaxLength >= 0) {
47+
error.exception.message = truncate(String(error.exception.message), conf.errorMessageMaxLength)
48+
}
49+
if (error.exception.type) {
50+
error.exception.type = truncate(String(error.exception.type), config.INTAKE_STRING_MAX_SIZE)
51+
}
52+
if (error.exception.code) {
53+
error.exception.code = truncate(String(error.exception.code), config.INTAKE_STRING_MAX_SIZE)
54+
}
55+
if (error.exception.module) {
56+
error.exception.module = truncate(String(error.exception.module), config.INTAKE_STRING_MAX_SIZE)
57+
}
58+
if (error.exception.stacktrace) {
59+
error.exception.stacktrace = truncFrames(error.exception.stacktrace)
60+
}
61+
}
62+
63+
truncContext(error.context)
64+
}
65+
66+
function truncContext (context) {
67+
if (!context) return
68+
69+
if (context.request) {
70+
if (context.request.method) {
71+
context.request.method = truncate(String(context.request.method), config.INTAKE_STRING_MAX_SIZE)
72+
}
73+
if (context.request.url) {
74+
if (context.request.url.protocol) {
75+
context.request.url.protocol = truncate(String(context.request.url.protocol), config.INTAKE_STRING_MAX_SIZE)
76+
}
77+
if (context.request.url.hostname) {
78+
context.request.url.hostname = truncate(String(context.request.url.hostname), config.INTAKE_STRING_MAX_SIZE)
79+
}
80+
if (context.request.url.port) {
81+
context.request.url.port = truncate(String(context.request.url.port), config.INTAKE_STRING_MAX_SIZE)
82+
}
83+
if (context.request.url.pathname) {
84+
context.request.url.pathname = truncate(String(context.request.url.pathname), config.INTAKE_STRING_MAX_SIZE)
85+
}
86+
if (context.request.url.search) {
87+
context.request.url.search = truncate(String(context.request.url.search), config.INTAKE_STRING_MAX_SIZE)
88+
}
89+
if (context.request.url.hash) {
90+
context.request.url.hash = truncate(String(context.request.url.hash), config.INTAKE_STRING_MAX_SIZE)
91+
}
92+
if (context.request.url.raw) {
93+
context.request.url.raw = truncate(String(context.request.url.raw), config.INTAKE_STRING_MAX_SIZE)
94+
}
95+
if (context.request.url.full) {
96+
context.request.url.full = truncate(String(context.request.url.full), config.INTAKE_STRING_MAX_SIZE)
97+
}
98+
}
99+
}
100+
if (context.user) {
101+
if (context.user.id) {
102+
context.user.id = truncate(String(context.user.id), config.INTAKE_STRING_MAX_SIZE)
103+
}
104+
if (context.user.email) {
105+
context.user.email = truncate(String(context.user.email), config.INTAKE_STRING_MAX_SIZE)
106+
}
107+
if (context.user.username) {
108+
context.user.username = truncate(String(context.user.username), config.INTAKE_STRING_MAX_SIZE)
109+
}
110+
}
111+
}
112+
113+
function truncFrames (frames) {
114+
frames.forEach(function (frame, i) {
115+
if (frame.pre_context) frame.pre_context = truncEach(frame.pre_context, 1000)
116+
if (frame.context_line) frame.context_line = truncate(String(frame.context_line), 1000)
117+
if (frame.post_context) frame.post_context = truncEach(frame.post_context, 1000)
118+
})
119+
120+
return frames
121+
}
122+
123+
function truncEach (arr, len) {
124+
return arr.map(function (str) {
125+
return truncate(String(str), len)
126+
})
127+
}

test/agent.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -573,6 +573,10 @@ test('#lambda()', function (t) {
573573
lambda.invoke(name, input, (err, result) => {
574574
t.error(err)
575575
t.equal(result, output)
576+
<<<<<<< HEAD
577+
=======
578+
t.end()
579+
>>>>>>> feat: support APM Server intake API version 2 (#465)
576580
})
577581
})
578582
.on('data-transaction', function (data) {
@@ -598,6 +602,10 @@ test('#lambda()', function (t) {
598602
lambda.invoke(name, input, (err, result) => {
599603
t.error(err)
600604
t.equal(result, output)
605+
<<<<<<< HEAD
606+
=======
607+
t.end()
608+
>>>>>>> feat: support APM Server intake API version 2 (#465)
601609
})
602610
})
603611
.on('data-transaction', function (data) {
@@ -623,6 +631,10 @@ test('#lambda()', function (t) {
623631
lambda.invoke(name, input, (err, result) => {
624632
t.error(err)
625633
t.equal(result, output)
634+
<<<<<<< HEAD
635+
=======
636+
t.end()
637+
>>>>>>> feat: support APM Server intake API version 2 (#465)
626638
})
627639
})
628640
.on('data-transaction', function (data) {

0 commit comments

Comments
 (0)