Skip to content

Commit 305ee50

Browse files
committed
feat(tracer): implement transaction event support
1 parent 7108d6a commit 305ee50

51 files changed

Lines changed: 2766 additions & 1975 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

lib/agent/agent.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
var Timer = require('./timer')
2+
3+
function Agent (name, interval, task) {
4+
this.name = name
5+
if (interval != null && typeof task === 'function') {
6+
this.timer = new Timer(function () {
7+
task()
8+
}, interval)
9+
}
10+
}
11+
12+
Agent.prototype.start = function () {
13+
if (this.timer) {
14+
this.timer.start()
15+
}
16+
}
17+
18+
Agent.prototype.stop = function () {
19+
if (this.timer) {
20+
this.timer.end()
21+
}
22+
}
23+
24+
Agent.prototype.initialize = function () { }
25+
26+
Agent.prototype.destruct = function () { this.stop() }
27+
28+
module.exports = Agent

lib/agent/api/index.js

Lines changed: 6 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ var bl = require('bl')
1212
var libPackage = require('../../../package')
1313

1414
function CollectorApi (options) {
15-
this.COLLECTOR_API_SAMPLE = url.resolve(options.collectorApiUrl, options.collectorApiSampleEndpoint)
1615
this.COLLECTOR_API_SERVICE = url.resolve(options.collectorApiUrl, options.collectorApiServiceEndpoint)
1716
this.COLLECTOR_API_METRICS = url.resolve(options.collectorApiUrl, options.collectorApiApmMetricsEndpoint)
1817
this.COLLECTOR_API_RPM_METRICS = url.resolve(options.collectorApiUrl, options.collectorApiRpmMetricsEndpoint)
@@ -38,17 +37,17 @@ function CollectorApi (options) {
3837
}
3938

4039
// USE THIS WITH CAUTION, IT WILL BE BLOCKING
41-
CollectorApi.prototype._sendSync = function (destinationUrl, data) {
40+
CollectorApi.prototype._sendSync = function (destinationUrl, data, options) {
4241
debug('sending data to trace servers sync: ', stringify(data))
4342
try {
4443
requestSync('POST', destinationUrl, {
4544
json: data,
46-
headers: {
45+
headers: assign({
4746
'Authorization': 'Bearer ' + this.apiKey,
4847
'Content-Type': 'application/json',
4948
'X-Reporter-Version': libPackage.version,
5049
'X-Reporter-Language': this.collectorLanguage
51-
},
50+
}, options && options.headers),
5251
timeout: 1000
5352
})
5453
} catch (ex) {
@@ -63,7 +62,7 @@ CollectorApi.prototype._withInstanceInfo = function (data) {
6362
}, data)
6463
}
6564

66-
CollectorApi.prototype._send = function (destinationUrl, data, callback) {
65+
CollectorApi.prototype._send = function (destinationUrl, data, callback, options) {
6766
var opts = url.parse(destinationUrl)
6867
var payload = stringify(data)
6968

@@ -76,13 +75,13 @@ CollectorApi.prototype._send = function (destinationUrl, data, callback) {
7675
method: 'POST',
7776
// if the proxy is not set, it will fallback to the default agent
7877
agent: this.proxyAgent,
79-
headers: {
78+
headers: assign({
8079
'Authorization': 'Bearer ' + this.apiKey,
8180
'Content-Type': 'application/json',
8281
'X-Reporter-Version': libPackage.version,
8382
'X-Reporter-Language': this.collectorLanguage,
8483
'Content-Length': Buffer.byteLength(payload)
85-
}
84+
}, options && options.headers)
8685
}
8786

8887
var req = https.request(requestOptions, function (res) {
@@ -218,21 +217,6 @@ CollectorApi.prototype.sendCustomMetrics = function (data) {
218217
}))
219218
}
220219

221-
CollectorApi.prototype.sendSamples = function (samples, sync) {
222-
var url = this.COLLECTOR_API_SAMPLE
223-
var metadata = {
224-
version: '2',
225-
instance: this._withInstanceInfo({}),
226-
service: {
227-
key: this.serviceKey ? this.serviceKey : undefined,
228-
name: this.serviceName
229-
}
230-
}
231-
232-
var data = assign({}, samples, metadata)
233-
sync ? this._sendSync(url, data) : this._send(url, data)
234-
}
235-
236220
CollectorApi.prototype.sendDependencies = function (dependencies) {
237221
var url = util.format(this.COLLECTOR_API_SECURITY_DEPENDENCIES, this.serviceKey)
238222
this._send(url, dependencies)

lib/agent/api/index.spec.js

Lines changed: 0 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ var CollectorApi = require('./')
77
var expect = require('chai').expect
88
var nock = require('nock')
99
var libPackage = require('../../../package')
10-
var assign = require('lodash.assign')
1110

1211
describe('The Trace CollectorApi module', function () {
1312
var defaultConfig
@@ -117,40 +116,6 @@ describe('The Trace CollectorApi module', function () {
117116
expect(sendStub).to.have.been.calledWith(sendUrl, data)
118117
})
119118

120-
it('enhances samples with required properties', function (done) {
121-
var collectorApi = CollectorApi.create(defaultConfig)
122-
var data = {
123-
test: 'clearly a mock'
124-
}
125-
126-
nock(defaultConfig.collectorApiUrl, {
127-
reqheaders: {
128-
'Authorization': 'Bearer testApiKey',
129-
'Content-Type': 'application/json',
130-
'X-Reporter-Version': libPackage.version,
131-
'X-Reporter-Language': defaultConfig.collectorLanguage
132-
}
133-
})
134-
.post(defaultConfig.collectorApiSampleEndpoint, function (body) {
135-
expect(body).to.eql(assign({
136-
version: '2',
137-
instance: {
138-
hostname: defaultConfig.system.hostname,
139-
pid: defaultConfig.system.processId
140-
},
141-
service: {
142-
name: defaultConfig.serviceName
143-
}
144-
}, data))
145-
return JSON.stringify(data)
146-
})
147-
.reply(201, function () {
148-
done()
149-
})
150-
151-
collectorApi.sendSamples(data)
152-
})
153-
154119
it('sends incomingEdgeMetrics with the required properties', function (done) {
155120
var serviceKey = 12
156121
var collectorApi = CollectorApi.create(defaultConfig)

lib/agent/control/control.js

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,23 @@
11
var debug = require('debug')('risingstack/trace')
22

3-
var Timer = require('../timer')
3+
var inherits = require('util').inherits
4+
var Agent = require('../agent')
45

56
function Control (options) {
6-
var _this = this
7-
this.name = 'Control'
8-
97
this.collectorApi = options.collectorApi
108
this.controlBus = options.controlBus
119
this.config = options.config
1210
this.updateInterval = this.config.updateInterval
11+
1312
this.latestCommandId = undefined
1413

1514
this.getUpdates()
1615

17-
this.timer = new Timer(function () {
18-
_this.getUpdates()
19-
}, this.updateInterval)
16+
Agent.call(this, 'Control', this.updateInterval, this.getUpdates.bind(this))
2017
}
2118

19+
inherits(Control, Agent)
20+
2221
Control.prototype.getUpdates = function () {
2322
var _this = this
2423
this.collectorApi.getUpdates({

lib/agent/healthcheck/index.js

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,16 @@
1-
var Timer = require('../timer')
1+
var inherits = require('util').inherits
2+
var Agent = require('../agent')
23

34
function Healthcheck (options) {
4-
this.name = 'Healthcheck'
5-
var _this = this
65
this.collectorApi = options.collectorApi
76
this.config = options.config
87
this.healthcheckInterval = this.config.healthcheckInterval
98

10-
this.timer = new Timer(function () {
11-
_this.ping()
12-
}, this.healthcheckInterval)
9+
Agent.call(this, 'Healthcheck', this.healthcheckInterval, this.ping.bind(this))
1310
}
1411

12+
inherits(Healthcheck, Agent)
13+
1514
Healthcheck.prototype.ping = function () {
1615
this.collectorApi.ping()
1716
}

0 commit comments

Comments
 (0)