From d2d82881be5042d71a9ffbed9f4b484d62a50368 Mon Sep 17 00:00:00 2001 From: gurke Date: Wed, 12 Apr 2023 12:45:00 +0200 Subject: [PATCH] chore(detectors/node): adapted breaking change in @opentelemetry/sdk-node@0.37.0 (#1462) - Refs: https://github.com/open-telemetry/opentelemetry-js/pull/3460 - Deprecated detectResources() in favor of a new method detectResourcesSync() which defers promises into the resource to be resolved later - introduced await resource.waitForAsyncAttributes?.(); --- .../README.md | 7 +-- .../package.json | 2 +- .../src/detectors/InstanaAgentDetector.ts | 5 +- ...nstanaAgentDetectorIntegrationTest.test.ts | 51 +++++++++++++++++-- 4 files changed, 51 insertions(+), 14 deletions(-) diff --git a/detectors/node/opentelemetry-resource-detector-instana/README.md b/detectors/node/opentelemetry-resource-detector-instana/README.md index 531bf044f2..fef7b05bbd 100644 --- a/detectors/node/opentelemetry-resource-detector-instana/README.md +++ b/detectors/node/opentelemetry-resource-detector-instana/README.md @@ -35,16 +35,11 @@ const globalResource = new Resource({ }); const sdk = new NodeSDK({ - autoDetectResources: false, resourceDetectors: [envDetector, processDetector, instanaAgentDetector], resource: globalResource, }); -(async () => { - await sdk.detectResources(); - - await sdk.start(); -}()); +sdk.start() ``` ## Useful links diff --git a/detectors/node/opentelemetry-resource-detector-instana/package.json b/detectors/node/opentelemetry-resource-detector-instana/package.json index 98842bc678..159e961e28 100644 --- a/detectors/node/opentelemetry-resource-detector-instana/package.json +++ b/detectors/node/opentelemetry-resource-detector-instana/package.json @@ -41,7 +41,7 @@ "devDependencies": { "@opentelemetry/api": "^1.3.0", "@opentelemetry/contrib-test-utils": "^0.33.1", - "@opentelemetry/sdk-node": "^0.35.1", + "@opentelemetry/sdk-node": "^0.37.0", "@types/mocha": "8.2.3", "@types/node": "18.11.7", "@types/semver": "7.3.8", diff --git a/detectors/node/opentelemetry-resource-detector-instana/src/detectors/InstanaAgentDetector.ts b/detectors/node/opentelemetry-resource-detector-instana/src/detectors/InstanaAgentDetector.ts index 64968ad0fb..e036523240 100644 --- a/detectors/node/opentelemetry-resource-detector-instana/src/detectors/InstanaAgentDetector.ts +++ b/detectors/node/opentelemetry-resource-detector-instana/src/detectors/InstanaAgentDetector.ts @@ -13,7 +13,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -import { Detector, Resource } from '@opentelemetry/resources'; +import { Detector, Resource, IResource } from '@opentelemetry/resources'; import { diag } from '@opentelemetry/api'; import { SemanticResourceAttributes } from '@opentelemetry/semantic-conventions'; import * as http from 'http'; @@ -22,7 +22,7 @@ class InstanaAgentDetector implements Detector { readonly INSTANA_AGENT_DEFAULT_HOST = 'localhost'; readonly INSTANA_AGENT_DEFAULT_PORT = 42699; - async detect(): Promise { + async detect(): Promise { const host = process.env.INSTANA_AGENT_HOST || this.INSTANA_AGENT_DEFAULT_HOST; const port = Number( @@ -112,7 +112,6 @@ class InstanaAgentDetector implements Detector { try { const data = JSON.parse(rawData); - if (data.pid && data.agentUuid) { return resolve(data); } diff --git a/detectors/node/opentelemetry-resource-detector-instana/test/InstanaAgentDetectorIntegrationTest.test.ts b/detectors/node/opentelemetry-resource-detector-instana/test/InstanaAgentDetectorIntegrationTest.test.ts index 99f4deb336..ee8124f3cc 100644 --- a/detectors/node/opentelemetry-resource-detector-instana/test/InstanaAgentDetectorIntegrationTest.test.ts +++ b/detectors/node/opentelemetry-resource-detector-instana/test/InstanaAgentDetectorIntegrationTest.test.ts @@ -25,6 +25,11 @@ import { SemanticResourceAttributes } from '@opentelemetry/semantic-conventions' import { NodeSDK } from '@opentelemetry/sdk-node'; import { instanaAgentDetector } from '../src'; +const delay = (ms: number) => + new Promise(resolve => { + setTimeout(resolve, ms); + }); + describe('[Integration] instanaAgentDetector', () => { beforeEach(() => { nock.disableNetConnect(); @@ -36,7 +41,7 @@ describe('[Integration] instanaAgentDetector', () => { nock.cleanAll(); }); - it('should return merged resource', async () => { + it('#1 should return merged resource', async () => { const mockedReply = { pid: 123, agentUuid: '14:7d:da:ff:fe:e4:08:d5', @@ -53,16 +58,54 @@ describe('[Integration] instanaAgentDetector', () => { }); const sdk = new NodeSDK({ - autoDetectResources: false, resourceDetectors: [envDetector, processDetector, instanaAgentDetector], resource: globalResource, }); - // attributes are automatically merged! - await sdk.detectResources(); + sdk.start(); + + const resource = sdk['_resource']; + // await sdk.detectResources(); [< @opentelemetry/sdk-node@0.37.0] + // await resource.waitForAsyncAttributes?.(); [>= @opentelemetry/sdk-node@0.37.0] + await resource.waitForAsyncAttributes?.(); + + assert.equal(resource.attributes['process.pid'], 123); + assert.equal(resource.attributes['process.runtime.name'], 'nodejs'); + assert.equal(resource.attributes['service.name'], 'TestService'); + assert.equal( + resource.attributes['service.instance.id'], + '14:7d:da:ff:fe:e4:08:d5' + ); + + scope.done(); + }); + + it('#2 should return merged resource', async () => { + const mockedReply = { + pid: 123, + agentUuid: '14:7d:da:ff:fe:e4:08:d5', + }; + + const scope = nock('http://localhost:42699') + .persist() + .put('/com.instana.plugin.nodejs.discovery') + .reply(200, () => mockedReply); + + const serviceName = 'TestService'; + const globalResource = new Resource({ + [SemanticResourceAttributes.SERVICE_NAME]: serviceName, + }); + + const sdk = new NodeSDK({ + resourceDetectors: [envDetector, processDetector, instanaAgentDetector], + resource: globalResource, + }); + sdk.start(); const resource = sdk['_resource']; + await delay(500); + assert.equal(resource.attributes['process.pid'], 123); assert.equal(resource.attributes['process.runtime.name'], 'nodejs'); assert.equal(resource.attributes['service.name'], 'TestService');