Skip to content
Open
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
16 changes: 11 additions & 5 deletions src/probe.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,22 @@ export type ProbeResult = { ok: boolean; ms: number; error?: string }
* Opens a TCP connection to the host and port parsed from `endpoint` to verify
* reachability before the OTel SDK initialises. Resolves within 5 seconds.
*/
export function probeEndpoint(endpoint: string): Promise<ProbeResult> {
let host: string
let port: number
export function parseEndpoint(endpoint: string): { host: string; port: number } | null {
try {
const url = new URL(endpoint)
host = url.hostname
port = parseInt(url.port || "4317", 10)
const defaultPort = url.protocol === "http:" ? 80 : url.protocol === "https:" ? 443 : 4317
return { host: url.hostname, port: url.port ? parseInt(url.port, 10) : defaultPort }
} catch {
return null
}
}

export function probeEndpoint(endpoint: string): Promise<ProbeResult> {
const parsed = parseEndpoint(endpoint)
if (!parsed) {
return Promise.resolve({ ok: false, ms: 0, error: `invalid endpoint URL: ${endpoint}` })
}
const { host, port } = parsed
return new Promise((resolve) => {
const start = Date.now()
const socket = net.createConnection({ host, port }, () => {
Expand Down
24 changes: 23 additions & 1 deletion tests/probe.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,27 @@
import { describe, test, expect } from "bun:test"
import { probeEndpoint } from "../src/probe.ts"
import { probeEndpoint, parseEndpoint } from "../src/probe.ts"

describe("parseEndpoint", () => {
test("uses port 80 for http:// URLs without explicit port", () => {
expect(parseEndpoint("http://api.honeycomb.io")).toEqual({ host: "api.honeycomb.io", port: 80 })
})

test("uses port 443 for https:// URLs without explicit port", () => {
expect(parseEndpoint("https://api.honeycomb.io")).toEqual({ host: "api.honeycomb.io", port: 443 })
})

test("uses explicit port when provided", () => {
expect(parseEndpoint("http://localhost:4317")).toEqual({ host: "localhost", port: 4317 })
})

test("defaults to 4317 for unknown protocols without explicit port", () => {
expect(parseEndpoint("grpc://api.honeycomb.io")).toEqual({ host: "api.honeycomb.io", port: 4317 })
})

test("returns null for invalid URLs", () => {
expect(parseEndpoint("not a url")).toBeNull()
})
})

describe("probeEndpoint", () => {
test("returns error for malformed URL (no scheme)", async () => {
Expand Down