Skip to content

Commit 9157fd4

Browse files
authored
Mock for http exporters (#1185)
1 parent 9502339 commit 9157fd4

24 files changed

Lines changed: 732 additions & 637 deletions

CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -390,6 +390,7 @@ list(APPEND CMAKE_PREFIX_PATH "${CMAKE_BINARY_DIR}")
390390

391391
include(CTest)
392392
if(BUILD_TESTING)
393+
add_definitions(-DENABLE_TEST)
393394
if(EXISTS ${CMAKE_BINARY_DIR}/lib/libgtest.a)
394395
# Prefer GTest from build tree. GTest is not always working with
395396
# CMAKE_PREFIX_PATH
@@ -426,6 +427,7 @@ include_directories(api/include)
426427
add_subdirectory(api)
427428

428429
if(NOT WITH_API_ONLY)
430+
set(BUILD_TESTING ${BUILD_TESTING})
429431
include_directories(sdk/include)
430432
include_directories(sdk)
431433
include_directories(ext/include)

ci/do_ci.ps1

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ $VCPKG_DIR="$SRC_DIR\vcpkg"
2222

2323
switch ($action) {
2424
"bazel.build" {
25-
bazel build $BAZEL_OPTIONS -- //...
25+
bazel build --copt=-DENABLE_TEST $BAZEL_OPTIONS -- //...
2626
$exit = $LASTEXITCODE
2727
if ($exit -ne 0) {
2828
exit $exit

ci/do_ci.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ mkdir -p "${BUILD_DIR}"
5959
[ -z "${PLUGIN_DIR}" ] && export PLUGIN_DIR=$HOME/plugin
6060
mkdir -p "${PLUGIN_DIR}"
6161

62-
BAZEL_OPTIONS="--copt=-DENABLE_METRICS_PREVIEW --copt=-DENABLE_LOGS_PREVIEW"
62+
BAZEL_OPTIONS="--copt=-DENABLE_METRICS_PREVIEW --copt=-DENABLE_LOGS_PREVIEW --copt=-DENABLE_TEST"
6363
BAZEL_TEST_OPTIONS="$BAZEL_OPTIONS --test_output=errors"
6464

6565
# https://github.com/bazelbuild/bazel/issues/4341

exporters/otlp/BUILD

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,7 @@ cc_test(
234234
deps = [
235235
":otlp_http_exporter",
236236
"//api",
237+
"//ext/src/http/client/nosend:http_client_nosend",
237238
"@com_google_googletest//:gtest_main",
238239
],
239240
)
@@ -249,6 +250,7 @@ cc_test(
249250
deps = [
250251
":otlp_http_log_exporter",
251252
"//api",
253+
"//ext/src/http/client/nosend:http_client_nosend",
252254
"@com_google_googletest//:gtest_main",
253255
],
254256
)

exporters/otlp/CMakeLists.txt

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ if(BUILD_TESTING)
170170
add_executable(otlp_http_exporter_test test/otlp_http_exporter_test.cc)
171171
target_link_libraries(
172172
otlp_http_exporter_test ${GTEST_BOTH_LIBRARIES} ${CMAKE_THREAD_LIBS_INIT}
173-
${GMOCK_LIB} opentelemetry_exporter_otlp_http)
173+
${GMOCK_LIB} opentelemetry_exporter_otlp_http http_client_nosend)
174174
gtest_add_tests(
175175
TARGET otlp_http_exporter_test
176176
TEST_PREFIX exporter.otlp.
@@ -180,9 +180,13 @@ if(BUILD_TESTING)
180180
add_executable(otlp_http_log_exporter_test
181181
test/otlp_http_log_exporter_test.cc)
182182
target_link_libraries(
183-
otlp_http_log_exporter_test ${GTEST_BOTH_LIBRARIES}
184-
${CMAKE_THREAD_LIBS_INIT} ${GMOCK_LIB}
185-
opentelemetry_exporter_otlp_http_log opentelemetry_logs)
183+
otlp_http_log_exporter_test
184+
${GTEST_BOTH_LIBRARIES}
185+
${CMAKE_THREAD_LIBS_INIT}
186+
${GMOCK_LIB}
187+
opentelemetry_exporter_otlp_http_log
188+
opentelemetry_logs
189+
http_client_nosend)
186190
gtest_add_tests(
187191
TARGET otlp_http_log_exporter_test
188192
TEST_PREFIX exporter.otlp.

exporters/otlp/include/opentelemetry/exporters/otlp/otlp_http_client.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,17 @@ class OtlpHttpClient
127127
std::string http_uri_;
128128
mutable opentelemetry::common::SpinLockMutex lock_;
129129
bool isShutdown() const noexcept;
130+
// For testing
131+
friend class OtlpHttpExporterTestPeer;
132+
friend class OtlpHttpLogExporterTestPeer;
133+
/**
134+
* Create an OtlpHttpClient using the specified http client.
135+
* Only tests can call this constructor directly.
136+
* @param options the Otlp http client options to be used for exporting
137+
* @param http_client the http client to be used for exporting
138+
*/
139+
OtlpHttpClient(OtlpHttpClientOptions &&options,
140+
std::shared_ptr<ext::http::client::HttpClient> http_client);
130141
};
131142
} // namespace otlp
132143
} // namespace exporter

exporters/otlp/include/opentelemetry/exporters/otlp/otlp_http_exporter.h

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -91,14 +91,19 @@ class OtlpHttpExporter final : public opentelemetry::sdk::trace::SpanExporter
9191
bool Shutdown(std::chrono::microseconds timeout = std::chrono::microseconds(0)) noexcept override;
9292

9393
private:
94-
// For testing
95-
friend class OtlpHttpExporterTestPeer;
96-
9794
// The configuration options associated with this exporter.
9895
const OtlpHttpExporterOptions options_;
9996

10097
// Object that stores the HTTP sessions that have been created
101-
OtlpHttpClient http_client_;
98+
std::unique_ptr<OtlpHttpClient> http_client_;
99+
// For testing
100+
friend class OtlpHttpExporterTestPeer;
101+
/**
102+
* Create an OtlpHttpExporter using the specified http client.
103+
* Only tests can call this constructor directly.
104+
* @param http_client the http client to be used for exporting
105+
*/
106+
OtlpHttpExporter(std::unique_ptr<OtlpHttpClient> http_client);
102107
};
103108
} // namespace otlp
104109
} // namespace exporter

