Skip to content

Commit

Permalink
Mock for http exporters (open-telemetry#1185)
Browse files Browse the repository at this point in the history
  • Loading branch information
esigo committed Feb 19, 2022
1 parent 9502339 commit 9157fd4
Show file tree
Hide file tree
Showing 24 changed files with 732 additions and 637 deletions.
2 changes: 2 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -390,6 +390,7 @@ list(APPEND CMAKE_PREFIX_PATH "${CMAKE_BINARY_DIR}")

include(CTest)
if(BUILD_TESTING)
add_definitions(-DENABLE_TEST)
if(EXISTS ${CMAKE_BINARY_DIR}/lib/libgtest.a)
# Prefer GTest from build tree. GTest is not always working with
# CMAKE_PREFIX_PATH
Expand Down Expand Up @@ -426,6 +427,7 @@ include_directories(api/include)
add_subdirectory(api)

if(NOT WITH_API_ONLY)
set(BUILD_TESTING ${BUILD_TESTING})
include_directories(sdk/include)
include_directories(sdk)
include_directories(ext/include)
Expand Down
2 changes: 1 addition & 1 deletion ci/do_ci.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ $VCPKG_DIR="$SRC_DIR\vcpkg"

switch ($action) {
"bazel.build" {
bazel build $BAZEL_OPTIONS -- //...
bazel build --copt=-DENABLE_TEST $BAZEL_OPTIONS -- //...
$exit = $LASTEXITCODE
if ($exit -ne 0) {
exit $exit
Expand Down
2 changes: 1 addition & 1 deletion ci/do_ci.sh
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ mkdir -p "${BUILD_DIR}"
[ -z "${PLUGIN_DIR}" ] && export PLUGIN_DIR=$HOME/plugin
mkdir -p "${PLUGIN_DIR}"

BAZEL_OPTIONS="--copt=-DENABLE_METRICS_PREVIEW --copt=-DENABLE_LOGS_PREVIEW"
BAZEL_OPTIONS="--copt=-DENABLE_METRICS_PREVIEW --copt=-DENABLE_LOGS_PREVIEW --copt=-DENABLE_TEST"
BAZEL_TEST_OPTIONS="$BAZEL_OPTIONS --test_output=errors"

# https://github.com/bazelbuild/bazel/issues/4341
Expand Down
2 changes: 2 additions & 0 deletions exporters/otlp/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,7 @@ cc_test(
deps = [
":otlp_http_exporter",
"//api",
"//ext/src/http/client/nosend:http_client_nosend",
"@com_google_googletest//:gtest_main",
],
)
Expand All @@ -249,6 +250,7 @@ cc_test(
deps = [
":otlp_http_log_exporter",
"//api",
"//ext/src/http/client/nosend:http_client_nosend",
"@com_google_googletest//:gtest_main",
],
)
Expand Down
12 changes: 8 additions & 4 deletions exporters/otlp/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ if(BUILD_TESTING)
add_executable(otlp_http_exporter_test test/otlp_http_exporter_test.cc)
target_link_libraries(
otlp_http_exporter_test ${GTEST_BOTH_LIBRARIES} ${CMAKE_THREAD_LIBS_INIT}
${GMOCK_LIB} opentelemetry_exporter_otlp_http)
${GMOCK_LIB} opentelemetry_exporter_otlp_http http_client_nosend)
gtest_add_tests(
TARGET otlp_http_exporter_test
TEST_PREFIX exporter.otlp.
Expand All @@ -180,9 +180,13 @@ if(BUILD_TESTING)
add_executable(otlp_http_log_exporter_test
test/otlp_http_log_exporter_test.cc)
target_link_libraries(
otlp_http_log_exporter_test ${GTEST_BOTH_LIBRARIES}
${CMAKE_THREAD_LIBS_INIT} ${GMOCK_LIB}
opentelemetry_exporter_otlp_http_log opentelemetry_logs)
otlp_http_log_exporter_test
${GTEST_BOTH_LIBRARIES}
${CMAKE_THREAD_LIBS_INIT}
${GMOCK_LIB}
opentelemetry_exporter_otlp_http_log
opentelemetry_logs
http_client_nosend)
gtest_add_tests(
TARGET otlp_http_log_exporter_test
TEST_PREFIX exporter.otlp.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,17 @@ class OtlpHttpClient
std::string http_uri_;
mutable opentelemetry::common::SpinLockMutex lock_;
bool isShutdown() const noexcept;
// For testing
friend class OtlpHttpExporterTestPeer;
friend class OtlpHttpLogExporterTestPeer;
/**
* Create an OtlpHttpClient using the specified http client.
* Only tests can call this constructor directly.
* @param options the Otlp http client options to be used for exporting
* @param http_client the http client to be used for exporting
*/
OtlpHttpClient(OtlpHttpClientOptions &&options,
std::shared_ptr<ext::http::client::HttpClient> http_client);
};
} // namespace otlp
} // namespace exporter
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,14 +91,19 @@ class OtlpHttpExporter final : public opentelemetry::sdk::trace::SpanExporter
bool Shutdown(std::chrono::microseconds timeout = std::chrono::microseconds(0)) noexcept override;

private:
// For testing
friend class OtlpHttpExporterTestPeer;

// The configuration options associated with this exporter.
const OtlpHttpExporterOptions options_;

// Object that stores the HTTP sessions that have been created
OtlpHttpClient http_client_;
std::unique_ptr<OtlpHttpClient> http_client_;
// For testing
friend class OtlpHttpExporterTestPeer;
/**
* Create an OtlpHttpExporter using the specified http client.
* Only tests can call this constructor directly.
* @param http_client the http client to be used for exporting
*/
OtlpHttpExporter(std::unique_ptr<OtlpHttpClient> http_client);
};
} // namespace otlp
} // namespace exporter
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,14 +90,19 @@ class OtlpHttpLogExporter final : public opentelemetry::sdk::logs::LogExporter
bool Shutdown(std::chrono::microseconds timeout = std::chrono::microseconds(0)) noexcept override;

