Skip to content

Commit

Permalink
Merge branch 'main' into exemplar
Browse files Browse the repository at this point in the history
  • Loading branch information
lalitb authored Oct 5, 2022
2 parents 0ac42d4 + 1bda597 commit 339bef5
Show file tree
Hide file tree
Showing 29 changed files with 117 additions and 106 deletions.
7 changes: 5 additions & 2 deletions examples/common/metrics_foo_library/foo_library.cc
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,16 @@ class MeasurementFetcher
if (nostd::holds_alternative<
nostd::shared_ptr<opentelemetry::metrics::ObserverResultT<double>>>(observer_result))
{
double val = (rand() % 700) + 1.1;
double random_incr = (rand() % 5) + 1.1;
value_ += random_incr;
nostd::get<nostd::shared_ptr<opentelemetry::metrics::ObserverResultT<double>>>(
observer_result)
->Observe(val /*, labelkv */);
->Observe(value_ /*, labelkv */);
}
}
static double value_;
};
double MeasurementFetcher::value_ = 0.0;
} // namespace

void foo_library::counter_example(const std::string &name)
Expand Down
2 changes: 1 addition & 1 deletion examples/metrics_simple/metrics_ostream.cc
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ void initMetrics(const std::string &name)
std::unique_ptr<metric_sdk::MeterSelector> observable_meter_selector{
new metric_sdk::MeterSelector(name, version, schema)};
std::unique_ptr<metric_sdk::View> observable_sum_view{
new metric_sdk::View{name, "description", metric_sdk::AggregationType::kSum}};
new metric_sdk::View{name, "test_description", metric_sdk::AggregationType::kSum}};
p->AddView(std::move(observable_instrument_selector), std::move(observable_meter_selector),
std::move(observable_sum_view));

Expand Down
11 changes: 1 addition & 10 deletions exporters/ostream/src/metric_exporter.cc
Original file line number Diff line number Diff line change
Expand Up @@ -205,16 +205,7 @@ void OStreamMetricExporter::printPointData(const opentelemetry::sdk::metrics::Po
}

sout_ << "\n buckets : ";
if (nostd::holds_alternative<std::list<double>>(histogram_point_data.boundaries_))
{
auto &double_boundaries = nostd::get<std::list<double>>(histogram_point_data.boundaries_);
printVec(sout_, double_boundaries);
}
else if (nostd::holds_alternative<std::list<long>>(histogram_point_data.boundaries_))
{
auto &long_boundaries = nostd::get<std::list<long>>(histogram_point_data.boundaries_);
printVec(sout_, long_boundaries);
}
printVec(sout_, histogram_point_data.boundaries_);

sout_ << "\n counts : ";
printVec(sout_, histogram_point_data.counts_);
Expand Down
2 changes: 1 addition & 1 deletion exporters/ostream/test/ostream_metric_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ TEST(OStreamMetricsExporter, ExportHistogramPointData)
histogram_point_data.min_ = 1.8;
histogram_point_data.max_ = 12.0;
metric_sdk::HistogramPointData histogram_point_data2{};
histogram_point_data2.boundaries_ = std::list<long>{10, 20, 30};
histogram_point_data2.boundaries_ = std::list<double>{10.0, 20.0, 30.0};
histogram_point_data2.count_ = 3;
histogram_point_data2.counts_ = {200, 300, 400, 500};
histogram_point_data2.sum_ = 900l;
Expand Down
17 changes: 3 additions & 14 deletions exporters/otlp/src/otlp_metric_utils.cc
Original file line number Diff line number Diff line change
Expand Up @@ -126,21 +126,10 @@ void OtlpMetricUtils::ConvertHistogramMetric(
}
}
// buckets
if ((nostd::holds_alternative<std::list<double>>(histogram_data.boundaries_)))
{
auto boundaries = nostd::get<std::list<double>>(histogram_data.boundaries_);
for (auto bound : boundaries)
{
proto_histogram_point_data->add_explicit_bounds(bound);
}
}
else

