Skip to content

Commit c663569

Browse files
committed
fix issues related to UTC time, fixes #194
1 parent d295b2b commit c663569

File tree

5 files changed

+33
-13
lines changed

5 files changed

+33
-13
lines changed

api/logs/+opentelemetry/+logs/Logger.m

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
classdef Logger < handle
22
% A logger that is used to emit log records
33

4-
% Copyright 2024 The MathWorks, Inc.
4+
% Copyright 2024-2025 The MathWorks, Inc.
55

66
properties (SetAccess=immutable)
77
Name (1,1) string % Logger name
@@ -38,7 +38,9 @@ function emitLogRecord(obj, severity, body, trailingnames, trailingvalues)
3838
% Parameters are:
3939
% "Context" - Span contained in a context object.
4040
% "Timestamp" - Timestamp of the log record specified as a
41-
% datetime. Default is the current time.
41+
% datetime. Default is the current time. If
42+
% Timestamp does not have a time zone
43+
% specified, it is interpreted as a UTC time.
4244
% "Attributes" - Attribute name-value pairs specified as
4345
% a dictionary.
4446
%

api/trace/+opentelemetry/+trace/Span.m

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
classdef Span < handle
22
% A span that represents a unit of work within a trace.
33

4-
% Copyright 2023-2024 The MathWorks, Inc.
4+
% Copyright 2023-2025 The MathWorks, Inc.
55

66
properties
77
Name (1,1) string % Name of span
@@ -42,6 +42,10 @@ function endSpan(obj, endtime)
4242
% ENDSPAN End the span.
4343
% ENDSPAN(SP) ends the span SP.
4444
%
45+
% ENDSPAN(SP, ENDTIME) also specifies the end time. If
46+
% ENDTIME does not have a time zone specified, it is
47+
% interpreted as a UTC time.
48+
%
4549
% See also OPENTELEMETRY.TRACE.TRACER.STARTSPAN
4650
if nargin < 2
4751
obj.Proxy.endSpan();
@@ -99,7 +103,9 @@ function addEvent(obj, eventname, varargin)
99103
% ADDEVENT(SP, NAME) records a event with the specified name
100104
% at the current time.
101105
%
102-
% ADDEVENT(SP, NAME, TIME) also specifies a event time.
106+
% ADDEVENT(SP, NAME, TIME) also specifies a event time. If
107+
% TIME does not have a time zone specified, it is
108+
% interpreted as a UTC time.
103109
%
104110
% ADDEVENT(..., ATTRIBUTES) or ADDEVENT(..., ATTRNAME1,
105111
% ATTRVALUE1, ATTRNAME2, ATTRVALUE2, ...) specifies
@@ -113,7 +119,7 @@ function addEvent(obj, eventname, varargin)
113119
eventtime = posixtime(varargin{1});
114120
varargin(1) = []; % remove the time input from varargin
115121
else
116-
eventtime = posixtime(datetime("now"));
122+
eventtime = posixtime(datetime("now", "TimeZone", "UTC"));
117123
end
118124

119125
eventname = opentelemetry.common.mustBeScalarString(eventname);

api/trace/+opentelemetry/+trace/Tracer.m

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
classdef Tracer < handle
22
% A tracer that is used to create spans.
33

4-
% Copyright 2023-2024 The MathWorks, Inc.
4+
% Copyright 2023-2025 The MathWorks, Inc.
55

66
properties (SetAccess=immutable)
77
Name (1,1) string % Tracer name
@@ -37,7 +37,9 @@
3737
% "SpanKind" - "server", "client", "producer",
3838
% "consumer", or "internal" (default)
3939
% "StartTime" - Starting time of span specified as a
40-
% datetime. Default is the current time.
40+
% datetime. Default is the current time. If
41+
% StartTime does not have a time zone
42+
% specified, it is interpreted as a UTC time.
4143
% "Attributes" - Attribute name-value pairs specified as
4244
% a dictionary.
4345
% "Links" - Link objects that specifies relationships

test/tlogs.m

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -285,15 +285,15 @@ function testTimestamp(testCase)
285285
% testTimestamp: specifying a timestamp
286286
lp = opentelemetry.sdk.logs.LoggerProvider();
287287
lg = getLogger(lp, "foo");
288-
timestamp = datetime(2020,3,12,9,45,0);
288+
timestamp = datetime(2020,3,12,9,45,0, "TimeZone", "UTC");
289289
emitLogRecord(lg, "info", "bar", "Timestamp", timestamp);
290290

