Skip to content

Commit

Permalink
chore(detectors/node): adapted breaking change in @opentelemetry/sdk-…
Browse files Browse the repository at this point in the history
…node@0.37.0 (#1462)

- Refs: open-telemetry/opentelemetry-js#3460
- Deprecated detectResources() in favor of a new method detectResourcesSync() which defers promises into the resource to be resolved later
- introduced await resource.waitForAsyncAttributes?.();
  • Loading branch information
kirrg001 committed Apr 12, 2023
1 parent 27943f4 commit d2d8288
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -22,7 +22,7 @@ class InstanaAgentDetector implements Detector {
readonly INSTANA_AGENT_DEFAULT_HOST = 'localhost';
readonly INSTANA_AGENT_DEFAULT_PORT = 42699;

async detect(): Promise<Resource> {
async detect(): Promise<IResource> {
const host =
process.env.INSTANA_AGENT_HOST || this.INSTANA_AGENT_DEFAULT_HOST;
const port = Number(
Expand Down Expand Up @@ -112,7 +112,6 @@ class InstanaAgentDetector implements Detector {

try {
const data = JSON.parse(rawData);

if (data.pid && data.agentUuid) {
return resolve(data);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<void>(resolve => {
setTimeout(resolve, ms);
});

describe('[Integration] instanaAgentDetector', () => {
beforeEach(() => {
nock.disableNetConnect();
Expand All @@ -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',
Expand All @@ -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');
Expand Down

0 comments on commit d2d8288

Please sign in to comment.