-
Notifications
You must be signed in to change notification settings - Fork 417
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Sync and Async Instruments SDK (#1184)
- Loading branch information
Showing
22 changed files
with
1,029 additions
and
52 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
179 changes: 179 additions & 0 deletions
179
sdk/include/opentelemetry/sdk/metrics/async_instruments.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,179 @@ | ||
// Copyright The OpenTelemetry Authors | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
#pragma once | ||
#ifndef ENABLE_METRICS_PREVIEW | ||
# include "opentelemetry/metrics/async_instruments.h" | ||
# include "opentelemetry/metrics/observer_result.h" | ||
# include "opentelemetry/sdk/instrumentationlibrary/instrumentation_library.h" | ||
# include "opentelemetry/sdk/metrics/measurement_processor.h" | ||
|
||
# include "opentelemetry/nostd/string_view.h" | ||
# include "opentelemetry/sdk/metrics/instruments.h" | ||
OPENTELEMETRY_BEGIN_NAMESPACE | ||
namespace sdk | ||
{ | ||
namespace metrics | ||
{ | ||
|
||
template <class T> | ||
class Asynchronous | ||
{ | ||
public: | ||
Asynchronous(nostd::string_view name, | ||
const opentelemetry::sdk::instrumentationlibrary::InstrumentationLibrary | ||
*instrumentation_library, | ||
MeasurementProcessor *measurement_processor, | ||
void (*callback)(opentelemetry::metrics::ObserverResult<T> &), | ||
nostd::string_view description = "", | ||
nostd::string_view unit = "") | ||
: name_(name), | ||
instrumentation_library_{instrumentation_library}, | ||
measurement_processor_{measurement_processor}, | ||
callback_(callback), | ||
description_(description), | ||
unit_(unit) | ||
{} | ||
|
||
protected: | ||
std::string name_; | ||
const opentelemetry::sdk::instrumentationlibrary::InstrumentationLibrary | ||
*instrumentation_library_; | ||
const MeasurementProcessor *measurement_processor_; | ||
void (*callback_)(opentelemetry::metrics::ObserverResult<T> &); | ||
std::string description_; | ||
std::string unit_; | ||
}; | ||
|
||
class LongObservableCounter : public opentelemetry::metrics::ObservableCounter<long>, | ||
public Asynchronous<long> | ||
{ | ||
public: | ||
LongObservableCounter(nostd::string_view name, | ||
const opentelemetry::sdk::instrumentationlibrary::InstrumentationLibrary | ||
*instrumentation_library, | ||
MeasurementProcessor *measurement_processor, | ||
void (*callback)(opentelemetry::metrics::ObserverResult<long> &), | ||
nostd::string_view description = "", | ||
nostd::string_view unit = "") | ||
: Asynchronous(name, | ||
instrumentation_library, | ||
measurement_processor, | ||
callback, | ||
description, | ||
unit) | ||
|
||
{} | ||
}; | ||
|
||
class DoubleObservableCounter : public opentelemetry::metrics::ObservableCounter<double>, | ||
public Asynchronous<double> | ||
{ | ||
public: | ||
DoubleObservableCounter(nostd::string_view name, | ||
const opentelemetry::sdk::instrumentationlibrary::InstrumentationLibrary | ||
*instrumentation_library, | ||
MeasurementProcessor *measurement_processor, | ||
void (*callback)(opentelemetry::metrics::ObserverResult<double> &), | ||
nostd::string_view description = "", | ||
nostd::string_view unit = "") | ||
: Asynchronous(name, | ||
instrumentation_library, | ||
measurement_processor, | ||
callback, | ||
description, | ||
unit) | ||
|
||
{} | ||
}; | ||
|
||
class LongObservableGauge : public opentelemetry::metrics::ObservableGauge<long>, | ||
public Asynchronous<long> | ||
{ | ||
public: | ||
LongObservableGauge(nostd::string_view name, | ||
const opentelemetry::sdk::instrumentationlibrary::InstrumentationLibrary | ||
*instrumentation_library, | ||
MeasurementProcessor *measurement_processor, | ||
void (*callback)(opentelemetry::metrics::ObserverResult<long> &), | ||
nostd::string_view description = "", | ||
nostd::string_view unit = "") | ||
: Asynchronous(name, | ||
instrumentation_library, | ||
measurement_processor, | ||
callback, | ||
description, | ||
unit) | ||
|
||
{} | ||
}; | ||
|
||
class DoubleObservableGauge : public opentelemetry::metrics::ObservableGauge<double>, | ||
public Asynchronous<double> | ||
{ | ||
public: | ||
DoubleObservableGauge(nostd::string_view name, | ||
const opentelemetry::sdk::instrumentationlibrary::InstrumentationLibrary | ||
*instrumentation_library, | ||
MeasurementProcessor *measurement_processor, | ||
void (*callback)(opentelemetry::metrics::ObserverResult<double> &), | ||
nostd::string_view description = "", | ||
nostd::string_view unit = "") | ||
: Asynchronous(name, | ||
instrumentation_library, | ||
measurement_processor, | ||
callback, | ||
description, | ||
unit) | ||
|
||
{} | ||
}; | ||
|
||
class LongObservableUpDownCounter : public opentelemetry::metrics::ObservableUpDownCounter<long>, | ||
public Asynchronous<long> | ||
{ | ||
public: | ||
LongObservableUpDownCounter( | ||
nostd::string_view name, | ||
const opentelemetry::sdk::instrumentationlibrary::InstrumentationLibrary | ||
*instrumentation_library, | ||
MeasurementProcessor *measurement_processor, | ||
void (*callback)(opentelemetry::metrics::ObserverResult<long> &), | ||
nostd::string_view description = "", | ||
nostd::string_view unit = "") | ||
: Asynchronous(name, | ||
instrumentation_library, | ||
measurement_processor, | ||
callback, | ||
description, | ||
unit) | ||
|
||
{} | ||
}; | ||
|
||
class DoubleObservableUpDownCounter | ||
: public opentelemetry::metrics::ObservableUpDownCounter<double>, | ||
public Asynchronous<double> | ||
{ | ||
public: | ||
DoubleObservableUpDownCounter( | ||
nostd::string_view name, | ||
const opentelemetry::sdk::instrumentationlibrary::InstrumentationLibrary | ||
*instrumentation_library, | ||
MeasurementProcessor *measurement_processor, | ||
void (*callback)(opentelemetry::metrics::ObserverResult<double> &), | ||
nostd::string_view description = "", | ||
nostd::string_view unit = "") | ||
: Asynchronous(name, | ||
instrumentation_library, | ||
measurement_processor, | ||
callback, | ||
description, | ||
unit) | ||
{} | ||
}; | ||
|
||
} // namespace metrics | ||
} // namespace sdk | ||
OPENTELEMETRY_END_NAMESPACE | ||
#endif |
108 changes: 108 additions & 0 deletions
108
sdk/include/opentelemetry/sdk/metrics/measurement_processor.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,108 @@ | ||
// Copyright The OpenTelemetry Authors | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
#pragma once | ||
#ifndef ENABLE_METRICS_PREVIEW | ||
|
||
# include "opentelemetry/common/key_value_iterable_view.h" | ||
# include "opentelemetry/sdk/metrics/instruments.h" | ||
# include "opentelemetry/sdk/metrics/metric_reader.h" | ||
# include "opentelemetry/sdk/metrics/state/sync_metric_storage.h" | ||
|
||
# include <map> | ||
|
||
OPENTELEMETRY_BEGIN_NAMESPACE | ||
namespace sdk | ||
{ | ||
namespace metrics | ||
{ | ||
|
||
static std::size_t MakeKey(const MetricReader &metric_reader) | ||
{ | ||
return reinterpret_cast<std::size_t>(&metric_reader); | ||
} | ||
class MeasurementProcessor | ||
{ | ||
public: | ||
virtual void RecordLong(long value) noexcept = 0; | ||
|
||
virtual void RecordLong(long value, | ||
const opentelemetry::common::KeyValueIterable &attributes) noexcept = 0; | ||
|
||
virtual void RecordDouble(double value) noexcept = 0; | ||
|
||
virtual void RecordDouble(double value, | ||
const opentelemetry::common::KeyValueIterable &attributes) noexcept = 0; | ||
|
||
virtual bool Collect(MetricReader &reader, | ||
AggregationTemporarily aggregation_temporarily, | ||
nostd::function_ref<bool(MetricData)> callback) noexcept = 0; | ||
}; | ||
|
||
class DefaultMeasurementProcessor : public MeasurementProcessor | ||
{ | ||
|
||
public: | ||
bool AddMetricStorage(const MetricReader &reader) | ||
{ | ||
// TBD = check if already present. | ||
metric_storages_[MakeKey(reader)] = std::unique_ptr<SyncMetricStorage>(new SyncMetricStorage()); | ||
return true; | ||
} | ||
|
||
virtual void RecordLong(long value) noexcept override | ||
{ | ||
for (const auto &kv : metric_storages_) | ||
{ | ||
kv.second->RecordLong(value); | ||
} | ||
} | ||
|
||
virtual void RecordLong( | ||
long value, | ||
const opentelemetry::common::KeyValueIterable &attributes) noexcept override | ||
{ | ||
for (const auto &kv : metric_storages_) | ||
{ | ||
kv.second->RecordLong(value, attributes); | ||
} | ||
} | ||
|
||
virtual void RecordDouble(double value) noexcept override | ||
{ | ||
for (const auto &kv : metric_storages_) | ||
{ | ||
kv.second->RecordDouble(value); | ||
} | ||
} | ||
|
||
virtual void RecordDouble( | ||
double value, | ||
const opentelemetry::common::KeyValueIterable &attributes) noexcept override | ||
{ | ||
for (const auto &kv : metric_storages_) | ||
{ | ||
kv.second->RecordDouble(value, attributes); | ||
} | ||
} | ||
|
||
bool Collect(MetricReader &reader, | ||
AggregationTemporarily aggregation_temporarily, | ||
nostd::function_ref<bool(MetricData)> callback) noexcept override | ||
{ | ||
auto i = metric_storages_.find(MakeKey(reader)); | ||
if (i != metric_storages_.end()) | ||
{ | ||
return i->second->Collect(aggregation_temporarily, callback); | ||
} | ||
return false; | ||
} | ||
|
||
private: | ||
std::map<std::size_t, std::unique_ptr<SyncMetricStorage>> metric_storages_; | ||
}; | ||
|
||
} // 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
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.
04e3a68
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Possible performance regression was detected for benchmark 'OpenTelemetry-cpp sdk Benchmark'.
Benchmark result of this commit is worse than the previous benchmark result exceeding threshold
2
.BM_BaselineBuffer/1
8119761.943817139
ns/iter573135.8528137207
ns/iter14.17
BM_BaselineBuffer/4
10742192.268371582
ns/iter3843520.1387147647
ns/iter2.79
This comment was automatically generated by workflow using github-action-benchmark.