Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.8/site-packages/google/cloud/logging_v2/services/metrics_service_v2/async_client.py: 48%

118 statements  

« prev     ^ index     » next       coverage.py v7.2.2, created at 2023-03-26 07:30 +0000

1# -*- coding: utf-8 -*- 

2# Copyright 2022 Google LLC 

3# 

4# Licensed under the Apache License, Version 2.0 (the "License"); 

5# you may not use this file except in compliance with the License. 

6# You may obtain a copy of the License at 

7# 

8# http://www.apache.org/licenses/LICENSE-2.0 

9# 

10# Unless required by applicable law or agreed to in writing, software 

11# distributed under the License is distributed on an "AS IS" BASIS, 

12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 

13# See the License for the specific language governing permissions and 

14# limitations under the License. 

15# 

16from collections import OrderedDict 

17import functools 

18import re 

19from typing import ( 

20 Dict, 

21 Mapping, 

22 MutableMapping, 

23 MutableSequence, 

24 Optional, 

25 Sequence, 

26 Tuple, 

27 Type, 

28 Union, 

29) 

30 

31from google.cloud.logging_v2 import gapic_version as package_version 

32 

33from google.api_core.client_options import ClientOptions 

34from google.api_core import exceptions as core_exceptions 

35from google.api_core import gapic_v1 

36from google.api_core import retry as retries 

37from google.auth import credentials as ga_credentials # type: ignore 

38from google.oauth2 import service_account # type: ignore 

39 

40try: 

41 OptionalRetry = Union[retries.Retry, gapic_v1.method._MethodDefault] 

42except AttributeError: # pragma: NO COVER 

43 OptionalRetry = Union[retries.Retry, object] # type: ignore 

44 

45from google.api import distribution_pb2 # type: ignore 

46from google.api import metric_pb2 # type: ignore 

47from google.cloud.logging_v2.services.metrics_service_v2 import pagers 

48from google.cloud.logging_v2.types import logging_metrics 

49from google.protobuf import timestamp_pb2 # type: ignore 

50from .transports.base import MetricsServiceV2Transport, DEFAULT_CLIENT_INFO 

51from .transports.grpc_asyncio import MetricsServiceV2GrpcAsyncIOTransport 

52from .client import MetricsServiceV2Client 

53 

54 

55class MetricsServiceV2AsyncClient: 

56 """Service for configuring logs-based metrics.""" 

57 

58 _client: MetricsServiceV2Client 

59 

60 DEFAULT_ENDPOINT = MetricsServiceV2Client.DEFAULT_ENDPOINT 

61 DEFAULT_MTLS_ENDPOINT = MetricsServiceV2Client.DEFAULT_MTLS_ENDPOINT 

62 

63 log_metric_path = staticmethod(MetricsServiceV2Client.log_metric_path) 

64 parse_log_metric_path = staticmethod(MetricsServiceV2Client.parse_log_metric_path) 

65 common_billing_account_path = staticmethod( 

66 MetricsServiceV2Client.common_billing_account_path 

67 ) 

68 parse_common_billing_account_path = staticmethod( 

69 MetricsServiceV2Client.parse_common_billing_account_path 

70 ) 

71 common_folder_path = staticmethod(MetricsServiceV2Client.common_folder_path) 

72 parse_common_folder_path = staticmethod( 

73 MetricsServiceV2Client.parse_common_folder_path 

74 ) 

75 common_organization_path = staticmethod( 

76 MetricsServiceV2Client.common_organization_path 

77 ) 

78 parse_common_organization_path = staticmethod( 

79 MetricsServiceV2Client.parse_common_organization_path 

80 ) 

81 common_project_path = staticmethod(MetricsServiceV2Client.common_project_path) 

82 parse_common_project_path = staticmethod( 

83 MetricsServiceV2Client.parse_common_project_path 

84 ) 

85 common_location_path = staticmethod(MetricsServiceV2Client.common_location_path) 

86 parse_common_location_path = staticmethod( 

87 MetricsServiceV2Client.parse_common_location_path 

88 ) 

89 

90 @classmethod 

91 def from_service_account_info(cls, info: dict, *args, **kwargs): 

