Skip to content

Commit

Permalink
Add google-cloud-monitoring client library support (#22)
Browse files Browse the repository at this point in the history
- Include `google-cloud-monitoring~=2.18` into the requirements
- Add cached `GcpApiManager.monitoring_metric_service` method returning
self-closing `google.cloud.monitoring_v3.MetricServiceClient`
- Add a placeholder subtest
`gamma.csm_observability_test.4_check_monitoring_metric_client` that
checks the monitoring API connection by making an RPC with intentionally
incorrect server-validated argument, and expecting an `InvalidArgument`
response from the server.
  • Loading branch information
sergiitk authored Jan 19, 2024
1 parent 270307a commit 903a880
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 0 deletions.
21 changes: 21 additions & 0 deletions framework/infrastructure/gcp/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

from absl import flags
from google.cloud import secretmanager_v1
import google.cloud.monitoring_v3
from google.longrunning import operations_pb2
from google.protobuf import json_format
from google.rpc import code_pb2
Expand Down Expand Up @@ -175,6 +176,26 @@ def secrets(version: str):

raise NotImplementedError(f"Secret Manager {version} not supported")

@functools.lru_cache(None)
def monitoring_metric_service(self, version: str):
"""Monitoring API Metric Service.
Manages metric descriptors, monitored resource descriptors,
and time series data.
https://cloud.google.com/python/docs/reference/monitoring/latest/google.cloud.monitoring_v3.services.metric_service.MetricServiceClient
"""
client = None
if version == "v3":
# TODO(sergiitk): set client_options arg if staging api is needed.
client = google.cloud.monitoring_v3.MetricServiceClient()

if not client:
raise NotImplementedError(f"Metric Service {version} not supported")

self._exit_stack.enter_context(client)
return client

@functools.lru_cache(None)
def iam(self, version: str) -> discovery.Resource:
"""Identity and Access Management (IAM) API.
Expand Down
1 change: 1 addition & 0 deletions requirements.lock
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ PyYAML==6.0
absl-py==0.15.0
google-api-python-client==1.12.11
google-cloud-secret-manager==2.15.1
google-cloud-monitoring==2.18.0
grpcio==1.57.0
grpcio-health-checking==1.57.0
grpcio-tools==1.57.0
Expand Down
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ PyYAML~=6.0
absl-py~=0.11
google-api-python-client~=1.12
google-cloud-secret-manager~=2.1
google-cloud-monitoring~=2.18
grpcio~=1.57
grpcio-health-checking~=1.57
grpcio-tools~=1.57
Expand Down
24 changes: 24 additions & 0 deletions tests/gamma/csm_observability_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@

from absl import flags
from absl.testing import absltest
from google.api_core import exceptions as gapi_errors
from google.cloud import monitoring_v3

from framework import xds_gamma_testcase
from framework import xds_k8s_testcase
Expand All @@ -29,13 +31,20 @@


class CsmObservabilityTest(xds_gamma_testcase.GammaXdsKubernetesTestCase):
metric_client: monitoring_v3.MetricServiceClient

@staticmethod
def is_supported(config: skips.TestConfig) -> bool:
if config.client_lang == _Lang.CPP and config.server_lang == _Lang.CPP:
# CSM Observability Test is only supported for CPP for now.
return config.version_gte("v1.61.x")
return False

@classmethod
def setUpClass(cls):
super().setUpClass()
cls.metric_client = cls.gcp_api_manager.monitoring_metric_service("v3")

def test_csm_observability(self):
# TODO(sergiitk): [GAMMA] Consider moving out custom gamma
# resource creation out of self.startTestServers()
Expand All @@ -52,6 +61,21 @@ def test_csm_observability(self):
with self.subTest("3_test_server_received_rpcs_from_test_client"):
self.assertSuccessfulRpcs(test_client)

# For now, this just makes a bogus call to ensure metrics client
# connected to the remote API service.
with self.subTest("4_check_monitoring_metric_client"):
with self.assertRaises(gapi_errors.GoogleAPICallError) as cm:
self.metric_client.list_metric_descriptors(
request=monitoring_v3.ListMetricDescriptorsRequest(
name="whatever",
)
)
err = cm.exception
self.assertIsInstance(err, gapi_errors.InvalidArgument)
self.assertIsNotNone(err.grpc_status_code)
self.assertStartsWith(err.message, "Name must begin with")
self.assertEndsWith(err.message, " got: whatever")


if __name__ == "__main__":
absltest.main()

0 comments on commit 903a880

Please sign in to comment.