Skip to content

Commit

Permalink
Add user agent for OTLP http/grpc client (#1657)
Browse files Browse the repository at this point in the history
  • Loading branch information
owent authored Oct 5, 2022
1 parent 1dc810d commit 787d9c1
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

#include "opentelemetry/sdk/common/attribute_utils.h"
#include "opentelemetry/sdk/common/env_variables.h"
#include "opentelemetry/sdk/version/version.h"

#include <algorithm>
#include <chrono>
Expand All @@ -21,6 +22,11 @@ namespace exporter
namespace otlp
{

inline const std::string GetOtlpDefaultUserAgent()
{
return "OTel-OTLP-Exporter-Cpp/" OPENTELEMETRY_SDK_VERSION;
}

inline const std::string GetOtlpDefaultGrpcEndpoint()
{
constexpr char kOtlpTracesEndpointEnv[] = "OTEL_EXPORTER_OTLP_TRACES_ENDPOINT";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ struct OtlpGrpcExporterOptions
std::chrono::system_clock::duration timeout = GetOtlpDefaultTimeout();
// Additional HTTP headers
OtlpHeaders metadata = GetOtlpDefaultHeaders();
// User agent
std::string user_agent = GetOtlpDefaultUserAgent();
};

} // namespace otlp
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,9 @@ struct OtlpHttpClientOptions
// Requests per connections
std::size_t max_requests_per_connection = 8;

// User agent
std::string user_agent = GetOtlpDefaultUserAgent();

inline OtlpHttpClientOptions(nostd::string_view input_url,
HttpRequestContentType input_content_type,
JsonBytesMappingKind input_json_bytes_mapping,
Expand All @@ -80,7 +83,8 @@ struct OtlpHttpClientOptions
std::chrono::system_clock::duration input_timeout,
const OtlpHeaders &input_http_headers,
std::size_t input_concurrent_sessions = 64,
std::size_t input_max_requests_per_connection = 8)
std::size_t input_max_requests_per_connection = 8,
nostd::string_view input_user_agent = GetOtlpDefaultUserAgent())
: url(input_url),
content_type(input_content_type),
json_bytes_mapping(input_json_bytes_mapping),
Expand All @@ -89,7 +93,8 @@ struct OtlpHttpClientOptions
timeout(input_timeout),
http_headers(input_http_headers),
max_concurrent_requests(input_concurrent_sessions),
max_requests_per_connection(input_max_requests_per_connection)
max_requests_per_connection(input_max_requests_per_connection),
user_agent(input_user_agent)
{}
};

Expand Down
8 changes: 6 additions & 2 deletions exporters/otlp/src/otlp_grpc_client.cc
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ std::shared_ptr<grpc::Channel> OtlpGrpcClient::MakeChannel(const OtlpGrpcExporte
}

std::string grpc_target = url.host_ + ":" + std::to_string(static_cast<int>(url.port_));
grpc::ChannelArguments grpc_arguments;
grpc_arguments.SetUserAgentPrefix(options.user_agent);

if (options.use_ssl_credentials)
{
Expand All @@ -67,11 +69,13 @@ std::shared_ptr<grpc::Channel> OtlpGrpcClient::MakeChannel(const OtlpGrpcExporte
{
ssl_opts.pem_root_certs = GetFileContents((options.ssl_credentials_cacert_path).c_str());
}
channel = grpc::CreateChannel(grpc_target, grpc::SslCredentials(ssl_opts));
channel =
grpc::CreateCustomChannel(grpc_target, grpc::SslCredentials(ssl_opts), grpc_arguments);
}
else
{
channel = grpc::CreateChannel(grpc_target, grpc::InsecureChannelCredentials());
channel =
grpc::CreateCustomChannel(grpc_target, grpc::InsecureChannelCredentials(), grpc_arguments);
}

return channel;
Expand Down
1 change: 1 addition & 0 deletions exporters/otlp/src/otlp_http_client.cc
Original file line number Diff line number Diff line change
Expand Up @@ -966,6 +966,7 @@ OtlpHttpClient::createSession(
request->SetMethod(http_client::Method::Post);
request->SetBody(body_vec);
request->ReplaceHeader("Content-Type", content_type);
request->ReplaceHeader("User-Agent", options_.user_agent);

// Returns the created session data
return HttpSessionData{
Expand Down
14 changes: 14 additions & 0 deletions exporters/otlp/test/otlp_http_exporter_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,13 @@ class OtlpHttpExporterTestPeer : public ::testing::Test
EXPECT_EQ("Custom-Header-Value", custom_header->second);
}

auto user_agent_header = mock_session->GetRequest()->headers_.find("User-Agent");
ASSERT_TRUE(user_agent_header != mock_session->GetRequest()->headers_.end());
if (user_agent_header != mock_session->GetRequest()->headers_.end())
{
EXPECT_EQ(GetOtlpDefaultUserAgent(), user_agent_header->second);
}

// let the otlp_http_client to continue
http_client::nosend::Response response;
response.Finish(*callback.get());
Expand Down Expand Up @@ -247,6 +254,13 @@ class OtlpHttpExporterTestPeer : public ::testing::Test
EXPECT_EQ("Custom-Header-Value", custom_header->second);
}

auto user_agent_header = mock_session->GetRequest()->headers_.find("User-Agent");
ASSERT_TRUE(user_agent_header != mock_session->GetRequest()->headers_.end());
if (user_agent_header != mock_session->GetRequest()->headers_.end())
{
EXPECT_EQ(GetOtlpDefaultUserAgent(), user_agent_header->second);
}

// let the otlp_http_client to continue
std::thread async_finish{[callback]() {
std::this_thread::sleep_for(std::chrono::milliseconds(100));
Expand Down

1 comment on commit 787d9c1

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Performance Alert ⚠️

Possible performance regression was detected for benchmark 'OpenTelemetry-cpp sdk Benchmark'.
Benchmark result of this commit is worse than the previous benchmark result exceeding threshold 2.

Benchmark suite Current: 787d9c1 Previous: 1dc810d Ratio
BM_BaselineBuffer/1 8844075.202941895 ns/iter 614246.1643247908 ns/iter 14.40

This comment was automatically generated by workflow using github-action-benchmark.

Please sign in to comment.