Skip to content

Commit 5137900

Browse files
committed
Use service config in grpc arguments
1 parent e608575 commit 5137900

File tree

1 file changed

+42
-0
lines changed

1 file changed

+42
-0
lines changed

exporters/otlp/src/otlp_grpc_client.cc

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#include <atomic>
1313
#include <chrono>
1414
#include <condition_variable>
15+
#include <cstdio>
1516
#include <fstream>
1617
#include <iterator>
1718
#include <memory>
@@ -23,6 +24,7 @@
2324
#include "opentelemetry/common/timestamp.h"
2425
#include "opentelemetry/ext/http/common/url_parser.h"
2526
#include "opentelemetry/nostd/function_ref.h"
27+
#include "opentelemetry/nostd/string_view.h"
2628
#include "opentelemetry/sdk/common/global_log_handler.h"
2729

2830
OPENTELEMETRY_BEGIN_NAMESPACE
@@ -347,6 +349,46 @@ std::shared_ptr<grpc::Channel> OtlpGrpcClient::MakeChannel(const OtlpGrpcClientO
347349
grpc_arguments.SetCompressionAlgorithm(GRPC_COMPRESS_GZIP);
348350
}
349351

352+
if (options.retry_policy_max_attempts > 0U &&
353+
options.retry_policy_initial_backoff > SecondsDecimal::zero() &&
354+
options.retry_policy_max_backoff > SecondsDecimal::zero() &&
355+
options.retry_policy_backoff_multiplier > 0.f)
356+
{
357+
static const auto kServiceConfigJson = opentelemetry::nostd::string_view{R"(
358+
{
359+
"methodConfig": [
360+
{
361+
"name": [{}],
362+
"retryPolicy": {
363+
"maxAttempts": %0000000000u,
364+
"initialBackoff": "%.1fs",
365+
"maxBackoff": "%.1fs",
366+
"backoffMultiplier": %.1f,
367+
"retryableStatusCodes": [
368+
"CANCELLED",
369+
"DEADLINE_EXCEEDED",
370+
"ABORTED",
371+
"OUT_OF_RANGE",
372+
"DATA_LOSS",
373+
"UNAVAILABLE"
374+
]
375+
}
376+
}
377+
]
378+
})"};
379+
380+
// Allocate string with buffer large enough to hold the formatted json config
381+
auto service_config = std::string(kServiceConfigJson.size(), '\0');
382+
// Prior to C++17, need to explicitly cast away constness from `data()` buffer
383+
std::snprintf(const_cast<decltype(service_config)::value_type *>(service_config.data()),
384+
service_config.size(), kServiceConfigJson.data(),
385+
options.retry_policy_max_attempts, options.retry_policy_initial_backoff.count(),
386+
options.retry_policy_max_backoff.count(),
387+
options.retry_policy_backoff_multiplier);
388+
389+
grpc_arguments.SetServiceConfigJSON(service_config);
390+
}
391+
350392
if (options.use_ssl_credentials)
351393
{
352394
grpc::SslCredentialsOptions ssl_opts;

0 commit comments

Comments
 (0)