summaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
authorsnek <snek@deno.com>2024-11-13 11:38:46 +0100
committerGitHub <noreply@github.com>2024-11-13 10:38:46 +0000
commitaa546189be730163ee5370029e4dfdb3b454ab96 (patch)
tree4407643e6908f82c9ac31d9ae5faf04b3ab8d413 /tests
parent7becd83a3828b35331d0fcb82c64146e520f154b (diff)
feat: OpenTelemetry Tracing API and Exporting (#26710)
Initial import of OTEL code supporting tracing. Metrics soon to come. Implements APIs for https://jsr.io/@deno/otel so that code using OpenTelemetry.js just works tm. There is still a lot of work to do with configuration and adding built-in tracing to core APIs, which will come in followup PRs. --------- Co-authored-by: Luca Casonato <hello@lcas.dev>
Diffstat (limited to 'tests')
-rw-r--r--tests/specs/cli/otel_basic/__test__.jsonc4
-rw-r--r--tests/specs/cli/otel_basic/child.ts20
-rw-r--r--tests/specs/cli/otel_basic/deno.json4
-rw-r--r--tests/specs/cli/otel_basic/main.ts76
4 files changed, 104 insertions, 0 deletions
diff --git a/tests/specs/cli/otel_basic/__test__.jsonc b/tests/specs/cli/otel_basic/__test__.jsonc
new file mode 100644
index 000000000..a9d4fff04
--- /dev/null
+++ b/tests/specs/cli/otel_basic/__test__.jsonc
@@ -0,0 +1,4 @@
+{
+ "args": "run -A main.ts",
+ "output": "processed\n"
+}
diff --git a/tests/specs/cli/otel_basic/child.ts b/tests/specs/cli/otel_basic/child.ts
new file mode 100644
index 000000000..72cffd9f0
--- /dev/null
+++ b/tests/specs/cli/otel_basic/child.ts
@@ -0,0 +1,20 @@
+// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
+
+async function inner() {
+ using _span = new Deno.tracing.Span("inner span");
+ console.log("log 1");
+ await 1;
+ console.log("log 2");
+}
+
+Deno.serve({
+ port: 0,
+ onListen({ port }) {
+ console.log(port.toString());
+ },
+ handler: async (_req) => {
+ using _span = new Deno.tracing.Span("outer span");
+ await inner();
+ return new Response(null, { status: 200 });
+ },
+});
diff --git a/tests/specs/cli/otel_basic/deno.json b/tests/specs/cli/otel_basic/deno.json
new file mode 100644
index 000000000..105514e13
--- /dev/null
+++ b/tests/specs/cli/otel_basic/deno.json
@@ -0,0 +1,4 @@
+{
+ "lock": false,
+ "importMap": "../../../../import_map.json"
+}
diff --git a/tests/specs/cli/otel_basic/main.ts b/tests/specs/cli/otel_basic/main.ts
new file mode 100644
index 000000000..66ef5c79c
--- /dev/null
+++ b/tests/specs/cli/otel_basic/main.ts
@@ -0,0 +1,76 @@
+// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
+
+import { assert, assertEquals } from "@std/assert";
+import { TextLineStream } from "@std/streams/text-line-stream";
+
+const logs = [];
+const spans = [];
+let child: Deno.ChildProcess;
+
+Deno.serve(
+ {
+ port: 0,
+ async onListen({ port }) {
+ const command = new Deno.Command(Deno.execPath(), {
+ args: ["run", "-A", "--unstable-otel", "child.ts"],
+ env: {
+ OTEL_EXPORTER_OTLP_PROTOCOL: "http/json",
+ OTEL_EXPORTER_OTLP_ENDPOINT: `http://localhost:${port}`,
+ OTEL_BSP_SCHEDULE_DELAY: "10",
+ OTEL_BLRP_SCHEDULE_DELAY: "10",
+ },
+ stdin: "piped",
+ stdout: "piped",
+ stderr: "inherit",
+ });
+ child = command.spawn();
+ const lines = child.stdout
+ .pipeThrough(new TextDecoderStream())
+ .pipeThrough(new TextLineStream())
+ .getReader();
+ const line = await lines.read();
+ await fetch(`http://localhost:${line.value}/`);
+ },
+ async handler(req) {
+ try {
+ const body = await req.json();
+ if (body.resourceLogs) {
+ logs.push(...body.resourceLogs[0].scopeLogs[0].logRecords);
+ }
+ if (body.resourceSpans) {
+ spans.push(...body.resourceSpans[0].scopeSpans[0].spans);
+ }
+
+ if (logs.length > 2 && spans.length > 1) {
+ child.kill();
+
+ const inner = spans.find((s) => s.name === "inner span");
+ const outer = spans.find((s) => s.name === "outer span");
+
+ assertEquals(inner.traceId, outer.traceId);
+ assertEquals(inner.parentSpanId, outer.spanId);
+
+ assertEquals(logs[1].body.stringValue, "log 1\n");
+ assertEquals(logs[1].traceId, inner.traceId);
+ assertEquals(logs[1].spanId, inner.spanId);
+
+ assertEquals(logs[2].body.stringValue, "log 2\n");
+ assertEquals(logs[2].traceId, inner.traceId);
+ assertEquals(logs[2].spanId, inner.spanId);
+
+ console.log("processed");
+ Deno.exit(0);
+ }
+
+ return Response.json({ partialSuccess: {} }, { status: 200 });
+ } catch (e) {
+ console.error(e);
+ Deno.exit(1);
+ }
+ },
+ },
+);
+
+setTimeout(() => {
+ assert(false, "test did not finish in time");
+}, 10e3);