92 """Creates an instance of this client using the provided credentials 

93 info. 

94 

95 Args: 

96 info (dict): The service account private key info. 

97 args: Additional arguments to pass to the constructor. 

98 kwargs: Additional arguments to pass to the constructor. 

99 

100 Returns: 

101 MetricsServiceV2AsyncClient: The constructed client. 

102 """ 

103 return MetricsServiceV2Client.from_service_account_info.__func__(MetricsServiceV2AsyncClient, info, *args, **kwargs) # type: ignore 

104 

105 @classmethod 

106 def from_service_account_file(cls, filename: str, *args, **kwargs): 

107 """Creates an instance of this client using the provided credentials 

108 file. 

109 

110 Args: 

111 filename (str): The path to the service account private key json 

112 file. 

113 args: Additional arguments to pass to the constructor. 

114 kwargs: Additional arguments to pass to the constructor. 

115 

116 Returns: 

117 MetricsServiceV2AsyncClient: The constructed client. 

118 """ 

119 return MetricsServiceV2Client.from_service_account_file.__func__(MetricsServiceV2AsyncClient, filename, *args, **kwargs) # type: ignore 

120 

121 from_service_account_json = from_service_account_file 

122 

123 @classmethod 

124 def get_mtls_endpoint_and_cert_source( 

125 cls, client_options: Optional[ClientOptions] = None 

126 ): 

127 """Return the API endpoint and client cert source for mutual TLS. 

128 

129 The client cert source is determined in the following order: 

130 (1) if `GOOGLE_API_USE_CLIENT_CERTIFICATE` environment variable is not "true", the 

131 client cert source is None. 

132 (2) if `client_options.client_cert_source` is provided, use the provided one; if the 

133 default client cert source exists, use the default one; otherwise the client cert 

134 source is None. 

135 

136 The API endpoint is determined in the following order: 

137 (1) if `client_options.api_endpoint` if provided, use the provided one. 

138 (2) if `GOOGLE_API_USE_CLIENT_CERTIFICATE` environment variable is "always", use the 

139 default mTLS endpoint; if the environment variable is "never", use the default API 

140 endpoint; otherwise if client cert source exists, use the default mTLS endpoint, otherwise 

141 use the default API endpoint. 

142 

143 More details can be found at https://google.aip.dev/auth/4114. 

144 

145 Args: 

146 client_options (google.api_core.client_options.ClientOptions): Custom options for the 

147 client. Only the `api_endpoint` and `client_cert_source` properties may be used 

148 in this method. 

149 

150 Returns: 

151 Tuple[str, Callable[[], Tuple[bytes, bytes]]]: returns the API endpoint and the 

152 client cert source to use. 

153 

154 Raises: 

155 google.auth.exceptions.MutualTLSChannelError: If any errors happen. 

156 """ 

157 return MetricsServiceV2Client.get_mtls_endpoint_and_cert_source(client_options) # type: ignore 

158 

159 @property 

160 def transport(self) -> MetricsServiceV2Transport: 

161 """Returns the transport used by the client instance. 

162 

163 Returns: 

164 MetricsServiceV2Transport: The transport used by the client instance. 

165 """ 

166 return self._client.transport 

167 

168 get_transport_class = functools.partial( 

169 type(MetricsServiceV2Client).get_transport_class, type(MetricsServiceV2Client) 

170 ) 

171 

172 def __init__( 

173 self, 

174 *, 

175 credentials: Optional[ga_credentials.Credentials] = None, 

176 transport: Union[str, MetricsServiceV2Transport] = "grpc_asyncio", 

177 client_options: Optional[ClientOptions] = None, 

178 client_info: gapic_v1.client_info.ClientInfo = DEFAULT_CLIENT_INFO, 

179 ) -> None: 

