Skip to content

Commit

Permalink
Add unit test of SumPointData for OtlpHttpMetricExporter
Browse files Browse the repository at this point in the history
Signed-off-by: owentou <owentou@tencent.com>
  • Loading branch information
owent committed Jul 12, 2022
1 parent eb4f0f9 commit 226a8e6
Show file tree
Hide file tree
Showing 11 changed files with 593 additions and 18 deletions.
42 changes: 42 additions & 0 deletions exporters/otlp/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,14 @@ cc_library(
name = "otlp_recordable",
srcs = [
"src/otlp_log_recordable.cc",
"src/otlp_metric_utils.cc",
"src/otlp_populate_attribute_utils.cc",
"src/otlp_recordable.cc",
"src/otlp_recordable_utils.cc",
],
hdrs = [
"include/opentelemetry/exporters/otlp/otlp_log_recordable.h",
"include/opentelemetry/exporters/otlp/otlp_metric_utils.h",
"include/opentelemetry/exporters/otlp/otlp_populate_attribute_utils.h",
"include/opentelemetry/exporters/otlp/otlp_recordable.h",
"include/opentelemetry/exporters/otlp/otlp_recordable_utils.h",
Expand Down Expand Up @@ -155,6 +157,30 @@ cc_library(
],
)

cc_library(
name = "otlp_http_metric_exporter",
srcs = [
"src/otlp_http_metric_exporter.cc",
],
hdrs = [
"include/opentelemetry/exporters/otlp/otlp_environment.h",
"include/opentelemetry/exporters/otlp/otlp_http_metric_exporter.h",
"include/opentelemetry/exporters/otlp/protobuf_include_prefix.h",
"include/opentelemetry/exporters/otlp/protobuf_include_suffix.h",
],
strip_include_prefix = "include",
tags = [
"otlp",
"otlp_http_metric",
],
deps = [
":otlp_http_client",
":otlp_recordable",
"//sdk/src/metrics",
"@com_github_opentelemetry_proto//:metrics_service_proto_cc",
],
)

cc_library(
name = "otlp_grpc_log_exporter",
srcs = [
Expand Down Expand Up @@ -257,6 +283,22 @@ cc_test(
],
)

cc_test(
name = "otlp_http_metric_exporter_test",
srcs = ["test/otlp_http_metric_exporter_test.cc"],
tags = [
"otlp",
"otlp_http_metric",
"test",
],
deps = [
":otlp_http_metric_exporter",
"//api",
"//ext/src/http/client/nosend:http_client_nosend",
"@com_google_googletest//:gtest_main",
],
)

cc_test(
name = "otlp_grpc_log_exporter_test",
srcs = ["test/otlp_grpc_log_exporter_test.cc"],
Expand Down
17 changes: 17 additions & 0 deletions exporters/otlp/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -233,5 +233,22 @@ if(BUILD_TESTING)
TEST_PREFIX exporter.otlp.
TEST_LIST otlp_http_log_exporter_test)
endif()

if(NOT WITH_METRICS_PREVIEW)
add_executable(otlp_http_metric_exporter_test
test/otlp_http_metric_exporter_test.cc)
target_link_libraries(
otlp_http_metric_exporter_test
${GTEST_BOTH_LIBRARIES}
${CMAKE_THREAD_LIBS_INIT}
${GMOCK_LIB}
opentelemetry_exporter_otlp_http_metric
opentelemetry_metrics
http_client_nosend)
gtest_add_tests(
TARGET otlp_http_metric_exporter_test
TEST_PREFIX exporter.otlp.
TEST_LIST otlp_http_metric_exporter_test)
endif()
endif()
endif() # BUILD_TESTING
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,11 @@ class OtlpHttpClient
std::function<bool(opentelemetry::sdk::common::ExportResult)> &&result_callback,
std::size_t max_running_requests) noexcept;

/**
* Force flush the HTTP client.
*/
bool ForceFlush(std::chrono::microseconds timeout = (std::chrono::microseconds::max)()) noexcept;

/**
* Shut down the HTTP client.
* @param timeout an optional timeout, the default timeout of 0 means that no
Expand All @@ -171,6 +176,12 @@ class OtlpHttpClient
*/
inline const OtlpHttpClientOptions &GetOptions() const noexcept { return options_; }

/**
* Get if this OTLP http client is shutdown.
* @return return true after Shutdown is called.
*/
bool IsShutdown() const noexcept;

private:
struct HttpSessionData
{
Expand Down Expand Up @@ -224,11 +235,11 @@ class OtlpHttpClient
*/
bool cleanupGCSessions() noexcept;

bool isShutdown() const noexcept;

// For testing
friend class OtlpHttpExporterTestPeer;
friend class OtlpHttpLogExporterTestPeer;
friend class OtlpHttpMetricExporterTestPeer;

/**
* Create an OtlpHttpClient using the specified http client.
* Only tests can call this constructor directly.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,12 @@ class OtlpHttpMetricExporter final : public opentelemetry::sdk::metrics::MetricE
opentelemetry::sdk::common::ExportResult Export(
const opentelemetry::sdk::metrics::ResourceMetrics &data) noexcept override;

/**
* Force flush the exporter.
*/
bool ForceFlush(
std::chrono::microseconds timeout = (std::chrono::microseconds::max)()) noexcept override;

bool Shutdown(
std::chrono::microseconds timeout = (std::chrono::microseconds::max)()) noexcept override;

Expand Down
34 changes: 20 additions & 14 deletions exporters/otlp/src/otlp_http_client.cc
Original file line number Diff line number Diff line change
Expand Up @@ -651,7 +651,7 @@ OtlpHttpClient::OtlpHttpClient(OtlpHttpClientOptions &&options)

OtlpHttpClient::~OtlpHttpClient()
{
if (!isShutdown())
if (!IsShutdown())
{
Shutdown();
}
Expand Down Expand Up @@ -760,17 +760,8 @@ sdk::common::ExportResult OtlpHttpClient::Export(
return opentelemetry::sdk::common::ExportResult::kSuccess;
}

bool OtlpHttpClient::Shutdown(std::chrono::microseconds timeout) noexcept
bool OtlpHttpClient::ForceFlush(std::chrono::microseconds timeout) noexcept
{
{
std::lock_guard<std::recursive_mutex> guard{session_manager_lock_};
is_shutdown_ = true;

// Shutdown the session manager
http_client_->CancelAllSessions();
http_client_->FinishAllSessions();
}

// ASAN will report chrono: runtime error: signed integer overflow: A + B cannot be represented
// in type 'long int' here. So we reset timeout to meet signed long int limit here.
timeout = opentelemetry::common::DurationUtil::AdjustWaitForTimeout(
Expand All @@ -793,14 +784,29 @@ bool OtlpHttpClient::Shutdown(std::chrono::microseconds timeout) noexcept
// checking and waiting, we should not wait forever.
session_waker_.wait_for(lock, options_.timeout);
}
return true;
}
else
{
session_waker_.wait_for(lock, timeout, [this] {
return session_waker_.wait_for(lock, timeout, [this] {
std::lock_guard<std::recursive_mutex> guard{session_manager_lock_};
return running_sessions_.empty();
});
}
}

bool OtlpHttpClient::Shutdown(std::chrono::microseconds timeout) noexcept
{
{
std::lock_guard<std::recursive_mutex> guard{session_manager_lock_};
is_shutdown_ = true;

// Shutdown the session manager
http_client_->CancelAllSessions();
http_client_->FinishAllSessions();
}

ForceFlush(timeout);

while (cleanupGCSessions())
;
Expand Down Expand Up @@ -907,7 +913,7 @@ OtlpHttpClient::createSession(
// Send the request
std::lock_guard<std::recursive_mutex> guard{session_manager_lock_};
// Return failure if this exporter has been shutdown
if (isShutdown())
if (IsShutdown())
{
const char *error_message = "[OTLP HTTP Client] Export failed, exporter is shutdown";
if (options_.console_debug)
Expand Down Expand Up @@ -976,7 +982,7 @@ bool OtlpHttpClient::cleanupGCSessions() noexcept
return !gc_sessions_.empty();
}

bool OtlpHttpClient::isShutdown() const noexcept
bool OtlpHttpClient::IsShutdown() const noexcept
{
return is_shutdown_;
}
Expand Down
5 changes: 5 additions & 0 deletions exporters/otlp/src/otlp_http_exporter.cc
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,11 @@ std::unique_ptr<opentelemetry::sdk::trace::Recordable> OtlpHttpExporter::MakeRec
opentelemetry::sdk::common::ExportResult OtlpHttpExporter::Export(
const nostd::span<std::unique_ptr<opentelemetry::sdk::trace::Recordable>> &spans) noexcept
{
if (http_client_->IsShutdown())
{
return opentelemetry::sdk::common::ExportResult::kFailure;
}

if (spans.empty())
{
return opentelemetry::sdk::common::ExportResult::kSuccess;
Expand Down
5 changes: 5 additions & 0 deletions exporters/otlp/src/otlp_http_log_exporter.cc
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,11 @@ std::unique_ptr<opentelemetry::sdk::logs::Recordable> OtlpHttpLogExporter::MakeR
opentelemetry::sdk::common::ExportResult OtlpHttpLogExporter::Export(
const nostd::span<std::unique_ptr<opentelemetry::sdk::logs::Recordable>> &logs) noexcept
{
if (http_client_->IsShutdown())
{
return opentelemetry::sdk::common::ExportResult::kFailure;
}

if (logs.empty())
{
return opentelemetry::sdk::common::ExportResult::kSuccess;
Expand Down
14 changes: 12 additions & 2 deletions exporters/otlp/src/otlp_http_metric_exporter.cc
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
#ifndef ENABLE_METRICS_PREVIEW

# include "opentelemetry/exporters/otlp/otlp_http_metric_exporter.h"
# include "opentelemetry/exporters/otlp/otlp_metrics_utils.h"
# include "opentelemetry/exporters/otlp/otlp_metric_utils.h"

# include "opentelemetry/exporters/otlp/protobuf_include_prefix.h"

Expand Down Expand Up @@ -64,12 +64,17 @@ OtlpHttpMetricExporter::OtlpHttpMetricExporter(std::unique_ptr<OtlpHttpClient> h
opentelemetry::sdk::common::ExportResult OtlpHttpMetricExporter::Export(
const opentelemetry::sdk::metrics::ResourceMetrics &data) noexcept
{
if (http_client_->IsShutdown())
{
return opentelemetry::sdk::common::ExportResult::kFailure;
}

if (data.instrumentation_info_metric_data_.empty())
{
return opentelemetry::sdk::common::ExportResult::kSuccess;
}
proto::collector::metrics::v1::ExportMetricsServiceRequest service_request;
OtlpMetricsUtils::PopulateRequest(data, &service_request);
OtlpMetricUtils::PopulateRequest(data, &service_request);
std::size_t metric_count = data.instrumentation_info_metric_data_.size();
# ifdef ENABLE_ASYNC_EXPORT
http_client_->Export(service_request, [metric_count](
Expand Down Expand Up @@ -103,6 +108,11 @@ opentelemetry::sdk::common::ExportResult OtlpHttpMetricExporter::Export(
# endif
}

bool OtlpHttpMetricExporter::ForceFlush(std::chrono::microseconds timeout) noexcept
{
return http_client_->ForceFlush(timeout);
}

bool OtlpHttpMetricExporter::Shutdown(std::chrono::microseconds timeout) noexcept
{
return http_client_->Shutdown(timeout);
Expand Down
11 changes: 11 additions & 0 deletions exporters/otlp/test/otlp_http_exporter_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -431,6 +431,17 @@ class OtlpHttpExporterTestPeer : public ::testing::Test
# endif
};

TEST(OtlpHttpExporterTest, Shutdown)
{
auto exporter = std::unique_ptr<opentelemetry::sdk::trace::SpanExporter>(new OtlpHttpExporter());
ASSERT_TRUE(exporter->Shutdown());

nostd::span<std::unique_ptr<opentelemetry::sdk::trace::Recordable>> spans = {};

auto result = exporter->Export(spans);
EXPECT_EQ(result, opentelemetry::sdk::common::ExportResult::kFailure);
}

// Create spans, let processor call Export()
TEST_F(OtlpHttpExporterTestPeer, ExportJsonIntegrationTestSync)
{
Expand Down
11 changes: 11 additions & 0 deletions exporters/otlp/test/otlp_http_log_exporter_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -482,6 +482,17 @@ class OtlpHttpLogExporterTestPeer : public ::testing::Test
# endif
};

TEST(OtlpHttpLogExporterTest, Shutdown)
{
auto exporter = std::unique_ptr<opentelemetry::sdk::logs::LogExporter>(new OtlpHttpLogExporter());
ASSERT_TRUE(exporter->Shutdown());

nostd::span<std::unique_ptr<opentelemetry::sdk::logs::Recordable>> logs = {};

auto result = exporter->Export(logs);
EXPECT_EQ(result, opentelemetry::sdk::common::ExportResult::kFailure);
}

// Create log records, let processor call Export()
TEST_F(OtlpHttpLogExporterTestPeer, ExportJsonIntegrationTestSync)
{
Expand Down
Loading

0 comments on commit 226a8e6

Please sign in to comment.