Skip to content

Commit

Permalink
feat(runtime): send console output to OpenTelemetry collector (#3)
Browse files Browse the repository at this point in the history
  • Loading branch information
piscisaureus authored Aug 9, 2024
1 parent 9e2fac0 commit dfbebad
Show file tree
Hide file tree
Showing 17 changed files with 538 additions and 7 deletions.
148 changes: 141 additions & 7 deletions Cargo.lock

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

17 changes: 17 additions & 0 deletions cli/args/flags.rs
Original file line number Diff line number Diff line change
Expand Up @@ -589,6 +589,7 @@ pub struct Flags {
pub permissions: PermissionFlags,
pub allow_scripts: PackagesAllowedScripts,
pub eszip: bool,
pub otel: bool,
}

#[derive(Clone, Debug, Eq, PartialEq, Default, Serialize, Deserialize)]
Expand Down Expand Up @@ -3330,6 +3331,7 @@ fn runtime_args(
.arg(enable_testing_features_arg())
.arg(strace_ops_arg())
.arg(eszip_arg())
.arg(otel_arg())
}

fn inspect_args(app: Command) -> Command {
Expand Down Expand Up @@ -3456,6 +3458,14 @@ fn eszip_arg() -> Arg {
.help("Run eszip")
}

fn otel_arg() -> Arg {
Arg::new("otel-internal-do-not-use")
.long("otel-internal-do-not-use")
.action(ArgAction::SetTrue)
.help("Enable OpenTelemetry")
.hide(true)
}

/// Used for subcommands that operate on executable scripts only.
/// `deno fmt` has its own `--ext` arg because its possible values differ.
/// If --ext is not provided and the script doesn't have a file extension,
Expand Down Expand Up @@ -4740,6 +4750,7 @@ fn runtime_args_parse(
env_file_arg_parse(flags, matches);
strace_ops_parse(flags, matches);
eszip_arg_parse(flags, matches);
otel_arg_parse(flags, matches);
}

fn inspect_arg_parse(flags: &mut Flags, matches: &mut ArgMatches) {
Expand Down Expand Up @@ -4831,6 +4842,12 @@ fn eszip_arg_parse(flags: &mut Flags, matches: &mut ArgMatches) {
}
}

fn otel_arg_parse(flags: &mut Flags, matches: &mut ArgMatches) {
if matches.get_flag("otel-internal-do-not-use") {
flags.otel = true;
}
}

fn ext_arg_parse(flags: &mut Flags, matches: &mut ArgMatches) {
flags.ext = matches.remove_one::<String>("ext");
}
Expand Down
9 changes: 9 additions & 0 deletions cli/args/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ use deno_runtime::deno_fs::DenoConfigFsAdapter;
use deno_runtime::deno_fs::RealFs;
use deno_runtime::deno_permissions::PermissionsContainer;
use deno_runtime::deno_tls::RootCertStoreProvider;
use deno_runtime::ops::otel::OtelConfig;
use deno_semver::npm::NpmPackageReqReference;
use import_map::resolve_import_map_value_from_specifier;

Expand Down Expand Up @@ -67,6 +68,7 @@ use once_cell::sync::Lazy;
use once_cell::sync::OnceCell;
use serde::Deserialize;
use serde::Serialize;
use std::borrow::Cow;
use std::collections::HashMap;
use std::env;
use std::io::BufReader;
Expand Down Expand Up @@ -1103,6 +1105,13 @@ impl CliOptions {
}
}

pub fn otel_config(&self) -> Option<OtelConfig> {
self.flags.otel.then(|| OtelConfig {
default_service_name: Cow::Borrowed("deno"),
default_service_version: Cow::Borrowed(crate::version::deno()),
})
}

pub fn env_file_name(&self) -> Option<&String> {
self.flags.env_file.as_ref()
}
Expand Down
1 change: 1 addition & 0 deletions cli/factory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -833,6 +833,7 @@ impl CliFactory {
} else {
None
},
self.options.otel_config(),
))
}

Expand Down
3 changes: 3 additions & 0 deletions cli/standalone/binary.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ use deno_core::serde_json;
use deno_core::url::Url;
use deno_npm::NpmSystemInfo;
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::VersionReqSpecifierParseError;
Expand Down Expand Up @@ -103,6 +104,7 @@ pub struct Metadata {
pub node_modules: Option<NodeModules>,
pub disable_deprecated_api_warning: bool,
pub unstable_config: UnstableConfig,
pub otel_config: Option<OtelConfig>, // None means disabled.
}

pub fn load_npm_vfs(root_dir_path: PathBuf) -> Result<FileBackedVfs, AnyError> {
Expand Down Expand Up @@ -645,6 +647,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(
Expand Down
1 change: 1 addition & 0 deletions cli/standalone/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -740,6 +740,7 @@ pub async fn run(
false,
// Code cache is not supported for standalone binary yet.
None,
metadata.otel_config,
);

// Initialize v8 once from the main thread.
Expand Down
6 changes: 6 additions & 0 deletions cli/tools/run/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.

use std::borrow::Cow;
use std::io::Read;

use deno_config::workspace::PackageJsonDepResolution;
Expand All @@ -11,6 +12,7 @@ use deno_core::futures::StreamExt;
use deno_core::unsync::spawn;
use deno_runtime::deno_permissions::Permissions;
use deno_runtime::deno_permissions::PermissionsContainer;
use deno_runtime::ops::otel::OtelConfig;
use deno_runtime::WorkerExecutionMode;
use eszip::EszipV2;
use tokio_util::compat::TokioAsyncReadCompatExt;
Expand Down Expand Up @@ -289,6 +291,10 @@ pub async fn run_eszip(
package_jsons: Default::default(),
pkg_json_resolution: PackageJsonDepResolution::Disabled,
},
otel_config: flags.otel.then(|| OtelConfig {
default_service_name: Cow::Borrowed("deno"),
default_service_version: Cow::Borrowed(crate::version::deno()),
}),
},
run_flags.script.as_bytes(),
"run-eszip",
Expand Down
Loading

0 comments on commit dfbebad

Please sign in to comment.