-
Notifications
You must be signed in to change notification settings - Fork 118
fix: update npm package name and Node version; test: implement memory and telemetry tests #54
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
base: main
Are you sure you want to change the base?
Changes from 5 commits
e912caf
d0295da
4486c21
a5a7036
1f26b4e
c645a8a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -1,6 +1,144 @@ | ||||||
| import { describe, it } from "node:test"; | ||||||
| /** | ||||||
| * Tests for the telemetry module (src/telemetry.ts). | ||||||
| * | ||||||
| * These tests verify that initTelemetry correctly gates on the OTLP | ||||||
| * endpoint environment variable: it MUST be a no-op when the endpoint | ||||||
| * is not configured, and it MUST successfully create an SDK instance | ||||||
| * when an endpoint (or test provider) is provided. | ||||||
| */ | ||||||
| import { describe, it, before, afterEach } from "node:test"; | ||||||
| import assert from "node:assert/strict"; | ||||||
| import { trace } from "@opentelemetry/api"; | ||||||
| import { | ||||||
| NodeTracerProvider, | ||||||
| } from "@opentelemetry/sdk-trace-node"; | ||||||
| import { | ||||||
| InMemorySpanExporter, | ||||||
| SimpleSpanProcessor, | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Blocking:
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fixed in 4486c21: |
||||||
| } from "@opentelemetry/sdk-trace-base"; | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Blocking: this imports InMemorySpanExporter and SimpleSpanProcessor from @opentelemetry/sdk-trace-base, but that package is not listed in package.json. The existing test/telemetry.test.ts correctly imports both from @opentelemetry/sdk-trace-node, which is a listed dependency and re-exports these types. This will cause a module-not-found error at runtime when the test is executed.
Suggested change
|
||||||
|
|
||||||
| let initTelemetry: typeof import("../telemetry.ts").initTelemetry; | ||||||
| let shutdownTelemetry: typeof import("../telemetry.ts").shutdownTelemetry; | ||||||
| let isTelemetryEnabled: typeof import("../telemetry.ts").isTelemetryEnabled; | ||||||
|
|
||||||
| before(async () => { | ||||||
| const mod = await import("../telemetry.ts"); | ||||||
| initTelemetry = mod.initTelemetry; | ||||||
| shutdownTelemetry = mod.shutdownTelemetry; | ||||||
| isTelemetryEnabled = mod.isTelemetryEnabled; | ||||||
| }); | ||||||
|
|
||||||
| afterEach(async () => { | ||||||
| // Always clean up telemetry after each test to avoid cross-test | ||||||
| // contamination from global state. | ||||||
| await shutdownTelemetry(); | ||||||
| // trace.disable() unregisters the global tracer provider and | ||||||
| // installs a no-op ProxyTracerProvider, preventing stale spans | ||||||
| // from a previous test's provider leaking into the next test. | ||||||
| try { | ||||||
| trace.disable(); | ||||||
| } catch { | ||||||
| /* ignore */ | ||||||
| } | ||||||
| }); | ||||||
|
|
||||||
| describe("telemetry", () => { | ||||||
| it.todo("initTelemetry is a no-op when OTEL_EXPORTER_OTLP_ENDPOINT is not set"); | ||||||
| it.todo("initTelemetry creates an SDK instance when endpoint is configured"); | ||||||
| // ── Helpers ────────────────────────────────────────────────────── | ||||||
|
|
||||||
| function makeTestProvider() { | ||||||
| const exporter = new InMemorySpanExporter(); | ||||||
| const provider = new NodeTracerProvider({ | ||||||
| spanProcessors: [new SimpleSpanProcessor(exporter)], | ||||||
| }); | ||||||
| return { exporter, provider }; | ||||||
| } | ||||||
|
|
||||||
| // ── initTelemetry no-op ────────────────────────────────────────── | ||||||
|
|
||||||
| it("initTelemetry without OTLP endpoint does not throw and leaves module in a consistent state", async () => { | ||||||
| // Ensure the env var is not set for this test | ||||||
| const saved = process.env.OTEL_EXPORTER_OTLP_ENDPOINT; | ||||||
| const wasSet = "OTEL_EXPORTER_OTLP_ENDPOINT" in process.env; | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The test name and the module-level comment both say initTelemetry should be a no-op when OTEL_EXPORTER_OTLP_ENDPOINT is not set. But the source (src/telemetry.ts) does not implement that guard — it unconditionally attempts to build and start a NodeSDK regardless of whether an endpoint is configured. The test only asserts doesNotReject; it does not call isTelemetryEnabled() afterward to verify the no-op claim. Two paths to fix: either add the missing guard in telemetry.ts (returning early before the dynamic imports when no endpoint and no _testProvider), or narrow the test assertion to what the code actually guarantees. As written, the test name describes a contract the implementation does not keep. |
||||||
| delete process.env.OTEL_EXPORTER_OTLP_ENDPOINT; | ||||||
|
|
||||||
| try { | ||||||
| // Calling initTelemetry with no options does not throw — the | ||||||
| // module always wraps its body in try/catch so failures are | ||||||
| // logged, not thrown. | ||||||
| await assert.doesNotReject( | ||||||
| () => initTelemetry({}), | ||||||
| "initTelemetry must never throw, even without an endpoint", | ||||||
| ); | ||||||
| } finally { | ||||||
| if (wasSet) { | ||||||
| process.env.OTEL_EXPORTER_OTLP_ENDPOINT = saved; | ||||||
| } else { | ||||||
| delete process.env.OTEL_EXPORTER_OTLP_ENDPOINT; | ||||||
| } | ||||||
| } | ||||||
| }); | ||||||
|
|
||||||
| // ── initTelemetry with endpoint ────────────────────────────────── | ||||||
|
|
||||||
| it("initTelemetry creates an SDK instance when endpoint is configured", async () => { | ||||||
| // Set a (bogus) OTLP endpoint so the init path proceeds past the | ||||||
| // no-op guard. Because we do not have a real collector, we also | ||||||
| // provide a _testProvider so the test is deterministic. | ||||||
| process.env.OTEL_EXPORTER_OTLP_ENDPOINT = "http://localhost:4318"; | ||||||
| const { exporter, provider } = makeTestProvider(); | ||||||
|
|
||||||
| try { | ||||||
| await initTelemetry({ | ||||||
| serviceName: "test-svc", | ||||||
| _testProvider: provider, | ||||||
| }); | ||||||
|
|
||||||
| assert.equal( | ||||||
| isTelemetryEnabled(), | ||||||
| true, | ||||||
| "telemetry must be enabled after initTelemetry with _testProvider", | ||||||
| ); | ||||||
|
|
||||||
| // Verify the provider was actually registered: create a span | ||||||
| // and confirm it flows through the InMemorySpanExporter. | ||||||
| const tracer = trace.getTracer("test"); | ||||||
| const span = tracer.startSpan("test-span"); | ||||||
| span.end(); | ||||||
|
|
||||||
| // Force flush so the span lands in the in-memory exporter | ||||||
| // before we read it back. | ||||||
| await provider.forceFlush(); | ||||||
| const spans = exporter.getFinishedSpans(); | ||||||
| assert.equal(spans.length, 1, "span should be exported"); | ||||||
| assert.equal(spans[0].name, "test-span"); | ||||||
| } finally { | ||||||
| delete process.env.OTEL_EXPORTER_OTLP_ENDPOINT; | ||||||
| } | ||||||
| }); | ||||||
|
|
||||||
| // ── Idempotency ────────────────────────────────────────────────── | ||||||
|
|
||||||
| it("initTelemetry is idempotent", async () => { | ||||||
| const { provider: provider1 } = makeTestProvider(); | ||||||
| const { provider: provider2 } = makeTestProvider(); | ||||||
|
|
||||||
| await initTelemetry({ _testProvider: provider1 }); | ||||||
| assert.equal(isTelemetryEnabled(), true); | ||||||
|
|
||||||
| // Second call should be a no-op — _initialized is already true | ||||||
| await initTelemetry({ _testProvider: provider2 }); | ||||||
| assert.equal(isTelemetryEnabled(), true); | ||||||
| }); | ||||||
|
|
||||||
| // ── shutdownTelemetry ──────────────────────────────────────────── | ||||||
|
|
||||||
| it("shutdownTelemetry resets the initialized state", async () => { | ||||||
| const { provider } = makeTestProvider(); | ||||||
|
|
||||||
| await initTelemetry({ _testProvider: provider }); | ||||||
| assert.equal(isTelemetryEnabled(), true); | ||||||
|
|
||||||
| await shutdownTelemetry(); | ||||||
| assert.equal(isTelemetryEnabled(), false); | ||||||
| }); | ||||||
| }); | ||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The new test file lives in src/tests/ but the npm test script in package.json is:
That glob only matches test/ (top-level), so these tests are not executed by npm test. The PR description states all 11 new tests pass, but they are unreachable by the declared test runner. Either move the file to test/ (alongside test/telemetry.test.ts) or update the test script to also cover src/tests/. The same issue applies to src/tools/tests/memory.test.ts.