Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(detectors/node): adapted breaking change in @opentelemetry/sdk-node@0.37.0 #1462

Merged
merged 3 commits into from
Apr 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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