forked from open-telemetry/opentelemetry-cpp
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Asynchronous Aggregation storage (open-telemetry#1232)
- Loading branch information
1 parent
fc82009
commit 52fadef
Showing
12 changed files
with
308 additions
and
68 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
// Copyright The OpenTelemetry Authors | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
#pragma once | ||
#ifndef ENABLE_METRICS_PREVIEW | ||
# include "opentelemetry/common/key_value_iterable.h" | ||
# include "opentelemetry/metrics/observer_result.h" | ||
# include "opentelemetry/sdk/metrics/state/attributes_hashmap.h" | ||
# include "opentelemetry/sdk/metrics/view/attributes_processor.h" | ||
|
||
# include <map> | ||
|
||
OPENTELEMETRY_BEGIN_NAMESPACE | ||
namespace sdk | ||
{ | ||
namespace metrics | ||
{ | ||
template <class T> | ||
class ObserverResult final : public opentelemetry::metrics::ObserverResult<T> | ||
{ | ||
public: | ||
ObserverResult(const AttributesProcessor *attributes_processor) | ||
: attributes_processor_(attributes_processor) | ||
{} | ||
|
||
void Observe(T value) noexcept override { data_.insert({{}, value}); } | ||
|
||
void Observe(T value, const opentelemetry::common::KeyValueIterable &attributes) noexcept override | ||
{ | ||
auto attr = attributes_processor_->process(attributes); | ||
data_.insert({attr, value}); | ||
} | ||
|
||
const std::unordered_map<MetricAttributes, T, AttributeHashGenerator> &GetMeasurements() | ||
{ | ||
return data_; | ||
} | ||
|
||
private: | ||
std::unordered_map<MetricAttributes, T, AttributeHashGenerator> data_; | ||
|
||
const AttributesProcessor *attributes_processor_; | ||
}; | ||
} // namespace metrics | ||
} // namespace sdk | ||
|
||
OPENTELEMETRY_END_NAMESPACE | ||
#endif |
80 changes: 80 additions & 0 deletions
80
sdk/include/opentelemetry/sdk/metrics/state/async_metric_storage.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
// Copyright The OpenTelemetry Authors | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
#pragma once | ||
#ifndef ENABLE_METRICS_PREVIEW | ||
# include "opentelemetry/sdk/common/attributemap_hash.h" | ||
# include "opentelemetry/sdk/metrics/instruments.h" | ||
|
||
# include "opentelemetry/sdk/instrumentationlibrary/instrumentation_library.h" | ||
# include "opentelemetry/sdk/metrics/aggregation/default_aggregation.h" | ||
# include "opentelemetry/sdk/metrics/state/attributes_hashmap.h" | ||
# include "opentelemetry/sdk/metrics/state/metric_storage.h" | ||
# include "opentelemetry/sdk/metrics/view/attributes_processor.h" | ||
# include "opentelemetry/sdk/resource/resource.h" | ||
|
||
# include <memory> | ||
# include "opentelemetry/sdk/metrics/observer_result.h" | ||
|
||
OPENTELEMETRY_BEGIN_NAMESPACE | ||
namespace sdk | ||
{ | ||
namespace metrics | ||
{ | ||
|
||
template <class T> | ||
class AsyncMetricStorage : public MetricStorage | ||
{ | ||
public: | ||
AsyncMetricStorage(InstrumentDescriptor instrument_descriptor, | ||
const AggregationType aggregation_type, | ||
void (*measurement_callback)(opentelemetry::metrics::ObserverResult<T> &), | ||
const AttributesProcessor *attributes_processor) | ||
: instrument_descriptor_(instrument_descriptor), | ||
aggregation_type_{aggregation_type}, | ||
measurement_collection_callback_{measurement_callback}, | ||
attributes_processor_{attributes_processor}, | ||
active_attributes_hashmap_(new AttributesHashMap()) | ||
{} | ||
|
||
bool Collect( | ||
MetricCollector *collector, | ||
nostd::span<MetricCollector *> collectors, | ||
opentelemetry::sdk::instrumentationlibrary::InstrumentationLibrary *instrumentation_library, | ||
opentelemetry::sdk::resource::Resource *resource, | ||
nostd::function_ref<bool(MetricData &)> metric_collection_callback) noexcept override | ||
{ | ||
opentelemetry::sdk::metrics::ObserverResult<T> ob_res(attributes_processor_); | ||
|
||
// read the measurement using configured callback | ||
measurement_collection_callback_(ob_res); | ||
|
||
// process the read measurements - aggregate and store in hashmap | ||
for (auto &measurement : ob_res.GetMeasurements()) | ||
{ | ||
auto agg = DefaultAggregation::CreateAggregation(aggregation_type_, instrument_descriptor_); | ||
agg->Aggregate(measurement.second); | ||
active_attributes_hashmap_->Set(measurement.first, std::move(agg)); | ||
} | ||
|
||
// TBD -> read aggregation from hashmap, and perform metric collection | ||
MetricData metric_data; | ||
if (metric_collection_callback(metric_data)) | ||
{ | ||
return true; | ||
} | ||
return false; | ||
} | ||
|
||
private: | ||
InstrumentDescriptor instrument_descriptor_; | ||
AggregationType aggregation_type_; | ||
void (*measurement_collection_callback_)(opentelemetry::metrics::ObserverResult<T> &); | ||
const AttributesProcessor *attributes_processor_; | ||
std::unique_ptr<AttributesHashMap> active_attributes_hashmap_; | ||
}; | ||
|
||
} // namespace metrics | ||
} // namespace sdk | ||
OPENTELEMETRY_END_NAMESPACE | ||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.