-
Notifications
You must be signed in to change notification settings - Fork 628
[EXPORTER] Add custom HTTP client #4071
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
marcalff
merged 41 commits into
open-telemetry:main
from
ltowarek:fix/issue-24-custom-http-client
Jun 12, 2026
Merged
Changes from all commits
Commits
Show all changes
41 commits
Select commit
Hold shift + click to select a range
72f8cce
[EXPORTER] Add WITH_CUSTOM_HTTP_CLIENT option
ltowarek 8fa2072
Fix formatting
ltowarek 87a12da
Add opentelemetry_http_client interface library
ltowarek 135c213
Add example and test
ltowarek 53debbe
Use WITH_HTTP_CLIENT_CURL=OFF
ltowarek 33f0e4d
Improve the custom_http_client example
ltowarek fba51be
Link opentelemetry_exporter_otlp_http_client with CURL
ltowarek 516c51b
Include custom_http_client example only in build-time
ltowarek 235c596
Install http_client
ltowarek b8eb45d
Make HttpClientFactory virtual
ltowarek b424e86
Fix http_client_factory_curl.h
ltowarek 5695f7d
Support HttpClientFactory in logs and metrics
ltowarek db50030
Remove old HttpClientFactory calls
ltowarek ffa0d3e
Merge branch 'main' of github.com:open-telemetry/opentelemetry-cpp in…
ltowarek a790ade
Support options, runtime_options and factory in the Create method
ltowarek 864d706
Add unit tests
ltowarek 316a8ba
Fix warnings
ltowarek e308513
Increase test coverage
ltowarek 2dd13e4
Fix formatting
ltowarek 9c3dd33
Fix Bazel
ltowarek 2c64733
Fix IWYU
ltowarek 6ca4b37
Fix clang-tidy
ltowarek 397cf92
Merge remote-tracking branch 'upstream/main' into fix/issue-24-custom…
ltowarek ad89717
Fix IWYU
ltowarek 9c2035d
Merge branch 'main' of github.com:open-telemetry/opentelemetry-cpp in…
ltowarek da2f405
Merge branch 'main' into fix/issue-24-custom-http-client
dbarker e467bfa
Merge branch 'main' into fix/issue-24-custom-http-client
marcalff 610bfe1
Merge branch 'main' into fix/issue-24-custom-http-client
dbarker 1bd6714
Make GetDefaultHttpClientFactory private
ltowarek ae521e5
Fix ifdef
ltowarek 19d9b57
Merge branch 'main' into fix/issue-24-custom-http-client
marcalff 9016ef2
Fix CI
ltowarek 21f1133
Merge branch 'fix/issue-24-custom-http-client' of github.com:ltowarek…
ltowarek 6d5a8aa
Merge branch 'main' into fix/issue-24-custom-http-client
marcalff bb1c8ce
Merge branch 'main' into fix/issue-24-custom-http-client
marcalff 3447790
Merge branch 'main' into fix/issue-24-custom-http-client
marcalff 5e8bbcb
Merge branch 'main' into fix/issue-24-custom-http-client
marcalff 6c3c771
Merge branch 'main' into fix/issue-24-custom-http-client
marcalff e1b20e3
Add example to the CI
ltowarek 5fa52bc
Fix CI
ltowarek aba37bf
Merge branch 'main' into fix/issue-24-custom-http-client
dbarker File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| # 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-cpp::ext | ||
| opentelemetry-cpp::sdk) | ||
|
|
||
| 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) | ||
|
|
||
| if(BUILD_TESTING) | ||
| add_test(NAME examples.custom_http_client | ||
| COMMAND "$<TARGET_FILE:example_custom_http_client>") | ||
| endif() | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,145 @@ | ||
| // Copyright The OpenTelemetry Authors | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| #include <chrono> | ||
| #include <cstddef> | ||
| #include <iostream> | ||
| #include <memory> | ||
| #include <string> | ||
|
|
||
| #include "opentelemetry/ext/http/client/http_client.h" | ||
| #include "opentelemetry/nostd/string_view.h" | ||
|
|
||
| #include "custom_http_client.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> LoggingHttpClientFactory::Create() | ||
| { | ||
| return std::make_shared<LoggingHttpClient>(); | ||
| } | ||
|
|
||
| std::shared_ptr<HttpClient> LoggingHttpClientFactory::Create( | ||
| const std::shared_ptr<sdk::common::ThreadInstrumentation> &) | ||
| { | ||
| return std::make_shared<LoggingHttpClient>(); | ||
| } | ||
|
|
||
| std::shared_ptr<HttpClientSync> LoggingHttpClientFactory::CreateSync() | ||
| { | ||
| return nullptr; | ||
| } | ||
|
|
||
| } // namespace client | ||
| } // namespace http | ||
| } // namespace ext | ||
| OPENTELEMETRY_END_NAMESPACE |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| // Copyright The OpenTelemetry Authors | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| #pragma once | ||
|
|
||
| #include "opentelemetry/ext/http/client/http_client_factory.h" | ||
| #include "opentelemetry/version.h" | ||
|
|
||
| OPENTELEMETRY_BEGIN_NAMESPACE | ||
| namespace ext | ||
| { | ||
| namespace http | ||
| { | ||
| namespace client | ||
| { | ||
|
|
||
| class LoggingHttpClientFactory : public HttpClientFactory | ||
| { | ||
| public: | ||
| std::shared_ptr<HttpClientSync> CreateSync() override; | ||
| std::shared_ptr<HttpClient> Create() override; | ||
| std::shared_ptr<HttpClient> Create( | ||
| const std::shared_ptr<sdk::common::ThreadInstrumentation> &thread_instrumentation) override; | ||
| }; | ||
|
|
||
| } // namespace client | ||
| } // namespace http | ||
| } // namespace ext | ||
| OPENTELEMETRY_END_NAMESPACE |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,42 @@ | ||
| // Copyright The OpenTelemetry Authors | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| #include <memory> | ||
| #include <string> | ||
| #include <utility> | ||
|
|
||
| #include "custom_http_client.h" | ||
| #include "opentelemetry/exporters/otlp/otlp_http_exporter_factory.h" | ||
| #include "opentelemetry/exporters/otlp/otlp_http_exporter_options.h" | ||
| #include "opentelemetry/sdk/trace/exporter.h" | ||
| #include "opentelemetry/sdk/trace/processor.h" | ||
| #include "opentelemetry/sdk/trace/simple_processor_factory.h" | ||
| #include "opentelemetry/sdk/trace/tracer_provider.h" | ||
| #include "opentelemetry/sdk/trace/tracer_provider_factory.h" | ||
| #include "opentelemetry/trace/span.h" | ||
| #include "opentelemetry/trace/tracer.h" | ||
|
|
||
| namespace trace_sdk = opentelemetry::sdk::trace; | ||
| namespace otlp = opentelemetry::exporter::otlp; | ||
| namespace http_client = opentelemetry::ext::http::client; | ||
|
|
||
| int main() | ||
| { | ||
| otlp::OtlpHttpExporterOptions options; | ||
| options.url = "http://localhost:4318/v1/traces"; | ||
|
|
||
| auto factory = std::make_shared<http_client::LoggingHttpClientFactory>(); | ||
| auto exporter = otlp::OtlpHttpExporterFactory::Create(options, factory); | ||
| 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; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.