180 """Instantiates the metrics service v2 client. 

181 

182 Args: 

183 credentials (Optional[google.auth.credentials.Credentials]): The 

184 authorization credentials to attach to requests. These 

185 credentials identify the application to the service; if none 

186 are specified, the client will attempt to ascertain the 

187 credentials from the environment. 

188 transport (Union[str, ~.MetricsServiceV2Transport]): The 

189 transport to use. If set to None, a transport is chosen 

190 automatically. 

191 client_options (ClientOptions): Custom options for the client. It 

192 won't take effect if a ``transport`` instance is provided. 

193 (1) The ``api_endpoint`` property can be used to override the 

194 default endpoint provided by the client. GOOGLE_API_USE_MTLS_ENDPOINT 

195 environment variable can also be used to override the endpoint: 

196 "always" (always use the default mTLS endpoint), "never" (always 

197 use the default regular endpoint) and "auto" (auto switch to the 

198 default mTLS endpoint if client certificate is present, this is 

199 the default value). However, the ``api_endpoint`` property takes 

200 precedence if provided. 

201 (2) If GOOGLE_API_USE_CLIENT_CERTIFICATE environment variable 

202 is "true", then the ``client_cert_source`` property can be used 

203 to provide client certificate for mutual TLS transport. If 

204 not provided, the default SSL client certificate will be used if 

205 present. If GOOGLE_API_USE_CLIENT_CERTIFICATE is "false" or not 

206 set, no client certificate will be used. 

207 

208 Raises: 

209 google.auth.exceptions.MutualTlsChannelError: If mutual TLS transport 

210 creation failed for any reason. 

211 """ 

212 self._client = MetricsServiceV2Client( 

213 credentials=credentials, 

214 transport=transport, 

215 client_options=client_options, 

216 client_info=client_info, 

217 ) 

218 

219 async def list_log_metrics( 

220 self, 

221 request: Optional[Union[logging_metrics.ListLogMetricsRequest, dict]] = None, 

222 *, 

223 parent: Optional[str] = None, 

224 retry: OptionalRetry = gapic_v1.method.DEFAULT, 

225 timeout: Union[float, object] = gapic_v1.method.DEFAULT, 

226 metadata: Sequence[Tuple[str, str]] = (), 

227 ) -> pagers.ListLogMetricsAsyncPager: 

228 r"""Lists logs-based metrics. 

229 

230 .. code-block:: python 

231 

232 # This snippet has been automatically generated and should be regarded as a 

233 # code template only. 

234 # It will require modifications to work: 

235 # - It may require correct/in-range values for request initialization. 

236 # - It may require specifying regional endpoints when creating the service 

237 # client as shown in: 

238 # https://googleapis.dev/python/google-api-core/latest/client_options.html 

239 from google.cloud import logging_v2 

240 

241 async def sample_list_log_metrics(): 

242 # Create a client 

243 client = logging_v2.MetricsServiceV2AsyncClient() 

244 

245 # Initialize request argument(s) 

246 request = logging_v2.ListLogMetricsRequest( 

247 parent="parent_value", 

248 ) 

249 

250 # Make the request 

251 page_result = client.list_log_metrics(request=request) 

252 

253 # Handle the response 

254 async for response in page_result: 

255 print(response) 

256 

257 Args: 

258 request (Optional[Union[google.cloud.logging_v2.types.ListLogMetricsRequest, dict]]): 

259 The request object. The parameters to ListLogMetrics. 

260 parent (:class:`str`): 

261 Required. The name of the project containing the 

262 metrics: 

263 

264 :: 

265 

266 "projects/[PROJECT_ID]" 

267 

268 This corresponds to the ``parent`` field 

269 on the ``request`` instance; if ``request`` is provided, this 

270 should not be set. 

271 retry (google.api_core.retry.Retry): Designation of what errors, if any, 

272 should be retried. 

273 timeout (float): The timeout for this request. 

274 metadata (Sequence[Tuple[str, str]]): Strings which should be 

275 sent along with the request as metadata. 

276 

277 Returns: 

278 google.cloud.logging_v2.services.metrics_service_v2.pagers.ListLogMetricsAsyncPager: 

279 Result returned from ListLogMetrics. 

280 Iterating over this object will yield 

281 results and resolve additional pages 

282 automatically. 

283 

284 """ 

285 # Create or coerce a protobuf request object. 

286 # Quick check: If we got a request object, we should *not* have 

287 # gotten any keyword arguments that map to the request. 

288 has_flattened_params = any([parent]) 

