This repository has been archived by the owner on Dec 31, 2023. It is now read-only.
/
_gapic.py
321 lines (272 loc) · 12.4 KB
/
_gapic.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
# Copyright 2017 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""Wrapper for interacting with the Stackdriver Trace API."""
from google.api_core.gapic_v1 import method
from google.cloud._helpers import _datetime_to_pb_timestamp
from google.cloud.trace_v2.gapic import trace_service_client
from google.cloud.trace_v2.proto import trace_pb2
from google.protobuf.json_format import ParseDict
from google.rpc import status_pb2 as google_dot_rpc_dot_status__pb2
from google.protobuf import wrappers_pb2 as google_dot_protobuf_dot_wrappers__pb2
class _TraceAPI(object):
"""
Wrapper to help mapping trace-related APIs.
See
https://cloud.google.com/trace/docs/reference/v2/rpc/google.devtools.
cloudtrace.v2
Args:
gapic (~google.cloud.trace_v2.gapic.trace_service_client.
TraceServiceClient): Required. API object used to make RPCs.
client (~google.cloud.trace_v2.client.Client): Required. The
client that owns this API object.
"""
def __init__(self, gapic_api, client):
self._gapic_api = gapic_api
self.client = client
def batch_write_spans(
self, name, spans, retry=method.DEFAULT, timeout=method.DEFAULT
):
"""
Sends new spans to Stackdriver Trace or updates existing traces. If the
name of a trace that you send matches that of an existing trace, new
spans are added to the existing trace. Attempt to update existing spans
results undefined behavior. If the name does not match, a new trace is
created with given set of spans.
Args:
name (str): Required. Name of the project where the spans belong.
The format is ``projects/PROJECT_ID``.
spans (list[Union[dict, ~google.cloud.trace_v2.types.Span]]): A
collection of spans. If a dict is provided, it must be of the
same form as the protobuf message
:class:`~google.cloud.trace_v2.types.Span`
retry (Optional[google.api_core.retry.Retry]): A retry object used
to retry requests. If ``None`` is specified, requests will not
be retried.
timeout (Optional[float]): The amount of time, in seconds, to wait
for the request to complete. Note that if ``retry`` is
specified, the timeout applies to each individual attempt.
Raises:
google.api_core.exceptions.GoogleAPICallError: If the request
failed for any reason.
google.api_core.exceptions.RetryError: If the request failed due
to a retryable error and retry attempts failed.
ValueError: If the parameters are invalid.
"""
spans_pb_list = []
for span_mapping in spans["spans"]:
span_pb = _dict_mapping_to_pb(span_mapping, "Span")
spans_pb_list.append(span_pb)
self._gapic_api.batch_write_spans(
name=name, spans=spans_pb_list, retry=retry, timeout=timeout
)
def create_span(
self,
name,
span_id,
display_name,
start_time,
end_time,
parent_span_id=None,
attributes=None,
stack_trace=None,
time_events=None,
links=None,
status=None,
same_process_as_parent_span=None,
child_span_count=None,
retry=method.DEFAULT,
timeout=method.DEFAULT,
):
"""
Creates a new Span.
Example:
>>> from google.cloud import trace_v2
>>>
>>> client = trace_v2.TraceServiceClient()
>>>
>>> name = client.span_path('[PROJECT]', '[TRACE]', '[SPAN]')
>>> span_id = ''
>>> display_name = {}
>>> start_time = {}
>>> end_time = {}
>>>
>>> response = client.create_span(name, span_id, display_name,
start_time, end_time)
Args:
name (str): The resource name of the span in the following format:
::
projects/[PROJECT_ID]/traces/[TRACE_ID]/spans/[SPAN_ID]
[TRACE_ID] is a unique identifier for a trace within a project.
[SPAN_ID] is a unique identifier for a span within a trace,
assigned when the span is created.
span_id (str): The [SPAN_ID] portion of the span's resource name.
The ID is a 16-character hexadecimal encoding of an 8-byte
array.
display_name (dict): A description of the span's operation
(up to 128 bytes). Stackdriver Trace displays the description
in the {% dynamic print site_values.console_name %}.
For example, the display name can be a qualified method name
or a file name and a line number where the operation is called.
A best practice is to use the same display name within an
application and at the same call point. This makes it easier to
correlate spans in different traces.
Contains two fields, value is the truncated name,
truncatedByteCount is the number of bytes removed from the
original string. If 0, then the string was not shortened.
start_time (:class:`~datetime.datetime`):
The start time of the span. On the client side, this is the
time kept by the local machine where the span execution starts.
On the server side, this is the time when the server's
application handler starts running.
end_time (:class:`~datetime.datetime`):
The end time of the span. On the client side, this is the time
kept by the local machine where the span execution ends. On the
server side, this is the time when the server application
handler stops running.
parent_span_id (str): The [SPAN_ID] of this span's parent span.
If this is a root span, then this field must be empty.
attributes (dict): A set of attributes on the span. There is a
limit of 32 attributes per span.
stack_trace (dict):
Stack trace captured at the start of the span.
Contains two fields, stackFrames is a list of stack frames in
this call stack, a maximum of 128 frames are allowed per
StackFrame; stackTraceHashId is used to conserve network
bandwidth for duplicate stack traces within a single trace.
time_events (dict):
The included time events. There can be up to 32 annotations
and 128 message events per span.
links (dict): A maximum of 128 links are allowed per Span.
status (dict): An optional final status for this span.
same_process_as_parent_span (bool): A highly recommended but not
required flag that identifies when a trace crosses a process
boundary. True when the parent_span belongs to the same process
as the current span.
child_span_count (int): An optional number of child spans that were
generated while this span was active. If set, allows
implementation to detect missing child spans.
retry (Optional[google.api_core.retry.Retry]): A retry object used
to retry requests. If ``None`` is specified, requests will not
be retried.
timeout (Optional[float]): The amount of time, in seconds, to wait
for the request to complete. Note that if ``retry`` is
specified, the timeout applies to each individual attempt.
Returns:
A :class:`~google.cloud.trace_v2.types.Span` instance.
Raises:
google.api_core.exceptions.GoogleAPICallError: If the request
failed for any reason.
google.api_core.exceptions.RetryError: If the request failed due
to a retryable error and retry attempts failed.
ValueError: If the parameters are invalid.
"""
# Convert the dict type parameters to protobuf
display_name = _dict_mapping_to_pb(display_name, "TruncatableString")
start_time = _datetime_to_pb_timestamp(start_time)
end_time = _datetime_to_pb_timestamp(end_time)
if attributes is not None:
attributes = _span_attrs_to_pb(attributes, "Attributes")
if stack_trace is not None:
stack_trace = _dict_mapping_to_pb(stack_trace, "StackTrace")
if time_events is not None:
time_events = _span_attrs_to_pb(time_events, "TimeEvents")
if links is not None:
links = _span_attrs_to_pb(links, "Links")
if status is not None:
status = _status_mapping_to_pb(status)
if same_process_as_parent_span is not None:
same_process_as_parent_span = _value_to_pb(
same_process_as_parent_span, "BoolValue"
)
if child_span_count is not None:
child_span_count = _value_to_pb(child_span_count, "Int32Value")
return self._gapic_api.create_span(
name=name,
span_id=span_id,
display_name=display_name,
start_time=start_time,
end_time=end_time,
parent_span_id=parent_span_id,
attributes=attributes,
stack_trace=stack_trace,
time_events=time_events,
links=links,
status=status,
same_process_as_parent_span=same_process_as_parent_span,
child_span_count=child_span_count,
)
def _dict_mapping_to_pb(mapping, proto_type):
"""
Convert a dict to protobuf.
Args:
mapping (dict): A dict that needs to be converted to protobuf.
proto_type (str): The type of the Protobuf.
Returns:
An instance of the specified protobuf.
"""
converted_pb = getattr(trace_pb2, proto_type)()
ParseDict(mapping, converted_pb)
return converted_pb
def _span_attrs_to_pb(span_attr, proto_type):
"""
Convert a span attribute dict to protobuf, including Links, Attributes,
TimeEvents.
Args:
span_attr (dict): A dict that needs to be converted to protobuf.
proto_type (str): The type of the Protobuf.
Returns:
An instance of the specified protobuf.
"""
attr_pb = getattr(trace_pb2.Span, proto_type)()
ParseDict(span_attr, attr_pb)
return attr_pb
def _status_mapping_to_pb(status):
"""
Convert a status dict to protobuf.
Args:
status (dict): A status that needs to be converted to protobuf.
Returns:
An instance of the specified protobuf.
"""
status_pb = google_dot_rpc_dot_status__pb2.Status()
ParseDict(status, status_pb)
return status_pb
def _value_to_pb(value, proto_type):
"""
Convert a value to protobuf. e.g. BoolValue, Int32Value.
Args:
value (dict): A dict that needs to be converted to protobuf.
proto_type (str): The type of the Protobuf.
Returns:
An instance of the specified protobuf.
"""
data_type_pb = getattr(google_dot_protobuf_dot_wrappers__pb2, proto_type)()
ParseDict(value, data_type_pb)
return data_type_pb
def make_trace_api(client):
"""
Create an instance of the gapic Trace API.
Args:
client (:class:`~google.cloud.trace_v2.client.Client`): The client
that holds configuration details.
Returns:
A :class:`~google.cloud.trace_v2._gapic._TraceAPI` instance with the
proper configurations.
"""
generated = trace_service_client.TraceServiceClient(
credentials=client._credentials,
client_info=client._client_info,
client_options=client._client_options,
)
return _TraceAPI(generated, client)