Skip to content

Commit

Permalink
Add configuration options for Aggregation creation (#1513)
Browse files Browse the repository at this point in the history
  • Loading branch information
esigo authored Aug 1, 2022
1 parent d452e8e commit 3af512e
Show file tree
Hide file tree
Showing 16 changed files with 182 additions and 54 deletions.
6 changes: 4 additions & 2 deletions examples/common/metrics_foo_library/foo_library.cc
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@
# include "foo_library.h"
# include <chrono>
# include <map>
# include <memory>
# include <thread>
# include <vector>
# include "opentelemetry/context/context.h"
# include "opentelemetry/metrics/provider.h"
# include "opentelemetry/nostd/shared_ptr.h"

namespace nostd = opentelemetry::nostd;
namespace metrics_api = opentelemetry::metrics;
Expand Down Expand Up @@ -72,8 +74,8 @@ void foo_library::histogram_example(const std::string &name)
std::string histogram_name = name + "_histogram";
auto provider = metrics_api::Provider::GetMeterProvider();
nostd::shared_ptr<metrics_api::Meter> meter = provider->GetMeter(name, "1.2.0");
auto histogram_counter = meter->CreateDoubleHistogram(histogram_name);
auto context = opentelemetry::context::Context{};
auto histogram_counter = meter->CreateDoubleHistogram(histogram_name, "des", "unit");
auto context = opentelemetry::context::Context{};
while (true)
{
double val = (rand() % 700) + 1.1;
Expand Down
10 changes: 8 additions & 2 deletions examples/metrics_simple/metrics_ostream.cc
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,14 @@ void initMetrics(const std::string &name)
new metric_sdk::InstrumentSelector(metric_sdk::InstrumentType::kHistogram, histogram_name)};
std::unique_ptr<metric_sdk::MeterSelector> histogram_meter_selector{
new metric_sdk::MeterSelector(name, version, schema)};
std::unique_ptr<metric_sdk::View> histogram_view{
new metric_sdk::View{name, "description", metric_sdk::AggregationType::kHistogram}};
std::shared_ptr<opentelemetry::sdk::metrics::AggregationConfig> aggregation_config{
new opentelemetry::sdk::metrics::HistogramAggregationConfig<double>};
static_cast<opentelemetry::sdk::metrics::HistogramAggregationConfig<double> *>(
aggregation_config.get())
->boundaries_ =
std::list<double>{0.0, 50.0, 100.0, 250.0, 500.0, 750.0, 1000.0, 2500.0, 5000.0, 10000.0};
std::unique_ptr<metric_sdk::View> histogram_view{new metric_sdk::View{
name, "description", metric_sdk::AggregationType::kHistogram, aggregation_config}};
p->AddView(std::move(histogram_instrument_selector), std::move(histogram_meter_selector),
std::move(histogram_view));
metrics_api::Provider::SetMeterProvider(provider);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0

#pragma once
#ifndef ENABLE_METRICS_PREVIEW
# include <list>
# include "opentelemetry/version.h"
OPENTELEMETRY_BEGIN_NAMESPACE
namespace sdk
{
namespace metrics
{
class AggregationConfig
{
public:
virtual ~AggregationConfig() = default;
};

template <typename T>
class HistogramAggregationConfig : public AggregationConfig
{
public:
std::list<T> boundaries_;
};
} // namespace metrics
} // namespace sdk
OPENTELEMETRY_END_NAMESPACE

#endif // ENABLE_METRICS_PREVIEW
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,15 @@

#pragma once
#ifndef ENABLE_METRICS_PREVIEW
# include <memory>
# include "opentelemetry/common/spin_lock_mutex.h"
# include "opentelemetry/sdk/metrics/aggregation/aggregation.h"
# include "opentelemetry/sdk/metrics/aggregation/aggregation_config.h"
# include "opentelemetry/sdk/metrics/aggregation/drop_aggregation.h"
# include "opentelemetry/sdk/metrics/aggregation/histogram_aggregation.h"
# include "opentelemetry/sdk/metrics/aggregation/lastvalue_aggregation.h"
# include "opentelemetry/sdk/metrics/aggregation/sum_aggregation.h"
# include "opentelemetry/sdk/metrics/data/point_data.h"
# include "opentelemetry/sdk/metrics/instruments.h"

# include <mutex>
Expand All @@ -18,11 +21,13 @@ namespace sdk
{
namespace metrics
{

class DefaultAggregation
{
public:
static std::unique_ptr<Aggregation> CreateAggregation(
const opentelemetry::sdk::metrics::InstrumentDescriptor &instrument_descriptor)
const opentelemetry::sdk::metrics::InstrumentDescriptor &instrument_descriptor,
const opentelemetry::sdk::metrics::AggregationConfig *aggregation_config)
{
switch (instrument_descriptor.type_)
{
Expand All @@ -34,11 +39,17 @@ class DefaultAggregation
? std::move(std::unique_ptr<Aggregation>(new LongSumAggregation()))
: std::move(std::unique_ptr<Aggregation>(new DoubleSumAggregation()));
break;
case InstrumentType::kHistogram:
case InstrumentType::kHistogram: {
return (instrument_descriptor.value_type_ == InstrumentValueType::kLong)
? std::move(std::unique_ptr<Aggregation>(new LongHistogramAggregation()))
: std::move(std::unique_ptr<Aggregation>(new DoubleHistogramAggregation()));
? std::move(std::unique_ptr<Aggregation>(new LongHistogramAggregation(
static_cast<
const opentelemetry::sdk::metrics::HistogramAggregationConfig<long> *>(
aggregation_config))))
: std::move(std::unique_ptr<Aggregation>(new DoubleHistogramAggregation(
static_cast<const opentelemetry::sdk::metrics::HistogramAggregationConfig<
double> *>(aggregation_config))));
break;
}
case InstrumentType::kObservableGauge:
return (instrument_descriptor.value_type_ == InstrumentValueType::kLong)
? std::move(std::unique_ptr<Aggregation>(new LongLastValueAggregation()))
Expand Down Expand Up @@ -88,7 +99,7 @@ class DefaultAggregation
}
break;
default:
return DefaultAggregation::CreateAggregation(instrument_descriptor);
return DefaultAggregation::CreateAggregation(instrument_descriptor, nullptr);
}
}

Expand Down Expand Up @@ -135,7 +146,7 @@ class DefaultAggregation
new DoubleSumAggregation(nostd::get<SumPointData>(point_data)));
}
default:
return DefaultAggregation::CreateAggregation(instrument_descriptor);
return DefaultAggregation::CreateAggregation(instrument_descriptor, nullptr);
}
}
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#ifndef ENABLE_METRICS_PREVIEW
# include "opentelemetry/common/spin_lock_mutex.h"
# include "opentelemetry/sdk/metrics/aggregation/aggregation.h"
# include "opentelemetry/sdk/metrics/aggregation/aggregation_config.h"

