Skip to content

Commit

Permalink
chore: fixed linting
Browse files Browse the repository at this point in the history
  • Loading branch information
kirrg001 committed Jul 15, 2022
1 parent b2f2698 commit 8cb7959
Show file tree
Hide file tree
Showing 5 changed files with 86 additions and 86 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,12 @@ import {
Detector,
Resource,
ResourceDetectionConfig,
} from "@opentelemetry/resources";
import { SemanticResourceAttributes } from "@opentelemetry/semantic-conventions";
import * as http from "http";
} from '@opentelemetry/resources';
import { SemanticResourceAttributes } from '@opentelemetry/semantic-conventions';
import * as http from 'http';

class InstanaAgentDetector implements Detector {
readonly INSTANA_AGENT_DEFAULT_HOST = "localhost";
readonly INSTANA_AGENT_DEFAULT_HOST = 'localhost';
readonly INSTANA_AGENT_DEFAULT_PORT = 42699;

async detect(_config?: ResourceDetectionConfig): Promise<Resource> {
Expand All @@ -37,12 +37,16 @@ class InstanaAgentDetector implements Detector {

return new Resource({
[SemanticResourceAttributes.PROCESS_PID]: data.pid,
"instana.host.id": data.agentUuid,
'instana.host.id': data.agentUuid,
// NOTE: The agent reads this property to correlate the process with the service
"service.instance.id": data.pid.toString(),
'service.instance.id': data.pid.toString(),
});
}

private timeout(ms: number) {
return new Promise(resolve => setTimeout(resolve, ms));
}

private async _retryHandler(
host: string,
port: number,
Expand All @@ -58,15 +62,11 @@ class InstanaAgentDetector implements Detector {
`Retrying to connect to the Instana agent on ${host}:${port}....`
);

function timeout(ms: number) {
return new Promise((resolve) => setTimeout(resolve, ms));
}

const retryTimeout = process.env.INSTANA_RETRY_TIMEOUT
? Number(process.env.INSTANA_RETRY_TIMEOUT)
: 1000;

await timeout(retryTimeout);
await this.timeout(retryTimeout);
return await this._retryHandler(host, port, tries + 1);
}

Expand All @@ -83,35 +83,35 @@ class InstanaAgentDetector implements Detector {
pid: process.pid,
};

let payloadStr = JSON.stringify(payload);
const contentLength = Buffer.from(payloadStr, "utf8").length;
const payloadStr = JSON.stringify(payload);
const contentLength = Buffer.from(payloadStr, 'utf8').length;

return new Promise(function (resolve, reject) {
return new Promise((resolve, reject) => {
// agent needs more than 1s!
const timeoutId = setTimeout(() => {
req.destroy();
reject(new Error("Instana Agent request timed out."));
reject(new Error('Instana Agent request timed out.'));
}, agentTimeout);

const req = http.request(
{
host,
port,
method: "PUT",
path: "/com.instana.plugin.nodejs.discovery",
method: 'PUT',
path: '/com.instana.plugin.nodejs.discovery',
headers: {
Accept: "application/json",
"Content-Type": "application/json; charset=UTF-8",
"Content-Length": contentLength,
Accept: 'application/json',
'Content-Type': 'application/json; charset=UTF-8',
'Content-Length': contentLength,
},
},
(res) => {
res => {
clearTimeout(timeoutId);
res.setEncoding("utf8");
let rawData = "";
res.setEncoding('utf8');
let rawData = '';

res.on("data", (chunk) => (rawData += chunk));
res.on("end", () => {
res.on('data', chunk => (rawData += chunk));
res.on('end', () => {
if (
res.statusCode &&
res.statusCode >= 200 &&
Expand All @@ -124,19 +124,19 @@ class InstanaAgentDetector implements Detector {
}
} else {
return reject(
new Error("Failed to load page, status code: " + res.statusCode)
new Error('Failed to load page, status code: ' + res.statusCode)
);
}
});
}
);

req.on("error", (err) => {
req.on('error', err => {
clearTimeout(timeoutId);
reject(err);
});

req.write(Buffer.from(payloadStr), "utf8");
req.write(Buffer.from(payloadStr), 'utf8');
req.end();
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,4 @@
* limitations under the License.
*/

export * from "./InstanaAgentDetector";
export * from './InstanaAgentDetector';
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,4 @@
* limitations under the License.
*/

export * from "./detectors";
export * from './detectors';
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,18 @@
* limitations under the License.
*/

import * as nock from "nock";
import * as assert from "assert";
import * as nock from 'nock';
import * as assert from 'assert';
import {
Resource,
processDetector,
envDetector,
} from "@opentelemetry/resources";
import { SemanticResourceAttributes } from "@opentelemetry/semantic-conventions";
import { NodeSDK } from "@opentelemetry/sdk-node";
import { instanaAgentDetector } from "../src";
} from '@opentelemetry/resources';
import { SemanticResourceAttributes } from '@opentelemetry/semantic-conventions';
import { NodeSDK } from '@opentelemetry/sdk-node';
import { instanaAgentDetector } from '../src';

describe("[Integration] instanaAgentDetector", () => {
describe('[Integration] instanaAgentDetector', () => {
beforeEach(() => {
nock.disableNetConnect();
nock.cleanAll();
Expand All @@ -36,18 +36,18 @@ describe("[Integration] instanaAgentDetector", () => {
nock.cleanAll();
});

it("should return merged resource", async () => {
it('should return merged resource', async () => {
const mockedReply = {
pid: 123,
agentUuid: "14:7d:da:ff:fe:e4:08:d5",
agentUuid: '14:7d:da:ff:fe:e4:08:d5',
};

const scope = nock("http://localhost:42699")
const scope = nock('http://localhost:42699')
.persist()
.put("/com.instana.plugin.nodejs.discovery")
.put('/com.instana.plugin.nodejs.discovery')
.reply(200, () => mockedReply);

const serviceName = "TestService";
const serviceName = 'TestService';
const globalResource = new Resource({
[SemanticResourceAttributes.SERVICE_NAME]: serviceName,
});
Expand All @@ -62,14 +62,14 @@ describe("[Integration] instanaAgentDetector", () => {
detectors: [envDetector, processDetector, instanaAgentDetector],
});

const resource = sdk["_resource"];
const resource = sdk['_resource'];

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['process.pid'], 123);
assert.equal(resource.attributes['process.runtime.name'], 'nodejs');
assert.equal(resource.attributes['service.name'], 'TestService');
assert.equal(
resource.attributes["instana.host.id"],
"14:7d:da:ff:fe:e4:08:d5"
resource.attributes['instana.host.id'],
'14:7d:da:ff:fe:e4:08:d5'
);

scope.done();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,15 @@
* limitations under the License.
*/

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

describe("[UNIT] instanaAgentDetector", () => {
describe("when agent is running", function () {
describe('[UNIT] instanaAgentDetector', () => {
describe('when agent is running', () => {
before(() => {
process.env.INSTANA_RETRY_TIMEOUT = "100";
process.env.INSTANA_RETRY_TIMEOUT = '100';
});

beforeEach(() => {
Expand All @@ -35,89 +35,89 @@ describe("[UNIT] instanaAgentDetector", () => {
nock.cleanAll();
});

it("should return agent resource with default host/port", async () => {
it('should return agent resource with default host/port', async () => {
const mockedReply = {
pid: 123,
agentUuid: "14:7d:da:ff:fe:e4:08:d5",
agentUuid: '14:7d:da:ff:fe:e4:08:d5',
};

const scope = nock("http://localhost:42699")
const scope = nock('http://localhost:42699')
.persist()
.put("/com.instana.plugin.nodejs.discovery")
.put('/com.instana.plugin.nodejs.discovery')
.reply(200, () => mockedReply);

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

scope.done();

assert.deepEqual(resource.attributes, {
"process.pid": 123,
"instana.host.id": "14:7d:da:ff:fe:e4:08:d5",
"service.instance.id": "123",
'process.pid': 123,
'instana.host.id': '14:7d:da:ff:fe:e4:08:d5',
'service.instance.id': '123',
});
});

it("should return agent resource with env variables", async () => {
process.env.INSTANA_AGENT_PORT = "88866";
process.env.INSTANA_AGENT_HOST = "instanaagent";
it('should return agent resource with env variables', async () => {
process.env.INSTANA_AGENT_PORT = '88866';
process.env.INSTANA_AGENT_HOST = 'instanaagent';

const mockedReply = {
pid: 222,
agentUuid: "14:7d:da:ff:fe:e4:08:d5",
agentUuid: '14:7d:da:ff:fe:e4:08:d5',
};

const scope = nock(
`http://${process.env.INSTANA_AGENT_HOST}:${process.env.INSTANA_AGENT_PORT}`
)
.persist()
.put("/com.instana.plugin.nodejs.discovery")
.put('/com.instana.plugin.nodejs.discovery')
.reply(200, () => mockedReply);

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

scope.done();

assert.deepEqual(resource.attributes, {
"process.pid": 222,
"instana.host.id": "14:7d:da:ff:fe:e4:08:d5",
"service.instance.id": "222",
'process.pid': 222,
'instana.host.id': '14:7d:da:ff:fe:e4:08:d5',
'service.instance.id': '222',
});

delete process.env.INSTANA_AGENT_PORT;
delete process.env.INSTANA_AGENT_HOST;
});

it("agent throws error", async () => {
const expectedError = new Error("Failed to load page, status code: 500");
const scope = nock("http://localhost:42699")
it('agent throws error', async () => {
const expectedError = new Error('Failed to load page, status code: 500');
const scope = nock('http://localhost:42699')
.persist()
.put("/com.instana.plugin.nodejs.discovery")
.put('/com.instana.plugin.nodejs.discovery')
.reply(500, () => new Error());

try {
await instanaAgentDetector.detect();
assert.ok(false, "Expected to throw");
assert.ok(false, 'Expected to throw');
} catch (err) {
assert.deepStrictEqual(err, expectedError);
}

scope.done();
});

it("agent timeout", async () => {
process.env.INSTANA_AGENT_TIMEOUT = "200";
const expectedError = new Error("Instana Agent request timed out.");
it('agent timeout', async () => {
process.env.INSTANA_AGENT_TIMEOUT = '200';
const expectedError = new Error('Instana Agent request timed out.');

nock("http://localhost:42699")
nock('http://localhost:42699')
.persist()
.put("/com.instana.plugin.nodejs.discovery")
.put('/com.instana.plugin.nodejs.discovery')
.reply((uri, requestBody, cb) => {
setTimeout(() => cb(null, [200, {}]), 500);
});

try {
await instanaAgentDetector.detect();
assert.ok(false, "Expected to throw");
assert.ok(false, 'Expected to throw');
} catch (err) {
assert.deepStrictEqual(err, expectedError);
}
Expand All @@ -126,15 +126,15 @@ describe("[UNIT] instanaAgentDetector", () => {
});
});

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

try {
await instanaAgentDetector.detect();
assert.ok(false, "Expected to throw");
assert.ok(false, 'Expected to throw');
} catch (err) {
assert.equal(err.code, "ECONNREFUSED");
assert.equal(err.code, 'ECONNREFUSED');
}

delete process.env.INSTANA_AGENT_PORT;
Expand Down

0 comments on commit 8cb7959

Please sign in to comment.