for (auto bound : histogram_data.boundaries_)
{
auto boundaries = nostd::get<std::list<long>>(histogram_data.boundaries_);
for (auto bound : boundaries)
{
proto_histogram_point_data->add_explicit_bounds(bound);
}
proto_histogram_point_data->add_explicit_bounds(bound);
}
// bucket counts
for (auto bucket_value : histogram_data.counts_)
Expand Down
8 changes: 4 additions & 4 deletions exporters/otlp/test/otlp_http_metric_exporter_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -482,14 +482,14 @@ class OtlpHttpMetricExporterTestPeer : public ::testing::Test
auto exporter = GetExporter(std::unique_ptr<OtlpHttpClient>{mock_otlp_http_client});

opentelemetry::sdk::metrics::HistogramPointData histogram_point_data{};
histogram_point_data.boundaries_ = std::list<double>{10.1, 20.2, 30.2};
histogram_point_data.boundaries_ = {10.1, 20.2, 30.2};
histogram_point_data.count_ = 3;
histogram_point_data.counts_ = {200, 300, 400, 500};
histogram_point_data.sum_ = 900.5;
histogram_point_data.min_ = 1.8;
histogram_point_data.max_ = 19.0;
opentelemetry::sdk::metrics::HistogramPointData histogram_point_data2{};
histogram_point_data2.boundaries_ = std::list<long>{10, 20, 30};
histogram_point_data2.boundaries_ = {10.0, 20.0, 30.0};
histogram_point_data2.count_ = 3;
histogram_point_data2.counts_ = {200, 300, 400, 500};
histogram_point_data2.sum_ = 900l;
Expand Down Expand Up @@ -619,12 +619,12 @@ class OtlpHttpMetricExporterTestPeer : public ::testing::Test
"library_name", "1.5.0");

