Skip to content

Commit e6c5dba

Browse files
committed
resource-detector-instana
- feat(resource-detector-instana): suppress tracing of the detectors internal HTTP requests
1 parent 052aede commit e6c5dba

File tree

8 files changed

+882
-48
lines changed

8 files changed

+882
-48
lines changed

detectors/node/opentelemetry-resource-detector-instana/README.md

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,21 +22,15 @@ npm install --save @opentelemetry/resource-detector-instana
2222

2323
```typescript
2424
import {
25-
Resource,
2625
processDetector,
2726
envDetector,
2827
} from "@opentelemetry/resources";
29-
import { SEMRESATTRS_SERVICE_NAME } from "@opentelemetry/semantic-conventions";
3028
import { NodeSDK } from "@opentelemetry/sdk-node";
3129
import { instanaAgentDetector } from "@opentelemetry/resource-detector-instana";
3230

33-
const globalResource = new Resource({
34-
[SEMRESATTRS_SERVICE_NAME]: "TestService",
35-
});
36-
3731
const sdk = new NodeSDK({
32+
serviceName: "TestService",
3833
resourceDetectors: [envDetector, processDetector, instanaAgentDetector],
39-
resource: globalResource,
4034
});
4135

4236
sdk.start()

detectors/node/opentelemetry-resource-detector-instana/package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
"author": "OpenTelemetry Authors",
2727
"license": "Apache-2.0",
2828
"engines": {
29-
"node": ">=8.12.0"
29+
"node": "^18.19.0 || >=20.6.0"
3030
},
3131
"files": [
3232
"build/src/**/*.js",
@@ -42,7 +42,7 @@
4242
"devDependencies": {
4343
"@opentelemetry/api": "^1.3.0",
4444
"@opentelemetry/contrib-test-utils": "^0.45.1",
45-
"@opentelemetry/sdk-node": "^0.57.2",
45+
"@opentelemetry/sdk-node": "^0.200.0-dev.0",
4646
"@types/mocha": "10.0.10",
4747
"@types/node": "18.18.14",
4848
"@types/semver": "7.5.8",
@@ -52,7 +52,7 @@
5252
"typescript": "4.4.4"
5353
},
5454
"dependencies": {
55-
"@opentelemetry/resources": "^1.10.0",
55+
"@opentelemetry/resources": "^2.0.0-dev.0",
5656
"@opentelemetry/semantic-conventions": "^1.27.0"
5757
},
5858
"peerDependencies": {

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

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,28 +13,44 @@
1313
* See the License for the specific language governing permissions and
1414
* limitations under the License.
1515
*/
16+
17+
import { context, diag } from '@opentelemetry/api';
18+
import { suppressTracing } from '@opentelemetry/core';
1619
import {
17-
DetectorSync,
18-
Resource,
19-
IResource,
20-
ResourceAttributes,
20+
ResourceDetector,
21+
DetectedResource,
22+
DetectedResourceAttributes,
2123
} from '@opentelemetry/resources';
22-
import { diag } from '@opentelemetry/api';
2324
import {
2425
SEMRESATTRS_PROCESS_PID,
2526
SEMRESATTRS_SERVICE_INSTANCE_ID,
2627
} from '@opentelemetry/semantic-conventions';
2728
import * as http from 'http';
2829

29-
class InstanaAgentDetector implements DetectorSync {
30+
class InstanaAgentDetector implements ResourceDetector {
3031
readonly INSTANA_AGENT_DEFAULT_HOST = 'localhost';
3132
readonly INSTANA_AGENT_DEFAULT_PORT = 42699;
3233

33-
detect(): IResource {
34-
return new Resource({}, this._getAttributes());
34+
detect(): DetectedResource {
35+
const dataPromise = context.with(suppressTracing(context.active()), () =>
36+
this._gatherData()
37+
);
38+
39+
const attrNames = [
40+
SEMRESATTRS_PROCESS_PID,
41+
SEMRESATTRS_SERVICE_INSTANCE_ID,
42+
];
43+
44+
const attributes = {} as DetectedResourceAttributes;
45+
attrNames.forEach(name => {
46+
// Each resource attribute is determined asynchronously in _gatherData().
47+
attributes[name] = dataPromise.then(data => data[name]);
48+
});
49+
50+
return { attributes };
3551
}
3652

37-
private async _getAttributes(): Promise<ResourceAttributes> {
53+
private async _gatherData(): Promise<DetectedResourceAttributes> {
3854
const host =
3955
process.env.INSTANA_AGENT_HOST || this.INSTANA_AGENT_DEFAULT_HOST;
4056
const port = Number(

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,4 @@
1414
* limitations under the License.
1515
*/
1616

17-
export * from './InstanaAgentDetector';
17+
export { instanaAgentDetector } from './InstanaAgentDetector';

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,4 @@
1414
* limitations under the License.
1515
*/
1616

17-
export * from './detectors';
17+
export { instanaAgentDetector } from './detectors';

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

Lines changed: 4 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,7 @@
1616

1717
import * as nock from 'nock';
1818
import * as assert from 'assert';
19-
import {
20-
Resource,
21-
processDetector,
22-
envDetector,
23-
} from '@opentelemetry/resources';
24-
import { SEMRESATTRS_SERVICE_NAME } from '@opentelemetry/semantic-conventions';
19+
import { processDetector, envDetector } from '@opentelemetry/resources';
2520
import { NodeSDK } from '@opentelemetry/sdk-node';
2621
import { instanaAgentDetector } from '../src';
2722

@@ -53,13 +48,9 @@ describe('[Integration] instanaAgentDetector', () => {
5348
.reply(200, () => mockedReply);
5449

5550
const serviceName = 'TestService';
56-
const globalResource = new Resource({
57-
[SEMRESATTRS_SERVICE_NAME]: serviceName,
58-
});
59-
6051
const sdk = new NodeSDK({
52+
serviceName,
6153
resourceDetectors: [envDetector, processDetector, instanaAgentDetector],
62-
resource: globalResource,
6354
});
6455

6556
sdk.start();
@@ -92,19 +83,14 @@ describe('[Integration] instanaAgentDetector', () => {
9283
.reply(200, () => mockedReply);
9384

9485
const serviceName = 'TestService';
95-
const globalResource = new Resource({
96-
[SEMRESATTRS_SERVICE_NAME]: serviceName,
97-
});
98-
9986
const sdk = new NodeSDK({
87+
serviceName,
10088
resourceDetectors: [envDetector, processDetector, instanaAgentDetector],
101-
resource: globalResource,
10289
});
10390

10491
sdk.start();
10592
const resource = sdk['_resource'];
106-
107-
await delay(500);
93+
await resource.waitForAsyncAttributes?.();
10894

10995
assert.equal(resource.attributes['process.pid'], 123);
11096
assert.equal(resource.attributes['process.runtime.name'], 'nodejs');

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

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
import * as nock from 'nock';
1818
import * as assert from 'assert';
1919
import { instanaAgentDetector } from '../src';
20+
import { detectResources } from '@opentelemetry/resources';
2021

2122
describe('[UNIT] instanaAgentDetector', () => {
2223
describe('when agent is running', () => {
@@ -53,7 +54,7 @@ describe('[UNIT] instanaAgentDetector', () => {
5354
.put('/com.instana.plugin.nodejs.discovery')
5455
.reply(200, () => mockedReply);
5556

56-
const resource = instanaAgentDetector.detect();
57+
const resource = detectResources({ detectors: [instanaAgentDetector] });
5758
await resource.waitForAsyncAttributes?.();
5859

5960
scope.done();
@@ -80,7 +81,7 @@ describe('[UNIT] instanaAgentDetector', () => {
8081
.put('/com.instana.plugin.nodejs.discovery')
8182
.reply(200, () => mockedReply);
8283

83-
const resource = instanaAgentDetector.detect();
84+
const resource = detectResources({ detectors: [instanaAgentDetector] });
8485
await resource.waitForAsyncAttributes?.();
8586

8687
scope.done();
@@ -97,7 +98,7 @@ describe('[UNIT] instanaAgentDetector', () => {
9798
.put('/com.instana.plugin.nodejs.discovery')
9899
.reply(500, () => new Error());
99100

100-
const resource = instanaAgentDetector.detect();
101+
const resource = detectResources({ detectors: [instanaAgentDetector] });
101102
await resource.waitForAsyncAttributes?.();
102103

103104
assert.deepStrictEqual(resource.attributes, {});
@@ -118,7 +119,7 @@ describe('[UNIT] instanaAgentDetector', () => {
118119
.delay(500)
119120
.reply(200, {});
120121

121-
const resource = instanaAgentDetector.detect();
122+
const resource = detectResources({ detectors: [instanaAgentDetector] });
122123
await resource.waitForAsyncAttributes?.();
123124

124125
assert.deepStrictEqual(resource.attributes, {});
@@ -131,7 +132,7 @@ describe('[UNIT] instanaAgentDetector', () => {
131132
process.env.INSTANA_AGENT_TIMEOUT_MS = '100';
132133
process.env.INSTANA_RETRY_TIMEOUT_MS = '100';
133134

134-
const resource = instanaAgentDetector.detect();
135+
const resource = detectResources({ detectors: [instanaAgentDetector] });
135136
await resource.waitForAsyncAttributes?.();
136137

137138
assert.deepStrictEqual(resource.attributes, {});

0 commit comments

Comments
 (0)