Skip to content

Commit

Permalink
Merge branch 'main' into FixDllMissingSymbols
Browse files Browse the repository at this point in the history
  • Loading branch information
ThomsonTan authored Jun 3, 2023
2 parents 9c26a8f + 082bb93 commit 8170280
Show file tree
Hide file tree
Showing 8 changed files with 358 additions and 4 deletions.
3 changes: 2 additions & 1 deletion exporters/otlp/src/otlp_metric_utils.cc
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,8 @@ void OtlpMetricUtils::ConvertSumMetric(const metric_sdk::MetricData &metric_data
{
sum->set_aggregation_temporality(
GetProtoAggregationTemporality(metric_data.aggregation_temporality));
sum->set_is_monotonic(true);
sum->set_is_monotonic(metric_data.instrument_descriptor.type_ ==
metric_sdk::InstrumentType::kCounter);
auto start_ts = metric_data.start_ts.time_since_epoch().count();
auto ts = metric_data.end_ts.time_since_epoch().count();
for (auto &point_data_with_attributes : metric_data.point_data_attr_)
Expand Down
47 changes: 44 additions & 3 deletions exporters/otlp/test/otlp_metrics_serialization_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,34 @@ static metrics_sdk::MetricData CreateSumAggregationData()
point_data_attr_1.point_data = s_data_1;

point_data_attr_2.attributes = {{"k2", "v2"}};
point_data_attr_2.point_data = s_data_1;
point_data_attr_2.point_data = s_data_2;
std::vector<metrics_sdk::PointDataAttributes> point_data_attr;
point_data_attr.push_back(point_data_attr_1);
point_data_attr.push_back(point_data_attr_2);
data.point_data_attr_ = std::move(point_data_attr);
return data;
}

static metrics_sdk::MetricData CreateUpDownCounterAggregationData()
{
metrics_sdk::MetricData data;
data.start_ts = opentelemetry::common::SystemTimestamp(std::chrono::system_clock::now());
metrics_sdk::InstrumentDescriptor inst_desc = {"UpDownCounter", "Robot Pose Y", "Meter",
metrics_sdk::InstrumentType::kUpDownCounter,
metrics_sdk::InstrumentValueType::kDouble};
metrics_sdk::SumPointData s_data_1, s_data_2;
s_data_1.value_ = 1.35;
s_data_2.value_ = 1.37;

data.aggregation_temporality = metrics_sdk::AggregationTemporality::kCumulative;
data.end_ts = opentelemetry::common::SystemTimestamp(std::chrono::system_clock::now());
data.instrument_descriptor = inst_desc;
metrics_sdk::PointDataAttributes point_data_attr_1, point_data_attr_2;
point_data_attr_1.attributes = {{"environment_id", "DEV-AYS"}};
point_data_attr_1.point_data = s_data_1;

point_data_attr_2.attributes = {{"robot_id", "DEV-AYS-03-02"}};
point_data_attr_2.point_data = s_data_2;
std::vector<metrics_sdk::PointDataAttributes> point_data_attr;
point_data_attr.push_back(point_data_attr_1);
point_data_attr.push_back(point_data_attr_2);
Expand Down Expand Up @@ -68,7 +95,7 @@ static metrics_sdk::MetricData CreateHistogramAggregationData()
point_data_attr_1.point_data = s_data_1;

point_data_attr_2.attributes = {{"k2", "v2"}};
point_data_attr_2.point_data = s_data_1;
point_data_attr_2.point_data = s_data_2;
std::vector<metrics_sdk::PointDataAttributes> point_data_attr;
point_data_attr.push_back(point_data_attr_1);
point_data_attr.push_back(point_data_attr_2);
Expand All @@ -95,7 +122,7 @@ static metrics_sdk::MetricData CreateObservableGaugeAggregationData()
point_data_attr_1.point_data = s_data_1;

point_data_attr_2.attributes = {{"k2", "v2"}};
point_data_attr_2.point_data = s_data_1;
point_data_attr_2.point_data = s_data_2;
std::vector<metrics_sdk::PointDataAttributes> point_data_attr;
point_data_attr.push_back(point_data_attr_1);
point_data_attr.push_back(point_data_attr_2);
Expand All @@ -120,6 +147,20 @@ TEST(OtlpMetricSerializationTest, Counter)
EXPECT_EQ(1, 1);
}

TEST(OtlpMetricSerializationTest, UpDownCounter)
{
metrics_sdk::MetricData data = CreateUpDownCounterAggregationData();
opentelemetry::proto::metrics::v1::Sum sum;
otlp_exporter::OtlpMetricUtils::ConvertSumMetric(data, &sum);
EXPECT_EQ(sum.aggregation_temporality(),
proto::metrics::v1::AggregationTemporality::AGGREGATION_TEMPORALITY_CUMULATIVE);
EXPECT_EQ(sum.is_monotonic(), false);
EXPECT_EQ(sum.data_points(0).as_double(), 1.35);
EXPECT_EQ(sum.data_points(1).as_double(), 1.37);

EXPECT_EQ(1, 1);
}

TEST(OtlpMetricSerializationTest, Histogram)
{
metrics_sdk::MetricData data = CreateHistogramAggregationData();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0

#pragma once

#include "opentelemetry/version.h"

#include <cstdint>

OPENTELEMETRY_BEGIN_NAMESPACE
namespace sdk
{
namespace metrics
{

/*
* An indexer for base2 exponential histograms. It is used to calculate index for a given value and
* scale.
*/
class Base2ExponentialHistogramIndexer
{
public:
/*
* Construct a new indexer for a given scale.
*/
explicit Base2ExponentialHistogramIndexer(int32_t scale = 0);
Base2ExponentialHistogramIndexer(const Base2ExponentialHistogramIndexer &other) = default;
Base2ExponentialHistogramIndexer &operator=(const Base2ExponentialHistogramIndexer &other) =
default;

/**
* Compute the index for the given value.
*
* @param value Measured value (must be non-zero).
* @return the index of the bucket which the value maps to.
*/
int32_t ComputeIndex(double value) const;

private:
int32_t scale_;
double scale_factor_;
};

} // namespace metrics
} // namespace sdk
OPENTELEMETRY_END_NAMESPACE
1 change: 1 addition & 0 deletions sdk/src/metrics/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ add_library(
state/observable_registry.cc
state/sync_metric_storage.cc
state/temporal_metric_storage.cc
aggregation/base2_exponential_histogram_indexer.cc
aggregation/histogram_aggregation.cc
aggregation/lastvalue_aggregation.cc
aggregation/sum_aggregation.cc
Expand Down
71 changes: 71 additions & 0 deletions sdk/src/metrics/aggregation/base2_exponential_histogram_indexer.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0

#include "opentelemetry/sdk/metrics/aggregation/base2_exponential_histogram_indexer.h"

#include <cmath>

OPENTELEMETRY_BEGIN_NAMESPACE
namespace sdk
{
namespace metrics
{

namespace
{

const double kLogBase2E = 1.0 / std::log(2.0);

double ComputeScaleFactor(int32_t scale)
{
return std::scalbn(kLogBase2E, scale);
}

// Compute the bucket index using a logarithm based approach.
int32_t GetIndexByLogarithm(double value, double scale_factor)
{
return static_cast<int32_t>(std::ceil(std::log(value) * scale_factor)) - 1;
}

// Compute the bucket index at scale 0.
int32_t MapToIndexScaleZero(double value)
{
// Note: std::frexp() rounds submnormal values to the smallest normal value and returns an
// exponent corresponding to fractions in the range [0.5, 1), whereas an exponent for the range
// [1, 2), so subtract 1 from the exponent immediately.
int exp;
double frac = std::frexp(value, &exp);
exp--;

if (frac == 0.5)
{
// Special case for powers of two: they fall into the bucket numbered one less.
exp--;
}
return exp;
}

} // namespace

Base2ExponentialHistogramIndexer::Base2ExponentialHistogramIndexer(int32_t scale)
: scale_(scale), scale_factor_(scale > 0 ? ComputeScaleFactor(scale) : 0)
{}

int32_t Base2ExponentialHistogramIndexer::ComputeIndex(double value) const
{
const double abs_value = std::fabs(value);
// For positive scales, compute the index by logarithm, which is simpler but may be
// inaccurate near bucket boundaries
if (scale_ > 0)
{
return GetIndexByLogarithm(abs_value, scale_factor_);
}
// For scale zero, compute the exact index by extracting the exponent.
// For negative scales, compute the exact index by extracting the exponent and shifting it to
// the right by -scale
return MapToIndexScaleZero(abs_value) >> -scale_;
}

} // namespace metrics
} // namespace sdk
OPENTELEMETRY_END_NAMESPACE
15 changes: 15 additions & 0 deletions sdk/test/metrics/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,21 @@ cc_test(
],
)

cc_test(
name = "base2_exponential_histogram_indexer_test",
srcs = [
"base2_exponential_histogram_indexer_test.cc",
],
tags = [
"metrics",
"test",
],
deps = [
"metrics_common_test_utils",
"@com_google_googletest//:gtest_main",
],
)

cc_test(
name = "histogram_aggregation_test",
srcs = [
Expand Down
1 change: 1 addition & 0 deletions sdk/test/metrics/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ foreach(
histogram_aggregation_test
attributes_processor_test
attributes_hashmap_test
base2_exponential_histogram_indexer_test
circular_buffer_counter_test
histogram_test
sync_metric_storage_counter_test
Expand Down
Loading

0 comments on commit 8170280

Please sign in to comment.