Skip to content

Commit 4e54452

Browse files
committed
fix default log timestamps, setting to current time. Fixes #193
1 parent be526bd commit 4e54452

File tree

2 files changed

+11
-1
lines changed

2 files changed

+11
-1
lines changed

api/logs/src/LoggerProxy.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#include "opentelemetry/logs/log_record.h"
1010
#include "opentelemetry/trace/context.h"
1111
#include "opentelemetry/context/context.h"
12+
#include "opentelemetry/common/timestamp.h"
1213

1314
#include "MatlabDataArray.hpp"
1415

@@ -40,6 +41,12 @@ void LoggerProxy::emitLogRecord(libmexclass::proxy::method::Context& context) {
4041

4142
nostd::unique_ptr<logs_api::LogRecord> rec = CppLogger->CreateLogRecord();
4243

44+
// Do not use the default timestamp, which is set to the start of UNIX epoch. Set both the
45+
// default timestamp and default observed timestamp to the current time
46+
auto now = common::SystemTimestamp(std::chrono::system_clock::now());
47+
rec->SetTimestamp(now);
48+
rec->SetObservedTimestamp(now);
49+
4350
// Add size attribute if body is nonscalar
4451
if (array_body) {
4552
rec->SetAttribute(bodyattrs.Attributes.back().first, bodyattrs.Attributes.back().second);

test/tlogs.m

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,10 @@ function testBasic(testCase)
7575
tol = seconds(4);
7676
verifyLessThanOrEqual(testCase, abs(datetime(double(string(...
7777
results.resourceLogs.scopeLogs.logRecords.observedTimeUnixNano))/1e9, ...
78-
"convertFrom", "posixtime", "TimeZone", "UTC") - expectedtimestamp), tol);
78+
"convertFrom", "posixtime", "TimeZone", "UTC") - expectedtimestamp), tol);
79+
% by default, Timestamp and ObservedTimestamp should be identical
80+
verifyEqual(testCase, results.resourceLogs.scopeLogs.logRecords.observedTimeUnixNano, ...
81+
results.resourceLogs.scopeLogs.logRecords.timeUnixNano);
7982

8083
% check resource
8184
resourcekeys = string({results.resourceLogs.resource.attributes.key});

0 commit comments

Comments
 (0)