private:
// For testing
friend class OtlpHttpLogExporterTestPeer;

// Configuration options for the exporter
const OtlpHttpLogExporterOptions options_;

// Object that stores the HTTP sessions that have been created
OtlpHttpClient http_client_;
std::unique_ptr<OtlpHttpClient> http_client_;
// For testing
friend class OtlpHttpLogExporterTestPeer;
/**
* Create an OtlpHttpLogExporter using the specified http client.
* Only tests can call this constructor directly.
* @param http_client the http client to be used for exporting
*/
OtlpHttpLogExporter(std::unique_ptr<OtlpHttpClient> http_client);
};
} // namespace otlp
} // namespace exporter
Expand Down
10 changes: 7 additions & 3 deletions exporters/otlp/src/otlp_http_client.cc
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,13 @@
#include "opentelemetry/ext/http/client/http_client_factory.h"
#include "opentelemetry/ext/http/common/url_parser.h"

#include "nlohmann/json.hpp"

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

#include <mutex>
#include "google/protobuf/message.h"
#include "google/protobuf/reflection.h"
#include "google/protobuf/stubs/common.h"
#include "nlohmann/json.hpp"

#if defined(GOOGLE_PROTOBUF_VERSION) && GOOGLE_PROTOBUF_VERSION >= 3007000
# include "google/protobuf/stubs/strutil.h"
Expand Down Expand Up @@ -362,7 +361,7 @@ static void ConvertGenericMessageToJson(nlohmann::json &value,
}
}

