16
16
17
17
import * as nock from 'nock' ;
18
18
import * as assert from 'assert' ;
19
- import { Resource } from '@opentelemetry/resources' ;
20
19
import { instanaAgentDetector } from '../src' ;
21
20
22
21
describe ( '[UNIT] instanaAgentDetector' , ( ) => {
@@ -54,7 +53,8 @@ describe('[UNIT] instanaAgentDetector', () => {
54
53
. put ( '/com.instana.plugin.nodejs.discovery' )
55
54
. reply ( 200 , ( ) => mockedReply ) ;
56
55
57
- const resource : Resource = await instanaAgentDetector . detect ( ) ;
56
+ const resource = instanaAgentDetector . detect ( ) ;
57
+ await resource . waitForAsyncAttributes ?.( ) ;
58
58
59
59
scope . done ( ) ;
60
60
@@ -80,7 +80,8 @@ describe('[UNIT] instanaAgentDetector', () => {
80
80
. put ( '/com.instana.plugin.nodejs.discovery' )
81
81
. reply ( 200 , ( ) => mockedReply ) ;
82
82
83
- const resource : Resource = await instanaAgentDetector . detect ( ) ;
83
+ const resource = instanaAgentDetector . detect ( ) ;
84
+ await resource . waitForAsyncAttributes ?.( ) ;
84
85
85
86
scope . done ( ) ;
86
87
@@ -90,28 +91,24 @@ describe('[UNIT] instanaAgentDetector', () => {
90
91
} ) ;
91
92
} ) ;
92
93
93
- it ( 'agent throws error' , async ( ) => {
94
- const expectedError = new Error ( 'Instana Agent returned status code 500' ) ;
94
+ it ( 'agent returns empty resource if request error' , async ( ) => {
95
95
const scope = nock ( 'http://localhost:42699' )
96
96
. persist ( )
97
97
. put ( '/com.instana.plugin.nodejs.discovery' )
98
98
. reply ( 500 , ( ) => new Error ( ) ) ;
99
99
100
- try {
101
- await instanaAgentDetector . detect ( ) ;
102
- assert . ok ( false , 'Expected to throw' ) ;
103
- } catch ( err ) {
104
- assert . deepStrictEqual ( err , expectedError ) ;
105
- }
100
+ const resource = instanaAgentDetector . detect ( ) ;
101
+ await resource . waitForAsyncAttributes ?.( ) ;
102
+
103
+ assert . deepStrictEqual ( resource . attributes , { } ) ;
106
104
107
105
scope . done ( ) ;
108
106
} ) ;
109
107
110
- it ( 'agent timeout' , async ( ) => {
108
+ it ( 'agent return empty resource if timeout' , async ( ) => {
111
109
process . env . INSTANA_AGENT_PORT = '56002' ;
112
110
process . env . INSTANA_AGENT_HOST = 'instanaagent' ;
113
111
process . env . INSTANA_AGENT_TIMEOUT_MS = '200' ;
114
- const expectedError = new Error ( 'Instana Agent request timed out.' ) ;
115
112
116
113
nock (
117
114
`http://${ process . env . INSTANA_AGENT_HOST } :${ process . env . INSTANA_AGENT_PORT } `
@@ -121,28 +118,23 @@ describe('[UNIT] instanaAgentDetector', () => {
121
118
. delay ( 500 )
122
119
. reply ( 200 , { } ) ;
123
120
124
- try {
125
- await instanaAgentDetector . detect ( ) ;
126
- assert . ok ( false , 'Expected to throw' ) ;
127
- } catch ( err ) {
128
- console . log ( err ) ;
129
- assert . deepStrictEqual ( err , expectedError ) ;
130
- }
121
+ const resource = instanaAgentDetector . detect ( ) ;
122
+ await resource . waitForAsyncAttributes ?.( ) ;
123
+
124
+ assert . deepStrictEqual ( resource . attributes , { } ) ;
131
125
} ) ;
132
126
} ) ;
133
127
134
128
describe ( 'when agent is not running' , ( ) => {
135
- it ( 'should not return agent resource' , async ( ) => {
129
+ it ( 'should return empty resource' , async ( ) => {
136
130
process . env . INSTANA_AGENT_PORT = '1111' ;
137
131
process . env . INSTANA_AGENT_TIMEOUT_MS = '100' ;
138
132
process . env . INSTANA_RETRY_TIMEOUT_MS = '100' ;
139
133
140
- try {
141
- await instanaAgentDetector . detect ( ) ;
142
- assert . ok ( false , 'Expected to throw' ) ;
143
- } catch ( err : any ) {
144
- assert . equal ( err . code , 'ECONNREFUSED' ) ;
145
- }
134
+ const resource = instanaAgentDetector . detect ( ) ;
135
+ await resource . waitForAsyncAttributes ?.( ) ;
136
+
137
+ assert . deepStrictEqual ( resource . attributes , { } ) ;
146
138
} ) ;
147
139
} ) ;
148
140
} ) ;
0 commit comments