Skip to content

Commit

Permalink
feat(runtime): remove public OTEL trace API (denoland#26854)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
lucacasonato authored Nov 18, 2024
1 parent 106d47a commit 594a998
Show file tree
Hide file tree
Showing 22 changed files with 1,079 additions and 604 deletions.
123 changes: 44 additions & 79 deletions cli/tsc/dts/lib.deno.unstable.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1252,80 +1252,53 @@ declare namespace Deno {
}

/**
* **UNSTABLE**: New API, yet to be vetted.
*
* APIs for working with the OpenTelemetry observability framework. Deno can
* export traces, metrics, and logs to OpenTelemetry compatible backends via
* the OTLP protocol.
*
* Deno automatically instruments the runtime with OpenTelemetry traces and
* metrics. This data is exported via OTLP to OpenTelemetry compatible
* backends. User logs from the `console` API are exported as OpenTelemetry
* logs via OTLP.
*
* User code can also create custom traces, metrics, and logs using the
* OpenTelemetry API. This is done using the official OpenTelemetry package
* for JavaScript:
* [`npm:@opentelemetry/api`](https://opentelemetry.io/docs/languages/js/).
* Deno integrates with this package to provide trace context propagation
* between native Deno APIs (like `Deno.serve` or `fetch`) and custom user
* code. Deno also provides APIs that allow exporting custom telemetry data
* via the same OTLP channel used by the Deno runtime. This is done using the
* [`jsr:@deno/otel`](https://jsr.io/@deno/otel) package.
*
* @example Using OpenTelemetry API to create custom traces
* ```ts,ignore
* import { trace } from "npm:@opentelemetry/api@1";
* import "jsr:@deno/[email protected]/register";
*
* const tracer = trace.getTracer("example-tracer");
*
* async function doWork() {
* return tracer.startActiveSpan("doWork", async (span) => {
* span.setAttribute("key", "value");
* await new Promise((resolve) => setTimeout(resolve, 1000));
* span.end();
* });
* }
*
* Deno.serve(async (req) => {
* await doWork();
* const resp = await fetch("https://example.com");
* return resp;
* });
* ```
*
* @category Telemetry
* @experimental
*/
export namespace tracing {
/**
* Whether tracing is enabled.
* @category Telemetry
* @experimental
*/
export const enabled: boolean;

/**
* Allowed attribute type.
* @category Telemetry
* @experimental
*/
export type AttributeValue = string | number | boolean | bigint;

/**
* A tracing span.
* @category Telemetry
* @experimental
*/
export class Span implements Disposable {
readonly traceId: string;
readonly spanId: string;
readonly parentSpanId: string;
readonly kind: string;
readonly name: string;
readonly startTime: number;
readonly endTime: number;
readonly status: null | { code: 1 } | { code: 2; message: string };
readonly attributes: Record<string, AttributeValue>;
readonly traceFlags: number;

/**
* Construct a new Span and enter it as the "current" span.
*/
constructor(
name: string,
kind?: "internal" | "server" | "client" | "producer" | "consumer",
);

/**
* Set an attribute on this span.
*/
setAttribute(
name: string,
value: AttributeValue,
): void;

/**
* Enter this span as the "current" span.
*/
enter(): void;

/**
* Exit this span as the "current" span and restore the previous one.
*/
exit(): void;

/**
* End this span, and exit it as the "current" span.
*/
end(): void;

[Symbol.dispose](): void;

/**
* Get the "current" span, if one exists.
*/
static current(): Span | undefined | null;
}

export namespace telemetry {
/**
* A SpanExporter compatible with OpenTelemetry.js
* https://open-telemetry.github.io/opentelemetry-js/interfaces/_opentelemetry_sdk_trace_base.SpanExporter.html
Expand All @@ -1345,14 +1318,6 @@ declare namespace Deno {
export {}; // only export exports
}

/**
* @category Telemetry
* @experimental
*/
export namespace metrics {
export {}; // only export exports
}

export {}; // only export exports
}

Expand Down
3 changes: 3 additions & 0 deletions ext/console/internal.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,7 @@ declare module "ext:deno_console/01_console.js" {
keys: (keyof TObject)[];
evaluate: boolean;
}): Record<string, unknown>;

class Console {
}
}
5 changes: 2 additions & 3 deletions runtime/js/90_deno_ns.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ import * as tty from "ext:runtime/40_tty.js";
import * as kv from "ext:deno_kv/01_db.ts";
import * as cron from "ext:deno_cron/01_cron.ts";
import * as webgpuSurface from "ext:deno_webgpu/02_surface.js";
import * as telemetry from "ext:runtime/telemetry.js";
import * as telemetry from "ext:runtime/telemetry.ts";

const denoNs = {
Process: process.Process,
Expand Down Expand Up @@ -185,8 +185,7 @@ denoNsUnstableById[unstableIds.webgpu] = {
// denoNsUnstableById[unstableIds.workerOptions] = { __proto__: null }

denoNsUnstableById[unstableIds.otel] = {
tracing: telemetry.tracing,
metrics: telemetry.metrics,
telemetry: telemetry.telemetry,
};

export { denoNs, denoNsUnstableById, unstableIds };
2 changes: 1 addition & 1 deletion runtime/js/99_main.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ import {
workerRuntimeGlobalProperties,
} from "ext:runtime/98_global_scope_worker.js";
import { SymbolDispose, SymbolMetadata } from "ext:deno_web/00_infra.js";
import { bootstrap as bootstrapOtel } from "ext:runtime/telemetry.js";
import { bootstrap as bootstrapOtel } from "ext:runtime/telemetry.ts";

// deno-lint-ignore prefer-primordials
if (Symbol.metadata) {
Expand Down
Loading

0 comments on commit 594a998

Please sign in to comment.