forked from googleapis/google-cloud-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient.py
More file actions
226 lines (205 loc) · 10 KB
/
Copy pathclient.py
File metadata and controls
226 lines (205 loc) · 10 KB
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
# 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.
"""Client for interacting with the Stackdriver Trace API."""
from google.cloud.trace._gapic import make_trace_api
from google.cloud.client import ClientWithProject
class Client(ClientWithProject):
"""
Client to bundle configuration needed for API requests.
Args:
project (str): The project which the client acts on behalf of.
If not passed, falls back to the default inferred from
the environment.
credentials (Optional[:class:`~google.auth.credentials.Credentials`]):
The OAuth2 Credentials to use for this client. If not passed,
falls back to the default inferred from the environment.
"""
SCOPE = ('https://www.googleapis.com/auth/cloud-platform',
'https://www.googleapis.com/auth/trace.append',)
"""The scopes required for authenticating as a Trace consumer."""
_trace_api = None
def __init__(self, project=None, credentials=None):
super(Client, self).__init__(
project=project, credentials=credentials)
@property
def trace_api(self):
"""
Helper for trace-related API calls.
See
https://cloud.google.com/trace/docs/reference/v2/rpc/google.devtools.
cloudtrace.v2
"""
self._trace_api = make_trace_api(self)
return self._trace_api
def batch_write_spans(self,
name,
spans,
retry=None,
timeout=None):
"""
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.
Example:
>>> from google.cloud import trace_v2
>>>
>>> client = trace_v2.Client()
>>>
>>> name = 'projects/[PROJECT_ID]'
>>> spans = {'spans': [{'endTime': '2017-11-21T23:50:58.890768Z',
'spanId': [SPAN_ID],
'startTime': '2017-11-21T23:50:58.890763Z',
'name': 'projects/[PROJECT_ID]/traces/
[TRACE_ID]/spans/[SPAN_ID]',
'displayName': {'value': 'sample span'}}]}
>>>
>>> client.batch_write_spans(name, spans)
Args:
name (str): Optional. Name of the project where the spans belong.
The format is ``projects/PROJECT_ID``.
spans (dict): A collection of 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.
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.
"""
self.trace_api.batch_write_spans(
name=name,
spans=spans,
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=None,
timeout=None):
"""
Creates a new Span.
Example:
>>> from google.cloud import trace_v2
>>>
>>> client = trace_v2.Client()
>>>
>>> name = 'projects/{project}/traces/{trace_id}/spans/{span_id}'.
format('[PROJECT]', '[TRACE_ID]', '[SPAN_ID]')
>>> span_id = '[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.
"""
return self.trace_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)