Skip to content

Commit

Permalink
chore(detectors/node): updated examples for detecting resources
Browse files Browse the repository at this point in the history
  • Loading branch information
kirrg001 committed Apr 11, 2023
1 parent e6993b6 commit 385b777
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 3 deletions.
23 changes: 22 additions & 1 deletion detectors/node/opentelemetry-resource-detector-instana/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,28 @@ const globalResource = new Resource({
});

const sdk = new NodeSDK({
autoDetectResources: false,
resourceDetectors: [envDetector, processDetector, instanaAgentDetector],
resource: globalResource,
});

sdk.start()
```

```typescript
import {
Resource,
processDetector,
envDetector,
} from "@opentelemetry/resources";
import { SemanticResourceAttributes } from "@opentelemetry/semantic-conventions";
import { NodeSDK } from "@opentelemetry/sdk-node";
import { instanaAgentDetector } from "@opentelemetry/resource-detector-instana";

const globalResource = new Resource({
[SemanticResourceAttributes.SERVICE_NAME]: "TestService",
});

const sdk = new NodeSDK({
resourceDetectors: [envDetector, processDetector, instanaAgentDetector],
resource: globalResource,
});
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 Down Expand Up @@ -53,12 +58,11 @@ describe('[Integration] instanaAgentDetector', () => {
});

const sdk = new NodeSDK({
autoDetectResources: false,
resourceDetectors: [envDetector, processDetector, instanaAgentDetector],
resource: globalResource,
});

sdk.detectResources();
sdk.start();

const resource = sdk['_resource'];
// await sdk.detectResources(); [< @opentelemetry/sdk-node@0.37.0]
Expand All @@ -75,4 +79,41 @@ describe('[Integration] instanaAgentDetector', () => {

scope.done();
});

it('[autoDetectResources:true] 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');
assert.equal(
resource.attributes['service.instance.id'],
'14:7d:da:ff:fe:e4:08:d5'
);

scope.done();
});
});

0 comments on commit 385b777

Please sign in to comment.