Skip to content

Commit

Permalink
feat: otel
Browse files Browse the repository at this point in the history
  • Loading branch information
devsnek committed Nov 11, 2024
1 parent b955d03 commit 82470d5
Show file tree
Hide file tree
Showing 27 changed files with 1,598 additions and 122 deletions.
323 changes: 278 additions & 45 deletions Cargo.lock

Large diffs are not rendered by default.

5 changes: 5 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,11 @@ yoke = { version = "0.7.4", features = ["derive"] }
zeromq = { version = "=0.4.0", default-features = false, features = ["tcp-transport", "tokio-runtime"] }
zstd = "=0.12.4"

opentelemetry = "0.26.0"
opentelemetry-otlp = { version = "0.26.0", features = ["logs", "http-proto", "http-json", "reqwest-rustls-webpki-roots"] }
opentelemetry-semantic-conventions = "0.26.0"
opentelemetry_sdk = "0.26.0"

# crypto
hkdf = "0.12.3"
rsa = { version = "0.9.3", default-features = false, features = ["std", "pem", "hazmat"] } # hazmat needed for PrehashSigner in ext/node
Expand Down
20 changes: 20 additions & 0 deletions cli/args/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -1129,6 +1130,25 @@ impl CliOptions {
}
}

pub fn otel_config(&self) -> Option<OtelConfig> {
if self
.flags
.unstable_config
.features
.contains(&String::from("otel"))
{
Some(OtelConfig {
default_service_name: Cow::Borrowed("deno"),
default_service_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()
}
Expand Down
1 change: 1 addition & 0 deletions cli/factory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
))
}

Expand Down
3 changes: 3 additions & 0 deletions cli/standalone/binary.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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(
Expand Down
1 change: 1 addition & 0 deletions cli/standalone/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
97 changes: 97 additions & 0 deletions cli/tsc/dts/lib.deno.unstable.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1225,6 +1225,103 @@ 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;

static current(): Span | undefined | null;
}

/**
* A SpanExporter compatible with OpenTelemetry.js
* @category Telemetry
* @experimental
*/
export class SpanExporter {}

/**
* A ContextManager compatible with OpenTelemetry.js
* @category Telemetry
* @experimental
*/
export class ContextManager {}

export {}; // only export exports
}

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

export {}; // only export exports
}

Expand Down
6 changes: 6 additions & 0 deletions cli/worker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -405,6 +407,7 @@ impl CliMainWorkerFactory {
storage_key_resolver: StorageKeyResolver,
subcommand: DenoSubcommand,
options: CliMainWorkerOptions,
otel_config: Option<OtelConfig>,
) -> Self {
Self {
shared: Arc::new(SharedWorkerState {
Expand All @@ -427,6 +430,7 @@ impl CliMainWorkerFactory {
storage_key_resolver,
options,
subcommand,
otel_config,
}),
}
}
Expand Down Expand Up @@ -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(),
Expand Down Expand Up @@ -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(),
Expand Down
Loading

0 comments on commit 82470d5

Please sign in to comment.