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

feat(detector-instana)!: change implementation to DetectorSync interface #2337

Merged
merged 11 commits into from
Sep 7, 2024
Merged
Original file line number Diff line number Diff line change
Expand Up @@ -13,31 +13,44 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { Detector, Resource, IResource } from '@opentelemetry/resources';
import {
DetectorSync,
Resource,
IResource,
ResourceAttributes,
} from '@opentelemetry/resources';
import { diag } from '@opentelemetry/api';
import {
SEMRESATTRS_PROCESS_PID,
SEMRESATTRS_SERVICE_INSTANCE_ID,
} from '@opentelemetry/semantic-conventions';
import * as http from 'http';

class InstanaAgentDetector implements Detector {
class InstanaAgentDetector implements DetectorSync {
blumamir marked this conversation as resolved.
Show resolved Hide resolved
readonly INSTANA_AGENT_DEFAULT_HOST = 'localhost';
readonly INSTANA_AGENT_DEFAULT_PORT = 42699;

async detect(): Promise<IResource> {
detect(): IResource {
return new Resource({}, this._getAttributes());
}

private async _getAttributes(): Promise<ResourceAttributes> {
const host =
process.env.INSTANA_AGENT_HOST || this.INSTANA_AGENT_DEFAULT_HOST;
const port = Number(
process.env.INSTANA_AGENT_PORT || this.INSTANA_AGENT_DEFAULT_PORT
);

const data = await this._retryHandler(host, port, 0);
try {
const data = await this._retryHandler(host, port, 0);

return new Resource({
[SEMRESATTRS_PROCESS_PID]: data.pid,
[SEMRESATTRS_SERVICE_INSTANCE_ID]: data.agentUuid,
});
return {
[SEMRESATTRS_PROCESS_PID]: data.pid,
[SEMRESATTRS_SERVICE_INSTANCE_ID]: data.agentUuid,
};
} catch {
return {};
}
}

private timeout(ms: number) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@

import * as nock from 'nock';
import * as assert from 'assert';
import { Resource } from '@opentelemetry/resources';
import { instanaAgentDetector } from '../src';

describe('[UNIT] instanaAgentDetector', () => {
Expand Down Expand Up @@ -54,7 +53,8 @@ describe('[UNIT] instanaAgentDetector', () => {
.put('/com.instana.plugin.nodejs.discovery')
.reply(200, () => mockedReply);

const resource: Resource = await instanaAgentDetector.detect();
const resource = instanaAgentDetector.detect();
await resource.waitForAsyncAttributes?.();

scope.done();

Expand All @@ -80,7 +80,8 @@ describe('[UNIT] instanaAgentDetector', () => {
.put('/com.instana.plugin.nodejs.discovery')
.reply(200, () => mockedReply);

const resource: Resource = await instanaAgentDetector.detect();
const resource = instanaAgentDetector.detect();
await resource.waitForAsyncAttributes?.();

scope.done();

Expand All @@ -90,28 +91,24 @@ describe('[UNIT] instanaAgentDetector', () => {
});
});

it('agent throws error', async () => {
const expectedError = new Error('Instana Agent returned status code 500');
it('agent returns empty resource if request error', async () => {
const scope = nock('http://localhost:42699')
.persist()
.put('/com.instana.plugin.nodejs.discovery')
.reply(500, () => new Error());

try {
await instanaAgentDetector.detect();
assert.ok(false, 'Expected to throw');
} catch (err) {
assert.deepStrictEqual(err, expectedError);
}
const resource = instanaAgentDetector.detect();
await resource.waitForAsyncAttributes?.();

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

scope.done();
});

it('agent timeout', async () => {
it('agent return empty resource if timeout', async () => {
process.env.INSTANA_AGENT_PORT = '56002';
process.env.INSTANA_AGENT_HOST = 'instanaagent';
process.env.INSTANA_AGENT_TIMEOUT_MS = '200';
const expectedError = new Error('Instana Agent request timed out.');

nock(
`http://${process.env.INSTANA_AGENT_HOST}:${process.env.INSTANA_AGENT_PORT}`
Expand All @@ -121,28 +118,23 @@ describe('[UNIT] instanaAgentDetector', () => {
.delay(500)
.reply(200, {});

try {
await instanaAgentDetector.detect();
assert.ok(false, 'Expected to throw');
} catch (err) {
console.log(err);
assert.deepStrictEqual(err, expectedError);
}
const resource = instanaAgentDetector.detect();
await resource.waitForAsyncAttributes?.();

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

describe('when agent is not running', () => {
it('should not return agent resource', async () => {
it('should return empty resource', async () => {
process.env.INSTANA_AGENT_PORT = '1111';
process.env.INSTANA_AGENT_TIMEOUT_MS = '100';
process.env.INSTANA_RETRY_TIMEOUT_MS = '100';

try {
await instanaAgentDetector.detect();
assert.ok(false, 'Expected to throw');
} catch (err: any) {
assert.equal(err.code, 'ECONNREFUSED');
}
const resource = instanaAgentDetector.detect();
await resource.waitForAsyncAttributes?.();

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