From 1f7ce8c5706c8b585b55eef8a1e8e6a2f264f013 Mon Sep 17 00:00:00 2001 From: Lalit Kumar Bhasin Date: Fri, 25 Jun 2021 15:00:46 +0530 Subject: [PATCH 01/15] global error handler --- .../elasticsearch/src/es_log_exporter.cc | 47 +++++---- exporters/jaeger/src/TUDPTransport.cc | 7 +- exporters/jaeger/src/udp_transport.cc | 6 +- exporters/otlp/src/otlp_grpc_exporter.cc | 7 +- exporters/otlp/src/otlp_http_exporter.cc | 61 +++++------- exporters/zipkin/src/zipkin_exporter.cc | 3 +- .../ext/http/client/http_client.h | 4 +- .../sdk/common/global_error_handler.h | 99 +++++++++++++++++++ 8 files changed, 164 insertions(+), 70 deletions(-) create mode 100644 sdk/include/opentelemetry/sdk/common/global_error_handler.h diff --git a/exporters/elasticsearch/src/es_log_exporter.cc b/exporters/elasticsearch/src/es_log_exporter.cc index c7201db907..cf86ca1907 100644 --- a/exporters/elasticsearch/src/es_log_exporter.cc +++ b/exporters/elasticsearch/src/es_log_exporter.cc @@ -3,8 +3,11 @@ #ifdef ENABLE_LOGS_PREVIEW +# include // std::stringstream + # include "opentelemetry/exporters/elasticsearch/es_log_exporter.h" # include "opentelemetry/exporters/elasticsearch/es_log_recordable.h" +# include "opentelemetry/sdk/common/global_error_handler.h" namespace nostd = opentelemetry::nostd; namespace sdklogs = opentelemetry::sdk::logs; @@ -74,23 +77,19 @@ class ResponseHandler : public http_client::EventHandler switch (state) { case http_client::SessionState::ConnectFailed: - if (console_debug_) - std::cout << "Connection to elasticsearch failed\n"; + OTEL_ERROR("[ES Trace Exporter] Connection to elasticsearch failed\n") cv_.notify_all(); break; case http_client::SessionState::SendFailed: - if (console_debug_) - std::cout << "Request failed to be sent to elasticsearch\n"; + OTEL_ERROR("[ES Trace Exporter] Request failed to be sent to elasticsearch\n") cv_.notify_all(); break; case http_client::SessionState::TimedOut: - if (console_debug_) - std::cout << "Request to elasticsearch timed out\n"; + OTEL_ERROR("[ES Trace Exporter] Request to elasticsearch timed out\n") cv_.notify_all(); break; case http_client::SessionState::NetworkError: - if (console_debug_) - std::cout << "Network error to elasticsearch\n"; + OTEL_ERROR("[ES Trace Exporter] Network error to elasticsearch\n") cv_.notify_all(); break; } @@ -131,11 +130,8 @@ sdk::common::ExportResult ElasticsearchLogExporter::Export( // Return failure if this exporter has been shutdown if (is_shutdown_) { - if (options_.console_debug_) - { - std::cout << "Export failed, exporter is shutdown" << std::endl; - } + OTEL_ERROR("[ES Trace Exporter] Export failed, exporter is shutdown\n") return sdk::common::ExportResult::kFailure; } @@ -172,8 +168,9 @@ sdk::common::ExportResult ElasticsearchLogExporter::Export( // Wait for the response to be received if (options_.console_debug_) { - std::cout << "waiting for response from Elasticsearch (timeout = " << options_.response_timeout_ - << " seconds)" << std::endl; + std::stringstream ss; + ss << "[ES Trace Exporter] DEBUG: waiting for response from Elasticsearch (timeout = " + << options_.response_timeout_ << " seconds)" << std::endl; } bool write_successful = handler->waitForResponse(); @@ -191,19 +188,21 @@ sdk::common::ExportResult ElasticsearchLogExporter::Export( std::string responseBody = handler->GetResponseBody(); if (responseBody.find("\"failed\" : 0") == std::string::npos) { - if (options_.console_debug_) - { - std::cout << "Logs were not written to Elasticsearch correctly, response body:" << std::endl; - std::cout << responseBody << std::endl; - } - - // TODO: Retry logic - return sdk::common::ExportResult::kFailure; + std::stringstream ss; + ss << "[ES Trace Exporter] DEBUG: Logs were not written to Elasticsearch correctly, response " + "body:" + << std::endl; + ss << responseBody << std::endl; + OTEL_ERROR(s.str()) } - return sdk::common::ExportResult::kSuccess; + // TODO: Retry logic + return sdk::common::ExportResult::kFailure; } +return sdk::common::ExportResult::kSuccess; +} // namespace logs + bool ElasticsearchLogExporter::Shutdown(std::chrono::microseconds timeout) noexcept { is_shutdown_ = true; @@ -214,7 +213,7 @@ bool ElasticsearchLogExporter::Shutdown(std::chrono::microseconds timeout) noexc return true; } -} // namespace logs +} // namespace exporter } // namespace exporter OPENTELEMETRY_END_NAMESPACE #endif diff --git a/exporters/jaeger/src/TUDPTransport.cc b/exporters/jaeger/src/TUDPTransport.cc index 123ba66d96..b6cf777365 100644 --- a/exporters/jaeger/src/TUDPTransport.cc +++ b/exporters/jaeger/src/TUDPTransport.cc @@ -1,7 +1,10 @@ // Copyright The OpenTelemetry Authors // SPDX-License-Identifier: Apache-2.0 +#include // std::stringstream + #include "TUDPTransport.h" +#include "opentelemetry/sdk/common/global_error_handler.h" OPENTELEMETRY_BEGIN_NAMESPACE namespace exporter @@ -51,7 +54,9 @@ void TUDPTransport::open() if (error) { - // TODO: log error + std::stringstream ss; + ss << "Jaeger Exporter: getaddrinfo failed with error: " << error; + OTEL_ERROR(ss.str()) return; } diff --git a/exporters/jaeger/src/udp_transport.cc b/exporters/jaeger/src/udp_transport.cc index 82624ae313..03b7ef96c0 100644 --- a/exporters/jaeger/src/udp_transport.cc +++ b/exporters/jaeger/src/udp_transport.cc @@ -1,6 +1,9 @@ // Copyright The OpenTelemetry Authors // SPDX-License-Identifier: Apache-2.0 +#include // std::stringstream + +#include "opentelemetry/sdk/common/global_error_handler.h" #include "udp_transport.h" OPENTELEMETRY_BEGIN_NAMESPACE @@ -36,7 +39,8 @@ void UDPTransport::InitSocket() int err = WSAStartup(wVersionRequested, &wsaData); if (err != 0) { - // TODO: handle error + ss << "Jaeger Exporter: WSAStartup failed with error: " << error; + OTEL_ERROR(ss.str()) return; } diff --git a/exporters/otlp/src/otlp_grpc_exporter.cc b/exporters/otlp/src/otlp_grpc_exporter.cc index e0aaa0a0ab..c96c36ded8 100644 --- a/exporters/otlp/src/otlp_grpc_exporter.cc +++ b/exporters/otlp/src/otlp_grpc_exporter.cc @@ -3,10 +3,11 @@ #include "opentelemetry/exporters/otlp/otlp_grpc_exporter.h" #include "opentelemetry/exporters/otlp/otlp_recordable.h" +#include "opentelemetry/sdk/common/global_error_handler.h" #include #include -#include +#include // std::stringstream OPENTELEMETRY_BEGIN_NAMESPACE namespace exporter @@ -111,7 +112,9 @@ sdk::common::ExportResult OtlpGrpcExporter::Export( if (!status.ok()) { - std::cerr << "[OTLP Exporter] Export() failed: " << status.error_message() << "\n"; + std::stringstream ss; + ss << "[OTLP Exporter] Export() failed: " << status.error_message() << "\n"; + OTEL_ERROR(ss.str()) return sdk::common::ExportResult::kFailure; } return sdk::common::ExportResult::kSuccess; diff --git a/exporters/otlp/src/otlp_http_exporter.cc b/exporters/otlp/src/otlp_http_exporter.cc index dec5268165..68cb6f31ca 100644 --- a/exporters/otlp/src/otlp_http_exporter.cc +++ b/exporters/otlp/src/otlp_http_exporter.cc @@ -111,114 +111,93 @@ class ResponseHandler : public http_client::EventHandler switch (state) { case http_client::SessionState::CreateFailed: - if (console_debug_) - { - std::cerr << "[OTLP HTTP Exporter] Session state: session create failed" << std::endl; - } + OTEL_ERROR("[OTLP HTTP Exporter] Session state: session create failed\n"); cv_.notify_all(); break; case http_client::SessionState::Created: if (console_debug_) { - std::cerr << "[OTLP HTTP Exporter] Session state: session created" << std::endl; + OTEL_ERROR("[OTLP HTTP Exporter] DEBUG: Session state: session created\n") } break; case http_client::SessionState::Destroyed: if (console_debug_) { - std::cerr << "[OTLP HTTP Exporter] Session state: session destroyed" << std::endl; + OTEL_ERROR("[OTLP HTTP Exporter] DEBUG: Session state: session destroyed\n") } break; case http_client::SessionState::Connecting: if (console_debug_) { - std::cerr << "[OTLP HTTP Exporter] Session state: connecting to peer" << std::endl; + OTEL_ERROR("[OTLP HTTP Exporter] DEBUG: Session state: connecting to peer\n") } break; case http_client::SessionState::ConnectFailed: - if (console_debug_) - { - std::cerr << "[OTLP HTTP Exporter] Session state: connection failed" << std::endl; - } + OTEL_ERROR("[OTLP HTTP Exporter] Session state: connection failed\n") cv_.notify_all(); break; case http_client::SessionState::Connected: if (console_debug_) { - std::cerr << "[OTLP HTTP Exporter] Session state: connected" << std::endl; + OTEL_ERROR("[OTLP HTTP Exporter] DEBUG Session state: connected\n") } break; case http_client::SessionState::Sending: if (console_debug_) { - std::cerr << "[OTLP HTTP Exporter] Session state: sending request" << std::endl; + OTEL_ERROR("[OTLP HTTP Exporter] DEBUG: Session state: sending request\n") } break; case http_client::SessionState::SendFailed: - if (console_debug_) - { - std::cerr << "[OTLP HTTP Exporter] Session state: request send failed" << std::endl; - } + OTEL_ERROR("[OTLP HTTP Exporter] Session state: request send failed\n") cv_.notify_all(); break; case http_client::SessionState::Response: if (console_debug_) { - std::cerr << "[OTLP HTTP Exporter] Session state: response received" << std::endl; + OTEL_ERROR("[OTLP HTTP Exporter] DEBUG:Session state: response received\n") } break; case http_client::SessionState::SSLHandshakeFailed: - if (console_debug_) - { - std::cerr << "[OTLP HTTP Exporter] Session state: SSL handshake failed" << std::endl; - } + OTEL_ERROR("[OTLP HTTP Exporter] Session state: SSL handshake failed\n") cv_.notify_all(); break; case http_client::SessionState::TimedOut: - if (console_debug_) - { - std::cerr << "[OTLP HTTP Exporter] Session state: request time out" << std::endl; - } + OTEL_ERROR("[OTLP HTTP Exporter] Session state: request time out\n") cv_.notify_all(); break; case http_client::SessionState::NetworkError: - if (console_debug_) - { - std::cerr << "[OTLP HTTP Exporter] Session state: network error" << std::endl; - } + OTEL_ERROR("[OTLP HTTP Exporter] Session state: network error\n") cv_.notify_all(); break; case http_client::SessionState::ReadError: if (console_debug_) { - std::cerr << "[OTLP HTTP Exporter] Session state: error reading response" << std::endl; + OTEL_ERROR("[OTLP HTTP Exporter] DEBUG: Session state: error reading responsen") } break; case http_client::SessionState::WriteError: if (console_debug_) { - std::cerr << "[OTLP HTTP Exporter] Session state: error writing request" << std::endl; + OTEL_ERROR("[OTLP HTTP Exporter] DEBUG:Session state: error writing request\n") } break; case http_client::SessionState::Cancelled: - if (console_debug_) - { - std::cerr << "[OTLP HTTP Exporter] Session state: (manually) cancelled" << std::endl; - } + OTEL_ERROR("[OTLP HTTP Exporter] Session state: (manually) cancelled\n") cv_.notify_all(); break; @@ -638,7 +617,9 @@ sdk::common::ExportResult OtlpHttpExporter::Export( json_request.dump(-1, ' ', false, nlohmann::detail::error_handler_t::replace); if (options_.console_debug) { - std::cout << "[OTLP HTTP Exporter] Request body(Json):\n" << post_body_json << std::endl; + std::stringstream ss; + ss << "[OTLP HTTP Exporter] DEBUG: Request body(Json):\n" << post_body_json << std::endl; + OTEL_ERROR(ss.str()) } body_vec.assign(post_body_json.begin(), post_body_json.end()); content_type = kHttpJsonContentType; @@ -660,8 +641,10 @@ sdk::common::ExportResult OtlpHttpExporter::Export( // Wait for the response to be received if (options_.console_debug) { - std::cout << "[OTLP HTTP Exporter] Waiting for response from " << options_.url - << " (timeout = " << options_.timeout.count() << " milliseconds)" << std::endl; + std::stringstream ss; + ss << "[OTLP HTTP Exporter] DEBUG: Waiting for response from " << options_.url + << " (timeout = " << options_.timeout.count() << " milliseconds)" << std::endl; + OTEL_ERROR(ss.str()) } bool write_successful = handler->waitForResponse(); diff --git a/exporters/zipkin/src/zipkin_exporter.cc b/exporters/zipkin/src/zipkin_exporter.cc index 717f9e7c97..05dd33d37d 100644 --- a/exporters/zipkin/src/zipkin_exporter.cc +++ b/exporters/zipkin/src/zipkin_exporter.cc @@ -5,6 +5,7 @@ #include "opentelemetry/exporters/zipkin/recordable.h" #include "opentelemetry/ext/http/client/http_client_factory.h" #include "opentelemetry/ext/http/common/url_parser.h" +#include "opentelemetry/sdk/common/global_error_handler.h" OPENTELEMETRY_BEGIN_NAMESPACE namespace exporter @@ -71,7 +72,7 @@ sdk::common::ExportResult ZipkinExporter::Export( { if (result.GetSessionState() == http_client::SessionState::ConnectFailed) { - // TODO -> Handle error / retries + OTEL_ERROR("ZIPKIN EXPORTER] Zipkin Exporter: Connection failed") } return sdk::common::ExportResult::kFailure; } diff --git a/ext/include/opentelemetry/ext/http/client/http_client.h b/ext/include/opentelemetry/ext/http/client/http_client.h index 4e3e9845e0..308335e492 100644 --- a/ext/include/opentelemetry/ext/http/client/http_client.h +++ b/ext/include/opentelemetry/ext/http/client/http_client.h @@ -183,8 +183,8 @@ class Result { if (response_ == nullptr) { - static NoopResponse res; - return res; + // let's not return nullptr + response_.reset(new NoopResponse()); } return *response_; } diff --git a/sdk/include/opentelemetry/sdk/common/global_error_handler.h b/sdk/include/opentelemetry/sdk/common/global_error_handler.h new file mode 100644 index 0000000000..923b054aa8 --- /dev/null +++ b/sdk/include/opentelemetry/sdk/common/global_error_handler.h @@ -0,0 +1,99 @@ +// Copyright The OpenTelemetry Authors +// SPDX-License-Identifier: Apache-2.0 + +#pragma once + +#include +#include +#include + +#include "opentelemetry/common/spin_lock_mutex.h" +#include "opentelemetry/nostd/shared_ptr.h" +#include "opentelemetry/nostd/string_view.h" +#include "opentelemetry/version.h" + +#ifndef IGNORE_OTEL_ERROR_LOGGER +# define OTEL_ERROR(message) \ + opentelemetry::sdk::common::Error::GlobalErrorHandler::GetErrorHandler()->Handle(message); +#else +# define OTEL_ERROR(message) +#endif + +OPENTELEMETRY_BEGIN_NAMESPACE +namespace sdk +{ +namespace common +{ +namespace Error +{ + +class ErrorHandler +{ +public: + virtual void Handle(nostd::string_view error) noexcept = 0; +}; + +class DefaultErrorHandler : public ErrorHandler +{ +public: + void Handle(nostd::string_view error) noexcept override + { + // std::cout::operator<<() should be thread safe without any chaining calls. + std::cerr << error; + } +}; + +class NoopErrorHandler : public ErrorHandler +{ +public: + void Handle(nostd::string_view error) noexcept override + { + // ignore the error message + } +}; + +/** + * Stores the singleton global ErrorHandler. + */ +class GlobalErrorHandler +{ +public: + /** + * Returns the singleton ErrorHandler. + * + * By default, a default ErrorHandler is returned. This will never return a + * nullptr ErrorHandler. + */ + static nostd::shared_ptr GetErrorHandler() noexcept + { + std::lock_guard guard(GetLock()); + return nostd::shared_ptr(GetHandler()); + } + + /** + * Changes the singleton ErrorHandler. + */ + static void SetErrorHandler(nostd::shared_ptr eh) noexcept + { + std::lock_guard guard(GetLock()); + GetHandler() = eh; + } + +private: + static nostd::shared_ptr &GetHandler() noexcept + { + static nostd::shared_ptr handler(new DefaultErrorHandler); + return handler; + } + + static opentelemetry::common::SpinLockMutex &GetLock() noexcept + { + static opentelemetry::common::SpinLockMutex lock; + return lock; + } +}; + +} // namespace Error +} // namespace common +} // namespace sdk +OPENTELEMETRY_END_NAMESPACE \ No newline at end of file From 6b6ad70024ce75129c41628981765a63947213fa Mon Sep 17 00:00:00 2001 From: Lalit Kumar Bhasin Date: Fri, 25 Jun 2021 15:30:24 +0530 Subject: [PATCH 02/15] fix macro --- exporters/elasticsearch/src/es_log_exporter.cc | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/exporters/elasticsearch/src/es_log_exporter.cc b/exporters/elasticsearch/src/es_log_exporter.cc index cf86ca1907..8320d944c6 100644 --- a/exporters/elasticsearch/src/es_log_exporter.cc +++ b/exporters/elasticsearch/src/es_log_exporter.cc @@ -171,6 +171,7 @@ sdk::common::ExportResult ElasticsearchLogExporter::Export( std::stringstream ss; ss << "[ES Trace Exporter] DEBUG: waiting for response from Elasticsearch (timeout = " << options_.response_timeout_ << " seconds)" << std::endl; + OTEL_ERROR(ss.str()) } bool write_successful = handler->waitForResponse(); @@ -193,14 +194,12 @@ sdk::common::ExportResult ElasticsearchLogExporter::Export( "body:" << std::endl; ss << responseBody << std::endl; - OTEL_ERROR(s.str()) + OTEL_ERROR(ss.str()) + // TODO: Retry logic + return sdk::common::ExportResult::kFailure; } - // TODO: Retry logic - return sdk::common::ExportResult::kFailure; -} - -return sdk::common::ExportResult::kSuccess; + return sdk::common::ExportResult::kSuccess; } // namespace logs bool ElasticsearchLogExporter::Shutdown(std::chrono::microseconds timeout) noexcept @@ -213,7 +212,7 @@ bool ElasticsearchLogExporter::Shutdown(std::chrono::microseconds timeout) noexc return true; } -} // namespace exporter +} // namespace logs } // namespace exporter OPENTELEMETRY_END_NAMESPACE #endif From 818d7d327fa4d5ffcf8d0c8e32c86cc16b88e6b5 Mon Sep 17 00:00:00 2001 From: Lalit Kumar Bhasin Date: Fri, 25 Jun 2021 15:40:52 +0530 Subject: [PATCH 03/15] fix syntax --- sdk/src/logs/logger.cc | 3 ++- sdk/src/trace/tracer_provider.cc | 8 +++++--- 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/sdk/src/logs/logger.cc b/sdk/src/logs/logger.cc index 970f61b7e9..3b7401a909 100644 --- a/sdk/src/logs/logger.cc +++ b/sdk/src/logs/logger.cc @@ -3,6 +3,7 @@ #ifdef ENABLE_LOGS_PREVIEW # include "opentelemetry/sdk/logs/logger.h" +# include "opentelemetry/sdk/common/global_error_handler.h" # include "opentelemetry/sdk/logs/log_record.h" # include "opentelemetry/trace/provider.h" @@ -48,7 +49,7 @@ void Logger::Log(opentelemetry::logs::Severity severity, auto recordable = processor->MakeRecordable(); if (recordable == nullptr) { - // TODO: Error diagnostics should indicate "recordable creation failed" to user + OTEL_ERROR("[LOGGER] Recordable creation failed") return; } diff --git a/sdk/src/trace/tracer_provider.cc b/sdk/src/trace/tracer_provider.cc index 13b41d73b6..73d4ced727 100644 --- a/sdk/src/trace/tracer_provider.cc +++ b/sdk/src/trace/tracer_provider.cc @@ -2,6 +2,7 @@ // SPDX-License-Identifier: Apache-2.0 #include "opentelemetry/sdk/trace/tracer_provider.h" +#include "opentelemetry/sdk/common/global_error_handler.h" OPENTELEMETRY_BEGIN_NAMESPACE namespace sdk @@ -40,9 +41,10 @@ nostd::shared_ptr TracerProvider::GetTracer( { library_name = ""; } - // if (library_name == "") { - // // TODO: log invalid library_name. - // } + if (library_name == "") + { + OTEL_ERROR("[TracerProvider::GetTracer] Library name is empty.") + } const std::lock_guard guard(lock_); From 8e8e6e8cccd3868c3a620829860a839a81999001 Mon Sep 17 00:00:00 2001 From: Lalit Kumar Bhasin Date: Fri, 25 Jun 2021 15:53:52 +0530 Subject: [PATCH 04/15] fix otlp http exporter --- exporters/otlp/src/otlp_http_exporter.cc | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/exporters/otlp/src/otlp_http_exporter.cc b/exporters/otlp/src/otlp_http_exporter.cc index 68cb6f31ca..d3d59541c5 100644 --- a/exporters/otlp/src/otlp_http_exporter.cc +++ b/exporters/otlp/src/otlp_http_exporter.cc @@ -16,11 +16,12 @@ #include "opentelemetry/proto/collector/trace/v1/trace_service.pb.h" #include "opentelemetry/exporters/otlp/protobuf_include_suffix.h" +#include "opentelemetry/sdk/common/global_error_handler.h" #include #include -#include #include +#include #include #include @@ -66,14 +67,16 @@ class ResponseHandler : public http_client::EventHandler if (console_debug_) { - std::cout << "[OTLP HTTP Exporter] Status:" << response.GetStatusCode() << std::endl - << "Header:" << std::endl; + std::stringstream ss; + ss << "[OTLP HTTP Exporter] DEBUG: Status:" << response.GetStatusCode() << std::endl + << "Header:" << std::endl; response.ForEachHeader([](opentelemetry::nostd::string_view header_name, opentelemetry::nostd::string_view header_value) { - std::cout << "\t" << header_name.data() << " : " << header_value.data() << std::endl; + ss << "\t" << header_name.data() << " : " << header_value.data() << std::endl; return true; }); - std::cout << "Body:" << std::endl << body_ << std::endl; + ss << "Body:" << std::endl << body_ << std::endl; + ERROR(ss.str()) } // Set the response_received_ flag to true and notify any threads waiting on this result From 5b3c3eb5813c61cc6a7678cf08e04a980b185a42 Mon Sep 17 00:00:00 2001 From: Lalit Kumar Bhasin Date: Fri, 25 Jun 2021 15:58:48 +0530 Subject: [PATCH 05/15] fix macro --- exporters/otlp/src/otlp_http_exporter.cc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/exporters/otlp/src/otlp_http_exporter.cc b/exporters/otlp/src/otlp_http_exporter.cc index d3d59541c5..0f7faf8f19 100644 --- a/exporters/otlp/src/otlp_http_exporter.cc +++ b/exporters/otlp/src/otlp_http_exporter.cc @@ -76,7 +76,7 @@ class ResponseHandler : public http_client::EventHandler return true; }); ss << "Body:" << std::endl << body_ << std::endl; - ERROR(ss.str()) + OTEL_ERROR(ss.str()) } // Set the response_received_ flag to true and notify any threads waiting on this result From e6d5647c0a7bfed30435a1a5e469f919e72014f1 Mon Sep 17 00:00:00 2001 From: Lalit Kumar Bhasin Date: Fri, 25 Jun 2021 16:06:19 +0530 Subject: [PATCH 06/15] fix lambda --- exporters/elasticsearch/src/es_log_exporter.cc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/exporters/elasticsearch/src/es_log_exporter.cc b/exporters/elasticsearch/src/es_log_exporter.cc index 8320d944c6..be4104568a 100644 --- a/exporters/elasticsearch/src/es_log_exporter.cc +++ b/exporters/elasticsearch/src/es_log_exporter.cc @@ -200,7 +200,7 @@ sdk::common::ExportResult ElasticsearchLogExporter::Export( } return sdk::common::ExportResult::kSuccess; -} // namespace logs +} bool ElasticsearchLogExporter::Shutdown(std::chrono::microseconds timeout) noexcept { From e370f815733553e37d12fabdbbf857636c908c90 Mon Sep 17 00:00:00 2001 From: Lalit Kumar Bhasin Date: Fri, 25 Jun 2021 20:50:37 +0530 Subject: [PATCH 07/15] fix otlp exporter --- exporters/otlp/src/otlp_http_exporter.cc | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/exporters/otlp/src/otlp_http_exporter.cc b/exporters/otlp/src/otlp_http_exporter.cc index 0f7faf8f19..e438849948 100644 --- a/exporters/otlp/src/otlp_http_exporter.cc +++ b/exporters/otlp/src/otlp_http_exporter.cc @@ -70,8 +70,8 @@ class ResponseHandler : public http_client::EventHandler std::stringstream ss; ss << "[OTLP HTTP Exporter] DEBUG: Status:" << response.GetStatusCode() << std::endl << "Header:" << std::endl; - response.ForEachHeader([](opentelemetry::nostd::string_view header_name, - opentelemetry::nostd::string_view header_value) { + response.ForEachHeader([&ss](opentelemetry::nostd::string_view header_name, + opentelemetry::nostd::string_view header_value) { ss << "\t" << header_name.data() << " : " << header_value.data() << std::endl; return true; }); From 610345b9b8f1bae5f9636860a4a9faf833512d0a Mon Sep 17 00:00:00 2001 From: Lalit Kumar Bhasin Date: Mon, 28 Jun 2021 14:21:48 +0530 Subject: [PATCH 08/15] review comments --- .../elasticsearch/src/es_log_exporter.cc | 26 +-- exporters/jaeger/src/TUDPTransport.cc | 6 +- exporters/jaeger/src/udp_transport.cc | 5 +- exporters/otlp/src/otlp_grpc_exporter.cc | 7 +- exporters/otlp/src/otlp_http_exporter.cc | 45 ++--- exporters/zipkin/src/zipkin_exporter.cc | 4 +- .../sdk/common/global_error_handler.h | 180 +++++++++++++++--- sdk/include/opentelemetry/sdk_config.h | 7 + sdk/src/logs/logger.cc | 3 +- sdk/src/trace/tracer_provider.cc | 4 +- 10 files changed, 204 insertions(+), 83 deletions(-) create mode 100644 sdk/include/opentelemetry/sdk_config.h diff --git a/exporters/elasticsearch/src/es_log_exporter.cc b/exporters/elasticsearch/src/es_log_exporter.cc index be4104568a..180323d8bf 100644 --- a/exporters/elasticsearch/src/es_log_exporter.cc +++ b/exporters/elasticsearch/src/es_log_exporter.cc @@ -7,7 +7,7 @@ # include "opentelemetry/exporters/elasticsearch/es_log_exporter.h" # include "opentelemetry/exporters/elasticsearch/es_log_recordable.h" -# include "opentelemetry/sdk/common/global_error_handler.h" +# include "opentelemetry/sdk_config.h" namespace nostd = opentelemetry::nostd; namespace sdklogs = opentelemetry::sdk::logs; @@ -77,19 +77,19 @@ class ResponseHandler : public http_client::EventHandler switch (state) { case http_client::SessionState::ConnectFailed: - OTEL_ERROR("[ES Trace Exporter] Connection to elasticsearch failed\n") + OTEL_INTERNAL_LOG_ERROR("[ES Trace Exporter] Connection to elasticsearch failed") cv_.notify_all(); break; case http_client::SessionState::SendFailed: - OTEL_ERROR("[ES Trace Exporter] Request failed to be sent to elasticsearch\n") + OTEL_INTERNAL_LOG_ERROR("[ES Trace Exporter] Request failed to be sent to elasticsearch") cv_.notify_all(); break; case http_client::SessionState::TimedOut: - OTEL_ERROR("[ES Trace Exporter] Request to elasticsearch timed out\n") + OTEL_INTERNAL_LOG_ERROR("[ES Trace Exporter] Request to elasticsearch timed out") cv_.notify_all(); break; case http_client::SessionState::NetworkError: - OTEL_ERROR("[ES Trace Exporter] Network error to elasticsearch\n") + OTEL_INTERNAL_LOG_ERROR("[ES Trace Exporter] Network error to elasticsearch") cv_.notify_all(); break; } @@ -131,7 +131,7 @@ sdk::common::ExportResult ElasticsearchLogExporter::Export( if (is_shutdown_) { - OTEL_ERROR("[ES Trace Exporter] Export failed, exporter is shutdown\n") + OTEL_INTERNAL_LOG_ERROR("[ES Trace Exporter] Export failed, exporter is shutdown") return sdk::common::ExportResult::kFailure; } @@ -168,10 +168,9 @@ sdk::common::ExportResult ElasticsearchLogExporter::Export( // Wait for the response to be received if (options_.console_debug_) { - std::stringstream ss; - ss << "[ES Trace Exporter] DEBUG: waiting for response from Elasticsearch (timeout = " - << options_.response_timeout_ << " seconds)" << std::endl; - OTEL_ERROR(ss.str()) + OTEL_INTERNAL_LOG_DEBUG( + "[ES Trace Exporter] DEBUG: waiting for response from Elasticsearch (timeout = " + << options_.response_timeout_ << " seconds)"); } bool write_successful = handler->waitForResponse(); @@ -189,12 +188,7 @@ sdk::common::ExportResult ElasticsearchLogExporter::Export( std::string responseBody = handler->GetResponseBody(); if (responseBody.find("\"failed\" : 0") == std::string::npos) { - std::stringstream ss; - ss << "[ES Trace Exporter] DEBUG: Logs were not written to Elasticsearch correctly, response " - "body:" - << std::endl; - ss << responseBody << std::endl; - OTEL_ERROR(ss.str()) + OTEL_INTERNAL_LOG_ERROR("[ES Trace Exporter] DEBUG: Logs were not written to Elasticsearch correctly, response body: "<< responseBody ; // TODO: Retry logic return sdk::common::ExportResult::kFailure; } diff --git a/exporters/jaeger/src/TUDPTransport.cc b/exporters/jaeger/src/TUDPTransport.cc index b6cf777365..c72881df91 100644 --- a/exporters/jaeger/src/TUDPTransport.cc +++ b/exporters/jaeger/src/TUDPTransport.cc @@ -4,7 +4,7 @@ #include // std::stringstream #include "TUDPTransport.h" -#include "opentelemetry/sdk/common/global_error_handler.h" +#include "opentelemetry/sdk_config.h" OPENTELEMETRY_BEGIN_NAMESPACE namespace exporter @@ -54,9 +54,7 @@ void TUDPTransport::open() if (error) { - std::stringstream ss; - ss << "Jaeger Exporter: getaddrinfo failed with error: " << error; - OTEL_ERROR(ss.str()) + OTEL_INTERNAL_LOG_ERROR("Jaeger Exporter: getaddrinfo failed with error: " << error); return; } diff --git a/exporters/jaeger/src/udp_transport.cc b/exporters/jaeger/src/udp_transport.cc index 03b7ef96c0..50ef1057fd 100644 --- a/exporters/jaeger/src/udp_transport.cc +++ b/exporters/jaeger/src/udp_transport.cc @@ -3,7 +3,7 @@ #include // std::stringstream -#include "opentelemetry/sdk/common/global_error_handler.h" +#include "opentelemetry/sdk_config.h" #include "udp_transport.h" OPENTELEMETRY_BEGIN_NAMESPACE @@ -39,8 +39,7 @@ void UDPTransport::InitSocket() int err = WSAStartup(wVersionRequested, &wsaData); if (err != 0) { - ss << "Jaeger Exporter: WSAStartup failed with error: " << error; - OTEL_ERROR(ss.str()) + OTEL_INTERNAL_LOG_ERROR("Jaeger Exporter: WSAStartup failed with error: " << error); return; } diff --git a/exporters/otlp/src/otlp_grpc_exporter.cc b/exporters/otlp/src/otlp_grpc_exporter.cc index c96c36ded8..5d1e6e310c 100644 --- a/exporters/otlp/src/otlp_grpc_exporter.cc +++ b/exporters/otlp/src/otlp_grpc_exporter.cc @@ -3,7 +3,7 @@ #include "opentelemetry/exporters/otlp/otlp_grpc_exporter.h" #include "opentelemetry/exporters/otlp/otlp_recordable.h" -#include "opentelemetry/sdk/common/global_error_handler.h" +#include "opentelemetry/sdk_config.h" #include #include @@ -112,9 +112,8 @@ sdk::common::ExportResult OtlpGrpcExporter::Export( if (!status.ok()) { - std::stringstream ss; - ss << "[OTLP Exporter] Export() failed: " << status.error_message() << "\n"; - OTEL_ERROR(ss.str()) + + OTEL_INTERNAL_LOG_ERROR("[OTLP Exporter] Export() failed: " << status.error_message()) return sdk::common::ExportResult::kFailure; } return sdk::common::ExportResult::kSuccess; diff --git a/exporters/otlp/src/otlp_http_exporter.cc b/exporters/otlp/src/otlp_http_exporter.cc index e438849948..80794f464f 100644 --- a/exporters/otlp/src/otlp_http_exporter.cc +++ b/exporters/otlp/src/otlp_http_exporter.cc @@ -76,7 +76,7 @@ class ResponseHandler : public http_client::EventHandler return true; }); ss << "Body:" << std::endl << body_ << std::endl; - OTEL_ERROR(ss.str()) + OTEL_INTERNAL_LOG_LEVEL_DEBUG(ss.str()) } // Set the response_received_ flag to true and notify any threads waiting on this result @@ -85,7 +85,7 @@ class ResponseHandler : public http_client::EventHandler cv_.notify_all(); } - /** + /**resource * A method the user calls to block their thread until the response is received. The longest * duration is the timeout of the request, set by SetTimeoutMs() */ @@ -114,93 +114,95 @@ class ResponseHandler : public http_client::EventHandler switch (state) { case http_client::SessionState::CreateFailed: - OTEL_ERROR("[OTLP HTTP Exporter] Session state: session create failed\n"); + OTEL_INTERNAL_LOG_ERROR("[OTLP HTTP Exporter] Session state: session create failed"); cv_.notify_all(); break; case http_client::SessionState::Created: if (console_debug_) { - OTEL_ERROR("[OTLP HTTP Exporter] DEBUG: Session state: session created\n") + OTEL_INTERNAL_LOG_ERROR("[OTLP HTTP Exporter] DEBUG: Session state: session created") } break; case http_client::SessionState::Destroyed: if (console_debug_) { - OTEL_ERROR("[OTLP HTTP Exporter] DEBUG: Session state: session destroyed\n") + OTEL_INTERNAL_LOG_ERROR("[OTLP HTTP Exporter] DEBUG: Session state: session destroyed") } break; case http_client::SessionState::Connecting: if (console_debug_) { - OTEL_ERROR("[OTLP HTTP Exporter] DEBUG: Session state: connecting to peer\n") + OTEL_INTERNAL_LOG_ERROR("[OTLP HTTP Exporter] DEBUG: Session state: connecting to peer") } break; case http_client::SessionState::ConnectFailed: - OTEL_ERROR("[OTLP HTTP Exporter] Session state: connection failed\n") + OTEL_INTERNAL_LOG_ERROR("[OTLP HTTP Exporter] Session state: connection failed") cv_.notify_all(); break; case http_client::SessionState::Connected: if (console_debug_) { - OTEL_ERROR("[OTLP HTTP Exporter] DEBUG Session state: connected\n") + OTEL_INTERNAL_LOG_DEBUG("[OTLP HTTP Exporter] Session state: connected") } break; case http_client::SessionState::Sending: if (console_debug_) { - OTEL_ERROR("[OTLP HTTP Exporter] DEBUG: Session state: sending request\n") + OTEL_INTERNAL_LOG_DEBUG("[OTLP HTTP Exporter] DEBUG: Session state: sending request") } break; case http_client::SessionState::SendFailed: - OTEL_ERROR("[OTLP HTTP Exporter] Session state: request send failed\n") + OTEL_INTERNAL_LOG_ERROR("[OTLP HTTP Exporter] Session state: request send failed\n") cv_.notify_all(); break; case http_client::SessionState::Response: if (console_debug_) { - OTEL_ERROR("[OTLP HTTP Exporter] DEBUG:Session state: response received\n") + OTEL_INTERNAL_LOG_DEBUG("[OTLP HTTP Exporter] DEBUG:Session state: response received\n") } break; case http_client::SessionState::SSLHandshakeFailed: - OTEL_ERROR("[OTLP HTTP Exporter] Session state: SSL handshake failed\n") + OTEL_INTERNAL_LOG_ERROR("[OTLP HTTP Exporter] Session state: SSL handshake failed\n") cv_.notify_all(); break; case http_client::SessionState::TimedOut: - OTEL_ERROR("[OTLP HTTP Exporter] Session state: request time out\n") + OTEL_INTERNAL_LOG_ERROR("[OTLP HTTP Exporter] Session state: request time out\n") cv_.notify_all(); break; case http_client::SessionState::NetworkError: - OTEL_ERROR("[OTLP HTTP Exporter] Session state: network error\n") + OTEL_INTERNAL_LOG_ERROR("[OTLP HTTP Exporter] Session state: network error\n") cv_.notify_all(); break; case http_client::SessionState::ReadError: if (console_debug_) { - OTEL_ERROR("[OTLP HTTP Exporter] DEBUG: Session state: error reading responsen") + OTEL_INTERNAL_LOG_DEBUG( + "[OTLP HTTP Exporter] DEBUG: Session state: error reading responsen") } break; case http_client::SessionState::WriteError: if (console_debug_) { - OTEL_ERROR("[OTLP HTTP Exporter] DEBUG:Session state: error writing request\n") + OTEL_INTERNAL_LOG_DEBUG( + "[OTLP HTTP Exporter] DEBUG:Session state: error writing request\n") } break; case http_client::SessionState::Cancelled: - OTEL_ERROR("[OTLP HTTP Exporter] Session state: (manually) cancelled\n") + OTEL_INTERNAL_LOG_ERROR("[OTLP HTTP Exporter] Session state: (manually) cancelled\n") cv_.notify_all(); break; @@ -622,7 +624,7 @@ sdk::common::ExportResult OtlpHttpExporter::Export( { std::stringstream ss; ss << "[OTLP HTTP Exporter] DEBUG: Request body(Json):\n" << post_body_json << std::endl; - OTEL_ERROR(ss.str()) + OTEL_INTERNAL_LOG_DEBUG(ss.str()) } body_vec.assign(post_body_json.begin(), post_body_json.end()); content_type = kHttpJsonContentType; @@ -644,10 +646,9 @@ sdk::common::ExportResult OtlpHttpExporter::Export( // Wait for the response to be received if (options_.console_debug) { - std::stringstream ss; - ss << "[OTLP HTTP Exporter] DEBUG: Waiting for response from " << options_.url - << " (timeout = " << options_.timeout.count() << " milliseconds)" << std::endl; - OTEL_ERROR(ss.str()) + OTEL_INTERNAL_LOG_ERROR("[OTLP HTTP Exporter] DEBUG: Waiting for response from " + << options_.url << " (timeout = " << options_.timeout.count() + << " milliseconds)") } bool write_successful = handler->waitForResponse(); diff --git a/exporters/zipkin/src/zipkin_exporter.cc b/exporters/zipkin/src/zipkin_exporter.cc index 05dd33d37d..2ae9fc651e 100644 --- a/exporters/zipkin/src/zipkin_exporter.cc +++ b/exporters/zipkin/src/zipkin_exporter.cc @@ -5,7 +5,7 @@ #include "opentelemetry/exporters/zipkin/recordable.h" #include "opentelemetry/ext/http/client/http_client_factory.h" #include "opentelemetry/ext/http/common/url_parser.h" -#include "opentelemetry/sdk/common/global_error_handler.h" +#include "opentelemetry/sdk_config.h" OPENTELEMETRY_BEGIN_NAMESPACE namespace exporter @@ -72,7 +72,7 @@ sdk::common::ExportResult ZipkinExporter::Export( { if (result.GetSessionState() == http_client::SessionState::ConnectFailed) { - OTEL_ERROR("ZIPKIN EXPORTER] Zipkin Exporter: Connection failed") + OTEL_INTERNAL_LOG_ERROR("ZIPKIN EXPORTER] Zipkin Exporter: Connection failed") } return sdk::common::ExportResult::kFailure; } diff --git a/sdk/include/opentelemetry/sdk/common/global_error_handler.h b/sdk/include/opentelemetry/sdk/common/global_error_handler.h index 923b054aa8..0e56ebf5e5 100644 --- a/sdk/include/opentelemetry/sdk/common/global_error_handler.h +++ b/sdk/include/opentelemetry/sdk/common/global_error_handler.h @@ -6,17 +6,20 @@ #include #include #include +#include #include "opentelemetry/common/spin_lock_mutex.h" #include "opentelemetry/nostd/shared_ptr.h" -#include "opentelemetry/nostd/string_view.h" +#include "opentelemetry/sdk/common/attribute_utils.h" #include "opentelemetry/version.h" -#ifndef IGNORE_OTEL_ERROR_LOGGER -# define OTEL_ERROR(message) \ - opentelemetry::sdk::common::Error::GlobalErrorHandler::GetErrorHandler()->Handle(message); -#else -# define OTEL_ERROR(message) +#define OTEL_INTERNAL_LOG_LEVEL_ERROR 0 +#define OTEL_INTERNAL_LOG_LEVEL_WARN 1 +#define OTEL_INTERNAL_LOG_LEVEL_INFO 2 +#define OTEL_INTERNAL_LOG_LEVEL_DEBUG 3 // to be disabled in release + +#ifndef OTEL_INTERNAL_LOG_LEVEL +# define OTEL_INTERNAL_LOG_LEVEL OTEL_INTERNAL_LOG_LEVEL_DEBUG // ERROR, WARN , INFO AND DEBUG #endif OPENTELEMETRY_BEGIN_NAMESPACE @@ -24,65 +27,112 @@ namespace sdk { namespace common { -namespace Error +namespace internal_log +{ + +enum class LogLevel +{ + Error = 0, + Warning, + Info, + Debug +}; + +inline std::string LevelToString(LogLevel level) { + switch (level) + { + case LogLevel::Error: + return "Error"; + case LogLevel::Warning: + return "Warning"; + case LogLevel::Info: + return "Info"; + case LogLevel::Debug: + return "Debug"; + } + return {}; +} -class ErrorHandler +class LogHandler { public: - virtual void Handle(nostd::string_view error) noexcept = 0; + virtual void Handle(LogLevel level, + const char *file, + int line, + const char *msg, + const sdk::common::AttributeMap &attributes) noexcept = 0; }; -class DefaultErrorHandler : public ErrorHandler +class DefaultLogHandler : public LogHandler { public: - void Handle(nostd::string_view error) noexcept override + void Handle(LogLevel level, + const char *file, + int line, + const char *msg, + const sdk::common::AttributeMap &attributes) noexcept override { - // std::cout::operator<<() should be thread safe without any chaining calls. - std::cerr << error; + std::stringstream output_s; + output_s << "[" << LevelToString(level) << "] "; + if (file != nullptr) + { + output_s << "File: " << file << ":" << line; + } + if (msg != nullptr) + { + output_s << msg; + } + output_s << std::endl; + // TBD - print attributes + std::cout << output_s.str(); // thread safe. } }; -class NoopErrorHandler : public ErrorHandler +class NoopLogHandler : public LogHandler { public: - void Handle(nostd::string_view error) noexcept override + void Handle(LogLevel level, + const char *file, + int line, + const char *msg, + const sdk::common::AttributeMap &error_attributes) noexcept override { - // ignore the error message + // ignore the log message } }; /** - * Stores the singleton global ErrorHandler. + * Stores the singleton global LogHandler. */ -class GlobalErrorHandler +class GlobalLogHandler { public: /** - * Returns the singleton ErrorHandler. + * Returns the singleton LogHandler. * - * By default, a default ErrorHandler is returned. This will never return a - * nullptr ErrorHandler. + * By default, a default LogHandler is returned. This will never return a + * nullptr LogHander. */ - static nostd::shared_ptr GetErrorHandler() noexcept + static nostd::shared_ptr GetLogHandler() noexcept { std::lock_guard guard(GetLock()); - return nostd::shared_ptr(GetHandler()); + return nostd::shared_ptr(GetHandler()); } /** - * Changes the singleton ErrorHandler. + * Changes the singleton LogHandler. */ - static void SetErrorHandler(nostd::shared_ptr eh) noexcept + static void SetLogHandler(nostd::shared_ptr eh) noexcept { std::lock_guard guard(GetLock()); GetHandler() = eh; } private: - static nostd::shared_ptr &GetHandler() noexcept + static nostd::shared_ptr &GetHandler() noexcept { - static nostd::shared_ptr handler(new DefaultErrorHandler); + static nostd::shared_ptr handler(new DefaultLogHandler); return handler; } @@ -93,7 +143,79 @@ class GlobalErrorHandler } }; -} // namespace Error +} // namespace internal_log } // namespace common } // namespace sdk -OPENTELEMETRY_END_NAMESPACE \ No newline at end of file +OPENTELEMETRY_END_NAMESPACE + +#define OTEL_INTERNAL_LOG_DISPATCH(level, message, attributes) \ + do \ + { \ + using namespace opentelemetry::sdk::common::internal_log; \ + std::stringstream tmp_stream; \ + tmp_stream << message; \ + GlobalLogHandler::GetLogHandler()->Handle(level, __FILE__, __LINE__, tmp_stream.str().c_str(), \ + attributes); \ + } while (0) + +#define OTEL_INTERNAL_LOG_GET_3RD_ARG(arg1, arg2, arg3, ...) arg3 + +#if OTEL_INTERNAL_LOG_LEVEL >= OTEL_INTERNAL_LOG_LEVEL_ERROR +# define OTEL_INTERNAL_LOG_ERROR_1_ARGS(message) \ + OTEL_INTERNAL_LOG_DISPATCH(opentelemetry::sdk::common::internal_log::LogLevel::Error, message, \ + {}) +# define OTEL_INTERNAL_LOG_ERROR_2_ARGS(message, attributes) \ + OTEL_INTERNAL_LOG_DISPATCH(opentelemetry::sdk::common::internal_log::LogLevel::Error, message, \ + attributes) +# define OTEL_INTERNAL_LOG_ERROR_MACRO(...) \ + OTEL_INTERNAL_LOG_GET_3RD_ARG(__VA_ARGS__, OTEL_INTERNAL_LOG_ERROR_2_ARGS, \ + OTEL_INTERNAL_LOG_ERROR_1_ARGS) +# define OTEL_INTERNAL_LOG_ERROR(...) OTEL_INTERNAL_LOG_ERROR_MACRO(__VA_ARGS__)(__VA_ARGS__) +#else +# define OTEL_INTERNAL_LOG_ERROR(...) +#endif + +#if OTEL_INTERNAL_LOG_LEVEL >= OTEL_INTERNAL_LOG_LEVEL_WARN +# define OTEL_INTERNAL_LOG_WARN_1_ARGS(message) \ + OTEL_INTERNAL_LOG_DISPATCH(opentelemetry::sdk::common::internal_log::LogLevel::Warn, message, \ + {}) +# define OTEL_INTERNAL_LOG_WARN_2_ARGS(message, attributes) \ + OTEL_INTERNAL_LOG_DISPATCH(opentelemetry::sdk::common::internal_log::LogLevel::Warn, message, \ + attributes) +# define OTEL_INTERNAL_LOG_WARN_MACRO(...) \ + OTEL_INTERNAL_LOG_GET_3RD_ARG(__VA_ARGS__, OTEL_INTERNAL_LOG_WARN_2_ARGS, \ + OTEL_INTERNAL_LOG_WARN_1_ARGS) +# define OTEL_INTERNAL_LOG_WARN(...) OTEL_INTERNAL_LOG_WARN_MACRO(__VA_ARGS__)(__VA_ARGS__) +#else +# define OTEL_INTERNAL_LOG_ERROR(...) +#endif + +#if OTEL_INTERNAL_LOG_LEVEL >= OTEL_INTERNAL_LOG_LEVEL_DEBUG +# define OTEL_INTERNAL_LOG_DEBUG_1_ARGS(message) \ + OTEL_INTERNAL_LOG_DISPATCH(opentelemetry::sdk::common::internal_log::LogLevel::Debug, message, \ + {}) +# define OTEL_INTERNAL_LOG_DEBUG_2_ARGS(message, attributes) \ + OTEL_INTERNAL_LOG_DISPATCH(opentelemetry::sdk::common::internal_logg::LogLevel::Debug, \ + message, attributes) +# define OTEL_INTERNAL_LOG_DEBUG_MACRO(...) \ + OTEL_INTERNAL_LOG_GET_3RD_ARG(__VA_ARGS__, OTEL_INTERNAL_LOG_DEBUG_2_ARGS, \ + OTEL_INTERNAL_LOG_DEBUG_1_ARGS) +# define OTEL_INTERNAL_LOG_DEBUG(...) OTEL_INTERNAL_LOG_DEBUG_MACRO(__VA_ARGS__)(__VA_ARGS__) +#else +# define OTEL_INTERNAL_LOG_DEBUG(...) +#endif + +#if OTEL_INTERNAL_LOG_LEVEL >= OTEL_INTERNAL_LOG_LEVEL_INFO +# define OTEL_INTERNAL_LOG_INFO_1_ARGS(message) \ + OTEL_INTERNAL_LOG_DISPATCH(opentelemetry::sdk::common::internal_logger::LogLevel::Info, \ + message, {}) +# define OTEL_INTERNAL_LOG_INFO_2_ARGS(message, attributes) \ + OTEL_INTERNAL_LOG_DISPATCH(opentelemetry::sdk::common::internal_logger::LogLevel::Info, \ + message, attributes) +# define OTEL_INTERNAL_LOG_INFO_MACRO(...) \ + OTEL_INTERNAL_LOG_GET_3RD_ARG(__VA_ARGS__, OTEL_INTERNAL_LOG_ERROR_2_ARGS, \ + OTEL_INTERNAL_LOG_ERROR_1_ARGS) +# define OTEL_INTERNAL_LOG_INFO(...) OTEL_INTERNAL_LOG_INFO_MACRO(__VA_ARGS__)(__VA_ARGS__) +#else +# define OTEL_INTERNAL_LOG_INFO(...) +#endif diff --git a/sdk/include/opentelemetry/sdk_config.h b/sdk/include/opentelemetry/sdk_config.h new file mode 100644 index 0000000000..e1c8bc91f4 --- /dev/null +++ b/sdk/include/opentelemetry/sdk_config.h @@ -0,0 +1,7 @@ +// Copyright The OpenTelemetry Authors +// SPDX-License-Identifier: Apache-2.0 + +#pragma once + +#include "opentelemetry/config.h" +#include "opentelemetry/sdk/common/global_error_handler.h" \ No newline at end of file diff --git a/sdk/src/logs/logger.cc b/sdk/src/logs/logger.cc index 3b7401a909..8f70d57050 100644 --- a/sdk/src/logs/logger.cc +++ b/sdk/src/logs/logger.cc @@ -5,6 +5,7 @@ # include "opentelemetry/sdk/logs/logger.h" # include "opentelemetry/sdk/common/global_error_handler.h" # include "opentelemetry/sdk/logs/log_record.h" +# include "opentelemetry/sdk_config.h" # include "opentelemetry/trace/provider.h" OPENTELEMETRY_BEGIN_NAMESPACE @@ -49,7 +50,7 @@ void Logger::Log(opentelemetry::logs::Severity severity, auto recordable = processor->MakeRecordable(); if (recordable == nullptr) { - OTEL_ERROR("[LOGGER] Recordable creation failed") + OTEL_INTERNAL_LOG_ERROR("[LOGGER] Recordable creation failed") return; } diff --git a/sdk/src/trace/tracer_provider.cc b/sdk/src/trace/tracer_provider.cc index 73d4ced727..ac8cbb2e7b 100644 --- a/sdk/src/trace/tracer_provider.cc +++ b/sdk/src/trace/tracer_provider.cc @@ -2,7 +2,7 @@ // SPDX-License-Identifier: Apache-2.0 #include "opentelemetry/sdk/trace/tracer_provider.h" -#include "opentelemetry/sdk/common/global_error_handler.h" +#include "opentelemetry/sdk_config.h" OPENTELEMETRY_BEGIN_NAMESPACE namespace sdk @@ -43,7 +43,7 @@ nostd::shared_ptr TracerProvider::GetTracer( } if (library_name == "") { - OTEL_ERROR("[TracerProvider::GetTracer] Library name is empty.") + OTEL_INTERNAL_LOG_ERROR("[TracerProvider::GetTracer] Library name is empty."); } const std::lock_guard guard(lock_); From 9398bcac3dbc175be05e277010d3a76f8b1230c6 Mon Sep 17 00:00:00 2001 From: Lalit Kumar Bhasin Date: Mon, 28 Jun 2021 14:46:39 +0530 Subject: [PATCH 09/15] fix logger --- sdk/src/logs/logger.cc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sdk/src/logs/logger.cc b/sdk/src/logs/logger.cc index 8f70d57050..ba5f1f5b1e 100644 --- a/sdk/src/logs/logger.cc +++ b/sdk/src/logs/logger.cc @@ -50,7 +50,7 @@ void Logger::Log(opentelemetry::logs::Severity severity, auto recordable = processor->MakeRecordable(); if (recordable == nullptr) { - OTEL_INTERNAL_LOG_ERROR("[LOGGER] Recordable creation failed") + OTEL_INTERNAL_LOG_ERROR("[LOGGER] Recordable creation failed"); return; } From 560fb05eb671564ec89edf08406f607a35e6cf30 Mon Sep 17 00:00:00 2001 From: Lalit Kumar Bhasin Date: Mon, 28 Jun 2021 15:03:02 +0530 Subject: [PATCH 10/15] fix build --- exporters/otlp/src/otlp_http_exporter.cc | 36 +++++++++++------------- 1 file changed, 17 insertions(+), 19 deletions(-) diff --git a/exporters/otlp/src/otlp_http_exporter.cc b/exporters/otlp/src/otlp_http_exporter.cc index 80794f464f..156b4550c9 100644 --- a/exporters/otlp/src/otlp_http_exporter.cc +++ b/exporters/otlp/src/otlp_http_exporter.cc @@ -76,7 +76,7 @@ class ResponseHandler : public http_client::EventHandler return true; }); ss << "Body:" << std::endl << body_ << std::endl; - OTEL_INTERNAL_LOG_LEVEL_DEBUG(ss.str()) + OTEL_INTERNAL_LOG_LEVEL_DEBUG(ss.str()); } // Set the response_received_ flag to true and notify any threads waiting on this result @@ -121,67 +121,67 @@ class ResponseHandler : public http_client::EventHandler case http_client::SessionState::Created: if (console_debug_) { - OTEL_INTERNAL_LOG_ERROR("[OTLP HTTP Exporter] DEBUG: Session state: session created") + OTEL_INTERNAL_LOG_ERROR("[OTLP HTTP Exporter] DEBUG: Session state: session created"); } break; case http_client::SessionState::Destroyed: if (console_debug_) { - OTEL_INTERNAL_LOG_ERROR("[OTLP HTTP Exporter] DEBUG: Session state: session destroyed") + OTEL_INTERNAL_LOG_ERROR("[OTLP HTTP Exporter] DEBUG: Session state: session destroyed"); } break; case http_client::SessionState::Connecting: if (console_debug_) { - OTEL_INTERNAL_LOG_ERROR("[OTLP HTTP Exporter] DEBUG: Session state: connecting to peer") + OTEL_INTERNAL_LOG_ERROR("[OTLP HTTP Exporter] DEBUG: Session state: connecting to peer"); } break; case http_client::SessionState::ConnectFailed: - OTEL_INTERNAL_LOG_ERROR("[OTLP HTTP Exporter] Session state: connection failed") + OTEL_INTERNAL_LOG_ERROR("[OTLP HTTP Exporter] Session state: connection failed"); cv_.notify_all(); break; case http_client::SessionState::Connected: if (console_debug_) { - OTEL_INTERNAL_LOG_DEBUG("[OTLP HTTP Exporter] Session state: connected") + OTEL_INTERNAL_LOG_DEBUG("[OTLP HTTP Exporter] Session state: connected"); } break; case http_client::SessionState::Sending: if (console_debug_) { - OTEL_INTERNAL_LOG_DEBUG("[OTLP HTTP Exporter] DEBUG: Session state: sending request") + OTEL_INTERNAL_LOG_DEBUG("[OTLP HTTP Exporter] DEBUG: Session state: sending request"); } break; case http_client::SessionState::SendFailed: - OTEL_INTERNAL_LOG_ERROR("[OTLP HTTP Exporter] Session state: request send failed\n") + OTEL_INTERNAL_LOG_ERROR("[OTLP HTTP Exporter] Session state: request send failed\n"); cv_.notify_all(); break; case http_client::SessionState::Response: if (console_debug_) { - OTEL_INTERNAL_LOG_DEBUG("[OTLP HTTP Exporter] DEBUG:Session state: response received\n") + OTEL_INTERNAL_LOG_DEBUG("[OTLP HTTP Exporter] DEBUG:Session state: response received\n"); } break; case http_client::SessionState::SSLHandshakeFailed: - OTEL_INTERNAL_LOG_ERROR("[OTLP HTTP Exporter] Session state: SSL handshake failed\n") + OTEL_INTERNAL_LOG_ERROR("[OTLP HTTP Exporter] Session state: SSL handshake failed\n"); cv_.notify_all(); break; case http_client::SessionState::TimedOut: - OTEL_INTERNAL_LOG_ERROR("[OTLP HTTP Exporter] Session state: request time out\n") + OTEL_INTERNAL_LOG_ERROR("[OTLP HTTP Exporter] Session state: request time out\n"); cv_.notify_all(); break; case http_client::SessionState::NetworkError: - OTEL_INTERNAL_LOG_ERROR("[OTLP HTTP Exporter] Session state: network error\n") + OTEL_INTERNAL_LOG_ERROR("[OTLP HTTP Exporter] Session state: network error\n"); cv_.notify_all(); break; @@ -189,7 +189,7 @@ class ResponseHandler : public http_client::EventHandler if (console_debug_) { OTEL_INTERNAL_LOG_DEBUG( - "[OTLP HTTP Exporter] DEBUG: Session state: error reading responsen") + "[OTLP HTTP Exporter] DEBUG: Session state: error reading responsen"); } break; @@ -197,12 +197,12 @@ class ResponseHandler : public http_client::EventHandler if (console_debug_) { OTEL_INTERNAL_LOG_DEBUG( - "[OTLP HTTP Exporter] DEBUG:Session state: error writing request\n") + "[OTLP HTTP Exporter] DEBUG:Session state: error writing request\n"); } break; case http_client::SessionState::Cancelled: - OTEL_INTERNAL_LOG_ERROR("[OTLP HTTP Exporter] Session state: (manually) cancelled\n") + OTEL_INTERNAL_LOG_ERROR("[OTLP HTTP Exporter] Session state: (manually) cancelled\n"); cv_.notify_all(); break; @@ -622,9 +622,7 @@ sdk::common::ExportResult OtlpHttpExporter::Export( json_request.dump(-1, ' ', false, nlohmann::detail::error_handler_t::replace); if (options_.console_debug) { - std::stringstream ss; - ss << "[OTLP HTTP Exporter] DEBUG: Request body(Json):\n" << post_body_json << std::endl; - OTEL_INTERNAL_LOG_DEBUG(ss.str()) + OTEL_INTERNAL_LOG_DEBUG("[OTLP HTTP Exporter] DEBUG: Request body(Json)" << post_body_json ); } body_vec.assign(post_body_json.begin(), post_body_json.end()); content_type = kHttpJsonContentType; @@ -648,7 +646,7 @@ sdk::common::ExportResult OtlpHttpExporter::Export( { OTEL_INTERNAL_LOG_ERROR("[OTLP HTTP Exporter] DEBUG: Waiting for response from " << options_.url << " (timeout = " << options_.timeout.count() - << " milliseconds)") + << " milliseconds)"); } bool write_successful = handler->waitForResponse(); From 2c981fcf0e85bd0846f5b778b476ca366842f8d9 Mon Sep 17 00:00:00 2001 From: Lalit Kumar Bhasin Date: Mon, 28 Jun 2021 19:20:25 +0530 Subject: [PATCH 11/15] more build fix --- .../elasticsearch/src/es_log_exporter.cc | 16 ++++--- exporters/otlp/src/otlp_grpc_exporter.cc | 2 +- exporters/otlp/src/otlp_http_exporter.cc | 44 +++++++++---------- exporters/zipkin/src/zipkin_exporter.cc | 2 +- 4 files changed, 32 insertions(+), 32 deletions(-) diff --git a/exporters/elasticsearch/src/es_log_exporter.cc b/exporters/elasticsearch/src/es_log_exporter.cc index 180323d8bf..777bfb9426 100644 --- a/exporters/elasticsearch/src/es_log_exporter.cc +++ b/exporters/elasticsearch/src/es_log_exporter.cc @@ -77,19 +77,19 @@ class ResponseHandler : public http_client::EventHandler switch (state) { case http_client::SessionState::ConnectFailed: - OTEL_INTERNAL_LOG_ERROR("[ES Trace Exporter] Connection to elasticsearch failed") + OTEL_INTERNAL_LOG_ERROR("[ES Trace Exporter] Connection to elasticsearch failed"); cv_.notify_all(); break; case http_client::SessionState::SendFailed: - OTEL_INTERNAL_LOG_ERROR("[ES Trace Exporter] Request failed to be sent to elasticsearch") + OTEL_INTERNAL_LOG_ERROR("[ES Trace Exporter] Request failed to be sent to elasticsearch"); cv_.notify_all(); break; case http_client::SessionState::TimedOut: - OTEL_INTERNAL_LOG_ERROR("[ES Trace Exporter] Request to elasticsearch timed out") + OTEL_INTERNAL_LOG_ERROR("[ES Trace Exporter] Request to elasticsearch timed out"); cv_.notify_all(); break; case http_client::SessionState::NetworkError: - OTEL_INTERNAL_LOG_ERROR("[ES Trace Exporter] Network error to elasticsearch") + OTEL_INTERNAL_LOG_ERROR("[ES Trace Exporter] Network error to elasticsearch"); cv_.notify_all(); break; } @@ -131,7 +131,7 @@ sdk::common::ExportResult ElasticsearchLogExporter::Export( if (is_shutdown_) { - OTEL_INTERNAL_LOG_ERROR("[ES Trace Exporter] Export failed, exporter is shutdown") + OTEL_INTERNAL_LOG_ERROR("[ES Trace Exporter] Export failed, exporter is shutdown"); return sdk::common::ExportResult::kFailure; } @@ -169,7 +169,7 @@ sdk::common::ExportResult ElasticsearchLogExporter::Export( if (options_.console_debug_) { OTEL_INTERNAL_LOG_DEBUG( - "[ES Trace Exporter] DEBUG: waiting for response from Elasticsearch (timeout = " + "[ES Trace Exporter] waiting for response from Elasticsearch (timeout = " << options_.response_timeout_ << " seconds)"); } bool write_successful = handler->waitForResponse(); @@ -188,7 +188,9 @@ sdk::common::ExportResult ElasticsearchLogExporter::Export( std::string responseBody = handler->GetResponseBody(); if (responseBody.find("\"failed\" : 0") == std::string::npos) { - OTEL_INTERNAL_LOG_ERROR("[ES Trace Exporter] DEBUG: Logs were not written to Elasticsearch correctly, response body: "<< responseBody ; + OTEL_INTERNAL_LOG_ERROR( + "[ES Trace Exporter] Logs were not written to Elasticsearch correctly, response body: " + << responseBody); // TODO: Retry logic return sdk::common::ExportResult::kFailure; } diff --git a/exporters/otlp/src/otlp_grpc_exporter.cc b/exporters/otlp/src/otlp_grpc_exporter.cc index 5d1e6e310c..4dd921a4f8 100644 --- a/exporters/otlp/src/otlp_grpc_exporter.cc +++ b/exporters/otlp/src/otlp_grpc_exporter.cc @@ -113,7 +113,7 @@ sdk::common::ExportResult OtlpGrpcExporter::Export( if (!status.ok()) { - OTEL_INTERNAL_LOG_ERROR("[OTLP Exporter] Export() failed: " << status.error_message()) + OTEL_INTERNAL_LOG_ERROR("[OTLP Exporter] Export() failed: " << status.error_message()); return sdk::common::ExportResult::kFailure; } return sdk::common::ExportResult::kSuccess; diff --git a/exporters/otlp/src/otlp_http_exporter.cc b/exporters/otlp/src/otlp_http_exporter.cc index 156b4550c9..cec530cfbf 100644 --- a/exporters/otlp/src/otlp_http_exporter.cc +++ b/exporters/otlp/src/otlp_http_exporter.cc @@ -68,15 +68,14 @@ class ResponseHandler : public http_client::EventHandler if (console_debug_) { std::stringstream ss; - ss << "[OTLP HTTP Exporter] DEBUG: Status:" << response.GetStatusCode() << std::endl - << "Header:" << std::endl; + ss << "[OTLP HTTP Exporter] Status:" << response.GetStatusCode() << "Header:"; response.ForEachHeader([&ss](opentelemetry::nostd::string_view header_name, opentelemetry::nostd::string_view header_value) { - ss << "\t" << header_name.data() << " : " << header_value.data() << std::endl; + ss << "\t" << header_name.data() << " : " << header_value.data() << ","; return true; }); - ss << "Body:" << std::endl << body_ << std::endl; - OTEL_INTERNAL_LOG_LEVEL_DEBUG(ss.str()); + ss << "Body:" << body_; + OTEL_INTERNAL_LOG_DEBUG(ss.str()); } // Set the response_received_ flag to true and notify any threads waiting on this result @@ -121,21 +120,21 @@ class ResponseHandler : public http_client::EventHandler case http_client::SessionState::Created: if (console_debug_) { - OTEL_INTERNAL_LOG_ERROR("[OTLP HTTP Exporter] DEBUG: Session state: session created"); + OTEL_INTERNAL_LOG_DEBUG("[OTLP HTTP Exporter] Session state: session created"); } break; case http_client::SessionState::Destroyed: if (console_debug_) { - OTEL_INTERNAL_LOG_ERROR("[OTLP HTTP Exporter] DEBUG: Session state: session destroyed"); + OTEL_INTERNAL_LOG_DEBUG("[OTLP HTTP Exporter] Session state: session destroyed"); } break; case http_client::SessionState::Connecting: if (console_debug_) { - OTEL_INTERNAL_LOG_ERROR("[OTLP HTTP Exporter] DEBUG: Session state: connecting to peer"); + OTEL_INTERNAL_LOG_DEBUG("[OTLP HTTP Exporter] Session state: connecting to peer"); } break; @@ -154,42 +153,41 @@ class ResponseHandler : public http_client::EventHandler case http_client::SessionState::Sending: if (console_debug_) { - OTEL_INTERNAL_LOG_DEBUG("[OTLP HTTP Exporter] DEBUG: Session state: sending request"); + OTEL_INTERNAL_LOG_DEBUG("[OTLP HTTP Exporter] Session state: sending request"); } break; case http_client::SessionState::SendFailed: - OTEL_INTERNAL_LOG_ERROR("[OTLP HTTP Exporter] Session state: request send failed\n"); + OTEL_INTERNAL_LOG_ERROR("[OTLP HTTP Exporter] Session state: request send failed"); cv_.notify_all(); break; case http_client::SessionState::Response: if (console_debug_) { - OTEL_INTERNAL_LOG_DEBUG("[OTLP HTTP Exporter] DEBUG:Session state: response received\n"); + OTEL_INTERNAL_LOG_DEBUG("[OTLP HTTP Exporter] Session state: response received"); } break; case http_client::SessionState::SSLHandshakeFailed: - OTEL_INTERNAL_LOG_ERROR("[OTLP HTTP Exporter] Session state: SSL handshake failed\n"); + OTEL_INTERNAL_LOG_ERROR("[OTLP HTTP Exporter] Session state: SSL handshake failed"); cv_.notify_all(); break; case http_client::SessionState::TimedOut: - OTEL_INTERNAL_LOG_ERROR("[OTLP HTTP Exporter] Session state: request time out\n"); + OTEL_INTERNAL_LOG_ERROR("[OTLP HTTP Exporter] Session state: request time out"); cv_.notify_all(); break; case http_client::SessionState::NetworkError: - OTEL_INTERNAL_LOG_ERROR("[OTLP HTTP Exporter] Session state: network error\n"); + OTEL_INTERNAL_LOG_ERROR("[OTLP HTTP Exporter] Session state: network error"); cv_.notify_all(); break; case http_client::SessionState::ReadError: if (console_debug_) { - OTEL_INTERNAL_LOG_DEBUG( - "[OTLP HTTP Exporter] DEBUG: Session state: error reading responsen"); + OTEL_INTERNAL_LOG_DEBUG("[OTLP HTTP Exporter] Session state: error reading response"); } break; @@ -197,7 +195,7 @@ class ResponseHandler : public http_client::EventHandler if (console_debug_) { OTEL_INTERNAL_LOG_DEBUG( - "[OTLP HTTP Exporter] DEBUG:Session state: error writing request\n"); + "[OTLP HTTP Exporter] DEBUG:Session state: error writing request"); } break; @@ -596,16 +594,16 @@ sdk::common::ExportResult OtlpHttpExporter::Export( { if (options_.console_debug) { - std::cout << "[OTLP HTTP Exporter] Request body(Binary):\n" - << service_request.Utf8DebugString() << std::endl; + OTEL_INTERNAL_LOG_DEBUG( + "[OTLP HTTP Exporter] Request body(Binary): " << service_request.Utf8DebugString()); } } else { if (options_.console_debug) { - std::cout << "[OTLP HTTP Exporter] Serialize body failed(Binary):" - << service_request.InitializationErrorString() << std::endl; + OTEL_INTERNAL_LOG_DEBUG("[OTLP HTTP Exporter] Serialize body failed(Binary):" + << service_request.InitializationErrorString()); } return sdk::common::ExportResult::kFailure; } @@ -622,7 +620,7 @@ sdk::common::ExportResult OtlpHttpExporter::Export( json_request.dump(-1, ' ', false, nlohmann::detail::error_handler_t::replace); if (options_.console_debug) { - OTEL_INTERNAL_LOG_DEBUG("[OTLP HTTP Exporter] DEBUG: Request body(Json)" << post_body_json ); + OTEL_INTERNAL_LOG_DEBUG("[OTLP HTTP Exporter] Request body(Json)" << post_body_json); } body_vec.assign(post_body_json.begin(), post_body_json.end()); content_type = kHttpJsonContentType; @@ -644,7 +642,7 @@ sdk::common::ExportResult OtlpHttpExporter::Export( // Wait for the response to be received if (options_.console_debug) { - OTEL_INTERNAL_LOG_ERROR("[OTLP HTTP Exporter] DEBUG: Waiting for response from " + OTEL_INTERNAL_LOG_DEBUG("[OTLP HTTP Exporter] DEBUG: Waiting for response from " << options_.url << " (timeout = " << options_.timeout.count() << " milliseconds)"); } diff --git a/exporters/zipkin/src/zipkin_exporter.cc b/exporters/zipkin/src/zipkin_exporter.cc index 2ae9fc651e..ca9069eea5 100644 --- a/exporters/zipkin/src/zipkin_exporter.cc +++ b/exporters/zipkin/src/zipkin_exporter.cc @@ -72,7 +72,7 @@ sdk::common::ExportResult ZipkinExporter::Export( { if (result.GetSessionState() == http_client::SessionState::ConnectFailed) { - OTEL_INTERNAL_LOG_ERROR("ZIPKIN EXPORTER] Zipkin Exporter: Connection failed") + OTEL_INTERNAL_LOG_ERROR("ZIPKIN EXPORTER] Zipkin Exporter: Connection failed"); } return sdk::common::ExportResult::kFailure; } From a8a12b89b004dde66a19371b1d22dd87d3b4696e Mon Sep 17 00:00:00 2001 From: Lalit Kumar Bhasin Date: Mon, 28 Jun 2021 20:06:32 +0530 Subject: [PATCH 12/15] rename log handler --- exporters/otlp/src/otlp_http_exporter.cc | 2 +- .../sdk/common/{global_error_handler.h => global_log_handler.h} | 0 sdk/include/opentelemetry/sdk_config.h | 2 +- sdk/src/common/global_log_handler.cc | 0 sdk/src/logs/logger.cc | 1 - 5 files changed, 2 insertions(+), 3 deletions(-) rename sdk/include/opentelemetry/sdk/common/{global_error_handler.h => global_log_handler.h} (100%) create mode 100644 sdk/src/common/global_log_handler.cc diff --git a/exporters/otlp/src/otlp_http_exporter.cc b/exporters/otlp/src/otlp_http_exporter.cc index cec530cfbf..80cbfa40c7 100644 --- a/exporters/otlp/src/otlp_http_exporter.cc +++ b/exporters/otlp/src/otlp_http_exporter.cc @@ -16,7 +16,7 @@ #include "opentelemetry/proto/collector/trace/v1/trace_service.pb.h" #include "opentelemetry/exporters/otlp/protobuf_include_suffix.h" -#include "opentelemetry/sdk/common/global_error_handler.h" +#include "opentelemetry/sdk_config.h" #include #include diff --git a/sdk/include/opentelemetry/sdk/common/global_error_handler.h b/sdk/include/opentelemetry/sdk/common/global_log_handler.h similarity index 100% rename from sdk/include/opentelemetry/sdk/common/global_error_handler.h rename to sdk/include/opentelemetry/sdk/common/global_log_handler.h diff --git a/sdk/include/opentelemetry/sdk_config.h b/sdk/include/opentelemetry/sdk_config.h index e1c8bc91f4..8ac72a10d5 100644 --- a/sdk/include/opentelemetry/sdk_config.h +++ b/sdk/include/opentelemetry/sdk_config.h @@ -4,4 +4,4 @@ #pragma once #include "opentelemetry/config.h" -#include "opentelemetry/sdk/common/global_error_handler.h" \ No newline at end of file +#include "opentelemetry/sdk/common/global_log_handler.h" \ No newline at end of file diff --git a/sdk/src/common/global_log_handler.cc b/sdk/src/common/global_log_handler.cc new file mode 100644 index 0000000000..e69de29bb2 diff --git a/sdk/src/logs/logger.cc b/sdk/src/logs/logger.cc index ba5f1f5b1e..fe61dd4ce8 100644 --- a/sdk/src/logs/logger.cc +++ b/sdk/src/logs/logger.cc @@ -3,7 +3,6 @@ #ifdef ENABLE_LOGS_PREVIEW # include "opentelemetry/sdk/logs/logger.h" -# include "opentelemetry/sdk/common/global_error_handler.h" # include "opentelemetry/sdk/logs/log_record.h" # include "opentelemetry/sdk_config.h" # include "opentelemetry/trace/provider.h" From dcf70a1b32d6801e6ac5111cc0c9181444cc5038 Mon Sep 17 00:00:00 2001 From: Lalit Kumar Bhasin Date: Thu, 29 Jul 2021 00:58:19 +0530 Subject: [PATCH 13/15] remove mutext for protecting global log handler --- .../sdk/common/global_log_handler.h | 17 ++++------------- 1 file changed, 4 insertions(+), 13 deletions(-) diff --git a/sdk/include/opentelemetry/sdk/common/global_log_handler.h b/sdk/include/opentelemetry/sdk/common/global_log_handler.h index 0e56ebf5e5..4322007534 100644 --- a/sdk/include/opentelemetry/sdk/common/global_log_handler.h +++ b/sdk/include/opentelemetry/sdk/common/global_log_handler.h @@ -112,22 +112,19 @@ class GlobalLogHandler * Returns the singleton LogHandler. * * By default, a default LogHandler is returned. This will never return a - * nullptr LogHander. + * nullptr LogHandler. */ static nostd::shared_ptr GetLogHandler() noexcept { - std::lock_guard guard(GetLock()); return nostd::shared_ptr(GetHandler()); } /** * Changes the singleton LogHandler. + * This should be called once at the start of application before creating TracerProvider + * instance. */ - static void SetLogHandler(nostd::shared_ptr eh) noexcept - { - std::lock_guard guard(GetLock()); - GetHandler() = eh; - } + static void SetLogHandler(nostd::shared_ptr eh) noexcept { GetHandler() = eh; } private: static nostd::shared_ptr &GetHandler() noexcept @@ -135,12 +132,6 @@ class GlobalLogHandler static nostd::shared_ptr handler(new DefaultLogHandler); return handler; } - - static opentelemetry::common::SpinLockMutex &GetLock() noexcept - { - static opentelemetry::common::SpinLockMutex lock; - return lock; - } }; } // namespace internal_log From 7a83f483b81426c699403cdc7cc1ef1de2bada63 Mon Sep 17 00:00:00 2001 From: Lalit Kumar Bhasin Date: Thu, 29 Jul 2021 01:03:42 +0530 Subject: [PATCH 14/15] remove mutext headers --- sdk/include/opentelemetry/sdk/common/global_log_handler.h | 3 --- 1 file changed, 3 deletions(-) diff --git a/sdk/include/opentelemetry/sdk/common/global_log_handler.h b/sdk/include/opentelemetry/sdk/common/global_log_handler.h index 4322007534..29353f8f16 100644 --- a/sdk/include/opentelemetry/sdk/common/global_log_handler.h +++ b/sdk/include/opentelemetry/sdk/common/global_log_handler.h @@ -3,12 +3,9 @@ #pragma once -#include #include -#include #include -#include "opentelemetry/common/spin_lock_mutex.h" #include "opentelemetry/nostd/shared_ptr.h" #include "opentelemetry/sdk/common/attribute_utils.h" #include "opentelemetry/version.h" From e3e450163fa4bd1736529d2847b960cd4b74a338 Mon Sep 17 00:00:00 2001 From: Lalit Kumar Bhasin Date: Thu, 29 Jul 2021 01:06:48 +0530 Subject: [PATCH 15/15] change default log level to warn --- sdk/include/opentelemetry/sdk/common/global_log_handler.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sdk/include/opentelemetry/sdk/common/global_log_handler.h b/sdk/include/opentelemetry/sdk/common/global_log_handler.h index 29353f8f16..f967111e3a 100644 --- a/sdk/include/opentelemetry/sdk/common/global_log_handler.h +++ b/sdk/include/opentelemetry/sdk/common/global_log_handler.h @@ -16,7 +16,7 @@ #define OTEL_INTERNAL_LOG_LEVEL_DEBUG 3 // to be disabled in release #ifndef OTEL_INTERNAL_LOG_LEVEL -# define OTEL_INTERNAL_LOG_LEVEL OTEL_INTERNAL_LOG_LEVEL_DEBUG // ERROR, WARN , INFO AND DEBUG +# define OTEL_INTERNAL_LOG_LEVEL OTEL_INTERNAL_LOG_LEVEL_WARN // ERROR and WARN #endif OPENTELEMETRY_BEGIN_NAMESPACE