Skip to content

Commit ef5efcb

Browse files
david-lunablumamir
andauthored
feat(detector-instana)!: change implementation to DetectorSync interface (#2337)
Co-authored-by: Amir Blum <[email protected]>
1 parent b09cb40 commit ef5efcb

File tree

2 files changed

+40
-35
lines changed

2 files changed

+40
-35
lines changed

detectors/node/opentelemetry-resource-detector-instana/src/detectors/InstanaAgentDetector.ts

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,31 +13,44 @@
1313
* See the License for the specific language governing permissions and
1414
* limitations under the License.
1515
*/
16-
import { Detector, Resource, IResource } from '@opentelemetry/resources';
16+
import {
17+
DetectorSync,
18+
Resource,
19+
IResource,
20+
ResourceAttributes,
21+
} from '@opentelemetry/resources';
1722
import { diag } from '@opentelemetry/api';
1823
import {
1924
SEMRESATTRS_PROCESS_PID,
2025
SEMRESATTRS_SERVICE_INSTANCE_ID,
2126
} from '@opentelemetry/semantic-conventions';
2227
import * as http from 'http';
2328

24-
class InstanaAgentDetector implements Detector {
29+
class InstanaAgentDetector implements DetectorSync {
2530
readonly INSTANA_AGENT_DEFAULT_HOST = 'localhost';
2631
readonly INSTANA_AGENT_DEFAULT_PORT = 42699;
2732

28-
async detect(): Promise<IResource> {
33+
detect(): IResource {
34+
return new Resource({}, this._getAttributes());
35+
}
36+
37+
private async _getAttributes(): Promise<ResourceAttributes> {
2938
const host =
3039
process.env.INSTANA_AGENT_HOST || this.INSTANA_AGENT_DEFAULT_HOST;
3140
const port = Number(
3241
process.env.INSTANA_AGENT_PORT || this.INSTANA_AGENT_DEFAULT_PORT
3342
);
3443

35-
const data = await this._retryHandler(host, port, 0);
44+
try {
45+
const data = await this._retryHandler(host, port, 0);
3646

37-
return new Resource({
38-
[SEMRESATTRS_PROCESS_PID]: data.pid,
39-
[SEMRESATTRS_SERVICE_INSTANCE_ID]: data.agentUuid,
40-
});
47+
return {
48+
[SEMRESATTRS_PROCESS_PID]: data.pid,
49+
[SEMRESATTRS_SERVICE_INSTANCE_ID]: data.agentUuid,
50+
};
51+
} catch {
52+
return {};
53+
}
4154
}
4255

4356
private timeout(ms: number) {

detectors/node/opentelemetry-resource-detector-instana/test/InstanaAgentDetectorUnitTest.test.ts

Lines changed: 19 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616

1717
import * as nock from 'nock';
1818
import * as assert from 'assert';
19-
import { Resource } from '@opentelemetry/resources';
2019
import { instanaAgentDetector } from '../src';
2120

2221
describe('[UNIT] instanaAgentDetector', () => {
@@ -54,7 +53,8 @@ describe('[UNIT] instanaAgentDetector', () => {
5453
.put('/com.instana.plugin.nodejs.discovery')
5554
.reply(200, () => mockedReply);
5655

57-
const resource: Resource = await instanaAgentDetector.detect();
56+
const resource = instanaAgentDetector.detect();
57+
await resource.waitForAsyncAttributes?.();
5858

5959
scope.done();
6060

@@ -80,7 +80,8 @@ describe('[UNIT] instanaAgentDetector', () => {
8080
.put('/com.instana.plugin.nodejs.discovery')
8181
.reply(200, () => mockedReply);
8282

83-
const resource: Resource = await instanaAgentDetector.detect();
83+
const resource = instanaAgentDetector.detect();
84+
await resource.waitForAsyncAttributes?.();
8485

8586
scope.done();
8687

@@ -90,28 +91,24 @@ describe('[UNIT] instanaAgentDetector', () => {
9091
});
9192
});
9293

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 () => {
9595
const scope = nock('http://localhost:42699')
9696
.persist()
9797
.put('/com.instana.plugin.nodejs.discovery')
9898
.reply(500, () => new Error());
9999

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, {});
106104

107105
scope.done();
108106
});
109107

110-
it('agent timeout', async () => {
108+
it('agent return empty resource if timeout', async () => {
111109
process.env.INSTANA_AGENT_PORT = '56002';
112110
process.env.INSTANA_AGENT_HOST = 'instanaagent';
113111
process.env.INSTANA_AGENT_TIMEOUT_MS = '200';
114-
const expectedError = new Error('Instana Agent request timed out.');
115112

116113
nock(
117114
`http://${process.env.INSTANA_AGENT_HOST}:${process.env.INSTANA_AGENT_PORT}`
@@ -121,28 +118,23 @@ describe('[UNIT] instanaAgentDetector', () => {
121118
.delay(500)
122119
.reply(200, {});
123120

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, {});
131125
});
132126
});
133127

134128
describe('when agent is not running', () => {
135-
it('should not return agent resource', async () => {
129+
it('should return empty resource', async () => {
136130
process.env.INSTANA_AGENT_PORT = '1111';
137131
process.env.INSTANA_AGENT_TIMEOUT_MS = '100';
138132
process.env.INSTANA_RETRY_TIMEOUT_MS = '100';
139133

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, {});
146138
});
147139
});
148140
});

0 commit comments

Comments
 (0)