289 if request is not None and has_flattened_params: 

290 raise ValueError( 

291 "If the `request` argument is set, then none of " 

292 "the individual field arguments should be set." 

293 ) 

294 

295 request = logging_metrics.ListLogMetricsRequest(request) 

296 

297 # If we have keyword arguments corresponding to fields on the 

298 # request, apply these. 

299 if parent is not None: 

300 request.parent = parent 

301 

302 # Wrap the RPC method; this adds retry and timeout information, 

303 # and friendly error handling. 

304 rpc = gapic_v1.method_async.wrap_method( 

305 self._client._transport.list_log_metrics, 

306 default_retry=retries.Retry( 

307 initial=0.1, 

308 maximum=60.0, 

309 multiplier=1.3, 

310 predicate=retries.if_exception_type( 

311 core_exceptions.DeadlineExceeded, 

312 core_exceptions.InternalServerError, 

313 core_exceptions.ServiceUnavailable, 

314 ), 

315 deadline=60.0, 

316 ), 

317 default_timeout=60.0, 

318 client_info=DEFAULT_CLIENT_INFO, 

319 ) 

320 

321 # Certain fields should be provided within the metadata header; 

322 # add these here. 

323 metadata = tuple(metadata) + ( 

324 gapic_v1.routing_header.to_grpc_metadata((("parent", request.parent),)), 

325 ) 

326 

327 # Send the request. 

328 response = await rpc( 

329 request, 

330 retry=retry, 

331 timeout=timeout, 

332 metadata=metadata, 

333 ) 

334 

335 # This method is paged; wrap the response in a pager, which provides 

336 # an `__aiter__` convenience method. 

337 response = pagers.ListLogMetricsAsyncPager( 

338 method=rpc, 

339 request=request, 

340 response=response, 

341 metadata=metadata, 

342 ) 

343 

344 # Done; return the response. 

345 return response 

346 

347 async def get_log_metric( 

348 self, 

349 request: Optional[Union[logging_metrics.GetLogMetricRequest, dict]] = None, 

350 *, 

351 metric_name: Optional[str] = None, 

352 retry: OptionalRetry = gapic_v1.method.DEFAULT, 

353 timeout: Union[float, object] = gapic_v1.method.DEFAULT, 

354 metadata: Sequence[Tuple[str, str]] = (), 

355 ) -> logging_metrics.LogMetric: 

356 r"""Gets a logs-based metric. 

357 

358 .. code-block:: python 

359 

360 # This snippet has been automatically generated and should be regarded as a 

361 # code template only. 

362 # It will require modifications to work: 

363 # - It may require correct/in-range values for request initialization. 

364 # - It may require specifying regional endpoints when creating the service 

365 # client as shown in: 

366 # https://googleapis.dev/python/google-api-core/latest/client_options.html 

367 from google.cloud import logging_v2 

368 

369 async def sample_get_log_metric(): 

370 # Create a client 

371 client = logging_v2.MetricsServiceV2AsyncClient() 

372 

373 # Initialize request argument(s) 

374 request = logging_v2.GetLogMetricRequest( 

375 metric_name="metric_name_value", 

376 ) 

377 

378 # Make the request 

379 response = await client.get_log_metric(request=request) 

380 

381 # Handle the response 

382 print(response) 

383 

384 Args: 

385 request (Optional[Union[google.cloud.logging_v2.types.GetLogMetricRequest, dict]]): 

386 The request object. The parameters to GetLogMetric. 

387 metric_name (:class:`str`): 

388 Required. The resource name of the desired metric: 

389 

390 :: 

391 

392 "projects/[PROJECT_ID]/metrics/[METRIC_ID]" 

393 

394 This corresponds to the ``metric_name`` field 

395 on the ``request`` instance; if ``request`` is provided, this 

396 should not be set. 

397 retry (google.api_core.retry.Retry): Designation of what errors, if any, 

398 should be retried. 

399 timeout (float): The timeout for this request. 

400 metadata (Sequence[Tuple[str, str]]): Strings which should be 

401 sent along with the request as metadata. 

402 

403 Returns: 

404 google.cloud.logging_v2.types.LogMetric: 

405 Describes a logs-based metric. The 

406 value of the metric is the number of log 

407 entries that match a logs filter in a 

408 given time interval. 

409 Logs-based metrics can also be used to 

410 extract values from logs and create a 

411 distribution of the values. The 

412 distribution records the statistics of 

413 the extracted values along with an 

414 optional histogram of the values as 

415 specified by the bucket options. 

416 

417 """ 

