Skip to content
Open
Show file tree
Hide file tree
Changes from 8 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 7 additions & 9 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -274,15 +274,13 @@ include(GNUInstallDirs)
# Do we need HTTP CLIENT CURL ?
#

if(WITH_OTLP_HTTP
OR WITH_ELASTICSEARCH
OR WITH_ZIPKIN
OR BUILD_W3CTRACECONTEXT_TEST
OR WITH_EXAMPLES_HTTP)
set(WITH_HTTP_CLIENT_CURL ON)
else()
set(WITH_HTTP_CLIENT_CURL OFF)
endif()
include(CMakeDependentOption)
cmake_dependent_option(
WITH_HTTP_CLIENT_CURL
"Use the curl HTTP client backend. Defaults to ON when any HTTP exporter is enabled; set OFF to supply a custom transport."
ON
"WITH_OTLP_HTTP OR WITH_ELASTICSEARCH OR WITH_ZIPKIN OR BUILD_W3CTRACECONTEXT_TEST OR WITH_EXAMPLES_HTTP"
OFF)

#
# Do we need ZLIB ?
Expand Down
6 changes: 6 additions & 0 deletions examples/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,9 @@ endif()
if(WITH_CONFIGURATION)
add_subdirectory(configuration)
endif()

# Build-tree only: depends on the opentelemetry_http_client INTERFACE target which
# is not exported in an installed package.
if(NOT WITH_HTTP_CLIENT_CURL AND WITH_OTLP_HTTP AND TARGET opentelemetry_http_client)
add_subdirectory(custom_http_client)
endif()
14 changes: 14 additions & 0 deletions examples/custom_http_client/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# Copyright The OpenTelemetry Authors
# SPDX-License-Identifier: Apache-2.0

add_library(custom_http_client_stub STATIC custom_http_client.cc)
target_link_libraries(custom_http_client_stub PUBLIC opentelemetry_ext
opentelemetry_sdk)

target_link_libraries(opentelemetry_http_client
INTERFACE custom_http_client_stub)

add_executable(example_custom_http_client main.cc)
target_link_libraries(
example_custom_http_client PRIVATE custom_http_client_stub
opentelemetry-cpp::otlp_http_exporter)
141 changes: 141 additions & 0 deletions examples/custom_http_client/custom_http_client.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0

#include <iostream>
#include <string>

#include "opentelemetry/ext/http/client/http_client.h"
#include "opentelemetry/ext/http/client/http_client_factory.h"
#include "opentelemetry/sdk/common/thread_instrumentation.h"

