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

Only capture hostname if the port is known #508

Merged
merged 1 commit into from
Jan 24, 2025
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
2 changes: 1 addition & 1 deletion library/agent/Agent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -485,7 +485,7 @@ export class Agent {
this.logger.log(`Failed to wrap file ${filename} in module ${module}`);
}

onConnectHostname(hostname: string, port: number | undefined) {
onConnectHostname(hostname: string, port: number) {
this.hostnames.add(hostname, port);
}

Expand Down
9 changes: 5 additions & 4 deletions library/agent/Hostnames.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,23 +21,24 @@ t.test("it works", async () => {
{ hostname: "google.com", port: 80, hits: 1 },
]);

hostnames.add("google.com", undefined);
hostnames.add("google.com", 0);
hostnames.add("google.com", -1);
t.same(hostnames.asArray(), [
{ hostname: "aikido.dev", port: 443, hits: 1 },
{ hostname: "aikido.dev", port: 80, hits: 1 },
{ hostname: "google.com", port: 80, hits: 1 },
{ hostname: "google.com", port: undefined, hits: 1 },
]);

hostnames.add("github.com", 80);
t.same(hostnames.asArray(), [
{ hostname: "aikido.dev", port: 80, hits: 1 },
{ hostname: "google.com", port: 80, hits: 1 },
{ hostname: "google.com", port: undefined, hits: 1 },
{ hostname: "github.com", port: 80, hits: 1 },
]);

hostnames.add("jetbrains.com", 80);
t.same(hostnames.asArray(), [
{ hostname: "google.com", port: undefined, hits: 1 },
{ hostname: "google.com", port: 80, hits: 1 },
{ hostname: "github.com", port: 80, hits: 1 },
{ hostname: "jetbrains.com", port: 80, hits: 1 },
]);
Expand Down
8 changes: 6 additions & 2 deletions library/agent/Hostnames.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,11 @@ export class Hostnames {

constructor(private readonly maxEntries: number = 200) {}

add(hostname: string, port: number | undefined = -1) {
add(hostname: string, port: number) {
if (port <= 0) {
return;
}

if (!this.map.has(hostname)) {
this.map.set(hostname, new Map([[port, 1]]));
} else {
Expand Down Expand Up @@ -46,7 +50,7 @@ export class Hostnames {
Array.from(ports.entries()).map(([port, hits]) => {
return {
hostname,
port: port === -1 ? undefined : port,
port,
hits,
};
})
Expand Down
4 changes: 3 additions & 1 deletion library/sinks/Fetch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@ export class Fetch implements Wrapper {
): InterceptorResult {
// Let the agent know that we are connecting to this hostname
// This is to build a list of all hostnames that the application is connecting to
agent.onConnectHostname(hostname, port);
if (typeof port === "number" && port > 0) {
agent.onConnectHostname(hostname, port);
}
const context = getContext();

if (!context) {
Expand Down
4 changes: 3 additions & 1 deletion library/sinks/HTTPRequest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@ export class HTTPRequest implements Wrapper {
): InterceptorResult {
// Let the agent know that we are connecting to this hostname
// This is to build a list of all hostnames that the application is connecting to
agent.onConnectHostname(url.hostname, port);
if (typeof port === "number" && port > 0) {
agent.onConnectHostname(url.hostname, port);
}
const context = getContext();

if (!context) {
Expand Down
4 changes: 3 additions & 1 deletion library/sinks/Undici.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,9 @@ export class Undici implements Wrapper {
): InterceptorResult {
// Let the agent know that we are connecting to this hostname
// This is to build a list of all hostnames that the application is connecting to
agent.onConnectHostname(hostname, port);
if (typeof port === "number" && port > 0) {
agent.onConnectHostname(hostname, port);
}
const context = getContext();

if (!context) {
Expand Down
Loading