Repository: denoland/deno
Pull request: fix(otel): set timestamp on opentelemetry log records

<!--
Before submitting a PR, please read https://docs.deno.com/runtime/manual/references/contributing

1. Give the PR a descriptive title.

  Examples of good title:
    - fix(std/http): Fix race condition in server
    - docs(console): Update docstrings
    - feat(doc): Handle nested reexports

  Examples of bad title:
    - fix #7123
    - update docs
    - fix bugs

2. Ensure there is a related issue and it is referenced in the PR text.
3. Ensure there are tests that cover the changes.
4. Ensure `cargo test` passes.
5. Ensure `./tools/format.js` passes without changing files.
6. Ensure `./tools/lint.js` passes.
7. Open as a draft PR if your work is still in progress. The CI won't run
   all steps, but you can add '[ci]' to a commit message to force it to.
8. If you would like to run the benchmarks on the CI, add the 'ci-bench' label.
-->
Sets the log record's Timestamp in addition to the Observed Timestamp. The Timestamp is described as "[...] time when the event occurred measured by the origin clock, i.e. the time at the source." ([ref](https://docs.rs/opentelemetry/0.29.0/opentelemetry/logs/trait.LogRecord.html#tymethod.set_timestamp))

Changed files:
filename: ext/telemetry/lib.rs
patch: @@ -882,7 +882,9 @@ pub fn handle_log(record: &log::Record) {
 
   let mut log_record = LogRecord::default();
 
-  log_record.set_observed_timestamp(SystemTime::now());
+  let now = SystemTime::now();
+  log_record.set_timestamp(now);
+  log_record.set_observed_timestamp(now);
   log_record.set_severity_number(match record.level() {
     Level::Error => Severity::Error,
     Level::Warn => Severity::Warn,
@@ -1124,7 +1126,9 @@ fn op_otel_log<'s>(
   };
 
   let mut log_record = LogRecord::default();
-  log_record.set_observed_timestamp(SystemTime::now());
+  let now = SystemTime::now();
+  log_record.set_timestamp(now);
+  log_record.set_observed_timestamp(now);
   let Ok(message) = message.try_cast() else {
     return;
   };
@@ -1188,7 +1192,9 @@ fn op_otel_log_foreign(
 
   let mut log_record = LogRecord::default();
 
-  log_record.set_observed_timestamp(SystemTime::now());
+  let now = SystemTime::now();
+  log_record.set_timestamp(now);
+  log_record.set_observed_timestamp(now);
   log_record.set_body(message.into());
   log_record.set_severity_number(severity);
   log_record.set_severity_text(severity.name());

filename: tests/specs/cli/otel_basic/basic.out
patch: @@ -161,7 +161,7 @@
   ],
   "logs": [
     {
-      "timeUnixNano": "0",
+      "timeUnixNano": "[WILDLINE]",
       "observedTimeUnixNano": "[WILDCARD]",
       "severityNumber": 9,
       "severityText": "INFO",
@@ -175,7 +175,7 @@
       "spanId": "0000000000000004"
     },
     {
-      "timeUnixNano": "0",
+      "timeUnixNano": "[WILDLINE]",
       "observedTimeUnixNano": "[WILDCARD]",
       "severityNumber": 9,
       "severityText": "INFO",

filename: tests/specs/cli/otel_basic/deno_dot_exit.out
patch: @@ -2,7 +2,7 @@
   "spans": [],
   "logs": [
     {
-      "timeUnixNano": "0",
+      "timeUnixNano": "[WILDCARD]",
       "observedTimeUnixNano": "[WILDCARD]",
       "severityNumber": 9,
       "severityText": "INFO",

filename: tests/specs/cli/otel_basic/natural_exit.out
patch: @@ -2,7 +2,7 @@
   "spans": [],
   "logs": [
     {
-      "timeUnixNano": "0",
+      "timeUnixNano": "[WILDLINE]",
       "observedTimeUnixNano": "[WILDCARD]",
       "severityNumber": 9,
       "severityText": "INFO",

filename: tests/specs/cli/otel_basic/uncaught.out
patch: @@ -6,7 +6,7 @@ throw new Error("uncaught");
   "spans": [],
   "logs": [
     {
-      "timeUnixNano": "0",
+      "timeUnixNano": "[WILDLINE]",
       "observedTimeUnixNano": "[WILDCARD]",
       "severityNumber": 9,
       "severityText": "INFO",
@@ -20,7 +20,7 @@ throw new Error("uncaught");
       "spanId": ""
     },
     {
-      "timeUnixNano": "0",
+      "timeUnixNano": "[WILDLINE]",
       "observedTimeUnixNano": "[WILDCARD]",
       "severityNumber": 17,
       "severityText": "ERROR",