Skip to content

Commit 91d1bd5

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

File tree

3 files changed

+134
-0
lines changed

3 files changed

+134
-0
lines changed

lib/agent.js

Lines changed: 6 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
@@ -169,6 +170,11 @@ Agent.prototype.start = function (opts) {
169170
this._apmServer.on('error', err => {
170171
this.logger.error('An error occrued while communicating with the APM Server:', err.message)
171172
})
173+
this._apmServer.on('error', err => {
174+
this.logger.error('An error occrued while communicating with the APM Server:', err.message)
175+
})
176+
177+
this._instrumentation.start()
172178

173179
this._instrumentation.start()
174180

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+
}

0 commit comments

Comments
 (0)