Skip to content

Commit b1253a7

Browse files
[SDK] Add credentials option to OTLP gRPC client (#3402) (#3403)
This allows passing custom `ChannelCredentials` when creating gRPC channels, to support authentication protocols that require short-lived tokens.
1 parent 53b153c commit b1253a7

File tree

8 files changed

+67
-4
lines changed

8 files changed

+67
-4
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,12 @@ Increment the:
1818
* [TEST] Test examples in CI with CMake Part 1
1919
[#3449](https://github.com/open-telemetry/opentelemetry-cpp/pull/3449)
2020

21+
* [EXPORTER] Add `credentials` option to OTLP Exporter’s gRPC Client
22+
[#3403](https://github.com/open-telemetry/opentelemetry-cpp/pull/3403)
23+
24+
* To enable with CMake: `-DWITH_OTLP_GRPC_CREDENTIAL_PREVIEW=ON`
25+
* To enable with Bazel: `--@io_opentelemetry_cpp//exporters/otlp:with_otlp_grpc_credential_preview=true`
26+
2127
## [1.21 2025-05-28]
2228

2329
* [BUILD] Remove WITH_ABSEIL

CMakeLists.txt

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,10 @@ option(WITH_OTLP_RETRY_PREVIEW
222222
"Whether to enable experimental retry functionality" OFF)
223223

224224
option(WITH_OTLP_GRPC_SSL_MTLS_PREVIEW
225-
"Whether to enable mTLS support fro gRPC" OFF)
225+
"Whether to enable mTLS support for gRPC" OFF)
226+
227+
option(WITH_OTLP_GRPC_CREDENTIAL_PREVIEW
228+
"Whether to enable gRPC credentials option in OTLP gRPC Exporter" OFF)
226229

227230
option(WITH_OTLP_GRPC "Whether to include the OTLP gRPC exporter in the SDK"
228231
OFF)
@@ -793,6 +796,9 @@ message(
793796
message(STATUS "WITH_REMOVE_METER_PREVIEW: ${WITH_REMOVE_METER_PREVIEW}")
794797
message(
795798
STATUS "WITH_OTLP_GRPC_SSL_MTLS_PREVIEW: ${WITH_OTLP_GRPC_SSL_MTLS_PREVIEW}")
799+
message(
800+
STATUS
801+
"WITH_OTLP_GRPC_CREDENTIAL_PREVIEW: ${WITH_OTLP_GRPC_CREDENTIAL_PREVIEW}")
796802
message(STATUS "WITH_OTLP_RETRY_PREVIEW: ${WITH_OTLP_RETRY_PREVIEW}")
797803
message(STATUS "---------------------------------------------")
798804
message(STATUS "third-party options")

api/CMakeLists.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,11 @@ if(WITH_OTLP_GRPC_SSL_MTLS_PREVIEW)
116116
INTERFACE ENABLE_OTLP_GRPC_SSL_MTLS_PREVIEW)
117117
endif()
118118

119+
if(WITH_OTLP_GRPC_CREDENTIAL_PREVIEW)
120+
target_compile_definitions(opentelemetry_api
121+
INTERFACE ENABLE_OTLP_GRPC_CREDENTIAL_PREVIEW)
122+
endif()
123+
119124
if(WITH_METRICS_EXEMPLAR_PREVIEW)
120125
target_compile_definitions(opentelemetry_api
121126
INTERFACE ENABLE_METRICS_EXEMPLAR_PREVIEW)

ci/do_ci.ps1

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -348,6 +348,7 @@ switch ($action) {
348348
-DWITH_METRICS_EXEMPLAR_PREVIEW=ON `
349349
-DWITH_ASYNC_EXPORT_PREVIEW=ON `
350350
-DWITH_OTLP_GRPC_SSL_MTLS_PREVIEW=ON `
351+
-DWITH_OTLP_GRPC_CREDENTIAL_PREVIEW=ON `
351352
-DWITH_OTLP_RETRY_PREVIEW=ON `
352353
-DWITH_OTLP_GRPC=ON `
353354
-DWITH_OTLP_HTTP=ON `
@@ -453,6 +454,7 @@ switch ($action) {
453454
-DWITH_ETW=ON `
454455
-DOPENTELEMETRY_INSTALL=ON `
455456
-DWITH_OTLP_GRPC_SSL_MTLS_PREVIEW=OFF `
457+
-DWITH_OTLP_GRPC_CREDENTIAL_PREVIEW=OFF `
456458
-DWITH_OTLP_RETRY_PREVIEW=OFF `
457459
-DWITH_OTLP_GRPC=OFF `
458460
-DWITH_OTLP_HTTP=OFF `

ci/do_ci.sh

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ MAKE_COMMAND="make -k -j \$(nproc)"
6969

7070
echo "make command: ${MAKE_COMMAND}"
7171

72-
BAZEL_OPTIONS_DEFAULT="--copt=-DENABLE_METRICS_EXEMPLAR_PREVIEW"
72+
BAZEL_OPTIONS_DEFAULT="--copt=-DENABLE_METRICS_EXEMPLAR_PREVIEW --//exporters/otlp:with_otlp_grpc_credential_preview=true"
7373
BAZEL_OPTIONS="$BAZEL_OPTIONS_DEFAULT"
7474

7575
BAZEL_TEST_OPTIONS="$BAZEL_OPTIONS --test_output=errors"
@@ -381,6 +381,7 @@ elif [[ "$1" == "cmake.exporter.otprotocol.test" ]]; then
381381
-DWITH_OTLP_HTTP=ON \
382382
-DWITH_OTLP_FILE=ON \
383383
-DWITH_OTLP_GRPC_SSL_MTLS_PREVIEW=ON \
384+
-DWITH_OTLP_GRPC_CREDENTIAL_PREVIEW=ON \
384385
-DWITH_OTLP_RETRY_PREVIEW=ON \
385386
"${SRC_DIR}"
386387
grpc_cpp_plugin=`which grpc_cpp_plugin`
@@ -467,6 +468,7 @@ elif [[ "$1" == "cmake.install.test" ]]; then
467468
-DWITH_ASYNC_EXPORT_PREVIEW=ON \
468469
-DWITH_THREAD_INSTRUMENTATION_PREVIEW=ON \
469470
-DWITH_OTLP_GRPC_SSL_MTLS_PREVIEW=ON \
471+
-DWITH_OTLP_GRPC_CREDENTIAL_PREVIEW=ON \
470472
-DWITH_OTLP_RETRY_PREVIEW=ON \
471473
-DWITH_OTLP_GRPC=ON \
472474
-DWITH_OTLP_HTTP=ON \
@@ -534,6 +536,7 @@ elif [[ "$1" == "cmake.fetch_content.test" ]]; then
534536
-DWITH_ASYNC_EXPORT_PREVIEW=ON \
535537
-DWITH_THREAD_INSTRUMENTATION_PREVIEW=ON \
536538
-DWITH_OTLP_GRPC_SSL_MTLS_PREVIEW=ON \
539+
-DWITH_OTLP_GRPC_CREDENTIAL_PREVIEW=ON \
537540
-DWITH_OTLP_RETRY_PREVIEW=ON \
538541
-DWITH_OTLP_GRPC=ON \
539542
-DWITH_OTLP_HTTP=ON \
@@ -592,8 +595,8 @@ elif [[ "$1" == "bazel.no_bzlmod.test" ]]; then
592595
bazel $BAZEL_STARTUP_OPTIONS test --enable_bzlmod=false $BAZEL_TEST_OPTIONS //...
593596
exit 0
594597
elif [[ "$1" == "bazel.test" ]]; then
595-
bazel $BAZEL_STARTUP_OPTIONS build $BAZEL_OPTIONS //...
596-
bazel $BAZEL_STARTUP_OPTIONS test $BAZEL_TEST_OPTIONS //...
598+
bazel $BAZEL_STARTUP_OPTIONS build $BAZEL_OPTIONS $BAZEL_WITH_PREVIEW //...
599+
bazel $BAZEL_STARTUP_OPTIONS test $BAZEL_TEST_OPTIONS $BAZEL_WITH_PREVIEW //...
597600
exit 0
598601
elif [[ "$1" == "bazel.with_async_export.test" ]]; then
599602
bazel $BAZEL_STARTUP_OPTIONS build $BAZEL_OPTIONS_ASYNC //...

exporters/otlp/BUILD

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# Copyright The OpenTelemetry Authors
22
# SPDX-License-Identifier: Apache-2.0
33

4+
load("@bazel_skylib//rules:common_settings.bzl", "bool_flag")
45
load("//bazel:otel_cc_benchmark.bzl", "otel_cc_benchmark")
56

67
package(default_visibility = ["//visibility:public"])
@@ -54,6 +55,10 @@ cc_library(
5455
"include/opentelemetry/exporters/otlp/protobuf_include_prefix.h",
5556
"include/opentelemetry/exporters/otlp/protobuf_include_suffix.h",
5657
],
58+
defines = select({
59+
":enable_otlp_grpc_credential_preview": ["ENABLE_OTLP_GRPC_CREDENTIAL_PREVIEW"],
60+
"//conditions:default": [],
61+
}),
5762
strip_include_prefix = "include",
5863
tags = [
5964
"otlp",
@@ -70,6 +75,18 @@ cc_library(
7075
],
7176
)
7277

78+
config_setting(
79+
name = "enable_otlp_grpc_credential_preview",
80+
flag_values = {
81+
":with_otlp_grpc_credential_preview": "true",
82+
},
83+
)
84+
85+
bool_flag(
86+
name = "with_otlp_grpc_credential_preview",
87+
build_setting_default = False,
88+
)
89+
7390
cc_library(
7491
name = "otlp_grpc_exporter",
7592
srcs = [

exporters/otlp/include/opentelemetry/exporters/otlp/otlp_grpc_client_options.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,14 @@
77
#include "opentelemetry/version.h"
88

99
#include <chrono>
10+
#include <memory>
1011
#include <string>
1112

13+
namespace grpc
14+
{
15+
class ChannelCredentials;
16+
}
17+
1218
OPENTELEMETRY_BEGIN_NAMESPACE
1319
namespace exporter
1420
{
@@ -43,6 +49,11 @@ struct OtlpGrpcClientOptions
4349
std::string ssl_client_cert_string;
4450
#endif
4551

52+
#ifdef ENABLE_OTLP_GRPC_CREDENTIAL_PREVIEW
53+
/** Use custom ChannelCredentials, instead of the SSL options above. */
54+
std::shared_ptr<grpc::ChannelCredentials> credentials;
55+
#endif
56+
4657
/** Export timeout. */
4758
std::chrono::system_clock::duration timeout;
4859

exporters/otlp/src/otlp_grpc_client.cc

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -433,6 +433,19 @@ std::shared_ptr<grpc::Channel> OtlpGrpcClient::MakeChannel(const OtlpGrpcClientO
433433
grpc::CreateCustomChannel(grpc_target, grpc::InsecureChannelCredentials(), grpc_arguments);
434434
}
435435

436+
#ifdef ENABLE_OTLP_GRPC_CREDENTIAL_PREVIEW
437+
if (options.credentials)
438+
{
439+
if (options.use_ssl_credentials)
440+
{
441+
OTEL_INTERNAL_LOG_WARN(
442+
"[OTLP GRPC Client] Both 'credentials' and 'use_ssl_credentials' options are set. "
443+
"The former takes priority.");
444+
}
445+
channel = grpc::CreateCustomChannel(grpc_target, options.credentials, grpc_arguments);
446+
}
447+
#endif // ENABLE_OTLP_GRPC_CREDENTIAL_PREVIEW
448+
436449
return channel;
437450
}
438451

0 commit comments

Comments
 (0)