1# -*- coding: utf-8 -*-
2# Copyright 2025 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#
16import abc
17from typing import Awaitable, Callable, Dict, Optional, Sequence, Union
18
19from google.cloud.logging_v2 import gapic_version as package_version
20
21import google.auth # type: ignore
22import google.api_core
23from google.api_core import exceptions as core_exceptions
24from google.api_core import gapic_v1
25from google.api_core import retry as retries
26from google.auth import credentials as ga_credentials # type: ignore
27from google.oauth2 import service_account # type: ignore
28import google.protobuf
29
30from google.cloud.logging_v2.types import logging_metrics
31from google.longrunning import operations_pb2 # type: ignore
32from google.protobuf import empty_pb2 # type: ignore
33
34DEFAULT_CLIENT_INFO = gapic_v1.client_info.ClientInfo(
35 gapic_version=package_version.__version__
36)
37
38if hasattr(DEFAULT_CLIENT_INFO, "protobuf_runtime_version"): # pragma: NO COVER
39 DEFAULT_CLIENT_INFO.protobuf_runtime_version = google.protobuf.__version__
40
41
42class MetricsServiceV2Transport(abc.ABC):
43 """Abstract transport class for MetricsServiceV2."""
44
45 AUTH_SCOPES = (
46 "https://www.googleapis.com/auth/cloud-platform",
47 "https://www.googleapis.com/auth/cloud-platform.read-only",
48 "https://www.googleapis.com/auth/logging.admin",
49 "https://www.googleapis.com/auth/logging.read",
50 "https://www.googleapis.com/auth/logging.write",
51 )
52
53 DEFAULT_HOST: str = "logging.googleapis.com"
54
55 def __init__(
56 self,
57 *,
58 host: str = DEFAULT_HOST,
59 credentials: Optional[ga_credentials.Credentials] = None,
60 credentials_file: Optional[str] = None,
61 scopes: Optional[Sequence[str]] = None,
62 quota_project_id: Optional[str] = None,
63 client_info: gapic_v1.client_info.ClientInfo = DEFAULT_CLIENT_INFO,
64 always_use_jwt_access: Optional[bool] = False,
65 api_audience: Optional[str] = None,
66 **kwargs,
67 ) -> None:
68 """Instantiate the transport.
69
70 Args:
71 host (Optional[str]):
72 The hostname to connect to (default: 'logging.googleapis.com').
73 credentials (Optional[google.auth.credentials.Credentials]): The
74 authorization credentials to attach to requests. These
75 credentials identify the application to the service; if none
76 are specified, the client will attempt to ascertain the
77 credentials from the environment.
78 credentials_file (Optional[str]): Deprecated. A file with credentials that can
79 be loaded with :func:`google.auth.load_credentials_from_file`.
80 This argument is mutually exclusive with credentials. This argument will be
81 removed in the next major version of this library.
82 scopes (Optional[Sequence[str]]): A list of scopes.
83 quota_project_id (Optional[str]): An optional project to use for billing
84 and quota.
85 client_info (google.api_core.gapic_v1.client_info.ClientInfo):
86 The client info used to send a user-agent string along with
87 API requests. If ``None``, then default info will be used.
88 Generally, you only need to set this if you're developing
89 your own client library.
90 always_use_jwt_access (Optional[bool]): Whether self signed JWT should
91 be used for service account credentials.
92 """
93
94 scopes_kwargs = {"scopes": scopes, "default_scopes": self.AUTH_SCOPES}
95
96 # Save the scopes.
97 self._scopes = scopes
98 if not hasattr(self, "_ignore_credentials"):
99 self._ignore_credentials: bool = False
100
101 # If no credentials are provided, then determine the appropriate
102 # defaults.
103 if credentials and credentials_file:
104 raise core_exceptions.DuplicateCredentialArgs(
105 "'credentials_file' and 'credentials' are mutually exclusive"
106 )
107
108 if credentials_file is not None:
109 credentials, _ = google.auth.load_credentials_from_file(
110 credentials_file, **scopes_kwargs, quota_project_id=quota_project_id
111 )
112 elif credentials is None and not self._ignore_credentials:
113 credentials, _ = google.auth.default(
114 **scopes_kwargs, quota_project_id=quota_project_id
115 )
116 # Don't apply audience if the credentials file passed from user.
117 if hasattr(credentials, "with_gdch_audience"):
118 credentials = credentials.with_gdch_audience(
119 api_audience if api_audience else host
120 )
121
122 # If the credentials are service account credentials, then always try to use self signed JWT.
123 if (
124 always_use_jwt_access
125 and isinstance(credentials, service_account.Credentials)
126 and hasattr(service_account.Credentials, "with_always_use_jwt_access")
127 ):
128 credentials = credentials.with_always_use_jwt_access(True)
129
130 # Save the credentials.
131 self._credentials = credentials
132
133 # Save the hostname. Default to port 443 (HTTPS) if none is specified.
134 if ":" not in host:
135 host += ":443"
136 self._host = host
137
138 @property
139 def host(self):
140 return self._host
141
142 def _prep_wrapped_messages(self, client_info):
143 # Precompute the wrapped methods.
144 self._wrapped_methods = {
145 self.list_log_metrics: gapic_v1.method.wrap_method(
146 self.list_log_metrics,
147 default_retry=retries.Retry(
148 initial=0.1,
149 maximum=60.0,
150 multiplier=1.3,
151 predicate=retries.if_exception_type(
152 core_exceptions.DeadlineExceeded,
153 core_exceptions.InternalServerError,
154 core_exceptions.ServiceUnavailable,
155 ),
156 deadline=60.0,
157 ),
158 default_timeout=60.0,
159 client_info=client_info,
160 ),
161 self.get_log_metric: gapic_v1.method.wrap_method(
162 self.get_log_metric,
163 default_retry=retries.Retry(
164 initial=0.1,
165 maximum=60.0,
166 multiplier=1.3,
167 predicate=retries.if_exception_type(
168 core_exceptions.DeadlineExceeded,
169 core_exceptions.InternalServerError,
170 core_exceptions.ServiceUnavailable,
171 ),
172 deadline=60.0,
173 ),
174 default_timeout=60.0,
175 client_info=client_info,
176 ),
177 self.create_log_metric: gapic_v1.method.wrap_method(
178 self.create_log_metric,
179 default_timeout=60.0,
180 client_info=client_info,
181 ),
182 self.update_log_metric: gapic_v1.method.wrap_method(
183 self.update_log_metric,
184 default_retry=retries.Retry(
185 initial=0.1,
186 maximum=60.0,
187 multiplier=1.3,
188 predicate=retries.if_exception_type(
189 core_exceptions.DeadlineExceeded,
190 core_exceptions.InternalServerError,
191 core_exceptions.ServiceUnavailable,
192 ),
193 deadline=60.0,
194 ),
195 default_timeout=60.0,
196 client_info=client_info,
197 ),
198 self.delete_log_metric: gapic_v1.method.wrap_method(
199 self.delete_log_metric,
200 default_retry=retries.Retry(
201 initial=0.1,
202 maximum=60.0,
203 multiplier=1.3,
204 predicate=retries.if_exception_type(
205 core_exceptions.DeadlineExceeded,
206 core_exceptions.InternalServerError,
207 core_exceptions.ServiceUnavailable,
208 ),
209 deadline=60.0,
210 ),
211 default_timeout=60.0,
212 client_info=client_info,
213 ),
214 self.cancel_operation: gapic_v1.method.wrap_method(
215 self.cancel_operation,
216 default_timeout=None,
217 client_info=client_info,
218 ),
219 self.get_operation: gapic_v1.method.wrap_method(
220 self.get_operation,
221 default_timeout=None,
222 client_info=client_info,
223 ),
224 self.list_operations: gapic_v1.method.wrap_method(
225 self.list_operations,
226 default_timeout=None,
227 client_info=client_info,
228 ),
229 }
230
231 def close(self):
232 """Closes resources associated with the transport.
233
234 .. warning::
235 Only call this method if the transport is NOT shared
236 with other clients - this may cause errors in other clients!
237 """
238 raise NotImplementedError()
239
240 @property
241 def list_log_metrics(
242 self,
243 ) -> Callable[
244 [logging_metrics.ListLogMetricsRequest],
245 Union[
246 logging_metrics.ListLogMetricsResponse,
247 Awaitable[logging_metrics.ListLogMetricsResponse],
248 ],
249 ]:
250 raise NotImplementedError()
251
252 @property
253 def get_log_metric(
254 self,
255 ) -> Callable[
256 [logging_metrics.GetLogMetricRequest],
257 Union[logging_metrics.LogMetric, Awaitable[logging_metrics.LogMetric]],
258 ]:
259 raise NotImplementedError()
260
261 @property
262 def create_log_metric(
263 self,
264 ) -> Callable[
265 [logging_metrics.CreateLogMetricRequest],
266 Union[logging_metrics.LogMetric, Awaitable[logging_metrics.LogMetric]],
267 ]:
268 raise NotImplementedError()
269
270 @property
271 def update_log_metric(
272 self,
273 ) -> Callable[
274 [logging_metrics.UpdateLogMetricRequest],
275 Union[logging_metrics.LogMetric, Awaitable[logging_metrics.LogMetric]],
276 ]:
277 raise NotImplementedError()
278
279 @property
280 def delete_log_metric(
281 self,
282 ) -> Callable[
283 [logging_metrics.DeleteLogMetricRequest],
284 Union[empty_pb2.Empty, Awaitable[empty_pb2.Empty]],
285 ]:
286 raise NotImplementedError()
287
288 @property
289 def list_operations(
290 self,
291 ) -> Callable[
292 [operations_pb2.ListOperationsRequest],
293 Union[
294 operations_pb2.ListOperationsResponse,
295 Awaitable[operations_pb2.ListOperationsResponse],
296 ],
297 ]:
298 raise NotImplementedError()
299
300 @property
301 def get_operation(
302 self,
303 ) -> Callable[
304 [operations_pb2.GetOperationRequest],
305 Union[operations_pb2.Operation, Awaitable[operations_pb2.Operation]],
306 ]:
307 raise NotImplementedError()
308
309 @property
310 def cancel_operation(
311 self,
312 ) -> Callable[[operations_pb2.CancelOperationRequest], None,]:
313 raise NotImplementedError()
314
315 @property
316 def kind(self) -> str:
317 raise NotImplementedError()
318
319
320__all__ = ("MetricsServiceV2Transport",)