Skip to content

Commit

Permalink
tests: added check for agent reset on restart
Browse files Browse the repository at this point in the history
  • Loading branch information
NatalieWolfe committed Sep 23, 2016
1 parent 94de3fb commit d52a8e1
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 50 deletions.
37 changes: 23 additions & 14 deletions lib/agent.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,16 @@ function Agent(config) {

// Reset the agent to add all the sub-objects it needs. These object are the
// ones that get re-created if the agent is told to restart from the collector.
this.events = null
this.customEvents = null
this.errors = null
this.mapper = null
this.metricNameNormalizer = null
this.metrics = null
this.transactionNameNormalizer = null
this.urlNormalizer = null
this.txSegmentNormalizer = null
this.userNormalizer = null
this.reset()

// Transaction tracing.
Expand All @@ -74,23 +84,23 @@ function Agent(config) {
this.queries = new QueryTracer(this.config)

// Set up all the configuration events the agent needs to listen for.
var self = this
this.config.on('apdex_t', this._apdexTChange.bind(this))
this.config.on('data_report_period', this._harvesterIntervalChange.bind(this))
this.config.on('agent_enabled', this._enabledChange.bind(this))
this.config.on('change', this._configChange.bind(this))
this.config.on(
'metric_name_rules',
this.metricNameNormalizer.load.bind(this.metricNameNormalizer)
)
this.config.on(
'transaction_name_rules',
this.transactionNameNormalizer.load.bind(this.transactionNameNormalizer)
)
this.config.on('url_rules', this.urlNormalizer.load.bind(this.urlNormalizer))
this.config.on(
'transaction_segment_terms',
this.txSegmentNormalizer.load.bind(this.txSegmentNormalizer)
)
this.config.on('metric_name_rules', function updateMetricNameNormalizer() {
self.metricNameNormalizer.load.apply(self.metricNameNormalizer, arguments)
})
this.config.on('transaction_name_rules', function updateTransactionNameNormalizer() {
self.transactionNameNormalizer.load.apply(self.transactionNameNormalizer, arguments)
})
this.config.on('url_rules', function updateUrlNormalizer() {
self.urlNormalizer.load.apply(self.urlNormalizer, arguments)
})
this.config.on('transaction_segment_terms', function updateSegmentNormalizer() {
self.txSegmentNormalizer.load.apply(self.txSegmentNormalizer, arguments)
})

// Entity tracking metrics.
this.totalActiveSegments = 0
Expand Down Expand Up @@ -308,7 +318,6 @@ Agent.prototype.reconfigure = function reconfigure(configuration) {
if (!configuration) throw new TypeError("must pass configuration")

this.config.onConnect(configuration)
this.reset()
}

/**
Expand Down
1 change: 1 addition & 0 deletions lib/metrics/normalizer.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ util.inherits(MetricNormalizer, EventEmitter)
*/
MetricNormalizer.prototype.load = function load(json) {
if (json) {
this.rules = []
logger.debug("Received %s %s normalization rule(s) from the server",
json.length, this.type)

Expand Down
1 change: 0 additions & 1 deletion lib/transaction/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,6 @@ Transaction.prototype.setName = function setName(requestURL, statusCode) {
transactionName: this.name}, 'Setting transaction name')

this._partialName = this.nameState.getName()

this.url = urltils.scrub(requestURL)
this.statusCode = statusCode

Expand Down
97 changes: 62 additions & 35 deletions test/unit/collector/api.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1378,49 +1378,76 @@ describe("CollectorAPI", function() {
api._runLifecycle(method, null, tested)
})

it("should reconnect and resubmit on ForceRestartException", function(done) {
var exception = {
exception: {
message: "Yo, break off a piece of that Irish Sprang!",
error_type: 'NewRelic::Agent::ForceRestartException'
describe('on ForceRestartException', function() {
var restart = null
var shutdown = null
var redirect = null
var connect = null
var succeed = null

beforeEach(function() {
var exception = {
exception: {
message: 'Yo, break off a piece of that Irish Sprang!',
error_type: 'NewRelic::Agent::ForceRestartException'
}
}
}

var restart = nock(URL)
.post(generate('metric_data', 31337))
.reply(200, exception)
var shutdown = nock(URL)
.post(generate('shutdown', 31337))
.reply(200, {return_value: null})
var redirect = nock(URL)
.post(generate('get_redirect_host'))
.reply(200, {return_value: "collector.newrelic.com"})
var connect = nock(URL)
.post(generate('connect'))
.reply(200, {return_value: {agent_run_id: 31338}})
var succeed = nock(URL)
.post(generate('metric_data', 31338))
.reply(200, {return_value: {}})

function tested(error) {
if (error) {
console.error(error.stack)
}
should.not.exist(error)
expect(api._agent.config.run_id).equal(31338) // has new run ID
restart = nock(URL)
.post(generate('metric_data', 31337))
.reply(200, exception)
shutdown = nock(URL)
.post(generate('shutdown', 31337))
.reply(200, {return_value: null})
redirect = nock(URL)
.post(generate('get_redirect_host'))
.reply(200, {return_value: 'collector.newrelic.com'})
connect = nock(URL)
.post(generate('connect'))
.reply(200, {return_value: {agent_run_id: 31338}})
succeed = nock(URL)
.post(generate('metric_data', 31338))
.reply(200, {return_value: {}})
})

function nockDone() {
restart.done()
shutdown.done()
redirect.done()
connect.done()
succeed.done()
done()
}

api._runLifecycle(method, null, tested)
it('should reconnect and resubmit', function(done) {
api._runLifecycle(method, null, function(error) {
if (error) {
console.error(error.stack)
}
expect(error).to.not.exist
expect(api._agent.config.run_id).equal(31338) // has new run ID
nockDone()
done()
})
})

it('should reconfigure the agent', function(done) {
var reconfigureCalled = false
var oldReconfigure = agent.reconfigure
agent.reconfigure = function() {
reconfigureCalled = true
return oldReconfigure.apply(this, arguments)
}

api._runLifecycle(method, null, function(err) {
expect(err).to.not.exist
expect(reconfigureCalled).to.be.true
nockDone()
done()
})
})
})

it("should stop the agent on ForceDisconnectException", function (done) {
it("should stop the agent on ForceDisconnectException", function(done) {
var exception = {
exception: {
message: "Wake up! Time to die!",
Expand All @@ -1429,11 +1456,11 @@ describe("CollectorAPI", function() {
}

var restart = nock(URL)
.post(generate('metric_data', 31337))
.reply(200, exception)
.post(generate('metric_data', 31337))
.reply(200, exception)
var shutdown = nock(URL)
.post(generate('shutdown', 31337))
.reply(200, {return_value: null})
.post(generate('shutdown', 31337))
.reply(200, {return_value: null})

function tested(error) {
expect(error.message).equal("Wake up! Time to die!")
Expand Down

0 comments on commit d52a8e1

Please sign in to comment.