# include <mutex>

Expand All @@ -17,7 +18,7 @@ namespace metrics
class LongHistogramAggregation : public Aggregation
{
public:
LongHistogramAggregation();
LongHistogramAggregation(const HistogramAggregationConfig<long> *aggregation_config = nullptr);
LongHistogramAggregation(HistogramPointData &&);
LongHistogramAggregation(const HistogramPointData &);

Expand Down Expand Up @@ -46,7 +47,8 @@ class LongHistogramAggregation : public Aggregation
class DoubleHistogramAggregation : public Aggregation
{
public:
DoubleHistogramAggregation();
DoubleHistogramAggregation(
const HistogramAggregationConfig<double> *aggregation_config = nullptr);
DoubleHistogramAggregation(HistogramPointData &&);
DoubleHistogramAggregation(const HistogramPointData &);

Expand Down
6 changes: 3 additions & 3 deletions sdk/include/opentelemetry/sdk/metrics/meter.h
Original file line number Diff line number Diff line change
Expand Up @@ -148,9 +148,9 @@ class Meter final : public opentelemetry::metrics::Meter
{
view_instr_desc.description_ = view.GetDescription();
}
auto storage = std::shared_ptr<AsyncMetricStorage<T>>(
new AsyncMetricStorage<T>(view_instr_desc, view.GetAggregationType(), callback,
&view.GetAttributesProcessor(), state));
auto storage = std::shared_ptr<AsyncMetricStorage<T>>(new AsyncMetricStorage<T>(
view_instr_desc, view.GetAggregationType(), callback, &view.GetAttributesProcessor(),
view.GetAggregationConfig(), state));
storage_registry_[instrument_descriptor.name_] = storage;
return true;
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

#pragma once
#ifndef ENABLE_METRICS_PREVIEW
# include "opentelemetry/nostd/shared_ptr.h"
# include "opentelemetry/sdk/common/attributemap_hash.h"
# include "opentelemetry/sdk/metrics/aggregation/default_aggregation.h"
# include "opentelemetry/sdk/metrics/instruments.h"
Expand Down Expand Up @@ -30,14 +31,15 @@ class AsyncMetricStorage : public MetricStorage
void (*measurement_callback)(opentelemetry::metrics::ObserverResult<T> &,
void *),
const AttributesProcessor *attributes_processor,
nostd::shared_ptr<AggregationConfig> aggregation_config,
void *state = nullptr)
: instrument_descriptor_(instrument_descriptor),
aggregation_type_{aggregation_type},
measurement_collection_callback_{measurement_callback},
attributes_processor_{attributes_processor},
state_{state},
cumulative_hash_map_(new AttributesHashMap()),
temporal_metric_storage_(instrument_descriptor)
temporal_metric_storage_(instrument_descriptor, aggregation_config)
{}

bool Collect(CollectorHandle *collector,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

#pragma once
#ifndef ENABLE_METRICS_PREVIEW
# include <utility>
# include "opentelemetry/common/key_value_iterable_view.h"
# include "opentelemetry/sdk/common/attributemap_hash.h"
# include "opentelemetry/sdk/metrics/aggregation/default_aggregation.h"
Expand All @@ -29,13 +30,14 @@ class SyncMetricStorage : public MetricStorage, public WritableMetricStorage
SyncMetricStorage(InstrumentDescriptor instrument_descriptor,
const AggregationType aggregation_type,
const AttributesProcessor *attributes_processor,
nostd::shared_ptr<ExemplarReservoir> &&exemplar_reservoir)
nostd::shared_ptr<ExemplarReservoir> &&exemplar_reservoir,
nostd::shared_ptr<AggregationConfig> aggregation_config)
: instrument_descriptor_(instrument_descriptor),
aggregation_type_{aggregation_type},
attributes_hashmap_(new AttributesHashMap()),
attributes_processor_{attributes_processor},
exemplar_reservoir_(exemplar_reservoir),
temporal_metric_storage_(instrument_descriptor)
temporal_metric_storage_(instrument_descriptor, aggregation_config)

{
create_default_aggregation_ = [&]() -> std::unique_ptr<Aggregation> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@

#pragma once
#ifndef ENABLE_METRICS_PREVIEW
# include "opentelemetry/nostd/shared_ptr.h"
# include "opentelemetry/sdk/metrics/aggregation/default_aggregation.h"
# include "opentelemetry/sdk/metrics/state/attributes_hashmap.h"
# include "opentelemetry/sdk/metrics/state/metric_collector.h"

Expand All @@ -23,7 +25,8 @@ struct LastReportedMetrics
class TemporalMetricStorage
{
public:
TemporalMetricStorage(InstrumentDescriptor instrument_descriptor);
TemporalMetricStorage(InstrumentDescriptor instrument_descriptor,
nostd::shared_ptr<AggregationConfig> aggregation_config);

bool buildMetrics(CollectorHandle *collector,
nostd::span<std::shared_ptr<CollectorHandle>> collectors,
Expand All @@ -43,6 +46,7 @@ class TemporalMetricStorage

// Lock while building metrics
mutable opentelemetry::common::SpinLockMutex lock_;
const nostd::shared_ptr<AggregationConfig> aggregation_config_;
};
} // namespace metrics
} // namespace sdk
Expand Down
14 changes: 12 additions & 2 deletions sdk/include/opentelemetry/sdk/metrics/view/view.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@

#pragma once
#ifndef ENABLE_METRICS_PREVIEW
# include "opentelemetry/nostd/shared_ptr.h"
# include "opentelemetry/nostd/string_view.h"
# include "opentelemetry/sdk/metrics/aggregation/aggregation_config.h"
# include "opentelemetry/sdk/metrics/aggregation/default_aggregation.h"
# include "opentelemetry/sdk/metrics/instruments.h"
# include "opentelemetry/sdk/metrics/view/attributes_processor.h"
Expand All @@ -22,14 +24,16 @@ class View
{
public:
View(const std::string &name,
const std::string &description = "",
AggregationType aggregation_type = AggregationType::kDefault,
const std::string &description = "",
AggregationType aggregation_type = AggregationType::kDefault,
std::shared_ptr<AggregationConfig> aggregation_config = std::shared_ptr<AggregationConfig>{},
std::unique_ptr<opentelemetry::sdk::metrics::AttributesProcessor> attributes_processor =
std::unique_ptr<opentelemetry::sdk::metrics::AttributesProcessor>(
new opentelemetry::sdk::metrics::DefaultAttributesProcessor()))
: name_(name),
description_(description),
aggregation_type_{aggregation_type},
aggregation_config_{aggregation_config},
attributes_processor_{std::move(attributes_processor)}
{}

Expand All @@ -39,6 +43,11 @@ class View

virtual AggregationType GetAggregationType() const noexcept { return aggregation_type_; }

virtual nostd::shared_ptr<AggregationConfig> GetAggregationConfig() const noexcept
{
return aggregation_config_;
}

virtual const opentelemetry::sdk::metrics::AttributesProcessor &GetAttributesProcessor()
const noexcept
{
Expand All @@ -49,6 +58,7 @@ class View
std::string name_;
std::string description_;
AggregationType aggregation_type_;
nostd::shared_ptr<AggregationConfig> aggregation_config_;
std::unique_ptr<opentelemetry::sdk::metrics::AttributesProcessor> attributes_processor_;
};
} // namespace metrics
Expand Down
26 changes: 21 additions & 5 deletions sdk/src/metrics/aggregation/histogram_aggregation.cc
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,17 @@ namespace sdk
namespace metrics
{

LongHistogramAggregation::LongHistogramAggregation()
LongHistogramAggregation::LongHistogramAggregation(
const HistogramAggregationConfig<long> *aggregation_config)
{
point_data_.boundaries_ = std::list<long>{0l, 5l, 10l, 25l, 50l, 75l, 100l, 250l, 500l, 1000l};
if (aggregation_config && aggregation_config->boundaries_.size())
{
point_data_.boundaries_ = aggregation_config->boundaries_;
}
else
{
point_data_.boundaries_ = std::list<long>{0l, 5l, 10l, 25l, 50l, 75l, 100l, 250l, 500l, 1000l};
}
point_data_.counts_ =
std::vector<uint64_t>(nostd::get<std::list<long>>(point_data_.boundaries_).size() + 1, 0);
point_data_.sum_ = 0l;
Expand Down Expand Up @@ -73,10 +81,18 @@ PointType LongHistogramAggregation::ToPoint() const noexcept
return point_data_;
}

DoubleHistogramAggregation::DoubleHistogramAggregation()
DoubleHistogramAggregation::DoubleHistogramAggregation(
const HistogramAggregationConfig<double> *aggregation_config)
{
point_data_.boundaries_ =
std::list<double>{0.0, 5.0, 10.0, 25.0, 50.0, 75.0, 100.0, 250.0, 500.0, 1000.0};
if (aggregation_config && aggregation_config->boundaries_.size())
{
point_data_.boundaries_ = aggregation_config->boundaries_;
}
else
{
point_data_.boundaries_ =
std::list<double>{0.0, 5.0, 10.0, 25.0, 50.0, 75.0, 100.0, 250.0, 500.0, 1000.0};
}
point_data_.counts_ =
std::vector<uint64_t>(nostd::get<std::list<double>>(point_data_.boundaries_).size() + 1, 0);
point_data_.sum_ = 0.0;
Expand Down
2 changes: 1 addition & 1 deletion sdk/src/metrics/meter.cc
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@ std::unique_ptr<WritableMetricStorage> Meter::RegisterMetricStorage(
}
auto storage = std::shared_ptr<SyncMetricStorage>(new SyncMetricStorage(
view_instr_desc, view.GetAggregationType(), &view.GetAttributesProcessor(),
NoExemplarReservoir::GetNoExemplarReservoir()));
NoExemplarReservoir::GetNoExemplarReservoir(), view.GetAggregationConfig()));
storage_registry_[instrument_descriptor.name_] = storage;
auto multi_storage = static_cast<MultiMetricStorage *>(storages.get());
multi_storage->AddStorage(storage);
Expand Down
Loading

0 comments on commit 3af512e

Please sign in to comment.