Skip to content

Commit

Permalink
refactor(unstable): move telemetry to own ext (#27067)
Browse files Browse the repository at this point in the history
Move telemetry to its own ext to clean up some code and resolve circular
deps.
  • Loading branch information
devsnek authored Nov 26, 2024
1 parent 114fe9b commit e61cf8d
Show file tree
Hide file tree
Showing 28 changed files with 164 additions and 88 deletions.
30 changes: 23 additions & 7 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ members = [
"ext/napi/sym",
"ext/net",
"ext/node",
"ext/telemetry",
"ext/url",
"ext/web",
"ext/webgpu",
Expand Down Expand Up @@ -82,6 +83,7 @@ deno_kv = { version = "0.87.0", path = "./ext/kv" }
deno_napi = { version = "0.110.0", path = "./ext/napi" }
deno_net = { version = "0.171.0", path = "./ext/net" }
deno_node = { version = "0.116.0", path = "./ext/node" }
deno_telemetry = { version = "0.1.0", path = "./ext/telemetry" }
deno_tls = { version = "0.166.0", path = "./ext/tls" }
deno_url = { version = "0.179.0", path = "./ext/url" }
deno_web = { version = "0.210.0", path = "./ext/web" }
Expand Down
1 change: 1 addition & 0 deletions cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ deno_resolver.workspace = true
deno_runtime = { workspace = true, features = ["include_js_files_for_snapshotting"] }
deno_semver.workspace = true
deno_task_shell = "=0.18.1"
deno_telemetry.workspace = true
deno_terminal.workspace = true
libsui = "0.5.0"
node_resolver.workspace = true
Expand Down
6 changes: 3 additions & 3 deletions cli/args/flags.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ use deno_path_util::normalize_path;
use deno_path_util::url_to_file_path;
use deno_runtime::deno_permissions::PermissionsOptions;
use deno_runtime::deno_permissions::SysDescriptor;
use deno_runtime::ops::otel::OtelConfig;
use deno_telemetry::OtelConfig;
use log::debug;
use log::Level;
use serde::Deserialize;
Expand Down Expand Up @@ -2662,7 +2662,7 @@ By default, outdated dependencies are only displayed.
Display outdated dependencies:
<p(245)>deno outdated</>
<p(245)>deno outdated --compatible</>
Update dependencies:
<p(245)>deno outdated --update</>
<p(245)>deno outdated --update --latest</>
Expand Down Expand Up @@ -3047,7 +3047,7 @@ fn task_subcommand() -> Command {
List all available tasks:
<p(245)>deno task</>
Evaluate a task from string
<p(245)>deno task --eval \"echo $(pwd)\"</>"
),
Expand Down
2 changes: 1 addition & 1 deletion cli/args/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ 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 deno_telemetry::OtelConfig;
use import_map::resolve_import_map_value_from_specifier;

pub use deno_config::deno_json::BenchConfig;
Expand Down
2 changes: 1 addition & 1 deletion cli/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -448,7 +448,7 @@ fn resolve_flags_and_init(
};

if let Some(otel_config) = flags.otel_config() {
deno_runtime::ops::otel::init(otel_config)?;
deno_telemetry::init(otel_config)?;
}
util::logger::init(flags.log_level);

Expand Down
2 changes: 1 addition & 1 deletion cli/mainrt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ fn main() {
match standalone {
Ok(Some(data)) => {
if let Some(otel_config) = data.metadata.otel_config.clone() {
deno_runtime::ops::otel::init(otel_config)?;
deno_telemetry::init(otel_config)?;
}
util::logger::init(data.metadata.log_level);
load_env_vars(&data.metadata.env_vars_from_env_file);
Expand Down
2 changes: 1 addition & 1 deletion cli/standalone/binary.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,11 @@ 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;
use deno_semver::VersionReqSpecifierParseError;
use deno_telemetry::OtelConfig;
use indexmap::IndexMap;
use log::Level;
use serde::Deserialize;
Expand Down
2 changes: 1 addition & 1 deletion cli/util/logger.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ impl log::Log for CliLogger {
// thread's state
DrawThread::hide();
self.0.log(record);
deno_runtime::ops::otel::handle_log(record);
deno_telemetry::handle_log(record);
DrawThread::show();
}
}
Expand Down
2 changes: 1 addition & 1 deletion cli/worker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ 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;
Expand All @@ -43,6 +42,7 @@ use deno_runtime::BootstrapOptions;
use deno_runtime::WorkerExecutionMode;
use deno_runtime::WorkerLogLevel;
use deno_semver::npm::NpmPackageReqReference;
use deno_telemetry::OtelConfig;
use deno_terminal::colors;
use node_resolver::NodeModuleKind;
use node_resolver::NodeResolutionMode;
Expand Down
44 changes: 21 additions & 23 deletions ext/fetch/26_fetch.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
/// <reference path="./lib.deno_fetch.d.ts" />
/// <reference lib="esnext" />

import { core, internals, primordials } from "ext:core/mod.js";
import { core, primordials } from "ext:core/mod.js";
import {
op_fetch,
op_fetch_promise_is_settled,
Expand All @@ -32,7 +32,6 @@ const {
SafePromisePrototypeFinally,
String,
StringPrototypeEndsWith,
StringPrototypeSlice,
StringPrototypeStartsWith,
StringPrototypeToLowerCase,
TypeError,
Expand All @@ -59,6 +58,17 @@ import {
toInnerResponse,
} from "ext:deno_fetch/23_response.js";
import * as abortSignal from "ext:deno_web/03_abort_signal.js";
import {
endSpan,
enterSpan,
exitSpan,
Span,
TRACING_ENABLED,
} from "ext:deno_telemetry/telemetry.ts";
import {
updateSpanFromRequest,
updateSpanFromResponse,
} from "ext:deno_telemetry/util.ts";

const REQUEST_BODY_HEADER_NAMES = [
"content-encoding",
Expand Down Expand Up @@ -343,9 +353,9 @@ function httpRedirectFetch(request, response, terminator) {
function fetch(input, init = { __proto__: null }) {
let span;
try {
if (internals.telemetry?.tracingEnabled) {
span = new internals.telemetry.Span("fetch", { kind: 2 });
internals.telemetry.enterSpan(span);
if (TRACING_ENABLED) {
span = new Span("fetch", { kind: 2 });
enterSpan(span);
}

// There is an async dispatch later that causes a stack trace disconnect.
Expand All @@ -361,16 +371,7 @@ function fetch(input, init = { __proto__: null }) {
const requestObject = new Request(input, init);

if (span) {
span.updateName(requestObject.method);
span.setAttribute("http.request.method", requestObject.method);
const url = new URL(requestObject.url);
span.setAttribute("url.full", requestObject.url);
span.setAttribute(
"url.scheme",
StringPrototypeSlice(url.protocol, 0, -1),
);
span.setAttribute("url.path", url.pathname);
span.setAttribute("url.query", StringPrototypeSlice(url.search, 1));
updateSpanFromRequest(span, requestObject);
}

// 3.
Expand Down Expand Up @@ -432,10 +433,7 @@ function fetch(input, init = { __proto__: null }) {
responseObject = fromInnerResponse(response, "immutable");

if (span) {
span.setAttribute(
"http.response.status_code",
String(responseObject.status),
);
updateSpanFromResponse(span, responseObject);
}

resolve(responseObject);
Expand All @@ -457,7 +455,7 @@ function fetch(input, init = { __proto__: null }) {
return result;
} finally {
if (span) {
internals.telemetry.endSpan(span);
endSpan(span);
}
}
})();
Expand All @@ -471,18 +469,18 @@ function fetch(input, init = { __proto__: null }) {
// XXX: This should always be true, otherwise `opPromise` would be present.
if (op_fetch_promise_is_settled(result)) {
// It's already settled.
internals.telemetry.endSpan(span);
endSpan(span);
} else {
// Not settled yet, we can return a new wrapper promise.
return SafePromisePrototypeFinally(result, () => {
internals.telemetry.endSpan(span);
endSpan(span);
});
}
}
return result;
} finally {
if (span) {
internals.telemetry.exitSpan(span);
exitSpan(span);
}
}
}
Expand Down
31 changes: 13 additions & 18 deletions ext/http/00_serve.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,7 @@ const {
PromisePrototypeCatch,
SafePromisePrototypeFinally,
PromisePrototypeThen,
String,
StringPrototypeIncludes,
StringPrototypeSlice,
Symbol,
TypeError,
TypedArrayPrototypeGetSymbolToStringTag,
Expand Down Expand Up @@ -91,6 +89,16 @@ import {
} from "ext:deno_net/01_net.js";
import { hasTlsKeyPairOptions, listenTls } from "ext:deno_net/02_tls.js";
import { SymbolAsyncDispose } from "ext:deno_web/00_infra.js";
import {
endSpan,
enterSpan,
Span,
TRACING_ENABLED,
} from "ext:deno_telemetry/telemetry.ts";
import {
updateSpanFromRequest,
updateSpanFromResponse,
} from "ext:deno_telemetry/util.ts";

const _upgraded = Symbol("_upgraded");

Expand Down Expand Up @@ -527,16 +535,7 @@ function mapToCallback(context, callback, onError) {
innerRequest.request = request;

if (span) {
span.updateName(request.method);
span.setAttribute("http.request.method", request.method);
const url = new URL(request.url);
span.setAttribute("url.full", request.url);
span.setAttribute(
"url.scheme",
StringPrototypeSlice(url.protocol, 0, -1),
);
span.setAttribute("url.path", url.pathname);
span.setAttribute("url.query", StringPrototypeSlice(url.search, 1));
updateSpanFromRequest(span, request);
}

response = await callback(
Expand Down Expand Up @@ -578,10 +577,7 @@ function mapToCallback(context, callback, onError) {
}

if (span) {
span.setAttribute(
"http.response.status_code",
String(response.status),
);
updateSpanFromResponse(span, response);
}

const inner = toInnerResponse(response);
Expand Down Expand Up @@ -617,8 +613,7 @@ function mapToCallback(context, callback, onError) {
fastSyncResponseOrStream(req, inner.body, status, innerRequest);
};

if (internals.telemetry?.tracingEnabled) {
const { Span, enterSpan, endSpan } = internals.telemetry;
if (TRACING_ENABLED) {
const origMapped = mapped;
mapped = function (req, _span) {
const oldCtx = getAsyncContext();
Expand Down
31 changes: 31 additions & 0 deletions ext/telemetry/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.

[package]
name = "deno_telemetry"
version = "0.1.0"
authors.workspace = true
edition.workspace = true
license.workspace = true
readme = "README.md"
repository.workspace = true
description = "Telemetry for Deno"

[lib]
path = "lib.rs"

[dependencies]
async-trait.workspace = true
deno_core.workspace = true
http-body-util.workspace = true
hyper.workspace = true
hyper-util.workspace = true
log.workspace = true
once_cell.workspace = true
opentelemetry.workspace = true
opentelemetry-http.workspace = true
opentelemetry-otlp.workspace = true
opentelemetry-semantic-conventions.workspace = true
opentelemetry_sdk.workspace = true
pin-project.workspace = true
serde.workspace = true
tokio.workspace = true
3 changes: 3 additions & 0 deletions ext/telemetry/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# `deno_telemetry`

This crate implements telemetry for Deno using OpenTelemetry.
Loading

0 comments on commit e61cf8d

Please sign in to comment.