Skip to content

Commit 06d3f6f

Browse files
feat: add helper methods metric_descriptor_path and parse_metric_descriptor_path (googleapis#12465)
BEGIN_COMMIT_OVERRIDE chore: Update gapic-generator-python to v1.15.0 END_COMMIT_OVERRIDE - [ ] Regenerate this pull request now. PiperOrigin-RevId: 616192742 Source-Link: googleapis/googleapis@2ac4161 Source-Link: https://github.com/googleapis/googleapis-gen/commit/e57565057c4f28fa0de75c03412195a0ba7e42e1 Copy-Tag: eyJwIjoicGFja2FnZXMvZ29vZ2xlLWNsb3VkLW1vbml0b3JpbmcvLk93bEJvdC55YW1sIiwiaCI6ImU1NzU2NTA1N2M0ZjI4ZmEwZGU3NWMwMzQxMjE5NWEwYmE3ZTQyZTEifQ== --------- Co-authored-by: Owl Bot <gcf-owl-bot[bot]@users.noreply.github.com>
1 parent d60ef17 commit 06d3f6f

3 files changed

Lines changed: 47 additions & 0 deletions

File tree

packages/google-cloud-monitoring/google/cloud/monitoring_v3/services/metric_service/async_client.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,10 @@ class MetricServiceAsyncClient:
7171
_DEFAULT_ENDPOINT_TEMPLATE = MetricServiceClient._DEFAULT_ENDPOINT_TEMPLATE
7272
_DEFAULT_UNIVERSE = MetricServiceClient._DEFAULT_UNIVERSE
7373

74+
metric_descriptor_path = staticmethod(MetricServiceClient.metric_descriptor_path)
75+
parse_metric_descriptor_path = staticmethod(
76+
MetricServiceClient.parse_metric_descriptor_path
77+
)
7478
monitored_resource_descriptor_path = staticmethod(
7579
MetricServiceClient.monitored_resource_descriptor_path
7680
)

packages/google-cloud-monitoring/google/cloud/monitoring_v3/services/metric_service/client.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,26 @@ def transport(self) -> MetricServiceTransport:
187187
"""
188188
return self._transport
189189

190+
@staticmethod
191+
def metric_descriptor_path(
192+
project: str,
193+
metric_descriptor: str,
194+
) -> str:
195+
"""Returns a fully-qualified metric_descriptor string."""
196+
return "projects/{project}/metricDescriptors/{metric_descriptor}".format(
197+
project=project,
198+
metric_descriptor=metric_descriptor,
199+
)
200+
201+
@staticmethod
202+
def parse_metric_descriptor_path(path: str) -> Dict[str, str]:
203+
"""Parses a metric_descriptor path into its component segments."""
204+
m = re.match(
205+
r"^projects/(?P<project>.+?)/metricDescriptors/(?P<metric_descriptor>.+?)$",
206+
path,
207+
)
208+
return m.groupdict() if m else {}
209+
190210
@staticmethod
191211
def monitored_resource_descriptor_path(
192212
project: str,

packages/google-cloud-monitoring/tests/unit/gapic/monitoring_v3/test_metric_service.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4536,6 +4536,29 @@ def test_metric_service_transport_channel_mtls_with_adc(transport_class):
45364536
assert transport.grpc_channel == mock_grpc_channel
45374537

45384538

4539+
def test_metric_descriptor_path():
4540+
project = "squid"
4541+
metric_descriptor = "clam"
4542+
expected = "projects/{project}/metricDescriptors/{metric_descriptor}".format(
4543+
project=project,
4544+
metric_descriptor=metric_descriptor,
4545+
)
4546+
actual = MetricServiceClient.metric_descriptor_path(project, metric_descriptor)
4547+
assert expected == actual
4548+
4549+
4550+
def test_parse_metric_descriptor_path():
4551+
expected = {
4552+
"project": "whelk",
4553+
"metric_descriptor": "octopus",
4554+
}
4555+
path = MetricServiceClient.metric_descriptor_path(**expected)
4556+
4557+
# Check that the path construction is reversible.
4558+
actual = MetricServiceClient.parse_metric_descriptor_path(path)
4559+
assert expected == actual
4560+
4561+
45394562
def test_monitored_resource_descriptor_path():
45404563
project = "oyster"
45414564
monitored_resource_descriptor = "nudibranch"

0 commit comments

Comments
 (0)