1 // Copyright The OpenTelemetry Authors
2 // SPDX-License-Identifier: Apache-2.0
4 #ifndef ENABLE_METRICS_PREVIEW
5 # include "opentelemetry/sdk/metrics/aggregation/lastvalue_aggregation.h"
6 # include "opentelemetry/common/timestamp.h"
7 # include "opentelemetry/version.h"
11 OPENTELEMETRY_BEGIN_NAMESPACE
17 LongLastValueAggregation::LongLastValueAggregation()
19 point_data_
.is_lastvalue_valid_
= false;
20 point_data_
.value_
= 0l;
23 LongLastValueAggregation::LongLastValueAggregation(LastValuePointData
&&data
)
24 : point_data_
{std::move(data
)}
27 LongLastValueAggregation::LongLastValueAggregation(const LastValuePointData
&data
)
31 void LongLastValueAggregation::Aggregate(long value
, const PointAttributes
&attributes
) noexcept
33 const std::lock_guard
<opentelemetry::common::SpinLockMutex
> locked(lock_
);
34 point_data_
.is_lastvalue_valid_
= true;
35 point_data_
.value_
= value
;
38 std::unique_ptr
<Aggregation
> LongLastValueAggregation::Merge(
39 const Aggregation
&delta
) const noexcept
41 if (nostd::get
<LastValuePointData
>(ToPoint()).sample_ts_
.time_since_epoch() >
42 nostd::get
<LastValuePointData
>(delta
.ToPoint()).sample_ts_
.time_since_epoch())
44 LastValuePointData merge_data
= std::move(nostd::get
<LastValuePointData
>(ToPoint()));
45 return std::unique_ptr
<Aggregation
>(new LongLastValueAggregation(std::move(merge_data
)));
49 LastValuePointData merge_data
= std::move(nostd::get
<LastValuePointData
>(delta
.ToPoint()));
50 return std::unique_ptr
<Aggregation
>(new LongLastValueAggregation(std::move(merge_data
)));
54 std::unique_ptr
<Aggregation
> LongLastValueAggregation::Diff(const Aggregation
&next
) const noexcept
56 if (nostd::get
<LastValuePointData
>(ToPoint()).sample_ts_
.time_since_epoch() >
57 nostd::get
<LastValuePointData
>(next
.ToPoint()).sample_ts_
.time_since_epoch())
59 LastValuePointData diff_data
= std::move(nostd::get
<LastValuePointData
>(ToPoint()));
60 return std::unique_ptr
<Aggregation
>(new LongLastValueAggregation(std::move(diff_data
)));
64 LastValuePointData diff_data
= std::move(nostd::get
<LastValuePointData
>(next
.ToPoint()));
65 return std::unique_ptr
<Aggregation
>(new LongLastValueAggregation(std::move(diff_data
)));
69 PointType
LongLastValueAggregation::ToPoint() const noexcept
74 DoubleLastValueAggregation::DoubleLastValueAggregation()
76 point_data_
.is_lastvalue_valid_
= false;
77 point_data_
.value_
= 0.0;
80 DoubleLastValueAggregation::DoubleLastValueAggregation(LastValuePointData
&&data
)
81 : point_data_
{std::move(data
)}
84 DoubleLastValueAggregation::DoubleLastValueAggregation(const LastValuePointData
&data
)
88 void DoubleLastValueAggregation::Aggregate(double value
, const PointAttributes
&attributes
) noexcept
90 const std::lock_guard
<opentelemetry::common::SpinLockMutex
> locked(lock_
);
91 point_data_
.is_lastvalue_valid_
= true;
92 point_data_
.value_
= value
;
95 std::unique_ptr
<Aggregation
> DoubleLastValueAggregation::Merge(
96 const Aggregation
&delta
) const noexcept
98 if (nostd::get
<LastValuePointData
>(ToPoint()).sample_ts_
.time_since_epoch() >
99 nostd::get
<LastValuePointData
>(delta
.ToPoint()).sample_ts_
.time_since_epoch())
101 LastValuePointData merge_data
= std::move(nostd::get
<LastValuePointData
>(ToPoint()));
102 return std::unique_ptr
<Aggregation
>(new LongLastValueAggregation(std::move(merge_data
)));
106 LastValuePointData merge_data
= std::move(nostd::get
<LastValuePointData
>(delta
.ToPoint()));
107 return std::unique_ptr
<Aggregation
>(new LongLastValueAggregation(std::move(merge_data
)));
111 std::unique_ptr
<Aggregation
> DoubleLastValueAggregation::Diff(
112 const Aggregation
&next
) const noexcept
114 if (nostd::get
<LastValuePointData
>(ToPoint()).sample_ts_
.time_since_epoch() >
115 nostd::get
<LastValuePointData
>(next
.ToPoint()).sample_ts_
.time_since_epoch())
117 LastValuePointData diff_data
= std::move(nostd::get
<LastValuePointData
>(ToPoint()));
118 return std::unique_ptr
<Aggregation
>(new LongLastValueAggregation(std::move(diff_data
)));
122 LastValuePointData diff_data
= std::move(nostd::get
<LastValuePointData
>(next
.ToPoint()));
123 return std::unique_ptr
<Aggregation
>(new LongLastValueAggregation(std::move(diff_data
)));
127 PointType
DoubleLastValueAggregation::ToPoint() const noexcept
131 } // namespace metrics
133 OPENTELEMETRY_END_NAMESPACE