418 # Create or coerce a protobuf request object. 

419 # Quick check: If we got a request object, we should *not* have 

420 # gotten any keyword arguments that map to the request. 

421 has_flattened_params = any([metric_name]) 

422 if request is not None and has_flattened_params: 

423 raise ValueError( 

424 "If the `request` argument is set, then none of " 

425 "the individual field arguments should be set." 

426 ) 

427 

428 request = logging_metrics.GetLogMetricRequest(request) 

429 

430 # If we have keyword arguments corresponding to fields on the 

431 # request, apply these. 

432 if metric_name is not None: 

433 request.metric_name = metric_name 

434 

435 # Wrap the RPC method; this adds retry and timeout information, 

436 # and friendly error handling. 

437 rpc = gapic_v1.method_async.wrap_method( 

438 self._client._transport.get_log_metric, 

439 default_retry=retries.Retry( 

440 initial=0.1, 

441 maximum=60.0, 

442 multiplier=1.3, 

443 predicate=retries.if_exception_type( 

444 core_exceptions.DeadlineExceeded, 

445 core_exceptions.InternalServerError, 

446 core_exceptions.ServiceUnavailable, 

447 ), 

448 deadline=60.0, 

449 ), 

450 default_timeout=60.0, 

451 client_info=DEFAULT_CLIENT_INFO, 

452 ) 

453 

454 # Certain fields should be provided within the metadata header; 

455 # add these here. 

456 metadata = tuple(metadata) + ( 

457 gapic_v1.routing_header.to_grpc_metadata( 

458 (("metric_name", request.metric_name),) 

459 ), 

460 ) 

461 

462 # Send the request. 

463 response = await rpc( 

464 request, 

465 retry=retry, 

466 timeout=timeout, 

467 metadata=metadata, 

468 ) 

469 

470 # Done; return the response. 

471 return response 

472 

473 async def create_log_metric( 

474 self, 

475 request: Optional[Union[logging_metrics.CreateLogMetricRequest, dict]] = None, 

476 *, 

477 parent: Optional[str] = None, 

478 metric: Optional[logging_metrics.LogMetric] = None, 

479 retry: OptionalRetry = gapic_v1.method.DEFAULT, 

480 timeout: Union[float, object] = gapic_v1.method.DEFAULT, 

481 metadata: Sequence[Tuple[str, str]] = (), 

482 ) -> logging_metrics.LogMetric: 

483 r"""Creates a logs-based metric. 

484 

485 .. code-block:: python 

486 

487 # This snippet has been automatically generated and should be regarded as a 

488 # code template only. 

489 # It will require modifications to work: 

490 # - It may require correct/in-range values for request initialization. 

491 # - It may require specifying regional endpoints when creating the service 

492 # client as shown in: 

493 # https://googleapis.dev/python/google-api-core/latest/client_options.html 

494 from google.cloud import logging_v2 

495 

496 async def sample_create_log_metric(): 

497 # Create a client 

498 client = logging_v2.MetricsServiceV2AsyncClient() 

499 

500 # Initialize request argument(s) 

501 metric = logging_v2.LogMetric() 

502 metric.name = "name_value" 

503 metric.filter = "filter_value" 

504 

505 request = logging_v2.CreateLogMetricRequest( 

506 parent="parent_value", 

507 metric=metric, 

508 ) 

509 

510 # Make the request 

511 response = await client.create_log_metric(request=request) 

512 

513 # Handle the response 

514 print(response) 

515 

516 Args: 

517 request (Optional[Union[google.cloud.logging_v2.types.CreateLogMetricRequest, dict]]): 

518 The request object. The parameters to CreateLogMetric. 

519 parent (:class:`str`): 

520 Required. The resource name of the project in which to 

521 create the metric: 

522 

523 :: 

524 

525 "projects/[PROJECT_ID]" 

526 

527 The new metric must be provided in the request. 

528 

529 This corresponds to the ``parent`` field 

530 on the ``request`` instance; if ``request`` is provided, this 

531 should not be set. 

532 metric (:class:`google.cloud.logging_v2.types.LogMetric`): 

533 Required. The new logs-based metric, 

534 which must not have an identifier that 

535 already exists. 

536 

537 This corresponds to the ``metric`` field 

538 on the ``request`` instance; if ``request`` is provided, this 

539 should not be set. 

540 retry (google.api_core.retry.Retry): Designation of what errors, if any, 

541 should be retried. 

542 timeout (float): The timeout for this request. 

543 metadata (Sequence[Tuple[str, str]]): Strings which should be 

544 sent along with the request as metadata. 

545 

546 Returns: 

547 google.cloud.logging_v2.types.LogMetric: 

548 Describes a logs-based metric. The 

549 value of the metric is the number of log 

550 entries that match a logs filter in a 

551 given time interval. 

552 Logs-based metrics can also be used to 

553 extract values from logs and create a 

554 distribution of the values. The 

555 distribution records the statistics of 

556 the extracted values along with an 

557 optional histogram of the values as 

558 specified by the bucket options. 

559 

560 """ 

