summaryrefslogtreecommitdiff
path: root/cli
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 /cli
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 'cli')
-rw-r--r--cli/args/mod.rs18
-rw-r--r--cli/factory.rs1
-rw-r--r--cli/standalone/binary.rs3
-rw-r--r--cli/standalone/mod.rs1
-rw-r--r--cli/tsc/dts/lib.deno.unstable.d.ts102
-rw-r--r--cli/worker.rs6
6 files changed, 131 insertions, 0 deletions
diff --git a/cli/args/mod.rs b/cli/args/mod.rs
index e19025f8b..3aaf2bd43 100644
--- a/cli/args/mod.rs
+++ b/cli/args/mod.rs
@@ -27,6 +27,7 @@ use deno_npm::npm_rc::ResolvedNpmRc;
use deno_npm::resolution::ValidSerializedNpmResolutionSnapshot;
use deno_npm::NpmSystemInfo;
use deno_path_util::normalize_path;
+use deno_runtime::ops::otel::OtelConfig;
use deno_semver::npm::NpmPackageReqReference;
use import_map::resolve_import_map_value_from_specifier;
@@ -1129,6 +1130,23 @@ impl CliOptions {
}
}
+ pub fn otel_config(&self) -> Option<OtelConfig> {
+ if self
+ .flags
+ .unstable_config
+ .features
+ .contains(&String::from("otel"))
+ {
+ Some(OtelConfig {
+ runtime_name: Cow::Borrowed("deno"),
+ runtime_version: Cow::Borrowed(crate::version::DENO_VERSION_INFO.deno),
+ ..Default::default()
+ })
+ } else {
+ None
+ }
+ }
+
pub fn env_file_name(&self) -> Option<&String> {
self.flags.env_file.as_ref()
}
diff --git a/cli/factory.rs b/cli/factory.rs
index 4a36c75ba..417f771a3 100644
--- a/cli/factory.rs
+++ b/cli/factory.rs
@@ -939,6 +939,7 @@ impl CliFactory {
StorageKeyResolver::from_options(cli_options),
cli_options.sub_command().clone(),
self.create_cli_main_worker_options()?,
+ self.cli_options()?.otel_config(),
))
}
diff --git a/cli/standalone/binary.rs b/cli/standalone/binary.rs
index 9e2651226..960aad157 100644
--- a/cli/standalone/binary.rs
+++ b/cli/standalone/binary.rs
@@ -47,6 +47,7 @@ use deno_runtime::deno_fs::FileSystem;
use deno_runtime::deno_fs::RealFs;
use deno_runtime::deno_io::fs::FsError;
use deno_runtime::deno_node::PackageJson;
+use deno_runtime::ops::otel::OtelConfig;
use deno_semver::npm::NpmVersionReqParseError;
use deno_semver::package::PackageReq;
use deno_semver::Version;
@@ -185,6 +186,7 @@ pub struct Metadata {
pub entrypoint_key: String,
pub node_modules: Option<NodeModules>,
pub unstable_config: UnstableConfig,
+ pub otel_config: Option<OtelConfig>, // None means disabled.
}
fn write_binary_bytes(
@@ -722,6 +724,7 @@ impl<'a> DenoCompileBinaryWriter<'a> {
sloppy_imports: cli_options.unstable_sloppy_imports(),
features: cli_options.unstable_features(),
},
+ otel_config: cli_options.otel_config(),
};
write_binary_bytes(
diff --git a/cli/standalone/mod.rs b/cli/standalone/mod.rs
index 85610f4c2..bb0ab423d 100644
--- a/cli/standalone/mod.rs
+++ b/cli/standalone/mod.rs
@@ -800,6 +800,7 @@ pub async fn run(data: StandaloneData) -> Result<i32, AnyError> {
serve_port: None,
serve_host: None,
},
+ metadata.otel_config,
);
// Initialize v8 once from the main thread.
diff --git a/cli/tsc/dts/lib.deno.unstable.d.ts b/cli/tsc/dts/lib.deno.unstable.d.ts
index 973a09d92..6234268c3 100644
--- a/cli/tsc/dts/lib.deno.unstable.d.ts
+++ b/cli/tsc/dts/lib.deno.unstable.d.ts
@@ -1225,6 +1225,108 @@ declare namespace Deno {
export {}; // only export exports
}
+ /**
+ * @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;
+ }
+
+ /**
+ * A SpanExporter compatible with OpenTelemetry.js
+ * https://open-telemetry.github.io/opentelemetry-js/interfaces/_opentelemetry_sdk_trace_base.SpanExporter.html
+ * @category Telemetry
+ * @experimental
+ */
+ export class SpanExporter {}
+
+ /**
+ * A ContextManager compatible with OpenTelemetry.js
+ * https://open-telemetry.github.io/opentelemetry-js/interfaces/_opentelemetry_api.ContextManager.html
+ * @category Telemetry
+ * @experimental
+ */
+ export class ContextManager {}
+
+ export {}; // only export exports
+ }
+
+ /**
+ * @category Telemetry
+ * @experimental
+ */
+ export namespace metrics {
+ export {}; // only export exports
+ }
+
export {}; // only export exports
}
diff --git a/cli/worker.rs b/cli/worker.rs
index baacd681a..402644a42 100644
--- a/cli/worker.rs
+++ b/cli/worker.rs
@@ -30,6 +30,7 @@ use deno_runtime::deno_tls::RootCertStoreProvider;
use deno_runtime::deno_web::BlobStore;
use deno_runtime::fmt_errors::format_js_error;
use deno_runtime::inspector_server::InspectorServer;
+use deno_runtime::ops::otel::OtelConfig;
use deno_runtime::ops::process::NpmProcessStateProviderRc;
use deno_runtime::ops::worker_host::CreateWebWorkerCb;
use deno_runtime::web_worker::WebWorker;
@@ -142,6 +143,7 @@ struct SharedWorkerState {
storage_key_resolver: StorageKeyResolver,
options: CliMainWorkerOptions,
subcommand: DenoSubcommand,
+ otel_config: Option<OtelConfig>, // `None` means OpenTelemetry is disabled.
}
impl SharedWorkerState {
@@ -405,6 +407,7 @@ impl CliMainWorkerFactory {
storage_key_resolver: StorageKeyResolver,
subcommand: DenoSubcommand,
options: CliMainWorkerOptions,
+ otel_config: Option<OtelConfig>,
) -> Self {
Self {
shared: Arc::new(SharedWorkerState {
@@ -427,6 +430,7 @@ impl CliMainWorkerFactory {
storage_key_resolver,
options,
subcommand,
+ otel_config,
}),
}
}
@@ -576,6 +580,7 @@ impl CliMainWorkerFactory {
mode,
serve_port: shared.options.serve_port,
serve_host: shared.options.serve_host.clone(),
+ otel_config: shared.otel_config.clone(),
},
extensions: custom_extensions,
startup_snapshot: crate::js::deno_isolate_init(),
@@ -775,6 +780,7 @@ fn create_web_worker_callback(
mode: WorkerExecutionMode::Worker,
serve_port: shared.options.serve_port,
serve_host: shared.options.serve_host.clone(),
+ otel_config: shared.otel_config.clone(),
},
extensions: vec![],
startup_snapshot: crate::js::deno_isolate_init(),