Skip to content

Commit f66398e

Browse files
authored
Merge pull request #197 from mathworks/timestamps
Fix two bugs related to Timestamps, fixes #193, fixes #194
2 parents be526bd + c663569 commit f66398e

File tree

9 files changed

+51
-29
lines changed

9 files changed

+51
-29
lines changed

.github/workflows/build_and_test_full.yml

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ env:
99
MLM_LICENSE_TOKEN: ${{ secrets.MLM_LICENSE_TOKEN }}
1010
jobs:
1111
get_version:
12-
runs-on: ubuntu-20.04
12+
runs-on: ubuntu-22.04
1313
outputs:
1414
version: ${{ steps.getversion.outputs.latest_tag }}
1515
steps:
@@ -25,9 +25,7 @@ jobs:
2525
working-directory: opentelemetry-matlab
2626
run: echo "latest_tag=$(sed -re 's/^v?([[:digit:]]+\.[[:digit:]]+\.[[:digit:]]+)?.*/\1/' <<< $(git describe --tags --abbrev=0))" >> $GITHUB_OUTPUT
2727
build-and-run-tests-ubuntu:
28-
# Running on ubuntu-latest would use a glibc version that is incompatible when using the built mex files on a Debian 11
29-
# Instead, run on ubuntu-20.04
30-
runs-on: ubuntu-20.04
28+
runs-on: ubuntu-22.04
3129
needs: get_version
3230
env:
3331
OPENTELEMETRY_MATLAB_INSTALL: "${{ github.workspace }}/otel_matlab_install"
@@ -57,9 +55,7 @@ jobs:
5755
with:
5856
select-by-folder: opentelemetry-matlab/test
5957
code-coverage-build-and-run-tests-ubuntu:
60-
# Running on ubuntu-latest would use a glibc version that is incompatible when using the built mex files on a Debian 11
61-
# Instead, run on ubuntu-20.04
62-
runs-on: ubuntu-20.04
58+
runs-on: ubuntu-22.04
6359
needs: get_version
6460
env:
6561
OPENTELEMETRY_MATLAB_INSTALL: "${{ github.workspace }}/otel_matlab_install"

.github/workflows/build_and_test_simple.yml

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ env:
1010
MLM_LICENSE_TOKEN: ${{ secrets.MLM_LICENSE_TOKEN }}
1111
jobs:
1212
get_version:
13-
runs-on: ubuntu-20.04
13+
runs-on: ubuntu-22.04
1414
outputs:
1515
version: ${{ steps.getversion.outputs.latest_tag }}
1616
steps:
@@ -26,9 +26,7 @@ jobs:
2626
working-directory: opentelemetry-matlab
2727
run: echo "latest_tag=$(sed -re 's/^v?([[:digit:]]+\.[[:digit:]]+\.[[:digit:]]+)?.*/\1/' <<< $(git describe --tags --abbrev=0))" >> $GITHUB_OUTPUT
2828
build-and-run-tests-ubuntu:
29-
# Running on ubuntu-latest would use a glibc version that is incompatible when using the built mex files on a Debian 11
30-
# Instead, run on ubuntu-20.04
31-
runs-on: ubuntu-20.04
29+
runs-on: ubuntu-22.04
3230
needs: get_version
3331
env:
3432
OPENTELEMETRY_MATLAB_INSTALL: "${{ github.workspace }}/otel_matlab_install"

.github/workflows/create_release.yml

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,7 @@ on:
55
- '[0-9]+.[0-9]+.[0-9]+'
66
jobs:
77
build-ubuntu:
8-
# Running on ubuntu-latest would use a glibc version that is incompatible when using the built mex files on a Debian 11
9-
# Instead, run on ubuntu-20.04
10-
runs-on: ubuntu-20.04
8+
runs-on: ubuntu-22.04
119
env:
1210
OPENTELEMETRY_MATLAB_INSTALL: "${{ github.workspace }}/otel_matlab_install"
1311
steps:
@@ -97,7 +95,7 @@ jobs:
9795
path: ${{ github.workspace }}/otel-matlab-${{ matrix.os }}.tar.gz
9896
package-mltbx:
9997
name: Package MATLAB Toolbox (MLTBX) Files
100-
runs-on: ubuntu-20.04
98+
runs-on: ubuntu-22.04
10199
permissions:
102100
contents: write
103101
needs:

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/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);

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: 6 additions & 3 deletions
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});
@@ -282,15 +285,15 @@ function testTimestamp(testCase)
282285
% testTimestamp: specifying a timestamp
283286
lp = opentelemetry.sdk.logs.LoggerProvider();
284287
lg = getLogger(lp, "foo");
285-
timestamp = datetime(2020,3,12,9,45,0);
288+
timestamp = datetime(2020,3,12,9,45,0, "TimeZone", "UTC");
286289
emitLogRecord(lg, "info", "bar", "Timestamp", timestamp);
287290

288291
% perform test comparisons
289292
forceFlush(lp, testCase.ForceFlushTimeout);
290293
results = readJsonResults(testCase);
291294
verifyEqual(testCase, datetime(double(string(...
292295
results{1}.resourceLogs.scopeLogs.logRecords.timeUnixNano))/1e9, ...
293-
"convertFrom", "posixtime"), timestamp); % convert from nanoseconds to seconds
296+
"convertFrom", "posixtime", "TimeZone", "UTC"), timestamp); % convert from nanoseconds to seconds
294297
end
295298

296299
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)