561 # Create or coerce a protobuf request object. 

562 # Quick check: If we got a request object, we should *not* have 

563 # gotten any keyword arguments that map to the request. 

564 has_flattened_params = any([parent, metric]) 

565 if request is not None and has_flattened_params: 

566 raise ValueError( 

567 "If the `request` argument is set, then none of " 

568 "the individual field arguments should be set." 

569 ) 

570 

571 request = logging_metrics.CreateLogMetricRequest(request) 

572 

573 # If we have keyword arguments corresponding to fields on the 

574 # request, apply these. 

575 if parent is not None: 

576 request.parent = parent 

577 if metric is not None: 

578 request.metric = metric 

579 

580 # Wrap the RPC method; this adds retry and timeout information, 

581 # and friendly error handling. 

582 rpc = gapic_v1.method_async.wrap_method( 

583 self._client._transport.create_log_metric, 

584 default_timeout=60.0, 

585 client_info=DEFAULT_CLIENT_INFO, 

586 ) 

587 

588 # Certain fields should be provided within the metadata header; 

589 # add these here. 

590 metadata = tuple(metadata) + ( 

591 gapic_v1.routing_header.to_grpc_metadata((("parent", request.parent),)), 

592 ) 

593 

594 # Send the request. 

595 response = await rpc( 

596 request, 

597 retry=retry, 

598 timeout=timeout, 

599 metadata=metadata, 

600 ) 

601 

602 # Done; return the response. 

603 return response 

604 

605 async def update_log_metric( 

606 self, 

607 request: Optional[Union[logging_metrics.UpdateLogMetricRequest, dict]] = None, 

608 *, 

609 metric_name: Optional[str] = None, 

610 metric: Optional[logging_metrics.LogMetric] = None, 

611 retry: OptionalRetry = gapic_v1.method.DEFAULT, 

612 timeout: Union[float, object] = gapic_v1.method.DEFAULT, 

613 metadata: Sequence[Tuple[str, str]] = (), 

614 ) -> logging_metrics.LogMetric: 