291291
% perform test comparisons
292292
forceFlush(lp, testCase.ForceFlushTimeout);
293293
results = readJsonResults(testCase);
294294
verifyEqual(testCase, datetime(double(string(...
295295
results{1}.resourceLogs.scopeLogs.logRecords.timeUnixNano))/1e9, ...
296-
"convertFrom", "posixtime"), timestamp); % convert from nanoseconds to seconds
296+
"convertFrom", "posixtime", "TimeZone", "UTC"), timestamp); % convert from nanoseconds to seconds
297297
end
298298

299299
function testAttributes(testCase)

test/ttrace.m

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -323,20 +323,20 @@ function testTime(testCase)
323323
% testTime: specifying start and end times
324324
tp = opentelemetry.sdk.trace.TracerProvider();
325325
tr = getTracer(tp, "tracer");
326-
starttime = datetime(2000,1,1,10,0,0);
327-
endtime = datetime(2001, 8, 31, 7, 30, 0);
326+
starttime = datetime(2000,1,1,10,0,0, "TimeZone", "UTC");
327+
endtime = datetime(2001, 8, 31, 7, 30, 0, "TimeZone", "UTC");
328328
sp = startSpan(tr, "foo", "StartTime", starttime);
329329
endSpan(sp, endtime);
330330

331331
% perform test comparisons
332332
results = readJsonResults(testCase);
333333
verifyEqual(testCase, datetime(double(string(...
334334
results{1}.resourceSpans.scopeSpans.spans.startTimeUnixNano))/1e9, ...
335-
"convertFrom", "posixtime"), starttime); % convert from nanoseconds to seconds
335+
"convertFrom", "posixtime", "TimeZone", "UTC"), starttime); % convert from nanoseconds to seconds
336336
% for end time, use a tolerance
337337
verifyLessThanOrEqual(testCase, abs(datetime(double(string(...
338338
results{1}.resourceSpans.scopeSpans.spans.endTimeUnixNano))/1e9, ...
339-
"convertFrom", "posixtime") - endtime), seconds(2));
339+
"convertFrom", "posixtime", "TimeZone", "UTC") - endtime), seconds(2));
340340
end
341341

342342
function testStatus(testCase)
@@ -551,17 +551,24 @@ function testEvents(testCase)
551551
nvattributes = {"doublescalar", 5, "int32array", reshape(int32(1:6),2,3), ...
552552
"stringscalar", "baz"};
553553
addEvent(sp, "baz", nvattributes{:});
554+
event1time = datetime("now", "TimeZone", "UTC");
554555
% dictionary
555556
attributes = dictionary(["doublearray", "int64scalar", "stringarray"], ...
556557
{reshape(1:4,1,2,2), int64(350), ["one", "two", "three"; "four", "five","six"]});
557558
addEvent(sp, "quux", attributes);
559+
event2time = datetime("now", "TimeZone", "UTC");
558560
endSpan(sp);
559561

560562
results = readJsonResults(testCase);
561563
nvattributesstruct = struct(nvattributes{:});
562564

565+
tol = seconds(2); % tolerance for testing times
566+
563567
% event 1
564568
verifyEqual(testCase, results{1}.resourceSpans.scopeSpans.spans.events(1).name, 'baz');
569+
verifyLessThanOrEqual(testCase, abs(datetime(double(string(...
570+
results{1}.resourceSpans.scopeSpans.spans.events(1).timeUnixNano))/1e9, ...
571+
"convertFrom", "posixtime", "TimeZone", "UTC") - event1time), tol);
565572

566573
event1keys = string({results{1}.resourceSpans.scopeSpans.spans.events(1).attributes.key});
567574

@@ -587,6 +594,9 @@ function testEvents(testCase)
587594

588595
% event 2
589596
verifyEqual(testCase, results{1}.resourceSpans.scopeSpans.spans.events(2).name, 'quux');
597+
verifyLessThanOrEqual(testCase, abs(datetime(double(string(...
598+
results{1}.resourceSpans.scopeSpans.spans.events(2).timeUnixNano))/1e9, ...
599+
"convertFrom", "posixtime", "TimeZone", "UTC") - event2time), tol);
590600

591601
event2keys = string({results{1}.resourceSpans.scopeSpans.spans.events(2).attributes.key});
592602

0 commit comments

Comments
 (0)