|
|
|
//
|
|
|
|
// 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_GCP_OBSERVABILITY_H
|
|
|
|
#define GRPCPP_EXT_GCP_OBSERVABILITY_H
|
|
|
|
|
|
|
|
#include "absl/status/status.h"
|
|
|
|
#include "absl/status/statusor.h"
|
|
|
|
|
|
|
|
#include <grpc/support/port_platform.h>
|
|
|
|
#include <grpcpp/impl/grpc_library.h>
|
|
|
|
|
|
|
|
namespace grpc {
|
|
|
|
|
|
|
|
// GcpObservability objects follow the RAII idiom and help manage the lifetime
|
|
|
|
// of gRPC Observability data exporting to GCP. `GcpObservability::Init()`
|
|
|
|
// should be invoked instead to return an `GcpObservability` instance.
|
|
|
|
// Observability data is flushed at regular intervals, and also when this
|
|
|
|
// instance goes out of scope and its destructor is invoked.
|
|
|
|
class GcpObservability {
|
|
|
|
public:
|
|
|
|
// Initialize GCP Observability for gRPC.
|
|
|
|
// This should be called before any other gRPC operations like creating a
|
|
|
|
// channel, server, credentials etc.
|
|
|
|
// The return value helps determine whether observability was
|
|
|
|
// successfully enabled or not. On success, an object of class `Observability`
|
|
|
|
// is returned. When this object goes out of scope, GCP Observability stats,
|
|
|
|
// tracing and logging data is flushed. On failure, the status message can be
|
|
|
|
// used to determine the cause of failure. It is up to the applications to
|
|
|
|
// either crash on failure, or continue without GCP observability being
|
|
|
|
// enabled. The status codes do not have any special meaning at present, and
|
|
|
|
// users should not make any assumptions based on the status code, other than
|
|
|
|
// a non-OK status code meaning that observability initialization failed.
|
|
|
|
//
|
|
|
|
// The expected usage is to call this at the top (or near the top) in
|
|
|
|
// main(), and let it go out of scope after all RPCs and activities that we
|
|
|
|
// want to observe are done. Please look at
|
|
|
|
// https://github.com/grpc/grpc/blob/master/examples/cpp/gcp_observability/helloworld/greeter_client.cc
|
|
|
|
// and
|
|
|
|
// https://github.com/grpc/grpc/blob/master/examples/cpp/gcp_observability/helloworld/greeter_server.cc
|
|
|
|
// for sample usage.
|
|
|
|
//
|
|
|
|
// It is possible for an initialized GcpObservability object to go out of
|
|
|
|
// scope while RPCs and other gRPC operations are still ongoing. In this case,
|
|
|
|
// GCP Observability tries to flush all observability data collected till that
|
|
|
|
// point.
|
|
|
|
//
|
|
|
|
// Note that this is a blocking call which properly sets up gRPC Observability
|
|
|
|
// to work with GCP and might take a few seconds to return. Similarly, the
|
|
|
|
// destruction of a non-moved-from `Observability` object is also blocking
|
|
|
|
// since it flushes the observability data to GCP.
|
|
|
|
//
|
|
|
|
// As an implementation detail, this properly initializes the OpenCensus stats
|
|
|
|
// and tracing plugin, so applications do not need to perform any additional
|
|
|
|
// gRPC C++ OpenCensus setup/registration to get GCP Observability for gRPC.
|
|
|
|
static absl::StatusOr<GcpObservability> Init();
|
|
|
|
|
|
|
|
GcpObservability() = default;
|
|
|
|
// Move constructor and Move-assignment operator.
|
|
|
|
// The moved-from object will no longer be valid and will not cause GCP
|
|
|
|
// Observability stats, tracing and logging data to flush.
|
|
|
|
GcpObservability(GcpObservability&& other) noexcept;
|
|
|
|
GcpObservability& operator=(GcpObservability&& other) noexcept;
|
|
|
|
|
|
|
|
// Delete copy and copy-assignment operator
|
|
|
|
GcpObservability(const GcpObservability&) = delete;
|
|
|
|
GcpObservability& operator=(const GcpObservability&) = delete;
|
|
|
|
|
|
|
|
private:
|
|
|
|
// Helper class that aids in implementing GCP Observability.
|
|
|
|
// Inheriting from GrpcLibrary makes sure that gRPC is initialized and remains
|
|
|
|
// initialized for the lifetime of GCP Observability. In the future, when gRPC
|
|
|
|
// initialization goes away, we might still want to keep gRPC Event Engine
|
|
|
|
// initialized, just in case, we need to perform some IO operations during
|
|
|
|
// observability close.
|
|
|
|
// Note that the lifetime guarantees are only one way, i.e., GcpObservability
|
|
|
|
// object guarantees that gRPC will not shutdown while the object is still in
|
|
|
|
// scope, but the other way around does not hold true. Even though that is not
|
|
|
|
// the expected usage, GCP Observability can shutdown before gRPC shuts down.
|
|
|
|
// It follows that gRPC should not hold any callbacks from GcpObservability. A
|
|
|
|
// change in this restriction should go through a design review.
|
|
|
|
class GcpObservabilityImpl : private internal::GrpcLibrary {
|
|
|
|
public:
|
|
|
|
~GcpObservabilityImpl() override;
|
|
|
|
};
|
|
|
|
std::unique_ptr<GcpObservabilityImpl> impl_;
|
|
|
|
};
|
|
|
|
|
|
|
|
namespace experimental {
|
|
|
|
// TODO(yashykt): Delete this after the 1.55 release.
|
|
|
|
GRPC_DEPRECATED("Use grpc::GcpObservability::Init() instead.")
|
|
|
|
absl::Status GcpObservabilityInit();
|
|
|
|
GRPC_DEPRECATED("Use grpc::GcpObservability::Init() instead.")
|
|
|
|
void GcpObservabilityClose();
|
|
|
|
} // namespace experimental
|
|
|
|
|
|
|
|
} // namespace grpc
|
|
|
|
|
|
|
|
#endif // GRPCPP_EXT_GCP_OBSERVABILITY_H
|