615 r"""Creates or updates a logs-based metric. 

616 

617 .. code-block:: python 

618 

619 # This snippet has been automatically generated and should be regarded as a 

620 # code template only. 

621 # It will require modifications to work: 

622 # - It may require correct/in-range values for request initialization. 

623 # - It may require specifying regional endpoints when creating the service 

624 # client as shown in: 

625 # https://googleapis.dev/python/google-api-core/latest/client_options.html 

626 from google.cloud import logging_v2 

627 

628 async def sample_update_log_metric(): 

629 # Create a client 

630 client = logging_v2.MetricsServiceV2AsyncClient() 

631 

632 # Initialize request argument(s) 

633 metric = logging_v2.LogMetric() 

634 metric.name = "name_value" 

635 metric.filter = "filter_value" 

636 

637 request = logging_v2.UpdateLogMetricRequest( 

638 metric_name="metric_name_value", 

639 metric=metric, 

640 ) 

641 

642 # Make the request 

643 response = await client.update_log_metric(request=request) 

644 

645 # Handle the response 

646 print(response) 

647 

648 Args: 

649 request (Optional[Union[google.cloud.logging_v2.types.UpdateLogMetricRequest, dict]]): 

650 The request object. The parameters to UpdateLogMetric. 

651 metric_name (:class:`str`): 

652 Required. The resource name of the metric to update: 

653 

654 :: 

655 

656 "projects/[PROJECT_ID]/metrics/[METRIC_ID]" 

657 

658 The updated metric must be provided in the request and 

659 it's ``name`` field must be the same as ``[METRIC_ID]`` 

660 If the metric does not exist in ``[PROJECT_ID]``, then a 

661 new metric is created. 

662 

663 This corresponds to the ``metric_name`` field 

664 on the ``request`` instance; if ``request`` is provided, this 

665 should not be set. 

666 metric (:class:`google.cloud.logging_v2.types.LogMetric`): 

667 Required. The updated metric. 

668 This corresponds to the ``metric`` field 

669 on the ``request`` instance; if ``request`` is provided, this 

670 should not be set. 

671 retry (google.api_core.retry.Retry): Designation of what errors, if any, 

672 should be retried. 

673 timeout (float): The timeout for this request. 

674 metadata (Sequence[Tuple[str, str]]): Strings which should be 

675 sent along with the request as metadata. 

676 

677 Returns: 

678 google.cloud.logging_v2.types.LogMetric: 

679 Describes a logs-based metric. The 

680 value of the metric is the number of log 

681 entries that match a logs filter in a 

682 given time interval. 

683 Logs-based metrics can also be used to 

684 extract values from logs and create a 

685 distribution of the values. The 

686 distribution records the statistics of 

687 the extracted values along with an 

688 optional histogram of the values as 

689 specified by the bucket options. 

690 

691 """ 

692 # Create or coerce a protobuf request object. 

693 # Quick check: If we got a request object, we should *not* have 

694 # gotten any keyword arguments that map to the request. 

695 has_flattened_params = any([metric_name, metric]) 

696 if request is not None and has_flattened_params: 

697 raise ValueError( 

698 "If the `request` argument is set, then none of " 

699 "the individual field arguments should be set." 

700 ) 

701 

702 request = logging_metrics.UpdateLogMetricRequest(request) 

703 

704 # If we have keyword arguments corresponding to fields on the 

705 # request, apply these. 

706 if metric_name is not None: 

707 request.metric_name = metric_name 

708 if metric is not None: 

709 request.metric = metric 

710 

711 # Wrap the RPC method; this adds retry and timeout information, 

712 # and friendly error handling. 

713 rpc = gapic_v1.method_async.wrap_method( 

714 self._client._transport.update_log_metric, 

715 default_retry=retries.Retry( 

716 initial=0.1, 

717 maximum=60.0, 

718 multiplier=1.3, 

719 predicate=retries.if_exception_type( 

720 core_exceptions.DeadlineExceeded, 

721 core_exceptions.InternalServerError, 

722 core_exceptions.ServiceUnavailable, 

723 ), 

724 deadline=60.0, 

725 ), 

726 default_timeout=60.0, 

727 client_info=DEFAULT_CLIENT_INFO, 

728 ) 

729 

730 # Certain fields should be provided within the metadata header; 

731 # add these here. 

732 metadata = tuple(metadata) + ( 

733 gapic_v1.routing_header.to_grpc_metadata( 

734 (("metric_name", request.metric_name),) 

735 ), 

736 ) 

737 

738 # Send the request. 

739 response = await rpc( 

740 request, 

741 retry=retry, 

742 timeout=timeout, 

743 metadata=metadata, 

744 ) 

745 

746 # Done; return the response. 

747 return response 

748 