exporters/otlp/include/opentelemetry/exporters/otlp/otlp_http_log_exporter.h

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -90,14 +90,19 @@ class OtlpHttpLogExporter final : public opentelemetry::sdk::logs::LogExporter
9090
bool Shutdown(std::chrono::microseconds timeout = std::chrono::microseconds(0)) noexcept override;
9191

9292
private:
93-
// For testing
94-
friend class OtlpHttpLogExporterTestPeer;
95-
9693
// Configuration options for the exporter
9794
const OtlpHttpLogExporterOptions options_;
9895

9996
// Object that stores the HTTP sessions that have been created
100-
OtlpHttpClient http_client_;
97+
std::unique_ptr<OtlpHttpClient> http_client_;
98+
// For testing
99+
friend class OtlpHttpLogExporterTestPeer;
100+
/**
101+
* Create an OtlpHttpLogExporter using the specified http client.
102+
* Only tests can call this constructor directly.
103+
* @param http_client the http client to be used for exporting
104+
*/
105+
OtlpHttpLogExporter(std::unique_ptr<OtlpHttpClient> http_client);
101106
};
102107
} // namespace otlp
103108
} // namespace exporter

exporters/otlp/src/otlp_http_client.cc

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,13 @@
1212
#include "opentelemetry/ext/http/client/http_client_factory.h"
1313
#include "opentelemetry/ext/http/common/url_parser.h"
1414

15-
#include "nlohmann/json.hpp"
16-
1715
#include "opentelemetry/exporters/otlp/protobuf_include_prefix.h"
1816

1917
#include <mutex>
2018
#include "google/protobuf/message.h"
2119
#include "google/protobuf/reflection.h"
2220
#include "google/protobuf/stubs/common.h"
21+
#include "nlohmann/json.hpp"
2322

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

365-
static bool SerializeToHttpBody(http_client::Body &output, const google::protobuf::Message &message)
364+
bool SerializeToHttpBody(http_client::Body &output, const google::protobuf::Message &message)
366365
{
367366
auto body_size = message.ByteSizeLong();
368367
if (body_size > 0)
@@ -566,6 +565,11 @@ OtlpHttpClient::OtlpHttpClient(OtlpHttpClientOptions &&options)
566565
: options_(options), http_client_(http_client::HttpClientFactory::Create())
567566
{}
568567

568+
OtlpHttpClient::OtlpHttpClient(OtlpHttpClientOptions &&options,
569+
std::shared_ptr<ext::http::client::HttpClient> http_client)
570+
: options_(options), http_client_(http_client)
571+
{}
572+
569573
// ----------------------------- HTTP Client methods ------------------------------
570574
opentelemetry::sdk::common::ExportResult OtlpHttpClient::Export(
571575
const google::protobuf::Message &message) noexcept

exporters/otlp/src/otlp_http_exporter.cc

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -23,15 +23,18 @@ OtlpHttpExporter::OtlpHttpExporter() : OtlpHttpExporter(OtlpHttpExporterOptions(
2323

2424
OtlpHttpExporter::OtlpHttpExporter(const OtlpHttpExporterOptions &options)
2525
: options_(options),
26-
http_client_(OtlpHttpClientOptions(options.url,
27-
options.content_type,
28-
options.json_bytes_mapping,
29-
options.use_json_name,
30-
options.console_debug,
31-
options.timeout,
32-
options.http_headers))
26+
http_client_(new OtlpHttpClient(OtlpHttpClientOptions(options.url,
27+
options.content_type,
28+
options.json_bytes_mapping,
29+
options.use_json_name,
30+
options.console_debug,
31+
options.timeout,
32+
options.http_headers)))
3333
{}
3434

35+
OtlpHttpExporter::OtlpHttpExporter(std::unique_ptr<OtlpHttpClient> http_client)
36+
: options_(OtlpHttpExporterOptions()), http_client_(std::move(http_client))
37+
{}
3538
// ----------------------------- Exporter methods ------------------------------
3639

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

5154
bool OtlpHttpExporter::Shutdown(std::chrono::microseconds timeout) noexcept
5255
{
53-
return http_client_.Shutdown(timeout);
56+
return http_client_->Shutdown(timeout);
5457
}
5558

5659
} // namespace otlp

0 commit comments

Comments
 (0)