static bool SerializeToHttpBody(http_client::Body &output, const google::protobuf::Message &message)
bool SerializeToHttpBody(http_client::Body &output, const google::protobuf::Message &message)
{
auto body_size = message.ByteSizeLong();
if (body_size > 0)
Expand Down Expand Up @@ -566,6 +565,11 @@ OtlpHttpClient::OtlpHttpClient(OtlpHttpClientOptions &&options)
: options_(options), http_client_(http_client::HttpClientFactory::Create())
{}

OtlpHttpClient::OtlpHttpClient(OtlpHttpClientOptions &&options,
std::shared_ptr<ext::http::client::HttpClient> http_client)
: options_(options), http_client_(http_client)
{}

// ----------------------------- HTTP Client methods ------------------------------
opentelemetry::sdk::common::ExportResult OtlpHttpClient::Export(
const google::protobuf::Message &message) noexcept
Expand Down
21 changes: 12 additions & 9 deletions exporters/otlp/src/otlp_http_exporter.cc
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,18 @@ OtlpHttpExporter::OtlpHttpExporter() : OtlpHttpExporter(OtlpHttpExporterOptions(

OtlpHttpExporter::OtlpHttpExporter(const OtlpHttpExporterOptions &options)
: options_(options),
http_client_(OtlpHttpClientOptions(options.url,
options.content_type,
options.json_bytes_mapping,
options.use_json_name,
options.console_debug,
options.timeout,
options.http_headers))
http_client_(new OtlpHttpClient(OtlpHttpClientOptions(options.url,
options.content_type,
options.json_bytes_mapping,
options.use_json_name,
options.console_debug,
options.timeout,
options.http_headers)))
{}

OtlpHttpExporter::OtlpHttpExporter(std::unique_ptr<OtlpHttpClient> http_client)
: options_(OtlpHttpExporterOptions()), http_client_(std::move(http_client))
{}
// ----------------------------- Exporter methods ------------------------------

std::unique_ptr<opentelemetry::sdk::trace::Recordable> OtlpHttpExporter::MakeRecordable() noexcept
Expand All @@ -45,12 +48,12 @@ opentelemetry::sdk::common::ExportResult OtlpHttpExporter::Export(
{
proto::collector::trace::v1::ExportTraceServiceRequest service_request;
OtlpRecordableUtils::PopulateRequest(spans, &service_request);
return http_client_.Export(service_request);
return http_client_->Export(service_request);
}

bool OtlpHttpExporter::Shutdown(std::chrono::microseconds timeout) noexcept
{
return http_client_.Shutdown(timeout);
return http_client_->Shutdown(timeout);
}

} // namespace otlp
Expand Down
21 changes: 12 additions & 9 deletions exporters/otlp/src/otlp_http_log_exporter.cc
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,18 @@ OtlpHttpLogExporter::OtlpHttpLogExporter() : OtlpHttpLogExporter(OtlpHttpLogExpo

OtlpHttpLogExporter::OtlpHttpLogExporter(const OtlpHttpLogExporterOptions &options)
: options_(options),
http_client_(OtlpHttpClientOptions(options.url,
options.content_type,
options.json_bytes_mapping,
options.use_json_name,
options.console_debug,
options.timeout,
options.http_headers))
http_client_(new OtlpHttpClient(OtlpHttpClientOptions(options.url,
options.content_type,
options.json_bytes_mapping,
options.use_json_name,
options.console_debug,
options.timeout,
options.http_headers)))
{}

OtlpHttpLogExporter::OtlpHttpLogExporter(std::unique_ptr<OtlpHttpClient> http_client)
: options_(OtlpHttpLogExporterOptions()), http_client_(std::move(http_client))
{}
// ----------------------------- Exporter methods ------------------------------

std::unique_ptr<opentelemetry::sdk::logs::Recordable> OtlpHttpLogExporter::MakeRecordable() noexcept
Expand All @@ -47,12 +50,12 @@ opentelemetry::sdk::common::ExportResult OtlpHttpLogExporter::Export(
{
proto::collector::logs::v1::ExportLogsServiceRequest service_request;
OtlpRecordableUtils::PopulateRequest(logs, &service_request);
return http_client_.Export(service_request);
return http_client_->Export(service_request);
}

bool OtlpHttpLogExporter::Shutdown(std::chrono::microseconds timeout) noexcept
{
return http_client_.Shutdown(timeout);
return http_client_->Shutdown(timeout);
}

} // namespace otlp
Expand Down
Loading

0 comments on commit 9157fd4

Please sign in to comment.