diff --git a/sdk/src/metrics/aggregation/sum_aggregation.cc b/sdk/src/metrics/aggregation/sum_aggregation.cc index dc08721682..b27e676a70 100644 --- a/sdk/src/metrics/aggregation/sum_aggregation.cc +++ b/sdk/src/metrics/aggregation/sum_aggregation.cc @@ -45,25 +45,53 @@ void LongSumAggregation::Aggregate(int64_t value, const PointAttributes & /* att std::unique_ptr LongSumAggregation::Merge(const Aggregation &delta) const noexcept { - int64_t merge_value = - nostd::get( - nostd::get((static_cast(delta).ToPoint())) - .value_) + - nostd::get(nostd::get(ToPoint()).value_); + auto delta_point = static_cast(delta).ToPoint(); + auto curr_point = ToPoint(); + + const auto *delta_sum = nostd::get_if(&delta_point); + const auto *curr_sum = nostd::get_if(&curr_point); + if (delta_sum == nullptr || curr_sum == nullptr) + { + OTEL_INTERNAL_LOG_ERROR("LongSumAggregation::Merge - Loss of type"); + return std::unique_ptr(new LongSumAggregation(point_data_.is_monotonic_)); + } + + const auto *delta_val = nostd::get_if(&delta_sum->value_); + const auto *curr_val = nostd::get_if(&curr_sum->value_); + if (delta_val == nullptr || curr_val == nullptr) + { + OTEL_INTERNAL_LOG_ERROR("LongSumAggregation::Merge - Loss of type"); + return std::unique_ptr(new LongSumAggregation(point_data_.is_monotonic_)); + } + std::unique_ptr aggr(new LongSumAggregation(point_data_.is_monotonic_)); - static_cast(aggr.get())->point_data_.value_ = merge_value; + static_cast(aggr.get())->point_data_.value_ = *delta_val + *curr_val; return aggr; } std::unique_ptr LongSumAggregation::Diff(const Aggregation &next) const noexcept { - int64_t diff_value = - nostd::get( - nostd::get((static_cast(next).ToPoint())) - .value_) - - nostd::get(nostd::get(ToPoint()).value_); + auto next_point = static_cast(next).ToPoint(); + auto curr_point = ToPoint(); + + const auto *next_sum = nostd::get_if(&next_point); + const auto *curr_sum = nostd::get_if(&curr_point); + if (next_sum == nullptr || curr_sum == nullptr) + { + OTEL_INTERNAL_LOG_ERROR("LongSumAggregation::Diff - Loss of type"); + return std::unique_ptr(new LongSumAggregation(point_data_.is_monotonic_)); + } + + const auto *next_val = nostd::get_if(&next_sum->value_); + const auto *curr_val = nostd::get_if(&curr_sum->value_); + if (next_val == nullptr || curr_val == nullptr) + { + OTEL_INTERNAL_LOG_ERROR("LongSumAggregation::Diff - Loss of type"); + return std::unique_ptr(new LongSumAggregation(point_data_.is_monotonic_)); + } + std::unique_ptr aggr(new LongSumAggregation(point_data_.is_monotonic_)); - static_cast(aggr.get())->point_data_.value_ = diff_value; + static_cast(aggr.get())->point_data_.value_ = *next_val - *curr_val; return aggr; } @@ -98,25 +126,53 @@ void DoubleSumAggregation::Aggregate(double value, std::unique_ptr DoubleSumAggregation::Merge(const Aggregation &delta) const noexcept { - double merge_value = - nostd::get( - nostd::get((static_cast(delta).ToPoint())) - .value_) + - nostd::get(nostd::get(ToPoint()).value_); + auto delta_point = static_cast(delta).ToPoint(); + auto curr_point = ToPoint(); + + const auto *delta_sum = nostd::get_if(&delta_point); + const auto *curr_sum = nostd::get_if(&curr_point); + if (delta_sum == nullptr || curr_sum == nullptr) + { + OTEL_INTERNAL_LOG_ERROR("DoubleSumAggregation::Merge - Loss of type"); + return std::unique_ptr(new DoubleSumAggregation(point_data_.is_monotonic_)); + } + + const auto *delta_val = nostd::get_if(&delta_sum->value_); + const auto *curr_val = nostd::get_if(&curr_sum->value_); + if (delta_val == nullptr || curr_val == nullptr) + { + OTEL_INTERNAL_LOG_ERROR("DoubleSumAggregation::Merge - Loss of type"); + return std::unique_ptr(new DoubleSumAggregation(point_data_.is_monotonic_)); + } + std::unique_ptr aggr(new DoubleSumAggregation(point_data_.is_monotonic_)); - static_cast(aggr.get())->point_data_.value_ = merge_value; + static_cast(aggr.get())->point_data_.value_ = *delta_val + *curr_val; return aggr; } std::unique_ptr DoubleSumAggregation::Diff(const Aggregation &next) const noexcept { - double diff_value = - nostd::get( - nostd::get((static_cast(next).ToPoint())) - .value_) - - nostd::get(nostd::get(ToPoint()).value_); + auto next_point = static_cast(next).ToPoint(); + auto curr_point = ToPoint(); + + const auto *next_sum = nostd::get_if(&next_point); + const auto *curr_sum = nostd::get_if(&curr_point); + if (next_sum == nullptr || curr_sum == nullptr) + { + OTEL_INTERNAL_LOG_ERROR("DoubleSumAggregation::Diff - Loss of type"); + return std::unique_ptr(new DoubleSumAggregation(point_data_.is_monotonic_)); + } + + const auto *next_val = nostd::get_if(&next_sum->value_); + const auto *curr_val = nostd::get_if(&curr_sum->value_); + if (next_val == nullptr || curr_val == nullptr) + { + OTEL_INTERNAL_LOG_ERROR("DoubleSumAggregation::Diff - Loss of type"); + return std::unique_ptr(new DoubleSumAggregation(point_data_.is_monotonic_)); + } + std::unique_ptr aggr(new DoubleSumAggregation(point_data_.is_monotonic_)); - static_cast(aggr.get())->point_data_.value_ = diff_value; + static_cast(aggr.get())->point_data_.value_ = *next_val - *curr_val; return aggr; }