From 085b3c8a5482d75494873fe35634c5621f8bb3ba Mon Sep 17 00:00:00 2001 From: Marc Alff Date: Wed, 8 May 2024 16:24:49 +0200 Subject: [PATCH] General cleanup for is_shutdown_ flags in exporters. --- .../elasticsearch/es_log_record_exporter.h | 4 +--- .../elasticsearch/src/es_log_record_exporter.cc | 2 -- .../exporters/memory/in_memory_span_exporter.h | 15 ++++++--------- .../exporters/ostream/log_record_exporter.h | 6 ++---- .../exporters/ostream/metric_exporter.h | 7 ++++--- .../exporters/ostream/span_exporter.h | 5 ++--- exporters/ostream/src/log_record_exporter.cc | 2 -- exporters/ostream/src/metric_exporter.cc | 7 +++---- exporters/ostream/src/span_exporter.cc | 3 +-- .../exporters/otlp/otlp_grpc_exporter.h | 5 ++--- .../otlp/otlp_grpc_log_record_exporter.h | 6 +++--- .../exporters/otlp/otlp_grpc_metric_exporter.h | 6 +++--- .../exporters/otlp/otlp_http_client.h | 1 - exporters/otlp/src/otlp_grpc_exporter.cc | 2 -- .../otlp/src/otlp_grpc_log_record_exporter.cc | 2 -- exporters/otlp/src/otlp_grpc_metric_exporter.cc | 2 -- .../opentelemetry/exporters/prometheus/exporter.h | 2 +- .../exporters/zipkin/zipkin_exporter.h | 6 +++--- exporters/zipkin/src/zipkin_exporter.cc | 2 -- 19 files changed, 31 insertions(+), 54 deletions(-) diff --git a/exporters/elasticsearch/include/opentelemetry/exporters/elasticsearch/es_log_record_exporter.h b/exporters/elasticsearch/include/opentelemetry/exporters/elasticsearch/es_log_record_exporter.h index 31d63af983..2035b32d4b 100644 --- a/exporters/elasticsearch/include/opentelemetry/exporters/elasticsearch/es_log_record_exporter.h +++ b/exporters/elasticsearch/include/opentelemetry/exporters/elasticsearch/es_log_record_exporter.h @@ -4,7 +4,6 @@ #pragma once #include "nlohmann/json.hpp" -#include "opentelemetry/common/spin_lock_mutex.h" #include "opentelemetry/ext/http/client/http_client_factory.h" #include "opentelemetry/nostd/shared_ptr.h" #include "opentelemetry/sdk/logs/exporter.h" @@ -110,14 +109,13 @@ class ElasticsearchLogRecordExporter final : public opentelemetry::sdk::logs::Lo private: // Stores if this exporter had its Shutdown() method called - bool is_shutdown_ = false; + std::atomic is_shutdown_{false}; // Configuration options for the exporter ElasticsearchExporterOptions options_; // Object that stores the HTTP sessions that have been created std::shared_ptr http_client_; - mutable opentelemetry::common::SpinLockMutex lock_; bool isShutdown() const noexcept; #ifdef ENABLE_ASYNC_EXPORT diff --git a/exporters/elasticsearch/src/es_log_record_exporter.cc b/exporters/elasticsearch/src/es_log_record_exporter.cc index e167faf3d9..196474ba77 100644 --- a/exporters/elasticsearch/src/es_log_record_exporter.cc +++ b/exporters/elasticsearch/src/es_log_record_exporter.cc @@ -460,7 +460,6 @@ bool ElasticsearchLogRecordExporter::ForceFlush( bool ElasticsearchLogRecordExporter::Shutdown(std::chrono::microseconds /* timeout */) noexcept { - const std::lock_guard locked(lock_); is_shutdown_ = true; // Shutdown the session manager @@ -472,7 +471,6 @@ bool ElasticsearchLogRecordExporter::Shutdown(std::chrono::microseconds /* timeo bool ElasticsearchLogRecordExporter::isShutdown() const noexcept { - const std::lock_guard locked(lock_); return is_shutdown_; } } // namespace logs diff --git a/exporters/memory/include/opentelemetry/exporters/memory/in_memory_span_exporter.h b/exporters/memory/include/opentelemetry/exporters/memory/in_memory_span_exporter.h index 27efae379b..0c2a14a673 100644 --- a/exporters/memory/include/opentelemetry/exporters/memory/in_memory_span_exporter.h +++ b/exporters/memory/include/opentelemetry/exporters/memory/in_memory_span_exporter.h @@ -2,12 +2,15 @@ // SPDX-License-Identifier: Apache-2.0 #pragma once + +#include #include -#include "opentelemetry/common/spin_lock_mutex.h" + #include "opentelemetry/exporters/memory/in_memory_span_data.h" #include "opentelemetry/sdk/trace/exporter.h" #include "opentelemetry/sdk/trace/span_data.h" #include "opentelemetry/sdk_config.h" +#include "opentelemetry/version.h" OPENTELEMETRY_BEGIN_NAMESPACE namespace exporter @@ -72,7 +75,6 @@ class InMemorySpanExporter final : public opentelemetry::sdk::trace::SpanExporte */ bool Shutdown(std::chrono::microseconds /* timeout */) noexcept override { - const std::lock_guard locked(lock_); is_shutdown_ = true; return true; } @@ -84,13 +86,8 @@ class InMemorySpanExporter final : public opentelemetry::sdk::trace::SpanExporte private: std::shared_ptr data_; - bool is_shutdown_ = false; - mutable opentelemetry::common::SpinLockMutex lock_; - bool isShutdown() const noexcept - { - const std::lock_guard locked(lock_); - return is_shutdown_; - } + std::atomic is_shutdown_{false}; + bool isShutdown() const noexcept { return is_shutdown_; } }; } // namespace memory } // namespace exporter diff --git a/exporters/ostream/include/opentelemetry/exporters/ostream/log_record_exporter.h b/exporters/ostream/include/opentelemetry/exporters/ostream/log_record_exporter.h index ba5b28b9de..3c11c53aee 100644 --- a/exporters/ostream/include/opentelemetry/exporters/ostream/log_record_exporter.h +++ b/exporters/ostream/include/opentelemetry/exporters/ostream/log_record_exporter.h @@ -4,13 +4,12 @@ #pragma once #include "opentelemetry/common/attribute_value.h" -#include "opentelemetry/common/spin_lock_mutex.h" #include "opentelemetry/nostd/span.h" #include "opentelemetry/sdk/common/attribute_utils.h" #include "opentelemetry/sdk/logs/exporter.h" - #include "opentelemetry/version.h" +#include #include #include #include @@ -59,8 +58,7 @@ class OStreamLogRecordExporter final : public opentelemetry::sdk::logs::LogRecor // The OStream to send the logs to std::ostream &sout_; // Whether this exporter has been shut down - bool is_shutdown_ = false; - mutable opentelemetry::common::SpinLockMutex lock_; + std::atomic is_shutdown_{false}; bool isShutdown() const noexcept; void printAttributes( const std::unordered_map &map, diff --git a/exporters/ostream/include/opentelemetry/exporters/ostream/metric_exporter.h b/exporters/ostream/include/opentelemetry/exporters/ostream/metric_exporter.h index 92419fdea0..0dd22203e5 100644 --- a/exporters/ostream/include/opentelemetry/exporters/ostream/metric_exporter.h +++ b/exporters/ostream/include/opentelemetry/exporters/ostream/metric_exporter.h @@ -3,10 +3,11 @@ #pragma once +#include #include +#include #include -#include "opentelemetry/common/spin_lock_mutex.h" #include "opentelemetry/sdk/metrics/data/metric_data.h" #include "opentelemetry/sdk/metrics/export/metric_producer.h" #include "opentelemetry/sdk/metrics/instruments.h" @@ -72,8 +73,8 @@ class OStreamMetricExporter final : public opentelemetry::sdk::metrics::PushMetr private: std::ostream &sout_; - bool is_shutdown_ = false; - mutable opentelemetry::common::SpinLockMutex lock_; + std::atomic is_shutdown_{false}; + std::mutex serialize_lock_; sdk::metrics::AggregationTemporality aggregation_temporality_; bool isShutdown() const noexcept; void printInstrumentationInfoMetricData(const sdk::metrics::ScopeMetrics &info_metrics, diff --git a/exporters/ostream/include/opentelemetry/exporters/ostream/span_exporter.h b/exporters/ostream/include/opentelemetry/exporters/ostream/span_exporter.h index 139653686d..153aa4701e 100644 --- a/exporters/ostream/include/opentelemetry/exporters/ostream/span_exporter.h +++ b/exporters/ostream/include/opentelemetry/exporters/ostream/span_exporter.h @@ -3,12 +3,12 @@ #pragma once -#include "opentelemetry/common/spin_lock_mutex.h" #include "opentelemetry/nostd/span.h" #include "opentelemetry/sdk/trace/exporter.h" #include "opentelemetry/sdk/trace/span_data.h" #include "opentelemetry/version.h" +#include #include #include #include @@ -51,8 +51,7 @@ class OStreamSpanExporter final : public opentelemetry::sdk::trace::SpanExporter private: std::ostream &sout_; - bool is_shutdown_ = false; - mutable opentelemetry::common::SpinLockMutex lock_; + std::atomic is_shutdown_{false}; bool isShutdown() const noexcept; // Mapping status number to the string from api/include/opentelemetry/trace/span_metadata.h diff --git a/exporters/ostream/src/log_record_exporter.cc b/exporters/ostream/src/log_record_exporter.cc index 7edae02b43..88b8ba5ba2 100644 --- a/exporters/ostream/src/log_record_exporter.cc +++ b/exporters/ostream/src/log_record_exporter.cc @@ -126,14 +126,12 @@ bool OStreamLogRecordExporter::ForceFlush(std::chrono::microseconds /* timeout * bool OStreamLogRecordExporter::Shutdown(std::chrono::microseconds) noexcept { - const std::lock_guard locked(lock_); is_shutdown_ = true; return true; } bool OStreamLogRecordExporter::isShutdown() const noexcept { - const std::lock_guard locked(lock_); return is_shutdown_; } diff --git a/exporters/ostream/src/metric_exporter.cc b/exporters/ostream/src/metric_exporter.cc index 003aaa075f..4871a28b3d 100644 --- a/exporters/ostream/src/metric_exporter.cc +++ b/exporters/ostream/src/metric_exporter.cc @@ -129,7 +129,7 @@ void OStreamMetricExporter::printInstrumentationInfoMetricData( const sdk::metrics::ResourceMetrics &data) { // sout_ is shared - const std::lock_guard locked(lock_); + const std::lock_guard serialize(serialize_lock_); sout_ << "{"; sout_ << "\n scope name\t: " << info_metric.scope_->GetName() << "\n schema url\t: " << info_metric.scope_->GetSchemaURL() @@ -246,20 +246,19 @@ void OStreamMetricExporter::printPointAttributes( bool OStreamMetricExporter::ForceFlush(std::chrono::microseconds /* timeout */) noexcept { - const std::lock_guard locked(lock_); + const std::lock_guard serialize(serialize_lock_); + sout_.flush(); return true; } bool OStreamMetricExporter::Shutdown(std::chrono::microseconds /* timeout */) noexcept { - const std::lock_guard locked(lock_); is_shutdown_ = true; return true; } bool OStreamMetricExporter::isShutdown() const noexcept { - const std::lock_guard locked(lock_); return is_shutdown_; } diff --git a/exporters/ostream/src/span_exporter.cc b/exporters/ostream/src/span_exporter.cc index 6a88c32235..2e6186b6f7 100644 --- a/exporters/ostream/src/span_exporter.cc +++ b/exporters/ostream/src/span_exporter.cc @@ -106,16 +106,15 @@ bool OStreamSpanExporter::ForceFlush(std::chrono::microseconds /* timeout */) no bool OStreamSpanExporter::Shutdown(std::chrono::microseconds /* timeout */) noexcept { - const std::lock_guard locked(lock_); is_shutdown_ = true; return true; } bool OStreamSpanExporter::isShutdown() const noexcept { - const std::lock_guard locked(lock_); return is_shutdown_; } + void OStreamSpanExporter::printAttributes( const std::unordered_map &map, const std::string prefix) diff --git a/exporters/otlp/include/opentelemetry/exporters/otlp/otlp_grpc_exporter.h b/exporters/otlp/include/opentelemetry/exporters/otlp/otlp_grpc_exporter.h index fdd6bc4a17..80346a253d 100644 --- a/exporters/otlp/include/opentelemetry/exporters/otlp/otlp_grpc_exporter.h +++ b/exporters/otlp/include/opentelemetry/exporters/otlp/otlp_grpc_exporter.h @@ -3,11 +3,11 @@ #pragma once +#include #include #include "opentelemetry/exporters/otlp/protobuf_include_prefix.h" -#include "opentelemetry/common/spin_lock_mutex.h" #include "opentelemetry/proto/collector/trace/v1/trace_service.grpc.pb.h" #include "opentelemetry/exporters/otlp/protobuf_include_suffix.h" @@ -92,8 +92,7 @@ class OtlpGrpcExporter final : public opentelemetry::sdk::trace::SpanExporter * @param stub the service stub to be used for exporting */ OtlpGrpcExporter(std::unique_ptr stub); - bool is_shutdown_ = false; - mutable opentelemetry::common::SpinLockMutex lock_; + std::atomic is_shutdown_{false}; bool isShutdown() const noexcept; }; } // namespace otlp diff --git a/exporters/otlp/include/opentelemetry/exporters/otlp/otlp_grpc_log_record_exporter.h b/exporters/otlp/include/opentelemetry/exporters/otlp/otlp_grpc_log_record_exporter.h index e4c2e96617..72af90701f 100644 --- a/exporters/otlp/include/opentelemetry/exporters/otlp/otlp_grpc_log_record_exporter.h +++ b/exporters/otlp/include/opentelemetry/exporters/otlp/otlp_grpc_log_record_exporter.h @@ -7,7 +7,6 @@ #include "opentelemetry/exporters/otlp/protobuf_include_prefix.h" #include "opentelemetry/proto/collector/logs/v1/logs_service.grpc.pb.h" -#include "opentelemetry/common/spin_lock_mutex.h" #include "opentelemetry/exporters/otlp/protobuf_include_suffix.h" // clang-format on @@ -16,6 +15,8 @@ #include "opentelemetry/exporters/otlp/otlp_grpc_log_record_exporter_options.h" #include "opentelemetry/sdk/logs/exporter.h" +#include + OPENTELEMETRY_BEGIN_NAMESPACE namespace exporter { @@ -92,8 +93,7 @@ class OtlpGrpcLogRecordExporter : public opentelemetry::sdk::logs::LogRecordExpo */ OtlpGrpcLogRecordExporter( std::unique_ptr stub); - bool is_shutdown_ = false; - mutable opentelemetry::common::SpinLockMutex lock_; + std::atomic is_shutdown_{false}; bool isShutdown() const noexcept; }; diff --git a/exporters/otlp/include/opentelemetry/exporters/otlp/otlp_grpc_metric_exporter.h b/exporters/otlp/include/opentelemetry/exporters/otlp/otlp_grpc_metric_exporter.h index 1385c381ec..3899e926f6 100644 --- a/exporters/otlp/include/opentelemetry/exporters/otlp/otlp_grpc_metric_exporter.h +++ b/exporters/otlp/include/opentelemetry/exporters/otlp/otlp_grpc_metric_exporter.h @@ -7,7 +7,6 @@ #include "opentelemetry/exporters/otlp/protobuf_include_prefix.h" #include "opentelemetry/proto/collector/metrics/v1/metrics_service.grpc.pb.h" -#include "opentelemetry/common/spin_lock_mutex.h" #include "opentelemetry/exporters/otlp/protobuf_include_suffix.h" // clang-format on @@ -16,6 +15,8 @@ #include "opentelemetry/exporters/otlp/otlp_grpc_metric_exporter_options.h" #include "opentelemetry/sdk/metrics/push_metric_exporter.h" +#include + OPENTELEMETRY_BEGIN_NAMESPACE namespace exporter { @@ -82,8 +83,7 @@ class OtlpGrpcMetricExporter : public opentelemetry::sdk::metrics::PushMetricExp */ OtlpGrpcMetricExporter( std::unique_ptr stub); - bool is_shutdown_ = false; - mutable opentelemetry::common::SpinLockMutex lock_; + std::atomic is_shutdown_{false}; bool isShutdown() const noexcept; }; } // namespace otlp diff --git a/exporters/otlp/include/opentelemetry/exporters/otlp/otlp_http_client.h b/exporters/otlp/include/opentelemetry/exporters/otlp/otlp_http_client.h index 870ea88544..9d79b5d99b 100644 --- a/exporters/otlp/include/opentelemetry/exporters/otlp/otlp_http_client.h +++ b/exporters/otlp/include/opentelemetry/exporters/otlp/otlp_http_client.h @@ -3,7 +3,6 @@ #pragma once -#include "opentelemetry/common/spin_lock_mutex.h" #include "opentelemetry/ext/http/client/http_client.h" #include "opentelemetry/nostd/variant.h" #include "opentelemetry/sdk/common/exporter_utils.h" diff --git a/exporters/otlp/src/otlp_grpc_exporter.cc b/exporters/otlp/src/otlp_grpc_exporter.cc index 607c5ac7c1..ed0fc74518 100644 --- a/exporters/otlp/src/otlp_grpc_exporter.cc +++ b/exporters/otlp/src/otlp_grpc_exporter.cc @@ -135,7 +135,6 @@ bool OtlpGrpcExporter::ForceFlush( bool OtlpGrpcExporter::Shutdown( OPENTELEMETRY_MAYBE_UNUSED std::chrono::microseconds timeout) noexcept { - const std::lock_guard locked(lock_); is_shutdown_ = true; #ifdef ENABLE_ASYNC_EXPORT return client_->Shutdown(timeout); @@ -146,7 +145,6 @@ bool OtlpGrpcExporter::Shutdown( bool OtlpGrpcExporter::isShutdown() const noexcept { - const std::lock_guard locked(lock_); return is_shutdown_; } diff --git a/exporters/otlp/src/otlp_grpc_log_record_exporter.cc b/exporters/otlp/src/otlp_grpc_log_record_exporter.cc index f5971228e9..1069703f96 100644 --- a/exporters/otlp/src/otlp_grpc_log_record_exporter.cc +++ b/exporters/otlp/src/otlp_grpc_log_record_exporter.cc @@ -138,7 +138,6 @@ opentelemetry::sdk::common::ExportResult OtlpGrpcLogRecordExporter::Export( bool OtlpGrpcLogRecordExporter::Shutdown( OPENTELEMETRY_MAYBE_UNUSED std::chrono::microseconds timeout) noexcept { - const std::lock_guard locked(lock_); is_shutdown_ = true; #ifdef ENABLE_ASYNC_EXPORT return client_->Shutdown(timeout); @@ -159,7 +158,6 @@ bool OtlpGrpcLogRecordExporter::ForceFlush( bool OtlpGrpcLogRecordExporter::isShutdown() const noexcept { - const std::lock_guard locked(lock_); return is_shutdown_; } diff --git a/exporters/otlp/src/otlp_grpc_metric_exporter.cc b/exporters/otlp/src/otlp_grpc_metric_exporter.cc index 1e0aeb29f9..6a78149eaa 100644 --- a/exporters/otlp/src/otlp_grpc_metric_exporter.cc +++ b/exporters/otlp/src/otlp_grpc_metric_exporter.cc @@ -141,7 +141,6 @@ bool OtlpGrpcMetricExporter::ForceFlush( bool OtlpGrpcMetricExporter::Shutdown( OPENTELEMETRY_MAYBE_UNUSED std::chrono::microseconds timeout) noexcept { - const std::lock_guard locked(lock_); is_shutdown_ = true; #ifdef ENABLE_ASYNC_EXPORT return client_->Shutdown(timeout); @@ -152,7 +151,6 @@ bool OtlpGrpcMetricExporter::Shutdown( bool OtlpGrpcMetricExporter::isShutdown() const noexcept { - const std::lock_guard locked(lock_); return is_shutdown_; } diff --git a/exporters/prometheus/include/opentelemetry/exporters/prometheus/exporter.h b/exporters/prometheus/include/opentelemetry/exporters/prometheus/exporter.h index 4bd624d1e2..34bdc4a0f8 100644 --- a/exporters/prometheus/include/opentelemetry/exporters/prometheus/exporter.h +++ b/exporters/prometheus/include/opentelemetry/exporters/prometheus/exporter.h @@ -8,7 +8,7 @@ #include #include -#include "opentelemetry/common/spin_lock_mutex.h" + #include "opentelemetry/exporters/prometheus/collector.h" #include "opentelemetry/exporters/prometheus/exporter_options.h" #include "opentelemetry/nostd/span.h" diff --git a/exporters/zipkin/include/opentelemetry/exporters/zipkin/zipkin_exporter.h b/exporters/zipkin/include/opentelemetry/exporters/zipkin/zipkin_exporter.h index 4de6513bfb..effa2dfe75 100644 --- a/exporters/zipkin/include/opentelemetry/exporters/zipkin/zipkin_exporter.h +++ b/exporters/zipkin/include/opentelemetry/exporters/zipkin/zipkin_exporter.h @@ -3,7 +3,6 @@ #pragma once -#include "opentelemetry/common/spin_lock_mutex.h" #include "opentelemetry/exporters/zipkin/zipkin_exporter_options.h" #include "opentelemetry/ext/http/client/http_client_factory.h" #include "opentelemetry/ext/http/common/url_parser.h" @@ -13,6 +12,8 @@ #include "nlohmann/json.hpp" +#include + OPENTELEMETRY_BEGIN_NAMESPACE namespace exporter { @@ -69,7 +70,7 @@ class ZipkinExporter final : public opentelemetry::sdk::trace::SpanExporter private: // The configuration options associated with this exporter. - bool is_shutdown_ = false; + std::atomic is_shutdown_{false}; ZipkinExporterOptions options_; std::shared_ptr http_client_; opentelemetry::ext::http::common::UrlParser url_parser_; @@ -84,7 +85,6 @@ class ZipkinExporter final : public opentelemetry::sdk::trace::SpanExporter */ ZipkinExporter(std::shared_ptr http_client); - mutable opentelemetry::common::SpinLockMutex lock_; bool isShutdown() const noexcept; }; } // namespace zipkin diff --git a/exporters/zipkin/src/zipkin_exporter.cc b/exporters/zipkin/src/zipkin_exporter.cc index 2dd8a3e884..97fdf4e63a 100644 --- a/exporters/zipkin/src/zipkin_exporter.cc +++ b/exporters/zipkin/src/zipkin_exporter.cc @@ -117,14 +117,12 @@ bool ZipkinExporter::ForceFlush(std::chrono::microseconds /* timeout */) noexcep bool ZipkinExporter::Shutdown(std::chrono::microseconds /* timeout */) noexcept { - const std::lock_guard locked(lock_); is_shutdown_ = true; return true; } bool ZipkinExporter::isShutdown() const noexcept { - const std::lock_guard locked(lock_); return is_shutdown_; }