[CSM] Create an experimental target (#34381)

pull/34382/head
Yash Tibrewal 2 years ago committed by GitHub
parent b038da5072
commit d670ffa92c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 13
      BUILD
  2. 82
      include/grpcpp/ext/csm_observability.h
  3. 1
      src/cpp/ext/csm/BUILD
  4. 13
      src/cpp/ext/csm/csm_observability.cc
  5. 50
      src/cpp/ext/csm/csm_observability.h
  6. 4
      test/cpp/ext/csm/csm_observability_test.cc

13
BUILD

@ -2391,6 +2391,19 @@ grpc_cc_library(
],
)
# This is an EXPERIMENTAL target subject to change.
grpc_cc_library(
name = "grpcpp_csm_observability",
hdrs = [
"include/grpcpp/ext/csm_observability.h",
],
language = "c++",
tags = ["nofixdeps"],
deps = [
"//src/cpp/ext/csm:csm_observability",
],
)
grpc_cc_library(
name = "work_serializer",
srcs = [

@ -0,0 +1,82 @@
//
//
// Copyright 2023 gRPC authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
//
#ifndef GRPCPP_EXT_CSM_OBSERVABILITY_H
#define GRPCPP_EXT_CSM_OBSERVABILITY_H
#include <grpc/support/port_platform.h>
#include <memory>
#include "absl/functional/any_invocable.h"
#include "absl/status/statusor.h"
#include "absl/strings/string_view.h"
#include "opentelemetry/sdk/metrics/meter_provider.h"
#include "src/cpp/ext/otel/otel_plugin.h"
namespace grpc {
namespace experimental {
// This is a no-op at present, but in the future, this object would be useful
// for performing cleanup.
class CsmObservability {};
class CsmObservabilityBuilder {
public:
CsmObservabilityBuilder& SetMeterProvider(
std::shared_ptr<opentelemetry::sdk::metrics::MeterProvider>
meter_provider);
// Methods to manipulate which instruments are enabled in the OTel Stats
// Plugin. The default set of instruments are -
// grpc.client.attempt.started
// grpc.client.attempt.duration
// grpc.client.attempt.sent_total_compressed_message_size
// grpc.client.attempt.rcvd_total_compressed_message_size
// grpc.server.call.started
// grpc.server.call.duration
// grpc.server.call.sent_total_compressed_message_size
// grpc.server.call.rcvd_total_compressed_message_size
CsmObservabilityBuilder& EnableMetric(absl::string_view metric_name);
CsmObservabilityBuilder& DisableMetric(absl::string_view metric_name);
CsmObservabilityBuilder& DisableAllMetrics();
// If set, \a target_selector is called per channel to decide whether to
// collect metrics on that target or not.
CsmObservabilityBuilder& SetTargetSelector(
absl::AnyInvocable<bool(absl::string_view /*target*/) const>
target_selector);
// If set, \a target_attribute_filter is called per channel to decide whether
// to record the target attribute on client or to replace it with "other".
// This helps reduce the cardinality on metrics in cases where many channels
// are created with different targets in the same binary (which might happen
// for example, if the channel target string uses IP addresses directly).
CsmObservabilityBuilder& SetTargetAttributeFilter(
absl::AnyInvocable<bool(absl::string_view /*target*/) const>
target_attribute_filter);
// Builds the CsmObservability plugin. The return status shows whether
// CsmObservability was successfully enabled or not.
absl::StatusOr<CsmObservability> BuildAndRegister();
private:
internal::OpenTelemetryPluginBuilder builder_;
};
} // namespace experimental
} // namespace grpc
#endif // GRPCPP_EXT_CSM_OBSERVABILITY_H

@ -37,6 +37,7 @@ grpc_cc_library(
hdrs = [
"csm_observability.h",
"metadata_exchange.h",
"//:include/grpcpp/ext/csm_observability.h",
],
external_deps = [
"absl/functional:any_invocable",

@ -20,12 +20,17 @@
#include "src/cpp/ext/csm/csm_observability.h"
#include <memory>
#include <string>
#include <utility>
#include "absl/functional/any_invocable.h"
#include "absl/status/statusor.h"
#include "absl/types/optional.h"
#include "opentelemetry/sdk/metrics/meter_provider.h"
#include <grpc/support/log.h>
#include <grpcpp/ext/csm_observability.h>
#include "src/core/ext/xds/xds_enabled_server.h"
#include "src/core/lib/channel/channel_args.h"
@ -33,7 +38,7 @@
#include "src/cpp/ext/otel/otel_plugin.h"
namespace grpc {
namespace internal {
namespace experimental {
//
// CsmObservabilityBuilder
@ -81,11 +86,15 @@ absl::StatusOr<CsmObservability> CsmObservabilityBuilder::BuildAndRegister() {
builder_.SetServerSelector([](const grpc_core::ChannelArgs& args) {
return args.GetBool(GRPC_ARG_XDS_ENABLED_SERVER).value_or(false);
});
builder_.SetTargetSelector(internal::CsmChannelTargetSelector);
builder_.BuildAndRegisterGlobal();
builder_.SetTargetSelector(CsmChannelTargetSelector);
return CsmObservability();
}
} // namespace experimental
namespace internal {
bool CsmChannelTargetSelector(absl::string_view target) {
auto uri = grpc_core::URI::Parse(target);
if (!uri.ok()) {

@ -21,61 +21,11 @@
#include <grpc/support/port_platform.h>
#include <memory>
#include "absl/functional/any_invocable.h"
#include "absl/status/statusor.h"
#include "absl/strings/string_view.h"
#include "opentelemetry/sdk/metrics/meter_provider.h"
#include "src/cpp/ext/otel/otel_plugin.h"
namespace grpc {
namespace internal {
// This is a no-op at present, but in the future, this object would be useful
// for performing cleanup.
class CsmObservability {};
class CsmObservabilityBuilder {
public:
CsmObservabilityBuilder& SetMeterProvider(
std::shared_ptr<opentelemetry::sdk::metrics::MeterProvider>
meter_provider);
// Methods to manipulate which instruments are enabled in the OTel Stats
// Plugin. The default set of instruments are -
// grpc.client.attempt.started
// grpc.client.attempt.duration
// grpc.client.attempt.sent_total_compressed_message_size
// grpc.client.attempt.rcvd_total_compressed_message_size
// grpc.server.call.started
// grpc.server.call.duration
// grpc.server.call.sent_total_compressed_message_size
// grpc.server.call.rcvd_total_compressed_message_size
CsmObservabilityBuilder& EnableMetric(absl::string_view metric_name);
CsmObservabilityBuilder& DisableMetric(absl::string_view metric_name);
CsmObservabilityBuilder& DisableAllMetrics();
// If set, \a target_selector is called per channel to decide whether to
// collect metrics on that target or not.
CsmObservabilityBuilder& SetTargetSelector(
absl::AnyInvocable<bool(absl::string_view /*target*/) const>
target_selector);
// If set, \a target_attribute_filter is called per channel to decide whether
// to record the target attribute on client or to replace it with "other".
// This helps reduce the cardinality on metrics in cases where many channels
// are created with different targets in the same binary (which might happen
// for example, if the channel target string uses IP addresses directly).
CsmObservabilityBuilder& SetTargetAttributeFilter(
absl::AnyInvocable<bool(absl::string_view /*target*/) const>
target_attribute_filter);
// Builds the CsmObservability plugin. The return status shows whether
// CsmObservability was successfully enabled or not.
absl::StatusOr<CsmObservability> BuildAndRegister();
private:
OpenTelemetryPluginBuilder builder_;
};
// EXPOSED FOR TESTING PURPOSES ONLY
// Returns true if the channel is a CSM channel.
bool CsmChannelTargetSelector(absl::string_view target);

@ -21,6 +21,8 @@
#include "google/cloud/opentelemetry/resource_detector.h"
#include "gtest/gtest.h"
#include <grpcpp/ext/csm_observability.h>
#include "src/core/lib/gprpp/env.h"
#include "test/core/util/test_config.h"
@ -30,7 +32,7 @@ namespace {
TEST(CsmObservabilityBuilderTest, Basic) {
EXPECT_TRUE(
internal::CsmObservabilityBuilder().BuildAndRegister().status().ok());
experimental::CsmObservabilityBuilder().BuildAndRegister().status().ok());
}
TEST(GsmDependencyTest, GoogleCloudOpenTelemetryDependency) {

Loading…
Cancel
Save