This commit is contained in:
Yao
2024-12-20 17:49:45 +08:00
parent 86b0363ce1
commit 654d456c7d
7011 changed files with 1705926 additions and 7 deletions

View File

@@ -0,0 +1,106 @@
//
//
// Copyright 2022 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_CALL_METRIC_RECORDER_H
#define GRPCPP_EXT_CALL_METRIC_RECORDER_H
#include <memory>
#include <string>
#include "absl/strings/string_view.h"
#include "absl/types/optional.h"
#include <grpcpp/impl/sync.h>
#include <grpcpp/support/slice.h>
namespace grpc {
namespace experimental {
/// Records call metrics for the purpose of load balancing.
/// During an RPC, call \a ServerContext::ExperimentalGetCallMetricRecorder()
/// method to retrive the recorder for the current call.
class CallMetricRecorder {
public:
virtual ~CallMetricRecorder() = default;
/// Records a call metric measurement for CPU utilization.
/// Multiple calls to this method will override the stored value.
/// Values may be larger than 1.0 when the usage exceeds the reporter
/// dependent notion of soft limits.
/// Values outside of the valid range [0, infy] are ignored.
virtual CallMetricRecorder& RecordCpuUtilizationMetric(double value) = 0;
/// Records a call metric measurement for memory utilization.
/// Multiple calls to this method will override the stored value.
/// Values outside of the valid range [0, 1] are ignored.
virtual CallMetricRecorder& RecordMemoryUtilizationMetric(double value) = 0;
/// Records a call metric measurement for application specific utilization.
/// Multiple calls to this method will override the stored value.
/// Values may be larger than 1.0 when the usage exceeds the reporter
/// dependent notion of soft limits.
/// Values outside of the valid range [0, infy] are ignored.
virtual CallMetricRecorder& RecordApplicationUtilizationMetric(
double value) = 0;
/// Records a call metric measurement for queries per second.
/// Multiple calls to this method will override the stored value.
/// Values outside of the valid range [0, infy) are ignored.
virtual CallMetricRecorder& RecordQpsMetric(double value) = 0;
/// Records a call metric measurement for errors per second.
/// Multiple calls to this method will override the stored value.
/// Values outside of the valid range [0, infy) are ignored.
virtual CallMetricRecorder& RecordEpsMetric(double value) = 0;
/// Records a call metric measurement for utilization.
/// Multiple calls to this method with the same name will
/// override the corresponding stored value. The lifetime of the
/// name string needs to be longer than the lifetime of the RPC
/// itself, since it's going to be sent as trailers after the RPC
/// finishes. It is assumed the strings are common names that
/// are global constants.
/// Values outside of the valid range [0, 1] are ignored.
virtual CallMetricRecorder& RecordUtilizationMetric(string_ref name,
double value) = 0;
/// Records a call metric measurement for request cost.
/// Multiple calls to this method with the same name will
/// override the corresponding stored value. The lifetime of the
/// name string needs to be longer than the lifetime of the RPC
/// itself, since it's going to be sent as trailers after the RPC
/// finishes. It is assumed the strings are common names that
/// are global constants.
virtual CallMetricRecorder& RecordRequestCostMetric(string_ref name,
double value) = 0;
/// Records an application-specific opaque metric measurement.
/// Multiple calls to this method with the same name will
/// override the corresponding stored value. The lifetime of the
/// name string needs to be longer than the lifetime of the RPC
/// itself, since it's going to be sent as trailers after the RPC
/// finishes. It is assumed the strings are common names that
/// are global constants.
virtual CallMetricRecorder& RecordNamedMetric(string_ref name,
double value) = 0;
};
} // namespace experimental
} // namespace grpc
#endif // GRPCPP_EXT_CALL_METRIC_RECORDER_H

View File

@@ -0,0 +1,47 @@
//
//
// Copyright 2016 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_HEALTH_CHECK_SERVICE_SERVER_BUILDER_OPTION_H
#define GRPCPP_EXT_HEALTH_CHECK_SERVICE_SERVER_BUILDER_OPTION_H
#include <memory>
#include <grpcpp/health_check_service_interface.h>
#include <grpcpp/impl/server_builder_option.h>
#include <grpcpp/support/config.h>
namespace grpc {
class HealthCheckServiceServerBuilderOption : public ServerBuilderOption {
public:
/// The ownership of \a hc will be taken and transferred to the grpc server.
/// To explicitly disable default service, pass in a nullptr.
explicit HealthCheckServiceServerBuilderOption(
std::unique_ptr<HealthCheckServiceInterface> hc);
~HealthCheckServiceServerBuilderOption() override {}
void UpdateArguments(ChannelArguments* args) override;
void UpdatePlugins(
std::vector<std::unique_ptr<ServerBuilderPlugin>>* plugins) override;
private:
std::unique_ptr<HealthCheckServiceInterface> hc_;
};
} // namespace grpc
#endif // GRPCPP_EXT_HEALTH_CHECK_SERVICE_SERVER_BUILDER_OPTION_H