749 async def delete_log_metric( 

750 self, 

751 request: Optional[Union[logging_metrics.DeleteLogMetricRequest, dict]] = None, 

752 *, 

753 metric_name: Optional[str] = None, 

754 retry: OptionalRetry = gapic_v1.method.DEFAULT, 

755 timeout: Union[float, object] = gapic_v1.method.DEFAULT, 

756 metadata: Sequence[Tuple[str, str]] = (), 

757 ) -> None: 

758 r"""Deletes a logs-based metric. 

759 

760 .. code-block:: python 

761 

762 # This snippet has been automatically generated and should be regarded as a 

763 # code template only. 

764 # It will require modifications to work: 

765 # - It may require correct/in-range values for request initialization. 

766 # - It may require specifying regional endpoints when creating the service 

767 # client as shown in: 

768 # https://googleapis.dev/python/google-api-core/latest/client_options.html 

769 from google.cloud import logging_v2 

770 

771 async def sample_delete_log_metric(): 

772 # Create a client 

773 client = logging_v2.MetricsServiceV2AsyncClient() 

774 

775 # Initialize request argument(s) 

776 request = logging_v2.DeleteLogMetricRequest( 

777 metric_name="metric_name_value", 

778 ) 

779 

780 # Make the request 

781 await client.delete_log_metric(request=request) 

782 

783 Args: 

784 request (Optional[Union[google.cloud.logging_v2.types.DeleteLogMetricRequest, dict]]): 

785 The request object. The parameters to DeleteLogMetric. 

786 metric_name (:class:`str`): 

787 Required. The resource name of the metric to delete: 

788 

789 :: 

790 

791 "projects/[PROJECT_ID]/metrics/[METRIC_ID]" 

792 

793 This corresponds to the ``metric_name`` field 

794 on the ``request`` instance; if ``request`` is provided, this 

795 should not be set. 

796 retry (google.api_core.retry.Retry): Designation of what errors, if any, 

797 should be retried. 

798 timeout (float): The timeout for this request. 

799 metadata (Sequence[Tuple[str, str]]): Strings which should be 

800 sent along with the request as metadata. 

801 """ 

802 # Create or coerce a protobuf request object. 

803 # Quick check: If we got a request object, we should *not* have 

804 # gotten any keyword arguments that map to the request. 

805 has_flattened_params = any([metric_name]) 

806 if request is not None and has_flattened_params: 

807 raise ValueError( 

808 "If the `request` argument is set, then none of " 

809 "the individual field arguments should be set." 

810 ) 

811 

812 request = logging_metrics.DeleteLogMetricRequest(request) 

813 

814 # If we have keyword arguments corresponding to fields on the 

815 # request, apply these. 

816 if metric_name is not None: 

817 request.metric_name = metric_name 

818 

819 # Wrap the RPC method; this adds retry and timeout information, 

820 # and friendly error handling. 

821 rpc = gapic_v1.method_async.wrap_method( 

822 self._client._transport.delete_log_metric, 

823 default_retry=retries.Retry( 

824 initial=0.1, 

825 maximum=60.0, 

826 multiplier=1.3, 

827 predicate=retries.if_exception_type( 

828 core_exceptions.DeadlineExceeded, 

829 core_exceptions.InternalServerError, 

830 core_exceptions.ServiceUnavailable, 

831 ), 

832 deadline=60.0, 

833 ), 

834 default_timeout=60.0, 

835 client_info=DEFAULT_CLIENT_INFO, 

836 ) 

837 

838 # Certain fields should be provided within the metadata header; 

839 # add these here. 

840 metadata = tuple(metadata) + ( 

841 gapic_v1.routing_header.to_grpc_metadata( 

842 (("metric_name", request.metric_name),) 

843 ), 

844 ) 

845 

846 # Send the request. 

847 await rpc( 

848 request, 

849 retry=retry, 

850 timeout=timeout, 

851 metadata=metadata, 

852 ) 

853 

854 async def __aenter__(self): 

855 return self 

856 

857 async def __aexit__(self, exc_type, exc, tb): 

858 await self.transport.close() 

859 

860 

861DEFAULT_CLIENT_INFO = gapic_v1.client_info.ClientInfo( 

862 gapic_version=package_version.__version__ 

863) 

864 

865 

866__all__ = ("MetricsServiceV2AsyncClient",)