opentelemetry::sdk::metrics::HistogramPointData histogram_point_data{};
histogram_point_data.boundaries_ = std::list<double>{10.1, 20.2, 30.2};
histogram_point_data.boundaries_ = {10.1, 20.2, 30.2};
histogram_point_data.count_ = 3;
histogram_point_data.counts_ = {200, 300, 400, 500};
histogram_point_data.sum_ = 900.5;
opentelemetry::sdk::metrics::HistogramPointData histogram_point_data2{};
histogram_point_data2.boundaries_ = std::list<long>{10, 20, 30};
histogram_point_data2.boundaries_ = {10.0, 20.0, 30.0};
histogram_point_data2.count_ = 3;
histogram_point_data2.counts_ = {200, 300, 400, 500};
histogram_point_data2.sum_ = 900l;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ class PrometheusExporterUtils
*/
template <typename T>
static void SetData(std::vector<T> values,
const opentelemetry::sdk::metrics::ListType &boundaries,
const std::list<double> &boundaries,
const std::vector<uint64_t> &counts,
const opentelemetry::sdk::metrics::PointAttributes &labels,
std::chrono::nanoseconds time,
Expand Down Expand Up @@ -103,9 +103,9 @@ class PrometheusExporterUtils
/**
* Handle Histogram
*/
template <typename T, typename U>
template <typename T>
static void SetValue(std::vector<T> values,
const std::list<U> &boundaries,
const std::list<double> &boundaries,
const std::vector<uint64_t> &counts,
::prometheus::ClientMetric *metric);
};
Expand Down
15 changes: 4 additions & 11 deletions exporters/prometheus/src/exporter_utils.cc
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ void PrometheusExporterUtils::SetData(std::vector<T> values,
*/
template <typename T>
void PrometheusExporterUtils::SetData(std::vector<T> values,
const opentelemetry::sdk::metrics::ListType &boundaries,
const std::list<double> &boundaries,
const std::vector<uint64_t> &counts,
const metric_sdk::PointAttributes &labels,
std::chrono::nanoseconds time,
Expand All @@ -206,14 +206,7 @@ void PrometheusExporterUtils::SetData(std::vector<T> values,
metric_family->metric.emplace_back();
prometheus_client::ClientMetric &metric = metric_family->metric.back();
SetMetricBasic(metric, time, labels);
if (nostd::holds_alternative<std::list<long>>(boundaries))
{
SetValue(values, nostd::get<std::list<long>>(boundaries), counts, &metric);
}
else
{
SetValue(values, nostd::get<std::list<double>>(boundaries), counts, &metric);
}
SetValue(values, boundaries, counts, &metric);
}

/**
Expand Down Expand Up @@ -321,9 +314,9 @@ void PrometheusExporterUtils::SetValue(std::vector<T> values,
/**
* Handle Histogram
*/
template <typename T, typename U>
template <typename T>
void PrometheusExporterUtils::SetValue(std::vector<T> values,
const std::list<U> &boundaries,
const std::list<double> &boundaries,
const std::vector<uint64_t> &counts,
prometheus_client::ClientMetric *metric)
{
Expand Down
4 changes: 2 additions & 2 deletions exporters/prometheus/test/prometheus_test_helper.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,12 @@ inline metric_sdk::ResourceMetrics CreateSumPointData()
inline metric_sdk::ResourceMetrics CreateHistogramPointData()
{
metric_sdk::HistogramPointData histogram_point_data{};
histogram_point_data.boundaries_ = std::list<double>{10.1, 20.2, 30.2};
histogram_point_data.boundaries_ = {10.1, 20.2, 30.2};
histogram_point_data.count_ = 3;
histogram_point_data.counts_ = {200, 300, 400, 500};
histogram_point_data.sum_ = 900.5;
metric_sdk::HistogramPointData histogram_point_data2{};
histogram_point_data2.boundaries_ = std::list<long>{10, 20, 30};
histogram_point_data2.boundaries_ = {10.0, 20.0, 30.0};
histogram_point_data2.count_ = 3;
histogram_point_data2.counts_ = {200, 300, 400, 500};
histogram_point_data2.sum_ = 900l;
Expand Down
2 changes: 1 addition & 1 deletion sdk/include/opentelemetry/sdk/logs/batch_log_processor.h
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ class BatchLogProcessor : public LogProcessor
* @param record the log record
*/

void OnReceive(std::unique_ptr<Recordable> &&record) noexcept override;
void OnEmit(std::unique_ptr<Recordable> &&record) noexcept override;

/**
* Export all log records that have not been exported yet.
Expand Down
4 changes: 2 additions & 2 deletions sdk/include/opentelemetry/sdk/logs/multi_log_processor.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,10 @@ class MultiLogProcessor : public LogProcessor
std::unique_ptr<Recordable> MakeRecordable() noexcept override;

/**
* OnReceive is called by the SDK once a log record has been successfully created.
* OnEmit is called by the SDK once a log record has been successfully created.
* @param record the log record
*/
void OnReceive(std::unique_ptr<Recordable> &&record) noexcept override;
void OnEmit(std::unique_ptr<Recordable> &&record) noexcept override;

/**
* Exports all log records that have not yet been exported to the configured Exporter.
Expand Down
4 changes: 2 additions & 2 deletions sdk/include/opentelemetry/sdk/logs/processor.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,10 @@ class LogProcessor
virtual std::unique_ptr<Recordable> MakeRecordable() noexcept = 0;

/**
* OnReceive is called by the SDK once a log record has been successfully created.
* OnEmit is called by the SDK once a log record has been successfully created.
* @param record the log record
*/
virtual void OnReceive(std::unique_ptr<Recordable> &&record) noexcept = 0;
virtual void OnEmit(std::unique_ptr<Recordable> &&record) noexcept = 0;

/**
* Exports all log records that have not yet been exported to the configured Exporter.
Expand Down
2 changes: 1 addition & 1 deletion sdk/include/opentelemetry/sdk/logs/simple_log_processor.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class SimpleLogProcessor : public LogProcessor

std::unique_ptr<Recordable> MakeRecordable() noexcept override;

void OnReceive(std::unique_ptr<Recordable> &&record) noexcept override;
void OnEmit(std::unique_ptr<Recordable> &&record) noexcept override;

bool ForceFlush(
std::chrono::microseconds timeout = std::chrono::microseconds::max()) noexcept override;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ template <typename T>
class HistogramAggregationConfig : public AggregationConfig
{
public:
std::list<T> boundaries_;
std::list<double> boundaries_;
bool record_min_max_ = true;
};
} // namespace metrics
Expand Down
5 changes: 2 additions & 3 deletions sdk/include/opentelemetry/sdk/metrics/data/point_data.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ namespace metrics
{

using ValueType = nostd::variant<long, double>;
using ListType = nostd::variant<std::list<long>, std::list<double>>;

// TODO: remove ctors and initializers from below classes when GCC<5 stops shipping on Ubuntu

Expand Down Expand Up @@ -55,8 +54,8 @@ class HistogramPointData
HistogramPointData &operator=(HistogramPointData &&) = default;
HistogramPointData(const HistogramPointData &) = default;
HistogramPointData() = default;

ListType boundaries_ = {};
HistogramPointData(std::list<double> &boundaries) : boundaries_(boundaries) {}
std::list<double> boundaries_ = {};
ValueType sum_ = {};
ValueType min_ = {};
ValueType max_ = {};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,9 @@ class AsyncMetricStorage : public MetricStorage, public AsyncWritableMetricStora
void Record(const std::unordered_map<MetricAttributes, T, AttributeHashGenerator> &measurements,
opentelemetry::common::SystemTimestamp /* observation_time */) noexcept
{
// process the read measurements - aggregate and store in hashmap
// Async counter always record monotonically increasing values, and the
// exporter/reader can request either for delta or cumulative value.
// So we convert the async counter value to delta before passing it to temporal storage.
std::lock_guard<opentelemetry::common::SpinLockMutex> guard(hashmap_lock_);
for (auto &measurement : measurements)
{
Expand All @@ -53,13 +55,14 @@ class AsyncMetricStorage : public MetricStorage, public AsyncWritableMetricStora
if (prev)
{
auto delta = prev->Diff(*aggr);
cumulative_hash_map_->Set(measurement.first,
DefaultAggregation::CloneAggregation(
aggregation_type_, instrument_descriptor_, *delta));
// store received value in cumulative map, and the diff in delta map (to pass it to temporal
// storage)
cumulative_hash_map_->Set(measurement.first, std::move(aggr));
delta_hash_map_->Set(measurement.first, std::move(delta));
}
else
{
// store received value in cumulative and delta map.
cumulative_hash_map_->Set(
measurement.first,
DefaultAggregation::CloneAggregation(aggregation_type_, instrument_descriptor_, *aggr));
Expand Down
2 changes: 1 addition & 1 deletion sdk/src/logs/batch_log_processor.cc
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ std::unique_ptr<Recordable> BatchLogProcessor::MakeRecordable() noexcept
return exporter_->MakeRecordable();
}

void BatchLogProcessor::OnReceive(std::unique_ptr<Recordable> &&record) noexcept
void BatchLogProcessor::OnEmit(std::unique_ptr<Recordable> &&record) noexcept
{
if (synchronization_data_->is_shutdown.load() == true)
{
Expand Down
2 changes: 1 addition & 1 deletion sdk/src/logs/logger.cc
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ void Logger::Log(opentelemetry::logs::Severity severity,
}

// Send the log record to the processor
processor.OnReceive(std::move(recordable));
processor.OnEmit(std::move(recordable));
}

const opentelemetry::sdk::instrumentationscope::InstrumentationScope &
Expand Down
4 changes: 2 additions & 2 deletions sdk/src/logs/multi_log_processor.cc
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ std::unique_ptr<Recordable> MultiLogProcessor::MakeRecordable() noexcept
return recordable;
}

void MultiLogProcessor::OnReceive(std::unique_ptr<Recordable> &&record) noexcept
void MultiLogProcessor::OnEmit(std::unique_ptr<Recordable> &&record) noexcept
{
if (!record)
{
Expand All @@ -61,7 +61,7 @@ void MultiLogProcessor::OnReceive(std::unique_ptr<Recordable> &&record) noexcept
auto recordable = multi_recordable->ReleaseRecordable(*processor);
if (recordable)
{
processor->OnReceive(std::move(recordable));
processor->OnEmit(std::move(recordable));
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion sdk/src/logs/simple_log_processor.cc
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ std::unique_ptr<Recordable> SimpleLogProcessor::MakeRecordable() noexcept
* Batches the log record it receives in a batch of 1 and immediately sends it
* to the configured exporter
*/
void SimpleLogProcessor::OnReceive(std::unique_ptr<Recordable> &&record) noexcept
void SimpleLogProcessor::OnEmit(std::unique_ptr<Recordable> &&record) noexcept
{
nostd::span<std::unique_ptr<Recordable>> batch(&record, 1);
// Get lock to ensure Export() is never called concurrently
Expand Down
16 changes: 7 additions & 9 deletions sdk/src/metrics/aggregation/histogram_aggregation.cc
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,15 @@ LongHistogramAggregation::LongHistogramAggregation(
}
else
{
point_data_.boundaries_ = std::list<long>{0l, 5l, 10l, 25l, 50l, 75l, 100l, 250l, 500l, 1000l};
point_data_.boundaries_ = {0.0, 5.0, 10.0, 25.0, 50.0, 75.0, 100.0, 250.0,
500.0, 750.0, 1000.0, 2500.0, 5000.0, 7500.0, 10000.0};
}

if (aggregation_config)
{
record_min_max_ = aggregation_config->record_min_max_;
}
point_data_.counts_ =
std::vector<uint64_t>(nostd::get<std::list<long>>(point_data_.boundaries_).size() + 1, 0);
point_data_.counts_ = std::vector<uint64_t>(point_data_.boundaries_.size() + 1, 0);
point_data_.sum_ = 0l;
point_data_.count_ = 0;
point_data_.record_min_max_ = record_min_max_;
Expand Down Expand Up @@ -59,8 +60,7 @@ void LongHistogramAggregation::Aggregate(long value,
point_data_.max_ = std::max(nostd::get<long>(point_data_.max_), value);
}
size_t index = 0;
for (auto it = nostd::get<std::list<long>>(point_data_.boundaries_).begin();
it != nostd::get<std::list<long>>(point_data_.boundaries_).end(); ++it)
for (auto it = point_data_.boundaries_.begin(); it != point_data_.boundaries_.end(); ++it)
{
if (value < *it)
{
Expand Down Expand Up @@ -114,8 +114,7 @@ DoubleHistogramAggregation::DoubleHistogramAggregation(
{
record_min_max_ = aggregation_config->record_min_max_;
}
point_data_.counts_ =
std::vector<uint64_t>(nostd::get<std::list<double>>(point_data_.boundaries_).size() + 1, 0);
point_data_.counts_ = std::vector<uint64_t>(point_data_.boundaries_.size() + 1, 0);
point_data_.sum_ = 0.0;
point_data_.count_ = 0;
point_data_.record_min_max_ = record_min_max_;
Expand Down Expand Up @@ -143,8 +142,7 @@ void DoubleHistogramAggregation::Aggregate(double value,
point_data_.max_ = std::max(nostd::get<double>(point_data_.max_), value);
}
size_t index = 0;
for (auto it = nostd::get<std::list<double>>(point_data_.boundaries_).begin();
it != nostd::get<std::list<double>>(point_data_.boundaries_).end(); ++it)
for (auto it = point_data_.boundaries_.begin(); it != point_data_.boundaries_.end(); ++it)
{
if (value < *it)
{
Expand Down
1 change: 0 additions & 1 deletion sdk/src/metrics/meter.cc
Original file line number Diff line number Diff line change
Expand Up @@ -293,7 +293,6 @@ std::unique_ptr<AsyncWritableMetricStorage> Meter::RegisterAsyncMetricStorage(
std::vector<MetricData> Meter::Collect(CollectorHandle *collector,
opentelemetry::common::SystemTimestamp collect_ts) noexcept
{

observable_registry_->Observe(collect_ts);
std::vector<MetricData> metric_data_list;
auto ctx = meter_context_.lock();
Expand Down
1 change: 1 addition & 0 deletions sdk/src/metrics/state/temporal_metric_storage.cc
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ bool TemporalMetricStorage::buildMetrics(CollectorHandle *collector,
opentelemetry::common::SystemTimestamp last_collection_ts = sdk_start_ts;
AggregationTemporality aggregation_temporarily =
collector->GetAggregationTemporality(instrument_descriptor_.type_);

if (delta_metrics->Size())
{
for (auto &col : collectors)
Expand Down
Loading

0 comments on commit 339bef5

Please sign in to comment.