OPENTELEMETRY_BEGIN_NAMESPACE
namespace ext
{
namespace http
{
namespace client
{

namespace
{

class LoggingRequest : public Request
{
public:
void SetMethod(Method method) noexcept override
{
switch (method)
{
case Method::Get:
method_ = "GET";
break;
case Method::Post:
method_ = "POST";
break;
case Method::Put:
method_ = "PUT";
break;
default:
method_ = "OTHER";
break;
}
}

void SetUri(nostd::string_view uri) noexcept override { uri_ = std::string(uri); }

void SetBody(Body &body) noexcept override { body_size_ = body.size(); }

void SetSslOptions(const HttpSslOptions &) noexcept override {}
void AddHeader(nostd::string_view, nostd::string_view) noexcept override {}
void ReplaceHeader(nostd::string_view, nostd::string_view) noexcept override {}
void SetTimeoutMs(std::chrono::milliseconds) noexcept override {}
void SetCompression(const Compression &) noexcept override {}
void EnableLogging(bool) noexcept override {}
void SetRetryPolicy(const RetryPolicy &) noexcept override {}

const std::string &GetMethod() const { return method_; }
const std::string &GetUri() const { return uri_; }
std::size_t GetBodySize() const { return body_size_; }

private:
std::string method_;
std::string uri_;
std::size_t body_size_ = 0;
};

class OkResponse : public NoopResponse
{
public:
StatusCode GetStatusCode() const noexcept override { return 200; }
};

class LoggingSession : public Session
{
public:
std::shared_ptr<Request> CreateRequest() noexcept override
{
request_ = std::make_shared<LoggingRequest>();
return request_;
}

void SendRequest(std::shared_ptr<EventHandler> handler) noexcept override
{
if (request_)
{
std::cout << "Custom HTTP client: " << request_->GetMethod() << " " << request_->GetUri()
<< " (" << request_->GetBodySize() << " bytes)\n";
}
if (!handler)
{
return;
}
OkResponse response;
handler->OnResponse(response);
handler->OnEvent(SessionState::Response, {});
std::cout << "Custom HTTP client: export acknowledged\n";
}

bool IsSessionActive() noexcept override { return false; }
bool CancelSession() noexcept override { return true; }
bool FinishSession() noexcept override { return true; }

private:
std::shared_ptr<LoggingRequest> request_;
};

class LoggingHttpClient : public HttpClient
{
public:
std::shared_ptr<Session> CreateSession(nostd::string_view url) noexcept override
{
std::cout << "Custom HTTP client: creating session for " << std::string(url) << "\n";
return std::make_shared<LoggingSession>();
}

bool CancelAllSessions() noexcept override { return true; }
bool FinishAllSessions() noexcept override { return true; }
void SetMaxSessionsPerConnection(std::size_t) noexcept override {}
};

} // namespace

std::shared_ptr<HttpClient> HttpClientFactory::Create()
{
return std::make_shared<LoggingHttpClient>();
}

std::shared_ptr<HttpClient> HttpClientFactory::Create(
const std::shared_ptr<sdk::common::ThreadInstrumentation> &)
{
return std::make_shared<LoggingHttpClient>();
}

std::shared_ptr<HttpClientSync> HttpClientFactory::CreateSync()
{
return nullptr;
}

} // namespace client
} // namespace http
} // namespace ext
OPENTELEMETRY_END_NAMESPACE
32 changes: 32 additions & 0 deletions examples/custom_http_client/main.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0

#include <memory>

#include "opentelemetry/exporters/otlp/otlp_http_exporter_factory.h"
#include "opentelemetry/exporters/otlp/otlp_http_exporter_options.h"
#include "opentelemetry/sdk/trace/simple_processor_factory.h"
#include "opentelemetry/sdk/trace/tracer_provider_factory.h"

namespace trace_sdk = opentelemetry::sdk::trace;
namespace otlp = opentelemetry::exporter::otlp;

int main()
{
otlp::OtlpHttpExporterOptions options;
options.url = "http://localhost:4318/v1/traces";

auto exporter = otlp::OtlpHttpExporterFactory::Create(options);
auto processor = trace_sdk::SimpleSpanProcessorFactory::Create(std::move(exporter));

std::shared_ptr<trace_sdk::TracerProvider> provider =
trace_sdk::TracerProviderFactory::Create(std::move(processor));

auto tracer = provider->GetTracer("custom-http-client-example");
auto span = tracer->StartSpan("custom-span");
span->SetAttribute("example.key", "value");
span->End();

provider->ForceFlush();
return 0;
}
22 changes: 12 additions & 10 deletions examples/http/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
# Copyright The OpenTelemetry Authors
# SPDX-License-Identifier: Apache-2.0

add_executable(http_client client.cc)
add_executable(http_server server.cc)
if(WITH_HTTP_CLIENT_CURL)
add_executable(http_client client.cc)
add_executable(http_server server.cc)

target_link_libraries(
http_client
PRIVATE opentelemetry-cpp::trace opentelemetry-cpp::http_client_curl
opentelemetry-cpp::ostream_span_exporter CURL::libcurl)
target_link_libraries(
http_client
PRIVATE opentelemetry-cpp::trace opentelemetry-cpp::http_client_curl
opentelemetry-cpp::ostream_span_exporter CURL::libcurl)

