Skip to content

Logs API - initialize LogRecordData.Timestamp with MinValue#7045

Open
Kielek wants to merge 5 commits intoopen-telemetry:mainfrom
Kielek:logrecorddata-timestamp
Open

Logs API - initialize LogRecordData.Timestamp with MinValue#7045
Kielek wants to merge 5 commits intoopen-telemetry:mainfrom
Kielek:logrecorddata-timestamp

Conversation

@Kielek
Copy link
Copy Markdown
Member

@Kielek Kielek commented Apr 8, 2026

Handles discussions from #6979 related to nullability.

Changes

Logs API - initialize LogRecordData.Timestamp with MinValue
MinValue is equivalent of 0/null value from OTel Spec.

Merge requirement checklist

  • CONTRIBUTING guidelines followed (license requirements, nullable enabled, static analysis, etc.)
  • Unit tests added/updated
  • Appropriate CHANGELOG.md files updated for non-trivial changes
  • [ ] Changes in public API reviewed (if applicable)

@github-actions github-actions bot added pkg:OpenTelemetry.Api Issues related to OpenTelemetry.Api NuGet package pkg:OpenTelemetry.Exporter.Console Issues related to OpenTelemetry.Exporter.Console NuGet package pkg:OpenTelemetry.Exporter.OpenTelemetryProtocol Issues related to OpenTelemetry.Exporter.OpenTelemetryProtocol NuGet package labels Apr 8, 2026
@github-actions github-actions bot added the pkg:OpenTelemetry Issues related to OpenTelemetry NuGet package label Apr 8, 2026
@codecov
Copy link
Copy Markdown

codecov bot commented Apr 8, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 88.82%. Comparing base (5d74aeb) to head (c6c9a55).
⚠️ Report is 1 commits behind head on main.
✅ All tests successful. No failed tests found.

Additional details and impacted files

Impacted file tree graph

@@            Coverage Diff             @@
##             main    #7045      +/-   ##
==========================================
+ Coverage   88.77%   88.82%   +0.05%     
==========================================
  Files         270      270              
  Lines       12929    12935       +6     
==========================================
+ Hits        11478    11490      +12     
+ Misses       1451     1445       -6     
Flag Coverage Δ
unittests-Project-Experimental 88.73% <100.00%> (-0.02%) ⬇️
unittests-Project-Stable 88.73% <100.00%> (+0.22%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

Files with missing lines Coverage Δ
src/OpenTelemetry.Api/Logs/LogRecordData.cs 100.00% <100.00%> (ø)
...metry.Exporter.Console/ConsoleLogRecordExporter.cs 97.10% <100.00%> (+0.04%) ⬆️
...ementation/Serializer/ProtobufOtlpLogSerializer.cs 99.26% <100.00%> (+0.02%) ⬆️
src/OpenTelemetry/Logs/LogRecord.cs 70.86% <ø> (ø)

... and 2 files with indirect coverage changes

@Kielek Kielek marked this pull request as ready for review April 8, 2026 09:16
@Kielek Kielek requested a review from a team as a code owner April 8, 2026 09:16
@Kielek
Copy link
Copy Markdown
Member Author

Kielek commented Apr 8, 2026

@juliuskoval, could you please review?

Comment on lines +185 to +197
if (logTimestamp != DateTime.MinValue)
{
// Timestamp was explicitly set: use it for both fields.
timeUnixNano = (ulong)logTimestamp.ToUnixTimeNanoseconds();
observedTimeUnixNano = timeUnixNano;
}
else
{
// Timestamp not set -> time_unix_nano = 0 ("unknown or missing" per OTLP spec).
// observed_time_unix_nano MUST still be populated (proto spec requirement).
timeUnixNano = 0;
observedTimeUnixNano = (ulong)DateTime.UtcNow.ToUnixTimeNanoseconds();
}
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Have you considered changing ToUnixTimeNanoseconds (and other extension methods) to return 0 when datetime is min?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, but we should handle timestamp and observed timespan differently, so I would keep as is.

{
// Timestamp was explicitly set: use it for both fields.
timeUnixNano = (ulong)logTimestamp.ToUnixTimeNanoseconds();
observedTimeUnixNano = timeUnixNano;
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not always setting observedTimeUnixNano = (ulong)DateTime.UtcNow.ToUnixTimeNanoseconds();?

From https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/logs/data-model.md#field-observedtimestamp:

This field SHOULD be set once the event is observed by OpenTelemetry.

I am not sure what would be the value of setting the same time for ObservedTimestamp and Timestamp.

Here is what we do in OTel Go: https://github.com/open-telemetry/opentelemetry-go/blob/3157d0a7d9d22c3343838da6b1a691557676072f/sdk/log/logger.go#L116-L119

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Setting ObservedTimestamp is addressed in #6979; this PR only addresses nullability of Timestamp if I understand it correctly. Changes from the other PR would allow users to set ObservedTimestamp in the API/SDK.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Julius is right, it is intermittent state and should be changed in the short cadence in #6973.
I hope that both will be merged together. For now, I am keeping existing behavior as much as possible.

@juliuskoval
Copy link
Copy Markdown
Contributor

@juliuskoval, could you please review?

You didn't add changelogs for OpenTelemetry.Exporter.OpenTelemetryProtocol and OpenTelemetry.Exporter.Console, but other than that it looks good to me.

@github-actions
Copy link
Copy Markdown
Contributor

This PR was marked stale due to lack of activity and will be closed in 7 days. Commenting or pushing will instruct the bot to automatically remove the label. This bot runs once per day.

@github-actions github-actions bot added the Stale Issues and pull requests which have been flagged for closing due to inactivity label Apr 17, 2026
@Kielek Kielek removed the Stale Issues and pull requests which have been flagged for closing due to inactivity label Apr 17, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

pkg:OpenTelemetry.Api Issues related to OpenTelemetry.Api NuGet package pkg:OpenTelemetry.Exporter.Console Issues related to OpenTelemetry.Exporter.Console NuGet package pkg:OpenTelemetry.Exporter.OpenTelemetryProtocol Issues related to OpenTelemetry.Exporter.OpenTelemetryProtocol NuGet package pkg:OpenTelemetry Issues related to OpenTelemetry NuGet package

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants