// // 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 GRPC_SRC_CPP_EXT_GCP_ENVIRONMENT_AUTODETECT_H #define GRPC_SRC_CPP_EXT_GCP_ENVIRONMENT_AUTODETECT_H #include #include #include #include #include #include "absl/base/thread_annotations.h" #include "absl/functional/any_invocable.h" #include "absl/status/status.h" #include #include "src/core/lib/gprpp/sync.h" namespace grpc { namespace internal { absl::Status GcpObservabilityInit(); class EnvironmentAutoDetect { public: struct ResourceType { // For example, "gce_instance", "gke_container", etc. std::string resource_type; // Values for all the labels listed in the associated resource type. std::map labels; }; static EnvironmentAutoDetect& Get(); // Exposed for testing purposes only explicit EnvironmentAutoDetect(std::string project_id); // \a callback will be invoked once the environment is done being detected. void NotifyOnDone(absl::AnyInvocable callback); const ResourceType* resource() { grpc_core::MutexLock lock(&mu_); return resource_.get(); } private: friend absl::Status grpc::internal::GcpObservabilityInit(); // GcpObservabilityInit() is responsible for setting up the singleton with the // project_id. static void Create(std::string project_id); const std::string project_id_; std::shared_ptr event_engine_; grpc_core::Mutex mu_; std::unique_ptr resource_ ABSL_GUARDED_BY(mu_); std::vector> callbacks_ ABSL_GUARDED_BY(mu_); }; } // namespace internal } // namespace grpc #endif // GRPC_SRC_CPP_EXT_GCP_ENVIRONMENT_AUTODETECT_H