target_link_libraries(
http_server
PRIVATE opentelemetry-cpp::trace opentelemetry-cpp::http_client_curl
opentelemetry-cpp::ostream_span_exporter)
target_link_libraries(
http_server
PRIVATE opentelemetry-cpp::trace opentelemetry-cpp::http_client_curl
opentelemetry-cpp::ostream_span_exporter)
endif()
9 changes: 8 additions & 1 deletion exporters/elasticsearch/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,16 @@ target_include_directories(

target_link_libraries(
opentelemetry_exporter_elasticsearch_logs
PUBLIC opentelemetry_trace opentelemetry_logs opentelemetry_http_client_curl
PUBLIC opentelemetry_trace opentelemetry_logs
"$<BUILD_INTERFACE:opentelemetry_http_client>"
nlohmann_json::nlohmann_json)

if(WITH_HTTP_CLIENT_CURL)
target_link_libraries(
opentelemetry_exporter_elasticsearch_logs
PUBLIC "$<INSTALL_INTERFACE:opentelemetry_http_client_curl>")
endif()

otel_add_component(
COMPONENT
exporters_elasticsearch
Expand Down
35 changes: 28 additions & 7 deletions exporters/otlp/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -316,15 +316,20 @@ if(WITH_OTLP_HTTP)

target_link_libraries(
opentelemetry_exporter_otlp_http_client
PUBLIC opentelemetry_sdk
opentelemetry_ext
# Links flags of opentelemetry_http_client_curl should be public when
# building internal components
PUBLIC opentelemetry_sdk opentelemetry_ext
"$<BUILD_INTERFACE:opentelemetry_proto>"
PRIVATE "$<INSTALL_INTERFACE:opentelemetry_proto>"
"$<BUILD_INTERFACE:opentelemetry_http_client_curl>"
"$<BUILD_INTERFACE:nlohmann_json::nlohmann_json>"
"$<INSTALL_INTERFACE:opentelemetry_http_client_curl>")
"$<BUILD_INTERFACE:nlohmann_json::nlohmann_json>")

target_link_libraries(
opentelemetry_exporter_otlp_http_client
PRIVATE "$<BUILD_INTERFACE:opentelemetry_http_client>" opentelemetry_common)

if(WITH_HTTP_CLIENT_CURL)
target_link_libraries(
opentelemetry_exporter_otlp_http_client
PRIVATE "$<INSTALL_INTERFACE:opentelemetry_http_client_curl>")
endif()

target_include_directories(
opentelemetry_exporter_otlp_http_client
Expand Down Expand Up @@ -1062,6 +1067,22 @@ if(BUILD_TESTING)
TARGET otlp_http_metric_exporter_factory_test
TEST_PREFIX exporter.otlp.
TEST_LIST otlp_http_metric_exporter_factory_test)

if(NOT WITH_HTTP_CLIENT_CURL)
add_executable(
otlp_http_exporter_custom_client_test
test/otlp_http_exporter_custom_client_test.cc
# Provides HttpClientFactory::Create() without touching the INTERFACE.
test/http_client_factory_nosend_stub.cc)
target_link_libraries(
otlp_http_exporter_custom_client_test ${GTEST_BOTH_LIBRARIES}
${CMAKE_THREAD_LIBS_INIT} ${GMOCK_LIB} opentelemetry_exporter_otlp_http
opentelemetry_http_client_nosend)
gtest_add_tests(
TARGET otlp_http_exporter_custom_client_test
TEST_PREFIX exporter.otlp.
TEST_LIST otlp_http_exporter_custom_client_test)
endif()
endif()

if(WITH_OTLP_FILE)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ class OPENTELEMETRY_EXPORT OtlpHttpExporter final : public opentelemetry::sdk::t
std::unique_ptr<OtlpHttpClient> http_client_;
// For testing
friend class OtlpHttpExporterTestPeer;
friend class OtlpHttpExporterCustomClientTestPeer;
/**
* Create an OtlpHttpExporter using the specified http client.
* Only tests can call this constructor directly.
Expand Down
23 changes: 23 additions & 0 deletions exporters/otlp/test/http_client_factory_nosend_stub.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0

#include "opentelemetry/ext/http/client/http_client_factory.h"
#include "opentelemetry/test_common/ext/http/client/nosend/http_client_nosend.h"

namespace http_client = opentelemetry::ext::http::client;

std::shared_ptr<http_client::HttpClient> http_client::HttpClientFactory::Create()
{
return std::make_shared<http_client::nosend::HttpClient>();
}

std::shared_ptr<http_client::HttpClient> http_client::HttpClientFactory::Create(
const std::shared_ptr<opentelemetry::sdk::common::ThreadInstrumentation> &)
{
return std::make_shared<http_client::nosend::HttpClient>();
}

std::shared_ptr<http_client::HttpClientSync> http_client::HttpClientFactory::CreateSync()
{
return nullptr;
}
Loading
Loading