mirror of https://github.com/grpc/grpc.git
[GSM Observability] Add mesh_id support in injected labels (#34247)
Changes - 1) Change local mesh labels to not be reported on 'started' metrics at all (even those that we know about) to be consistent. (Since xDS labels atleast on the server side would not be available on started metric.) 2) Add mesh_id as a local label that is populated by reading the xDS bootstrap. As part of this, also added a minimal xds bootstrap parsing logic. <!-- If you know who should review your pull request, please assign it to that person, otherwise the pull request would get assigned randomly. If your pull request is for a specific language, please add the appropriate lang label. -->pull/34285/head
parent
07b71b77ee
commit
938d19f63e
11 changed files with 446 additions and 157 deletions
@ -0,0 +1,137 @@ |
||||
//
|
||||
//
|
||||
// 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.
|
||||
//
|
||||
//
|
||||
|
||||
#include "gtest/gtest.h" |
||||
|
||||
#include <grpcpp/grpcpp.h> |
||||
|
||||
#include "src/core/lib/gpr/tmpfile.h" |
||||
#include "src/core/lib/gprpp/env.h" |
||||
#include "src/cpp/ext/gsm/metadata_exchange.h" |
||||
#include "test/core/util/test_config.h" |
||||
|
||||
namespace grpc { |
||||
namespace testing { |
||||
namespace { |
||||
|
||||
class TestScenario { |
||||
public: |
||||
enum class XdsBootstrapSource : std::uint8_t { kFromFile, kFromConfig }; |
||||
|
||||
explicit TestScenario(XdsBootstrapSource bootstrap_source) |
||||
: bootstrap_source_(bootstrap_source) {} |
||||
|
||||
static std::string Name(const ::testing::TestParamInfo<TestScenario>& info) { |
||||
switch (info.param.bootstrap_source_) { |
||||
case TestScenario::XdsBootstrapSource::kFromFile: |
||||
return "BootstrapFromFile"; |
||||
case TestScenario::XdsBootstrapSource::kFromConfig: |
||||
return "BootstrapFromConfig"; |
||||
} |
||||
} |
||||
|
||||
XdsBootstrapSource bootstrap_source() const { return bootstrap_source_; } |
||||
|
||||
private: |
||||
XdsBootstrapSource bootstrap_source_; |
||||
}; |
||||
|
||||
class MeshIdTest : public ::testing::TestWithParam<TestScenario> { |
||||
protected: |
||||
void SetBootstrap(const char* bootstrap) { |
||||
switch (GetParam().bootstrap_source()) { |
||||
case TestScenario::XdsBootstrapSource::kFromFile: { |
||||
ASSERT_EQ(bootstrap_file_name_, nullptr); |
||||
FILE* bootstrap_file = |
||||
gpr_tmpfile("gcp_observability_config", &bootstrap_file_name_); |
||||
fputs(bootstrap, bootstrap_file); |
||||
fclose(bootstrap_file); |
||||
grpc_core::SetEnv("GRPC_XDS_BOOTSTRAP", bootstrap_file_name_); |
||||
break; |
||||
} |
||||
case TestScenario::XdsBootstrapSource::kFromConfig: |
||||
grpc_core::SetEnv("GRPC_XDS_BOOTSTRAP_CONFIG", bootstrap); |
||||
break; |
||||
} |
||||
} |
||||
|
||||
~MeshIdTest() override { |
||||
grpc_core::UnsetEnv("GRPC_GCP_OBSERVABILITY_CONFIG"); |
||||
grpc_core::UnsetEnv("GRPC_XDS_BOOTSTRAP"); |
||||
if (bootstrap_file_name_ != nullptr) { |
||||
remove(bootstrap_file_name_); |
||||
gpr_free(bootstrap_file_name_); |
||||
} |
||||
} |
||||
|
||||
char* bootstrap_file_name_ = nullptr; |
||||
}; |
||||
|
||||
TEST_P(MeshIdTest, Empty) { |
||||
SetBootstrap(""); |
||||
EXPECT_EQ(grpc::internal::GetMeshId(), "unknown"); |
||||
} |
||||
|
||||
TEST_P(MeshIdTest, NothingSet) { |
||||
EXPECT_EQ(grpc::internal::GetMeshId(), "unknown"); |
||||
} |
||||
|
||||
TEST_P(MeshIdTest, BadJson) { |
||||
SetBootstrap("{"); |
||||
EXPECT_EQ(grpc::internal::GetMeshId(), "unknown"); |
||||
} |
||||
|
||||
TEST_P(MeshIdTest, UnexpectedMeshIdFormatType1) { |
||||
SetBootstrap( |
||||
"{\"node\": {\"id\": " |
||||
"\"abcdef\"}}"); |
||||
EXPECT_EQ(grpc::internal::GetMeshId(), "unknown"); |
||||
} |
||||
|
||||
TEST_P(MeshIdTest, UnexpectedMeshIdFormatType2) { |
||||
SetBootstrap( |
||||
"{\"node\": {\"id\": " |
||||
"\"projects/1234567890/networks/mesh-id/nodes/" |
||||
"01234567-89ab-4def-8123-456789abcdef\"}}"); |
||||
EXPECT_EQ(grpc::internal::GetMeshId(), "unknown"); |
||||
} |
||||
|
||||
TEST_P(MeshIdTest, Basic) { |
||||
SetBootstrap( |
||||
"{\"node\": {\"id\": " |
||||
"\"projects/1234567890/networks/mesh:mesh-id/nodes/" |
||||
"01234567-89ab-4def-8123-456789abcdef\"}}"); |
||||
EXPECT_EQ(grpc::internal::GetMeshId(), "mesh-id"); |
||||
} |
||||
|
||||
INSTANTIATE_TEST_SUITE_P( |
||||
MeshId, MeshIdTest, |
||||
::testing::Values( |
||||
TestScenario(TestScenario::XdsBootstrapSource::kFromFile), |
||||
TestScenario(TestScenario::XdsBootstrapSource::kFromConfig)), |
||||
&TestScenario::Name); |
||||
|
||||
} // namespace
|
||||
} // namespace testing
|
||||
} // namespace grpc
|
||||
|
||||
int main(int argc, char** argv) { |
||||
grpc::testing::TestEnvironment env(&argc, argv); |
||||
::testing::InitGoogleTest(&argc, argv); |
||||
return RUN_ALL_TESTS(); |
||||
} |
Loading…
Reference in new issue