View File

@@ -0,0 +1,121 @@
//
//
// 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_SERVER_METRIC_RECORDER_H
#define GRPCPP_EXT_SERVER_METRIC_RECORDER_H
#include <functional>
#include <map>
#include <memory>
#include <grpcpp/impl/sync.h>
#include <grpcpp/support/string_ref.h>
namespace grpc_core {
struct BackendMetricData;
} // namespace grpc_core
namespace grpc {
class BackendMetricState;
namespace experimental {
/// Records server wide metrics to be reported to the client.
/// Server implementation creates an instance and reports server metrics to it,
/// and then passes it to
/// ServerBuilder::experimental_type::EnableCallMetricRecording or
/// experimental::OrcaService that read metrics to include in the report.
class ServerMetricRecorder {
public:
// Factory method. Use this to create.
static std::unique_ptr<ServerMetricRecorder> Create();
/// Records the server CPU utilization in the range [0, infy).
/// Values may be larger than 1.0 when the usage exceeds the reporter
/// dependent notion of soft limits. Values outside of the valid range are
/// rejected. Overrides the stored value when called again with a valid value.
void SetCpuUtilization(double value);
/// Records the server memory utilization in the range [0, 1].
/// Values outside of the valid range are rejected.
/// Overrides the stored value when called again with a valid value.
void SetMemoryUtilization(double value);
/// Records the application specific utilization in the range [0, infy].
/// Values outside of the valid range are rejected.
/// Overrides the stored value when called again with a valid value.
void SetApplicationUtilization(double value);
/// Records number of queries per second to the server in the range [0, infy).
/// Values outside of the valid range are rejected.
/// Overrides the stored value when called again with a valid value.
void SetQps(double value);
/// Records number of errors per second to the server in the range [0, infy).
/// Values outside of the valid range are rejected.
/// Overrides the stored value when called again with a valid value.
void SetEps(double value);
/// Records a named resource utilization value in the range [0, 1].
/// Values outside of the valid range are rejected.
/// Overrides the stored value when called again with the same name.
/// The name string should remain valid while this utilization remains
/// in this recorder. It is assumed that strings are common names that are
/// global constants.
void SetNamedUtilization(string_ref name, double value);
/// Replaces all named resource utilization values. No range validation.
/// The name strings should remain valid while utilization values remain
/// in this recorder. It is assumed that strings are common names that are
/// global constants.
void SetAllNamedUtilization(std::map<string_ref, double> named_utilization);
/// Clears the server CPU utilization if recorded.
void ClearCpuUtilization();
/// Clears the server memory utilization if recorded.
void ClearMemoryUtilization();
/// Clears the application specific utilization if recorded.
void ClearApplicationUtilization();
/// Clears number of queries per second to the server if recorded.
void ClearQps();
/// Clears number of errors per second to the server if recorded.
void ClearEps();
/// Clears a named utilization value if exists.
void ClearNamedUtilization(string_ref name);
private:
// To access GetMetrics().
friend class grpc::BackendMetricState;
friend class OrcaService;
struct BackendMetricDataState;
// No direct creation, use the factory method Create() above.
ServerMetricRecorder();
// Updates the metric state by applying `updater` to the data and incrementing
// the sequence number.
void UpdateBackendMetricDataState(
std::function<void(grpc_core::BackendMetricData*)> updater);
grpc_core::BackendMetricData GetMetrics() const;
// Returned metric data is guaranteed to be identical between two calls if the
// sequence numbers match.
std::shared_ptr<const BackendMetricDataState> GetMetricsIfChanged() const;
mutable grpc::internal::Mutex mu_;
std::shared_ptr<const BackendMetricDataState> metric_state_
ABSL_GUARDED_BY(mu_);
};
} // namespace experimental
} // namespace grpc
#endif // GRPCPP_EXT_SERVER_METRIC_RECORDER_H