|
| 1 | +// Copyright 2025 Google LLC |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// https://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +#ifdef GOOGLE_CLOUD_CPP_BIGTABLE_WITH_OTEL_METRICS |
| 16 | + |
| 17 | +#include "google/cloud/bigtable/internal/metrics.h" |
| 18 | +#include "google/cloud/bigtable/version.h" |
| 19 | +#include "google/cloud/testing_util/validate_metadata.h" |
| 20 | +#include <gmock/gmock.h> |
| 21 | + |
| 22 | +namespace google { |
| 23 | +namespace cloud { |
| 24 | +namespace bigtable_internal { |
| 25 | +GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_BEGIN |
| 26 | +namespace { |
| 27 | + |
| 28 | +using ::google::cloud::testing_util::SetServerMetadata; |
| 29 | + |
| 30 | +using ::testing::Eq; |
| 31 | +using ::testing::Pair; |
| 32 | +using ::testing::UnorderedElementsAre; |
| 33 | + |
| 34 | +TEST(LabelMap, IntoLabelMap) { |
| 35 | + ResourceLabels r{"my-project", "my-instance", "my-table", "my-cluster", |
| 36 | + "my-zone"}; |
| 37 | + DataLabels d{"my-method", "my-streaming", "my-client-name", |
| 38 | + "my-client-uid", "my-app-profile", "my-status"}; |
| 39 | + |
| 40 | + auto label_map = IntoLabelMap(r, d); |
| 41 | + |
| 42 | + EXPECT_THAT( |
| 43 | + label_map, |
| 44 | + UnorderedElementsAre( |
| 45 | + Pair("project_id", "my-project"), Pair("instance", "my-instance"), |
| 46 | + Pair("table", "my-table"), Pair("cluster", "my-cluster"), |
| 47 | + Pair("zone", "my-zone"), Pair("method", "my-method"), |
| 48 | + Pair("streaming", "my-streaming"), |
| 49 | + Pair("client_name", "my-client-name"), |
| 50 | + Pair("client_uid", "my-client-uid"), |
| 51 | + Pair("app_profile", "my-app-profile"), Pair("status", "my-status"))); |
| 52 | +} |
| 53 | + |
| 54 | +TEST(GetResponseParamsFromMetadata, NonEmptyHeader) { |
| 55 | + google::bigtable::v2::ResponseParams expected_response_params; |
| 56 | + expected_response_params.set_cluster_id("my-cluster"); |
| 57 | + expected_response_params.set_zone_id("my-zone"); |
| 58 | + grpc::ClientContext client_context; |
| 59 | + RpcMetadata server_metadata; |
| 60 | + server_metadata.trailers.emplace( |
| 61 | + "x-goog-ext-425905942-bin", expected_response_params.SerializeAsString()); |
| 62 | + SetServerMetadata(client_context, server_metadata); |
| 63 | + |
| 64 | + auto result = GetResponseParamsFromTrailingMetadata(client_context); |
| 65 | + ASSERT_TRUE(result); |
| 66 | + EXPECT_THAT(result->cluster_id(), Eq("my-cluster")); |
| 67 | + EXPECT_THAT(result->zone_id(), Eq("my-zone")); |
| 68 | +} |
| 69 | + |
| 70 | +TEST(GetResponseParamsFromMetadata, EmptyHeader) { |
| 71 | + grpc::ClientContext client_context; |
| 72 | + RpcMetadata server_metadata; |
| 73 | + SetServerMetadata(client_context, server_metadata); |
| 74 | + |
| 75 | + auto result = GetResponseParamsFromTrailingMetadata(client_context); |
| 76 | + EXPECT_FALSE(result); |
| 77 | +} |
| 78 | + |
| 79 | +} // namespace |
| 80 | +GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_END |
| 81 | +} // namespace bigtable_internal |
| 82 | +} // namespace cloud |
| 83 | +} // namespace google |
| 84 | + |
| 85 | +#endif // GOOGLE_CLOUD_CPP_BIGTABLE_WITH_OTEL_METRICS |
0 commit comments