Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WASM Support #57

Merged
merged 1 commit into from
Aug 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,10 @@ tracing-subscriber = { version = "0.3.0", default-features = false, features = [
[target.'cfg(not(target_os = "windows"))'.dev-dependencies]
pprof = { version = "0.11.1", features = ["flamegraph", "criterion"] }

[target.'cfg(all(target_arch = "wasm32", not(target_os = "wasi")))'.dependencies]
js-sys = "0.3.64"
web-time = "0.2.0"

[lib]
bench = false

Expand Down
11 changes: 7 additions & 4 deletions src/layer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,17 @@ use std::any::TypeId;
use std::fmt;
use std::marker;
use std::thread;
use std::time::{Instant, SystemTime};
#[cfg(not(target_arch = "wasm32"))]
use std::time::Instant;
use tracing_core::span::{self, Attributes, Id, Record};
use tracing_core::{field, Event, Subscriber};
#[cfg(feature = "tracing-log")]
use tracing_log::NormalizeEvent;
use tracing_subscriber::layer::Context;
use tracing_subscriber::registry::LookupSpan;
use tracing_subscriber::Layer;
#[cfg(target_arch = "wasm32")]
use web_time::Instant;

const SPAN_NAME_FIELD: &str = "otel.name";
const SPAN_KIND_FIELD: &str = "otel.kind";
Expand Down Expand Up @@ -680,7 +683,7 @@ where
let mut builder = self
.tracer
.span_builder(attrs.metadata().name())
.with_start_time(SystemTime::now())
.with_start_time(crate::time::now())
// Eagerly assign span id so children have stable parent id
.with_span_id(self.tracer.new_span_id());

Expand Down Expand Up @@ -839,7 +842,7 @@ where

let mut otel_event = otel::Event::new(
String::new(),
SystemTime::now(),
crate::time::now(),
vec![Key::new("level").string(meta.level().as_str()), target],
0,
);
Expand Down Expand Up @@ -924,7 +927,7 @@ where

// Assign end time, build and start span, drop span to export
builder
.with_end_time(SystemTime::now())
.with_end_time(crate::time::now())
.start_with_context(&self.tracer, &parent_cx);
}
}
Expand Down
14 changes: 14 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -150,3 +150,17 @@ pub struct OtelData {
/// The otel span data recorded during the current tracing span.
pub builder: opentelemetry::trace::SpanBuilder,
}

pub(crate) mod time {
use std::time::SystemTime;

#[cfg(not(target_arch = "wasm32"))]
pub(crate) fn now() -> SystemTime {
SystemTime::now()
}

#[cfg(target_arch = "wasm32")]
pub(crate) fn now() -> SystemTime {
SystemTime::UNIX_EPOCH + std::time::Duration::from_millis(js_sys::Date::now() as u64)
}
}