Skip to content

Commit 594a998

Browse files
authored
feat(runtime): remove public OTEL trace API (#26854)
This PR removes the public Deno.tracing.Span API. We are not confident we can ship an API that is better than the `@opentelemetry/api` API, because V8 CPED does not support us using `using` to manage span context. If this changes, we can revisit this decision. For now, users wanting custom spans can instrument their code using the `@opentelemetry/api` API and `@deno/otel`. This PR also speeds up the OTEL trace generation by a 30% by using Uint8Array instead of strings for the trace ID and span ID.
1 parent 106d47a commit 594a998

File tree

22 files changed

+1079
-604
lines changed

22 files changed

+1079
-604
lines changed

cli/tsc/dts/lib.deno.unstable.d.ts

+44-79
Original file line numberDiff line numberDiff line change
@@ -1252,80 +1252,53 @@ declare namespace Deno {
12521252
}
12531253

12541254
/**
1255+
* **UNSTABLE**: New API, yet to be vetted.
1256+
*
1257+
* APIs for working with the OpenTelemetry observability framework. Deno can
1258+
* export traces, metrics, and logs to OpenTelemetry compatible backends via
1259+
* the OTLP protocol.
1260+
*
1261+
* Deno automatically instruments the runtime with OpenTelemetry traces and
1262+
* metrics. This data is exported via OTLP to OpenTelemetry compatible
1263+
* backends. User logs from the `console` API are exported as OpenTelemetry
1264+
* logs via OTLP.
1265+
*
1266+
* User code can also create custom traces, metrics, and logs using the
1267+
* OpenTelemetry API. This is done using the official OpenTelemetry package
1268+
* for JavaScript:
1269+
* [`npm:@opentelemetry/api`](https://opentelemetry.io/docs/languages/js/).
1270+
* Deno integrates with this package to provide trace context propagation
1271+
* between native Deno APIs (like `Deno.serve` or `fetch`) and custom user
1272+
* code. Deno also provides APIs that allow exporting custom telemetry data
1273+
* via the same OTLP channel used by the Deno runtime. This is done using the
1274+
* [`jsr:@deno/otel`](https://jsr.io/@deno/otel) package.
1275+
*
1276+
* @example Using OpenTelemetry API to create custom traces
1277+
* ```ts,ignore
1278+
* import { trace } from "npm:@opentelemetry/api@1";
1279+
* import "jsr:@deno/[email protected]/register";
1280+
*
1281+
* const tracer = trace.getTracer("example-tracer");
1282+
*
1283+
* async function doWork() {
1284+
* return tracer.startActiveSpan("doWork", async (span) => {
1285+
* span.setAttribute("key", "value");
1286+
* await new Promise((resolve) => setTimeout(resolve, 1000));
1287+
* span.end();
1288+
* });
1289+
* }
1290+
*
1291+
* Deno.serve(async (req) => {
1292+
* await doWork();
1293+
* const resp = await fetch("https://example.com");
1294+
* return resp;
1295+
* });
1296+
* ```
1297+
*
12551298
* @category Telemetry
12561299
* @experimental
12571300
*/
1258-
export namespace tracing {
1259-
/**
1260-
* Whether tracing is enabled.
1261-
* @category Telemetry
1262-
* @experimental
1263-
*/
1264-
export const enabled: boolean;
1265-
1266-
/**
1267-
* Allowed attribute type.
1268-
* @category Telemetry
1269-
* @experimental
1270-
*/
1271-
export type AttributeValue = string | number | boolean | bigint;
1272-
1273-
/**
1274-
* A tracing span.
1275-
* @category Telemetry
1276-
* @experimental
1277-
*/
1278-
export class Span implements Disposable {
1279-
readonly traceId: string;
1280-
readonly spanId: string;
1281-
readonly parentSpanId: string;
1282-
readonly kind: string;
1283-
readonly name: string;
1284-
readonly startTime: number;
1285-
readonly endTime: number;
1286-
readonly status: null | { code: 1 } | { code: 2; message: string };
1287-
readonly attributes: Record<string, AttributeValue>;
1288-
readonly traceFlags: number;
1289-
1290-
/**
1291-
* Construct a new Span and enter it as the "current" span.
1292-
*/
1293-
constructor(
1294-
name: string,
1295-
kind?: "internal" | "server" | "client" | "producer" | "consumer",
1296-
);
1297-
1298-
/**
1299-
* Set an attribute on this span.
1300-
*/
1301-
setAttribute(
1302-
name: string,
1303-
value: AttributeValue,
1304-
): void;
1305-
1306-
/**
1307-
* Enter this span as the "current" span.
1308-
*/
1309-
enter(): void;
1310-
1311-
/**
1312-
* Exit this span as the "current" span and restore the previous one.
1313-
*/
1314-
exit(): void;
1315-
1316-
/**
1317-
* End this span, and exit it as the "current" span.
1318-
*/
1319-
end(): void;
1320-
1321-
[Symbol.dispose](): void;
1322-
1323-
/**
1324-
* Get the "current" span, if one exists.
1325-
*/
1326-
static current(): Span | undefined | null;
1327-
}
1328-
1301+
export namespace telemetry {
13291302
/**
13301303
* A SpanExporter compatible with OpenTelemetry.js
13311304
* https://open-telemetry.github.io/opentelemetry-js/interfaces/_opentelemetry_sdk_trace_base.SpanExporter.html
@@ -1345,14 +1318,6 @@ declare namespace Deno {
13451318
export {}; // only export exports
13461319
}
13471320

1348-
/**
1349-
* @category Telemetry
1350-
* @experimental
1351-
*/
1352-
export namespace metrics {
1353-
export {}; // only export exports
1354-
}
1355-
13561321
export {}; // only export exports
13571322
}
13581323

ext/console/internal.d.ts

+3
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,7 @@ declare module "ext:deno_console/01_console.js" {
99
keys: (keyof TObject)[];
1010
evaluate: boolean;
1111
}): Record<string, unknown>;
12+
13+
class Console {
14+
}
1215
}

runtime/js/90_deno_ns.js

+2-3
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ import * as tty from "ext:runtime/40_tty.js";
2929
import * as kv from "ext:deno_kv/01_db.ts";
3030
import * as cron from "ext:deno_cron/01_cron.ts";
3131
import * as webgpuSurface from "ext:deno_webgpu/02_surface.js";
32-
import * as telemetry from "ext:runtime/telemetry.js";
32+
import * as telemetry from "ext:runtime/telemetry.ts";
3333

3434
const denoNs = {
3535
Process: process.Process,
@@ -185,8 +185,7 @@ denoNsUnstableById[unstableIds.webgpu] = {
185185
// denoNsUnstableById[unstableIds.workerOptions] = { __proto__: null }
186186

187187
denoNsUnstableById[unstableIds.otel] = {
188-
tracing: telemetry.tracing,
189-
metrics: telemetry.metrics,
188+
telemetry: telemetry.telemetry,
190189
};
191190

192191
export { denoNs, denoNsUnstableById, unstableIds };

runtime/js/99_main.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ import {
8686
workerRuntimeGlobalProperties,
8787
} from "ext:runtime/98_global_scope_worker.js";
8888
import { SymbolDispose, SymbolMetadata } from "ext:deno_web/00_infra.js";
89-
import { bootstrap as bootstrapOtel } from "ext:runtime/telemetry.js";
89+
import { bootstrap as bootstrapOtel } from "ext:runtime/telemetry.ts";
9090

9191
// deno-lint-ignore prefer-primordials
9292
if (Symbol.metadata) {

0 commit comments

Comments
 (0)