@@ -9,14 +9,14 @@ const tap = require('tap')
9
9
const dns = require ( 'dns' )
10
10
const events = require ( 'events' )
11
11
const https = require ( 'https' )
12
-
12
+ const sinon = require ( 'sinon' )
13
+ const proxyquire = require ( 'proxyquire' )
14
+ const RemoteMethod = require ( '../../../lib/collector/remote-method' )
13
15
const url = require ( 'url' )
14
16
const Config = require ( '../../../lib/config' )
15
- const RemoteMethod = require ( '../../../lib/collector/remote-method' )
16
17
const helper = require ( '../../lib/agent_helper' )
17
18
require ( '../../lib/metrics_helper' )
18
19
const NAMES = require ( '../../../lib/metrics/names' )
19
-
20
20
const BARE_AGENT = { config : { } , metrics : { measureBytes ( ) { } } }
21
21
22
22
function generate ( method , runID , protocolVersion ) {
@@ -369,7 +369,7 @@ tap.test('when posting to collector', (t) => {
369
369
} )
370
370
} )
371
371
372
- t . end ( 'should use the right URL' , ( t ) => {
372
+ t . test ( 'should use the right URL' , ( t ) => {
373
373
const sendMetrics = nockMetricDataUncompressed ( )
374
374
method . _post ( '[]' , { } , ( error ) => {
375
375
t . error ( error )
@@ -378,12 +378,13 @@ tap.test('when posting to collector', (t) => {
378
378
} )
379
379
} )
380
380
381
- t . end ( 'should respect the put_for_data_send config' , ( t ) => {
381
+ t . test ( 'should respect the put_for_data_send config' , ( t ) => {
382
382
const putMetrics = nock ( URL )
383
383
. put ( generate ( 'metric_data' , RUN_ID ) )
384
384
. reply ( 200 , { return_value : [ ] } )
385
385
386
386
config . put_for_data_send = true
387
+
387
388
method . _post ( '[]' , { } , ( error ) => {
388
389
t . error ( error )
389
390
t . ok ( putMetrics . isDone ( ) )
@@ -937,3 +938,90 @@ tap.test('record data usage supportability metrics', (t) => {
937
938
)
938
939
} )
939
940
} )
941
+
942
+ tap . test ( '_safeRequest logging' , ( t ) => {
943
+ t . autoend ( )
944
+ t . beforeEach ( ( t ) => {
945
+ const sandbox = sinon . createSandbox ( )
946
+ const loggerMock = require ( '../mocks/logger' ) ( sandbox )
947
+ const RemoteMethod = proxyquire ( '../../../lib/collector/remote-method' , {
948
+ '../logger' : {
949
+ child : sandbox . stub ( ) . callsFake ( ( ) => loggerMock )
950
+ }
951
+ } )
952
+ sandbox . stub ( RemoteMethod . prototype , '_request' )
953
+ t . context . loggerMock = loggerMock
954
+ t . context . RemoteMethod = RemoteMethod
955
+ t . context . sandbox = sandbox
956
+ t . context . options = {
957
+ host : 'collector.newrelic.com' ,
958
+ port : 80 ,
959
+ onError : ( ) => { } ,
960
+ onResponse : ( ) => { } ,
961
+ body : 'test-body' ,
962
+ path : '/nonexistent'
963
+ }
964
+ t . context . config = { license_key : 'shhh-dont-tell' , max_payload_size_in_bytes : 10000 }
965
+ } )
966
+
967
+ t . afterEach ( ( t ) => {
968
+ const { sandbox } = t . context
969
+ sandbox . restore ( )
970
+ } )
971
+
972
+ t . test ( 'should redact license key in logs' , ( t ) => {
973
+ const { RemoteMethod, loggerMock, options, config } = t . context
974
+ loggerMock . traceEnabled . returns ( true )
975
+ const method = new RemoteMethod ( 'test' , { config } )
976
+ method . _safeRequest ( options )
977
+ t . same (
978
+ loggerMock . trace . args ,
979
+ [
980
+ [
981
+ { body : options . body } ,
982
+ 'Posting to %s://%s:%s%s' ,
983
+ 'https' ,
984
+ options . host ,
985
+ options . port ,
986
+ '/agent_listener/invoke_raw_method?marshal_format=json&protocol_version=17&license_key=REDACTED&method=test'
987
+ ]
988
+ ] ,
989
+ 'should redact key in trace level log'
990
+ )
991
+ t . end ( )
992
+ } )
993
+
994
+ t . test ( 'should call logger if trace is not enabled but audit logging is enabled' , ( t ) => {
995
+ const { RemoteMethod, loggerMock, options, config } = t . context
996
+ loggerMock . traceEnabled . returns ( false )
997
+ config . logging = { level : 'info' }
998
+ config . audit_log = { enabled : true , endpoints : [ 'test' ] }
999
+ const method = new RemoteMethod ( 'test' , { config } )
1000
+ method . _safeRequest ( options )
1001
+ t . same (
1002
+ loggerMock . info . args ,
1003
+ [
1004
+ [
1005
+ { body : options . body } ,
1006
+ 'Posting to %s://%s:%s%s' ,
1007
+ 'https' ,
1008
+ options . host ,
1009
+ options . port ,
1010
+ '/agent_listener/invoke_raw_method?marshal_format=json&protocol_version=17&license_key=REDACTED&method=test'
1011
+ ]
1012
+ ] ,
1013
+ 'should redact key in trace level log'
1014
+ )
1015
+ t . end ( )
1016
+ } )
1017
+
1018
+ t . test ( 'should not call logger if trace or audit logging is not enabled' , ( t ) => {
1019
+ const { RemoteMethod, loggerMock, options, config } = t . context
1020
+ loggerMock . traceEnabled . returns ( false )
1021
+ const method = new RemoteMethod ( 'test' , { config } )
1022
+ method . _safeRequest ( options )
1023
+ t . ok ( loggerMock . trace . callCount === 0 , 'should not log outgoing message to collector' )
1024
+ t . ok ( loggerMock . info . callCount === 0 , 'should not log outgoing message to collector' )
1025
+ t . end ( )
1026
+ } )
1027
+ } )
0 commit comments