Skip to content

[SDK] Add credentials option to OTLP gRPC client (#3402) #3403

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
merged 4 commits into from
Jun 7, 2025
Merged
Show file tree
Hide file tree
Changes from all 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
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,12 @@ Increment the:
* [TEST] Test examples in CI with CMake Part 1
[#3449](https://github.com/open-telemetry/opentelemetry-cpp/pull/3449)

* [EXPORTER] Add `credentials` option to OTLP Exporter’s gRPC Client
[#3403](https://github.com/open-telemetry/opentelemetry-cpp/pull/3403)

* To enable with CMake: `-DWITH_OTLP_GRPC_CREDENTIAL_PREVIEW=ON`
* To enable with Bazel: `--@io_opentelemetry_cpp//exporters/otlp:with_otlp_grpc_credential_preview=true`

## [1.21 2025-05-28]

* [BUILD] Remove WITH_ABSEIL
Expand Down
8 changes: 7 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,10 @@ option(WITH_OTLP_RETRY_PREVIEW
"Whether to enable experimental retry functionality" OFF)

option(WITH_OTLP_GRPC_SSL_MTLS_PREVIEW
"Whether to enable mTLS support fro gRPC" OFF)
"Whether to enable mTLS support for gRPC" OFF)

option(WITH_OTLP_GRPC_CREDENTIAL_PREVIEW
"Whether to enable gRPC credentials option in OTLP gRPC Exporter" OFF)

option(WITH_OTLP_GRPC "Whether to include the OTLP gRPC exporter in the SDK"
OFF)
Expand Down Expand Up @@ -793,6 +796,9 @@ message(
message(STATUS "WITH_REMOVE_METER_PREVIEW: ${WITH_REMOVE_METER_PREVIEW}")
message(
STATUS "WITH_OTLP_GRPC_SSL_MTLS_PREVIEW: ${WITH_OTLP_GRPC_SSL_MTLS_PREVIEW}")
message(
STATUS
"WITH_OTLP_GRPC_CREDENTIAL_PREVIEW: ${WITH_OTLP_GRPC_CREDENTIAL_PREVIEW}")
message(STATUS "WITH_OTLP_RETRY_PREVIEW: ${WITH_OTLP_RETRY_PREVIEW}")
message(STATUS "---------------------------------------------")
message(STATUS "third-party options")
Expand Down
5 changes: 5 additions & 0 deletions api/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,11 @@ if(WITH_OTLP_GRPC_SSL_MTLS_PREVIEW)
INTERFACE ENABLE_OTLP_GRPC_SSL_MTLS_PREVIEW)
endif()

if(WITH_OTLP_GRPC_CREDENTIAL_PREVIEW)
target_compile_definitions(opentelemetry_api
INTERFACE ENABLE_OTLP_GRPC_CREDENTIAL_PREVIEW)
endif()

if(WITH_METRICS_EXEMPLAR_PREVIEW)
target_compile_definitions(opentelemetry_api
INTERFACE ENABLE_METRICS_EXEMPLAR_PREVIEW)
Expand Down
2 changes: 2 additions & 0 deletions ci/do_ci.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -348,6 +348,7 @@ switch ($action) {
-DWITH_METRICS_EXEMPLAR_PREVIEW=ON `
-DWITH_ASYNC_EXPORT_PREVIEW=ON `
-DWITH_OTLP_GRPC_SSL_MTLS_PREVIEW=ON `
-DWITH_OTLP_GRPC_CREDENTIAL_PREVIEW=ON `
-DWITH_OTLP_RETRY_PREVIEW=ON `
-DWITH_OTLP_GRPC=ON `
-DWITH_OTLP_HTTP=ON `
Expand Down Expand Up @@ -453,6 +454,7 @@ switch ($action) {
-DWITH_ETW=ON `
-DOPENTELEMETRY_INSTALL=ON `
-DWITH_OTLP_GRPC_SSL_MTLS_PREVIEW=OFF `
-DWITH_OTLP_GRPC_CREDENTIAL_PREVIEW=OFF `
-DWITH_OTLP_RETRY_PREVIEW=OFF `
-DWITH_OTLP_GRPC=OFF `
-DWITH_OTLP_HTTP=OFF `
Expand Down
9 changes: 6 additions & 3 deletions ci/do_ci.sh
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ MAKE_COMMAND="make -k -j \$(nproc)"

echo "make command: ${MAKE_COMMAND}"

BAZEL_OPTIONS_DEFAULT="--copt=-DENABLE_METRICS_EXEMPLAR_PREVIEW"
BAZEL_OPTIONS_DEFAULT="--copt=-DENABLE_METRICS_EXEMPLAR_PREVIEW --//exporters/otlp:with_otlp_grpc_credential_preview=true"
BAZEL_OPTIONS="$BAZEL_OPTIONS_DEFAULT"

BAZEL_TEST_OPTIONS="$BAZEL_OPTIONS --test_output=errors"
Expand Down Expand Up @@ -381,6 +381,7 @@ elif [[ "$1" == "cmake.exporter.otprotocol.test" ]]; then
-DWITH_OTLP_HTTP=ON \
-DWITH_OTLP_FILE=ON \
-DWITH_OTLP_GRPC_SSL_MTLS_PREVIEW=ON \
-DWITH_OTLP_GRPC_CREDENTIAL_PREVIEW=ON \
-DWITH_OTLP_RETRY_PREVIEW=ON \
"${SRC_DIR}"
grpc_cpp_plugin=`which grpc_cpp_plugin`
Expand Down Expand Up @@ -467,6 +468,7 @@ elif [[ "$1" == "cmake.install.test" ]]; then
-DWITH_ASYNC_EXPORT_PREVIEW=ON \
-DWITH_THREAD_INSTRUMENTATION_PREVIEW=ON \
-DWITH_OTLP_GRPC_SSL_MTLS_PREVIEW=ON \
-DWITH_OTLP_GRPC_CREDENTIAL_PREVIEW=ON \
-DWITH_OTLP_RETRY_PREVIEW=ON \
-DWITH_OTLP_GRPC=ON \
-DWITH_OTLP_HTTP=ON \
Expand Down Expand Up @@ -534,6 +536,7 @@ elif [[ "$1" == "cmake.fetch_content.test" ]]; then
-DWITH_ASYNC_EXPORT_PREVIEW=ON \
-DWITH_THREAD_INSTRUMENTATION_PREVIEW=ON \
-DWITH_OTLP_GRPC_SSL_MTLS_PREVIEW=ON \
-DWITH_OTLP_GRPC_CREDENTIAL_PREVIEW=ON \
-DWITH_OTLP_RETRY_PREVIEW=ON \
-DWITH_OTLP_GRPC=ON \
-DWITH_OTLP_HTTP=ON \
Expand Down Expand Up @@ -592,8 +595,8 @@ elif [[ "$1" == "bazel.no_bzlmod.test" ]]; then
bazel $BAZEL_STARTUP_OPTIONS test --enable_bzlmod=false $BAZEL_TEST_OPTIONS //...
exit 0
elif [[ "$1" == "bazel.test" ]]; then
bazel $BAZEL_STARTUP_OPTIONS build $BAZEL_OPTIONS //...
bazel $BAZEL_STARTUP_OPTIONS test $BAZEL_TEST_OPTIONS //...
bazel $BAZEL_STARTUP_OPTIONS build $BAZEL_OPTIONS $BAZEL_WITH_PREVIEW //...
bazel $BAZEL_STARTUP_OPTIONS test $BAZEL_TEST_OPTIONS $BAZEL_WITH_PREVIEW //...
exit 0
elif [[ "$1" == "bazel.with_async_export.test" ]]; then
bazel $BAZEL_STARTUP_OPTIONS build $BAZEL_OPTIONS_ASYNC //...
Expand Down
17 changes: 17 additions & 0 deletions exporters/otlp/BUILD
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# Copyright The OpenTelemetry Authors
# SPDX-License-Identifier: Apache-2.0

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

package(default_visibility = ["//visibility:public"])
Expand Down Expand Up @@ -54,6 +55,10 @@ cc_library(
"include/opentelemetry/exporters/otlp/protobuf_include_prefix.h",
"include/opentelemetry/exporters/otlp/protobuf_include_suffix.h",
],
defines = select({
":enable_otlp_grpc_credential_preview": ["ENABLE_OTLP_GRPC_CREDENTIAL_PREVIEW"],
"//conditions:default": [],
}),
strip_include_prefix = "include",
tags = [
"otlp",
Expand All @@ -70,6 +75,18 @@ cc_library(
],
)

config_setting(
name = "enable_otlp_grpc_credential_preview",
flag_values = {
":with_otlp_grpc_credential_preview": "true",
},
)

bool_flag(
name = "with_otlp_grpc_credential_preview",
build_setting_default = False,
)

cc_library(
name = "otlp_grpc_exporter",
srcs = [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,14 @@
#include "opentelemetry/version.h"

#include <chrono>
#include <memory>
#include <string>

namespace grpc
{
class ChannelCredentials;
}

OPENTELEMETRY_BEGIN_NAMESPACE
namespace exporter
{
Expand Down Expand Up @@ -43,6 +49,11 @@ struct OtlpGrpcClientOptions
std::string ssl_client_cert_string;
#endif

#ifdef ENABLE_OTLP_GRPC_CREDENTIAL_PREVIEW
/** Use custom ChannelCredentials, instead of the SSL options above. */
std::shared_ptr<grpc::ChannelCredentials> credentials;
#endif

/** Export timeout. */
std::chrono::system_clock::duration timeout;

Expand Down
13 changes: 13 additions & 0 deletions exporters/otlp/src/otlp_grpc_client.cc
Original file line number Diff line number Diff line change
Expand Up @@ -433,6 +433,19 @@ std::shared_ptr<grpc::Channel> OtlpGrpcClient::MakeChannel(const OtlpGrpcClientO
grpc::CreateCustomChannel(grpc_target, grpc::InsecureChannelCredentials(), grpc_arguments);
}

#ifdef ENABLE_OTLP_GRPC_CREDENTIAL_PREVIEW
if (options.credentials)
{
if (options.use_ssl_credentials)
{
OTEL_INTERNAL_LOG_WARN(
"[OTLP GRPC Client] Both 'credentials' and 'use_ssl_credentials' options are set. "
"The former takes priority.");
}
channel = grpc::CreateCustomChannel(grpc_target, options.credentials, grpc_arguments);
}
#endif // ENABLE_OTLP_GRPC_CREDENTIAL_PREVIEW

return channel;
}

Expand Down
Loading