-
Notifications
You must be signed in to change notification settings - Fork 26
Add OpenTelemetry lib and metric-interfaces #588
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
maladetska
wants to merge
17
commits into
ydb-platform:main
Choose a base branch
from
maladetska:METRICS-0
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
a102d53
init commit
9011838
.
bbc803b
make metrics as client_component
feb3e8d
fixes
0e31bc1
add includes
23414c1
fixes and add metric tests
aa80faa
add table metrics
b46d384
fix semconv
fe6450e
fix namespaces, CMakeLists
1071974
unify client operation metrics pipeline
5e4ea26
add cloned log_lazy
947b4ca
fix SafeLogMetricsError, SafeLogSpanError
f6a019e
Rename TOperationMetrics/TOperationSpan -> TRequestMetrics/TRequestSp…
5f8a13e
Create TRequestObservation, add ydbClientType to avoid metrics confus…
94cbdd0
fix test
75c777d
Add db.namespace, add ExternalMetricRegistry to TClientStatCollector,…
0f0d655
fix test
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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
Gazizonoki marked this conversation as resolved.
Show resolved
Hide resolved
|
This file contains hidden or 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,57 @@ | ||
| #pragma once | ||
|
|
||
| #include <cstdint> | ||
| #include <map> | ||
| #include <memory> | ||
| #include <string> | ||
| #include <vector> | ||
|
|
||
| namespace NYdb::inline V3::NMetrics { | ||
|
|
||
| using TLabels = std::map<std::string, std::string>; | ||
|
|
||
| class ICounter { | ||
Gazizonoki marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| public: | ||
| virtual ~ICounter() = default; | ||
| virtual void Inc() = 0; | ||
| }; | ||
|
|
||
| class IGauge { | ||
| public: | ||
| virtual ~IGauge() = default; | ||
| virtual void Add(double delta) = 0; | ||
| virtual void Set(double value) = 0; | ||
| }; | ||
|
|
||
| class IHistogram { | ||
| public: | ||
| virtual ~IHistogram() = default; | ||
| virtual void Record(double value) = 0; | ||
| }; | ||
|
|
||
| class IMetricRegistry { | ||
| public: | ||
| virtual ~IMetricRegistry() = default; | ||
|
|
||
| virtual std::shared_ptr<ICounter> Counter( | ||
| const std::string& name, | ||
| const TLabels& labels = {}, | ||
| const std::string& description = {}, | ||
| const std::string& unit = {} | ||
| ) = 0; | ||
| virtual std::shared_ptr<IGauge> Gauge( | ||
| const std::string& name, | ||
| const TLabels& labels = {}, | ||
| const std::string& description = {}, | ||
| const std::string& unit = {} | ||
| ) = 0; | ||
| virtual std::shared_ptr<IHistogram> Histogram( | ||
| const std::string& name, | ||
| const std::vector<double>& buckets, | ||
| const TLabels& labels = {}, | ||
| const std::string& description = {}, | ||
| const std::string& unit = {} | ||
| ) = 0; | ||
| }; | ||
|
|
||
| } // namespace NYdb::NMetrics | ||
This file contains hidden or 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,39 @@ | ||
| #pragma once | ||
|
|
||
| #include <cstdint> | ||
| #include <map> | ||
| #include <memory> | ||
| #include <string> | ||
|
|
||
| namespace NYdb::inline V3::NTrace { | ||
|
|
||
| enum class ESpanKind { | ||
| INTERNAL, | ||
| SERVER, | ||
| CLIENT, | ||
| PRODUCER, | ||
| CONSUMER | ||
| }; | ||
|
|
||
| class ISpan { | ||
| public: | ||
| virtual ~ISpan() = default; | ||
| virtual void End() = 0; | ||
| virtual void SetAttribute(const std::string& key, const std::string& value) = 0; | ||
| virtual void SetAttribute(const std::string& key, int64_t value) = 0; | ||
| virtual void AddEvent(const std::string& name, const std::map<std::string, std::string>& attributes = {}) = 0; | ||
| }; | ||
|
|
||
| class ITracer { | ||
| public: | ||
| virtual ~ITracer() = default; | ||
| virtual std::shared_ptr<ISpan> StartSpan(const std::string& name, ESpanKind kind = ESpanKind::INTERNAL) = 0; | ||
| }; | ||
|
|
||
| class ITraceProvider { | ||
| public: | ||
| virtual ~ITraceProvider() = default; | ||
| virtual std::shared_ptr<ITracer> GetTracer(const std::string& name) = 0; | ||
| }; | ||
|
|
||
| } // namespace NYdb::NTrace |
This file contains hidden or 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,2 @@ | ||
| add_subdirectory(metrics) | ||
| add_subdirectory(trace) |
This file contains hidden or 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,3 @@ | ||
| if (YDB_SDK_ENABLE_OTEL_METRICS) | ||
| add_subdirectory(otel EXCLUDE_FROM_ALL) | ||
| endif() |
This file contains hidden or 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,17 @@ | ||
| _ydb_sdk_add_library(open_telemetry_metrics) | ||
| target_sources(open_telemetry_metrics PRIVATE | ||
| src/metrics.cpp | ||
| ) | ||
| target_include_directories(open_telemetry_metrics PUBLIC | ||
| $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include> | ||
| $<INSTALL_INTERFACE:include> | ||
| ) | ||
| target_link_libraries(open_telemetry_metrics PUBLIC | ||
| client-metrics | ||
| client-resources | ||
| opentelemetry-cpp::api | ||
| opentelemetry-cpp::metrics | ||
| ) | ||
| _ydb_sdk_make_client_component(OpenTelemetryMetrics open_telemetry_metrics) | ||
|
|
||
| _ydb_sdk_install_headers(${CMAKE_INSTALL_INCLUDEDIR} DIRECTORY include/) |
16 changes: 16 additions & 0 deletions
16
plugins/metrics/otel/include/ydb-cpp-sdk/open_telemetry/metrics.h
This file contains hidden or 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,16 @@ | ||
| #pragma once | ||
|
|
||
| #include <ydb-cpp-sdk/client/metrics/metrics.h> | ||
|
|
||
| #include <opentelemetry/nostd/shared_ptr.h> | ||
|
|
||
| namespace opentelemetry::metrics { | ||
| class MeterProvider; | ||
| } | ||
|
|
||
| namespace NYdb::inline V3::NMetrics { | ||
|
|
||
| std::shared_ptr<IMetricRegistry> CreateOtelMetricRegistry( | ||
| opentelemetry::nostd::shared_ptr<opentelemetry::metrics::MeterProvider> meterProvider); | ||
|
|
||
| } // namespace NYdb::NMetrics |
This file contains hidden or 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,169 @@ | ||
| #include <ydb-cpp-sdk/open_telemetry/metrics.h> | ||
| #include <ydb-cpp-sdk/client/resources/ydb_resources.h> | ||
|
|
||
| #include <opentelemetry/common/key_value_iterable_view.h> | ||
| #include <opentelemetry/context/runtime_context.h> | ||
| #include <opentelemetry/metrics/meter.h> | ||
| #include <opentelemetry/metrics/meter_provider.h> | ||
| #include <opentelemetry/metrics/sync_instruments.h> | ||
| #include <opentelemetry/sdk/metrics/meter_context.h> | ||
| #include <opentelemetry/sdk/metrics/meter_provider.h> | ||
|
|
||
| namespace NYdb::inline V3::NMetrics { | ||
|
|
||
| namespace { | ||
|
|
||
| using namespace opentelemetry; | ||
|
|
||
| common::KeyValueIterableView<TLabels> MakeAttributes(const TLabels& labels) { | ||
| return common::KeyValueIterableView<TLabels>(labels); | ||
| } | ||
|
|
||
| class TOtelCounter : public ICounter { | ||
| public: | ||
| TOtelCounter(nostd::shared_ptr<metrics::Counter<uint64_t>> counter, const TLabels& labels) | ||
| : Counter_(std::move(counter)) | ||
| , Labels_(labels) | ||
| {} | ||
|
|
||
| void Inc() override { | ||
| Counter_->Add(1, MakeAttributes(Labels_), context::RuntimeContext::GetCurrent()); | ||
| } | ||
|
|
||
| private: | ||
| nostd::shared_ptr<metrics::Counter<uint64_t>> Counter_; | ||
| TLabels Labels_; | ||
| }; | ||
|
|
||
| class TOtelUpDownCounterGauge : public IGauge { | ||
| public: | ||
| TOtelUpDownCounterGauge(nostd::shared_ptr<metrics::UpDownCounter<double>> counter, const TLabels& labels) | ||
| : Counter_(std::move(counter)) | ||
| , Labels_(labels) | ||
| {} | ||
|
|
||
| void Add(double delta) override { | ||
| Counter_->Add(delta, MakeAttributes(Labels_), context::RuntimeContext::GetCurrent()); | ||
| Value_ += delta; | ||
| } | ||
|
|
||
| void Set(double value) override { | ||
| Counter_->Add(value - Value_, MakeAttributes(Labels_), context::RuntimeContext::GetCurrent()); | ||
| Value_ = value; | ||
| } | ||
|
|
||
| private: | ||
| nostd::shared_ptr<metrics::UpDownCounter<double>> Counter_; | ||
| TLabels Labels_; | ||
| double Value_ = 0; | ||
| }; | ||
|
|
||
| class TOtelHistogram : public IHistogram { | ||
| public: | ||
| TOtelHistogram(nostd::shared_ptr<metrics::Histogram<double>> histogram, const TLabels& labels) | ||
| : Histogram_(std::move(histogram)) | ||
| , Labels_(labels) | ||
| {} | ||
|
|
||
| void Record(double value) override { | ||
| Histogram_->Record(value, MakeAttributes(Labels_), context::RuntimeContext::GetCurrent()); | ||
| } | ||
|
|
||
| private: | ||
| nostd::shared_ptr<metrics::Histogram<double>> Histogram_; | ||
| TLabels Labels_; | ||
| }; | ||
|
|
||
| class TOtelMetricRegistry : public IMetricRegistry { | ||
| public: | ||
| TOtelMetricRegistry(nostd::shared_ptr<metrics::MeterProvider> meterProvider) | ||
| : MeterProvider_(std::move(meterProvider)) | ||
| , Meter_(MeterProvider_->GetMeter("ydb-cpp-sdk", GetSdkSemver())) | ||
| {} | ||
|
|
||
| std::shared_ptr<ICounter> Counter(const std::string& name | ||
| , const TLabels& labels | ||
| , const std::string& description | ||
| , const std::string& unit | ||
| ) override { | ||
| auto counter = Meter_->CreateUInt64Counter(name, description, unit); | ||
| return std::make_shared<TOtelCounter>(std::move(counter), labels); | ||
| } | ||
|
|
||
| std::shared_ptr<IGauge> Gauge(const std::string& name | ||
| , const TLabels& labels | ||
| , const std::string& description | ||
| , const std::string& unit | ||
| ) override { | ||
| auto counter = Meter_->CreateDoubleUpDownCounter(name, description, unit); | ||
| return std::make_shared<TOtelUpDownCounterGauge>(std::move(counter), labels); | ||
| } | ||
|
|
||
| std::shared_ptr<IHistogram> Histogram(const std::string& name | ||
| , const std::vector<double>& buckets | ||
| , const TLabels& labels | ||
| , const std::string& description | ||
| , const std::string& unit | ||
| ) override { | ||
| ConfigureHistogramBuckets(name, unit, buckets); | ||
| auto histogram = Meter_->CreateDoubleHistogram(name, description, unit); | ||
| return std::make_shared<TOtelHistogram>(std::move(histogram), labels); | ||
| } | ||
|
|
||
| private: | ||
| void ConfigureHistogramBuckets(const std::string& name, const std::string& unit, const std::vector<double>& buckets) { | ||
| if (buckets.empty()) { | ||
| return; | ||
| } | ||
|
|
||
| auto* sdkProvider = dynamic_cast<sdk::metrics::MeterProvider*>(MeterProvider_.get()); | ||
| if (!sdkProvider) { | ||
| return; | ||
| } | ||
|
|
||
| { | ||
| std::lock_guard lock(HistogramViewsLock_); | ||
| if (!HistogramViews_.insert(name).second) { | ||
| return; | ||
| } | ||
| } | ||
|
|
||
| auto selector = std::make_unique<sdk::metrics::InstrumentSelector>( | ||
| sdk::metrics::InstrumentType::kHistogram, | ||
| name, | ||
| unit | ||
| ); | ||
| auto meterSelector = std::make_unique<sdk::metrics::MeterSelector>( | ||
| "ydb-cpp-sdk", | ||
| GetSdkSemver(), | ||
| {} | ||
| ); | ||
|
|
||
| auto histogramConfig = std::make_shared<sdk::metrics::HistogramAggregationConfig>(); | ||
| histogramConfig->boundaries_ = buckets; | ||
|
|
||
| auto view = std::make_unique<sdk::metrics::View>( | ||
| {}, | ||
| {}, | ||
| sdk::metrics::AggregationType::kHistogram, | ||
| histogramConfig | ||
| ); | ||
|
|
||
| sdkProvider->AddView(std::move(selector), std::move(meterSelector), std::move(view)); | ||
| } | ||
|
|
||
| nostd::shared_ptr<metrics::MeterProvider> MeterProvider_; | ||
| nostd::shared_ptr<metrics::Meter> Meter_; | ||
| std::mutex HistogramViewsLock_; | ||
| std::unordered_set<std::string> HistogramViews_; | ||
| }; | ||
|
|
||
| } // namespace | ||
|
|
||
| std::shared_ptr<IMetricRegistry> CreateOtelMetricRegistry( | ||
| opentelemetry::nostd::shared_ptr<opentelemetry::metrics::MeterProvider> meterProvider) | ||
| { | ||
| return std::make_shared<TOtelMetricRegistry>(std::move(meterProvider)); | ||
| } | ||
|
|
||
| } // namespace NYdb::NMetrics |
This file contains hidden or 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,3 @@ | ||
| if (YDB_SDK_ENABLE_OTEL_TRACE) | ||
| add_subdirectory(otel EXCLUDE_FROM_ALL) | ||
| endif() |
This file contains hidden or 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,16 @@ | ||
| _ydb_sdk_add_library(open_telemetry_trace) | ||
| target_sources(open_telemetry_trace PRIVATE | ||
| src/trace.cpp | ||
| ) | ||
| target_include_directories(open_telemetry_trace PUBLIC | ||
| $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include> | ||
| $<INSTALL_INTERFACE:include> | ||
| ) | ||
| target_link_libraries(open_telemetry_trace PUBLIC | ||
| client-trace | ||
| opentelemetry-cpp::api | ||
| opentelemetry-cpp::trace | ||
| ) | ||
| _ydb_sdk_make_client_component(OpenTelemetryTrace open_telemetry_trace) | ||
|
|
||
| _ydb_sdk_install_headers(${CMAKE_INSTALL_INCLUDEDIR} DIRECTORY include/) |
16 changes: 16 additions & 0 deletions
16
plugins/trace/otel/include/ydb-cpp-sdk/open_telemetry/trace.h
This file contains hidden or 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,16 @@ | ||
| #pragma once | ||
|
|
||
| #include <ydb-cpp-sdk/client/trace/trace.h> | ||
|
|
||
| #include <opentelemetry/nostd/shared_ptr.h> | ||
|
|
||
| namespace opentelemetry::trace { | ||
| class TracerProvider; | ||
| } | ||
|
|
||
| namespace NYdb::inline V3::NTrace { | ||
|
|
||
| std::shared_ptr<ITraceProvider> CreateOtelTraceProvider( | ||
| opentelemetry::nostd::shared_ptr<opentelemetry::trace::TracerProvider> tracerProvider); | ||
|
|
||
| } // namespace NYdb::NTrace |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.