-
Notifications
You must be signed in to change notification settings - Fork 115
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
Open
Oxygen56
wants to merge
5
commits into
open-gitagent:main
Choose a base branch
from
Oxygen56:fix/install-sh-and-tests
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+343
−15
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
e912caf
fix: correct npm package name, Node version check, and doc versions
Oxygen56 d0295da
test: implement unit tests for memory and telemetry modules
Oxygen56 4486c21
fix: address review feedback — import paths, forceFlush, git config s…
Oxygen56 a5a7036
fix: use 'KEY' in process.env pattern for env var restoration
Oxygen56 1f26b4e
merge: resolve conflicts with main
Oxygen56 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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, | ||
| } from "@opentelemetry/sdk-trace-base"; | ||
|
|
||
| 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; | ||
| 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); | ||
| }); | ||
| }); | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Blocking:
InMemorySpanExporterandSimpleSpanProcessorare not exported from@opentelemetry/sdk-trace-node. Import from@opentelemetry/sdk-trace-baseinstead.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.
Fixed in 4486c21:
InMemorySpanExporterandSimpleSpanProcessornow import from@opentelemetry/sdk-trace-base, whileNodeTracerProviderstays in@opentelemetry/sdk-trace-node.