Skip to content

Commit cc7f9bf

Browse files
RoadRunnralbertored
authored andcommitted
fixup: change timestamp types
1 parent f3c7cbe commit cc7f9bf

File tree

1 file changed

+27
-23
lines changed

1 file changed

+27
-23
lines changed

apps/opentelemetry_experimental/src/otel_metric_exporter_prometheus.erl

Lines changed: 27 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ fake_info_metric(Name, Scope, Attributes, Description) ->
112112
description=Description,
113113
data=#gauge{datapoints=[#datapoint{
114114
attributes=Attributes, value=1, exemplars=[],
115-
flags=0, start_time_unix_nano=0, time_unix_nano=0
115+
flags=0, start_time=0, time=0
116116
}]}
117117
}.
118118

@@ -191,7 +191,7 @@ data(MetricName, Datapoints, Scope, AddCreated, #opts{add_scope_info=AddScopeInf
191191

192192
lists:foldl(
193193
fun(DP, Acc) ->
194-
datapoint(DP, MetricName, AddCreated, ScopeLabels, Acc)
194+
datapoint(DP, MetricName, AddCreated, ScopeLabels, Acc)
195195
end,
196196
[[], []],
197197
Datapoints
@@ -200,12 +200,12 @@ data(MetricName, Datapoints, Scope, AddCreated, #opts{add_scope_info=AddScopeInf
200200
datapoint(#datapoint{} = DP, MetricName, AddCreated, ScopeLabels, [Points, Created]) ->
201201
Labels = surround_labels(join_labels(ScopeLabels, labels(DP#datapoint.attributes))),
202202
Point = [MetricName, Labels, " ", number_to_binary(DP#datapoint.value), "\n"],
203-
Created1 = created(AddCreated, Created, MetricName, Labels, DP#datapoint.start_time_unix_nano),
203+
Created1 = created(AddCreated, Created, MetricName, Labels, DP#datapoint.start_time),
204204
[[Point | Points], Created1];
205205
datapoint(#histogram_datapoint{} = DP, MetricName, AddCreated, ScopeLabels, [Points, Created]) ->
206206
Labels = join_labels(ScopeLabels, labels(DP#histogram_datapoint.attributes)),
207207
SurroundedLabels = surround_labels(Labels),
208-
208+
209209
Count = lists:sum(DP#histogram_datapoint.bucket_counts),
210210
CountPoint = [MetricName, "_count", SurroundedLabels, " ", number_to_binary(Count), "\n"],
211211

@@ -223,14 +223,14 @@ datapoint(#histogram_datapoint{} = DP, MetricName, AddCreated, ScopeLabels, [Poi
223223
lists:zip(DP#histogram_datapoint.bucket_counts, DP#histogram_datapoint.explicit_bounds ++ [<<"+Inf">>])
224224
),
225225

226-
Created1 = created(AddCreated, Created, MetricName, SurroundedLabels, DP#histogram_datapoint.start_time_unix_nano),
226+
Created1 = created(AddCreated, Created, MetricName, SurroundedLabels, DP#histogram_datapoint.start_time),
227227

228228
[[Buckets, CountPoint, SumPoint | Points], Created1].
229229

230-
created(false, Created, _MetricName, _Labels, _Value) ->
230+
created(false, Created, _MetricName, _Labels, _StartTime) ->
231231
Created;
232-
created(true, Created, MetricName, Labels, Value) ->
233-
[[MetricName, "_created", Labels, " ", number_to_binary(Value), "\n"] | Created].
232+
created(true, Created, MetricName, Labels, StartTime) ->
233+
[[MetricName, "_created", Labels, " ", number_to_binary(opentelemetry:timestamp_to_nano(StartTime)), "\n"] | Created].
234234

235235
join_labels(<<>>, L) -> L;
236236
join_labels(L, <<>> )-> L;
@@ -328,12 +328,16 @@ escape_help_char(X) ->
328328

329329
-ifdef(TEST).
330330

331+
nano_to_timestamp(Nano) ->
332+
Offset = erlang:time_offset(),
333+
erlang:convert_time_unit(Nano, nanosecond, native) - Offset.
334+
331335
metrics_to_string(Metrics) ->
332336
metrics_to_string(Metrics, #{}).
333337

334338
metrics_to_string(Metrics, Opts) ->
335339
Resource = otel_resource:create(#{"res" => "b"}, "url"),
336-
{ok, Opts1} = init(Opts),
340+
{ok, Opts1} = init(Opts#{order => ordered}),
337341
lists:flatten(io_lib:format("~ts", [parse_metrics(Metrics, Resource, Opts1)])).
338342

339343
lines_join(Lines) ->
@@ -376,15 +380,15 @@ monotonic_counter_test() ->
376380
datapoints = [
377381
#datapoint{
378382
attributes = #{},
379-
start_time_unix_nano = 0,
380-
time_unix_nano = 1,
383+
start_time = nano_to_timestamp(0),
384+
time = nano_to_timestamp(1),
381385
value = 2,
382386
flags = 0
383387
},
384388
#datapoint{
385389
attributes = #{<<"foo">> => 1},
386-
start_time_unix_nano = 123,
387-
time_unix_nano = 456,
390+
start_time = nano_to_timestamp(123),
391+
time = nano_to_timestamp(456),
388392
value = 789,
389393
flags = 0
390394
}
@@ -427,8 +431,8 @@ not_monotonic_counter_test() ->
427431
datapoints = [
428432
#datapoint{
429433
attributes = #{},
430-
start_time_unix_nano = 0,
431-
time_unix_nano = 1,
434+
start_time = nano_to_timestamp(0),
435+
time = nano_to_timestamp(1),
432436
value = 2,
433437
flags = 0
434438
}
@@ -465,8 +469,8 @@ gauge_test() ->
465469
datapoints = [
466470
#datapoint{
467471
attributes = #{<<"foo">> => 1},
468-
start_time_unix_nano = 123,
469-
time_unix_nano = 456,
472+
start_time = nano_to_timestamp(123),
473+
time = nano_to_timestamp(456),
470474
value = 2.0,
471475
flags = 0
472476
}
@@ -506,8 +510,8 @@ monotonic_histogram_test() ->
506510
datapoints = [
507511
#histogram_datapoint{
508512
attributes = #{},
509-
start_time_unix_nano = 0,
510-
time_unix_nano = 1,
513+
start_time = nano_to_timestamp(0),
514+
time = nano_to_timestamp(1),
511515
count = 3,
512516
sum = 7,
513517
bucket_counts = [2,0,1],
@@ -557,8 +561,8 @@ not_monotonic_histogram_test() ->
557561
datapoints = [
558562
#histogram_datapoint{
559563
attributes = #{},
560-
start_time_unix_nano = 0,
561-
time_unix_nano = 1,
564+
start_time = nano_to_timestamp(0),
565+
time = nano_to_timestamp(1),
562566
count = 1,
563567
sum = 3,
564568
bucket_counts = [0,0,1],
@@ -608,8 +612,8 @@ no_otel_scope_test() ->
608612
datapoints = [
609613
#datapoint{
610614
attributes = #{},
611-
start_time_unix_nano = 0,
612-
time_unix_nano = 1,
615+
start_time = nano_to_timestamp(0),
616+
time = nano_to_timestamp(1),
613617
value = 2,
614618
flags = 0
615619
}

0 commit comments

Comments
 (0)