google-cloud-trace 0.21.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 5c1a9cb5d58e30bc4fba83ba6c1e5e3086b7334d
4
+ data.tar.gz: cfe2ae3c6deff51e73be893b811a24143e8ecc11
5
+ SHA512:
6
+ metadata.gz: ef0e5a93830a6bca11771fda5d855ed0c3e1ed2c7e9b559fb8036ecff6c98396b86fccd868e9f02358278331e50b95b64fe62834abf95fed45389b8f3b184f4a
7
+ data.tar.gz: e40003165b0f933489545d65d98a9e29a976731be8f35d0cee300a8d0c686e0bb151119ed94e91fbc8fe6dcbc8772c52ad0317d16c094dc79a2d8ed8495aec92
data/.yardopts ADDED
@@ -0,0 +1,7 @@
1
+ --no-private
2
+ --title=Stackdriver Trace
3
+ --markup markdown
4
+
5
+ ./lib/**/*.rb
6
+ -
7
+ README.md
data/LICENSE ADDED
@@ -0,0 +1,201 @@
1
+ Apache License
2
+ Version 2.0, January 2004
3
+ http://www.apache.org/licenses/
4
+
5
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
6
+
7
+ 1. Definitions.
8
+
9
+ "License" shall mean the terms and conditions for use, reproduction,
10
+ and distribution as defined by Sections 1 through 9 of this document.
11
+
12
+ "Licensor" shall mean the copyright owner or entity authorized by
13
+ the copyright owner that is granting the License.
14
+
15
+ "Legal Entity" shall mean the union of the acting entity and all
16
+ other entities that control, are controlled by, or are under common
17
+ control with that entity. For the purposes of this definition,
18
+ "control" means (i) the power, direct or indirect, to cause the
19
+ direction or management of such entity, whether by contract or
20
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
21
+ outstanding shares, or (iii) beneficial ownership of such entity.
22
+
23
+ "You" (or "Your") shall mean an individual or Legal Entity
24
+ exercising permissions granted by this License.
25
+
26
+ "Source" form shall mean the preferred form for making modifications,
27
+ including but not limited to software source code, documentation
28
+ source, and configuration files.
29
+
30
+ "Object" form shall mean any form resulting from mechanical
31
+ transformation or translation of a Source form, including but
32
+ not limited to compiled object code, generated documentation,
33
+ and conversions to other media types.
34
+
35
+ "Work" shall mean the work of authorship, whether in Source or
36
+ Object form, made available under the License, as indicated by a
37
+ copyright notice that is included in or attached to the work
38
+ (an example is provided in the Appendix below).
39
+
40
+ "Derivative Works" shall mean any work, whether in Source or Object
41
+ form, that is based on (or derived from) the Work and for which the
42
+ editorial revisions, annotations, elaborations, or other modifications
43
+ represent, as a whole, an original work of authorship. For the purposes
44
+ of this License, Derivative Works shall not include works that remain
45
+ separable from, or merely link (or bind by name) to the interfaces of,
46
+ the Work and Derivative Works thereof.
47
+
48
+ "Contribution" shall mean any work of authorship, including
49
+ the original version of the Work and any modifications or additions
50
+ to that Work or Derivative Works thereof, that is intentionally
51
+ submitted to Licensor for inclusion in the Work by the copyright owner
52
+ or by an individual or Legal Entity authorized to submit on behalf of
53
+ the copyright owner. For the purposes of this definition, "submitted"
54
+ means any form of electronic, verbal, or written communication sent
55
+ to the Licensor or its representatives, including but not limited to
56
+ communication on electronic mailing lists, source code control systems,
57
+ and issue tracking systems that are managed by, or on behalf of, the
58
+ Licensor for the purpose of discussing and improving the Work, but
59
+ excluding communication that is conspicuously marked or otherwise
60
+ designated in writing by the copyright owner as "Not a Contribution."
61
+
62
+ "Contributor" shall mean Licensor and any individual or Legal Entity
63
+ on behalf of whom a Contribution has been received by Licensor and
64
+ subsequently incorporated within the Work.
65
+
66
+ 2. Grant of Copyright License. Subject to the terms and conditions of
67
+ this License, each Contributor hereby grants to You a perpetual,
68
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
69
+ copyright license to reproduce, prepare Derivative Works of,
70
+ publicly display, publicly perform, sublicense, and distribute the
71
+ Work and such Derivative Works in Source or Object form.
72
+
73
+ 3. Grant of Patent License. Subject to the terms and conditions of
74
+ this License, each Contributor hereby grants to You a perpetual,
75
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
76
+ (except as stated in this section) patent license to make, have made,
77
+ use, offer to sell, sell, import, and otherwise transfer the Work,
78
+ where such license applies only to those patent claims licensable
79
+ by such Contributor that are necessarily infringed by their
80
+ Contribution(s) alone or by combination of their Contribution(s)
81
+ with the Work to which such Contribution(s) was submitted. If You
82
+ institute patent litigation against any entity (including a
83
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
84
+ or a Contribution incorporated within the Work constitutes direct
85
+ or contributory patent infringement, then any patent licenses
86
+ granted to You under this License for that Work shall terminate
87
+ as of the date such litigation is filed.
88
+
89
+ 4. Redistribution. You may reproduce and distribute copies of the
90
+ Work or Derivative Works thereof in any medium, with or without
91
+ modifications, and in Source or Object form, provided that You
92
+ meet the following conditions:
93
+
94
+ (a) You must give any other recipients of the Work or
95
+ Derivative Works a copy of this License; and
96
+
97
+ (b) You must cause any modified files to carry prominent notices
98
+ stating that You changed the files; and
99
+
100
+ (c) You must retain, in the Source form of any Derivative Works
101
+ that You distribute, all copyright, patent, trademark, and
102
+ attribution notices from the Source form of the Work,
103
+ excluding those notices that do not pertain to any part of
104
+ the Derivative Works; and
105
+
106
+ (d) If the Work includes a "NOTICE" text file as part of its
107
+ distribution, then any Derivative Works that You distribute must
108
+ include a readable copy of the attribution notices contained
109
+ within such NOTICE file, excluding those notices that do not
110
+ pertain to any part of the Derivative Works, in at least one
111
+ of the following places: within a NOTICE text file distributed
112
+ as part of the Derivative Works; within the Source form or
113
+ documentation, if provided along with the Derivative Works; or,
114
+ within a display generated by the Derivative Works, if and
115
+ wherever such third-party notices normally appear. The contents
116
+ of the NOTICE file are for informational purposes only and
117
+ do not modify the License. You may add Your own attribution
118
+ notices within Derivative Works that You distribute, alongside
119
+ or as an addendum to the NOTICE text from the Work, provided
120
+ that such additional attribution notices cannot be construed
121
+ as modifying the License.
122
+
123
+ You may add Your own copyright statement to Your modifications and
124
+ may provide additional or different license terms and conditions
125
+ for use, reproduction, or distribution of Your modifications, or
126
+ for any such Derivative Works as a whole, provided Your use,
127
+ reproduction, and distribution of the Work otherwise complies with
128
+ the conditions stated in this License.
129
+
130
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
131
+ any Contribution intentionally submitted for inclusion in the Work
132
+ by You to the Licensor shall be under the terms and conditions of
133
+ this License, without any additional terms or conditions.
134
+ Notwithstanding the above, nothing herein shall supersede or modify
135
+ the terms of any separate license agreement you may have executed
136
+ with Licensor regarding such Contributions.
137
+
138
+ 6. Trademarks. This License does not grant permission to use the trade
139
+ names, trademarks, service marks, or product names of the Licensor,
140
+ except as required for reasonable and customary use in describing the
141
+ origin of the Work and reproducing the content of the NOTICE file.
142
+
143
+ 7. Disclaimer of Warranty. Unless required by applicable law or
144
+ agreed to in writing, Licensor provides the Work (and each
145
+ Contributor provides its Contributions) on an "AS IS" BASIS,
146
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
147
+ implied, including, without limitation, any warranties or conditions
148
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
149
+ PARTICULAR PURPOSE. You are solely responsible for determining the
150
+ appropriateness of using or redistributing the Work and assume any
151
+ risks associated with Your exercise of permissions under this License.
152
+
153
+ 8. Limitation of Liability. In no event and under no legal theory,
154
+ whether in tort (including negligence), contract, or otherwise,
155
+ unless required by applicable law (such as deliberate and grossly
156
+ negligent acts) or agreed to in writing, shall any Contributor be
157
+ liable to You for damages, including any direct, indirect, special,
158
+ incidental, or consequential damages of any character arising as a
159
+ result of this License or out of the use or inability to use the
160
+ Work (including but not limited to damages for loss of goodwill,
161
+ work stoppage, computer failure or malfunction, or any and all
162
+ other commercial damages or losses), even if such Contributor
163
+ has been advised of the possibility of such damages.
164
+
165
+ 9. Accepting Warranty or Additional Liability. While redistributing
166
+ the Work or Derivative Works thereof, You may choose to offer,
167
+ and charge a fee for, acceptance of support, warranty, indemnity,
168
+ or other liability obligations and/or rights consistent with this
169
+ License. However, in accepting such obligations, You may act only
170
+ on Your own behalf and on Your sole responsibility, not on behalf
171
+ of any other Contributor, and only if You agree to indemnify,
172
+ defend, and hold each Contributor harmless for any liability
173
+ incurred by, or claims asserted against, such Contributor by reason
174
+ of your accepting any such warranty or additional liability.
175
+
176
+ END OF TERMS AND CONDITIONS
177
+
178
+ APPENDIX: How to apply the Apache License to your work.
179
+
180
+ To apply the Apache License to your work, attach the following
181
+ boilerplate notice, with the fields enclosed by brackets "[]"
182
+ replaced with your own identifying information. (Don't include
183
+ the brackets!) The text should be enclosed in the appropriate
184
+ comment syntax for the file format. We also recommend that a
185
+ file or class name and description of purpose be included on the
186
+ same "printed page" as the copyright notice for easier
187
+ identification within third-party archives.
188
+
189
+ Copyright [yyyy] [name of copyright owner]
190
+
191
+ Licensed under the Apache License, Version 2.0 (the "License");
192
+ you may not use this file except in compliance with the License.
193
+ You may obtain a copy of the License at
194
+
195
+ http://www.apache.org/licenses/LICENSE-2.0
196
+
197
+ Unless required by applicable law or agreed to in writing, software
198
+ distributed under the License is distributed on an "AS IS" BASIS,
199
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
200
+ See the License for the specific language governing permissions and
201
+ limitations under the License.
data/README.md ADDED
@@ -0,0 +1,45 @@
1
+ Stackdriver Trace API for Ruby
2
+ =================================================
3
+
4
+ google-cloud-trace uses [Google API extensions][google-gax] to provide an
5
+ easy-to-use client library for the [Stackdriver Trace API][] (v1) defined in the [googleapis][] git repository
6
+
7
+
8
+ [googleapis]: https://github.com/googleapis/googleapis/tree/master/google/google/devtools/cloudtrace/v1
9
+ [google-gax]: https://github.com/googleapis/gax-ruby
10
+ [Stackdriver Trace API]: https://developers.google.com/apis-explorer/#p/cloudtrace/v1/
11
+
12
+ Getting started
13
+ ---------------
14
+
15
+ google-cloud-trace will allow you to connect to the [Stackdriver Trace API][] and access all its methods.
16
+
17
+ In order to achieve so you need to set up authentication as well as install the library locally.
18
+
19
+
20
+ Setup Authentication
21
+ --------------------
22
+
23
+ To authenticate all your API calls, first install and setup the [Google Cloud SDK][].
24
+ Once done, you can then run the following command in your terminal:
25
+
26
+ $ gcloud beta auth application-default login
27
+
28
+ or
29
+
30
+ $ gcloud auth login
31
+
32
+ Please see [gcloud beta auth application-default login][] document for the difference between these commands.
33
+
34
+ [Google Cloud SDK]: https://cloud.google.com/sdk/
35
+ [gcloud beta auth application-default login]: https://cloud.google.com/sdk/gcloud/reference/beta/auth/application-default/login
36
+
37
+
38
+ Installation
39
+ -------------------
40
+
41
+ Install this library using gem:
42
+
43
+ $ [sudo] gem install google-cloud-trace
44
+
45
+ At this point you are all set to continue.
@@ -0,0 +1,97 @@
1
+ # Copyright 2016 Google Inc. All rights reserved.
2
+ #
3
+ # Licensed under the Apache License, Version 2.0 (the "License");
4
+ # you may not use this file except in compliance with the License.
5
+ # You may obtain a copy of the License at
6
+ #
7
+ # http://www.apache.org/licenses/LICENSE-2.0
8
+ #
9
+ # Unless required by applicable law or agreed to in writing, software
10
+ # distributed under the License is distributed on an "AS IS" BASIS,
11
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ # See the License for the specific language governing permissions and
13
+ # limitations under the License.
14
+
15
+ ##
16
+ # This file is here to be autorequired by bundler, so that the .trace and
17
+ # #trace methods can be available, but the library and all dependencies won't
18
+ # be loaded until required and used.
19
+
20
+
21
+ gem "google-cloud-core"
22
+ require "google/cloud"
23
+
24
+ module Google
25
+ module Cloud
26
+ ##
27
+ # Creates a new object for connecting to the Stackdriver Trace service.
28
+ # Each call creates a new connection.
29
+ #
30
+ # For more information on connecting to Google Cloud see the [Authentication
31
+ # Guide](https://googlecloudplatform.github.io/google-cloud-ruby/#/docs/guides/authentication).
32
+ #
33
+ # @param [String, Array<String>] scope The OAuth 2.0 scopes controlling the
34
+ # set of resources and operations that the connection can access. See
35
+ # [Using OAuth 2.0 to Access Google
36
+ # APIs](https://developers.google.com/identity/protocols/OAuth2).
37
+ # The default scope is `https://www.googleapis.com/auth/cloud-platform`
38
+ # @param [Integer] timeout Default timeout to use in requests. Optional.
39
+ #
40
+ # @return [Google::Cloud::Trace::Project]
41
+ #
42
+ # @example
43
+ # require "google/cloud"
44
+ #
45
+ # gcloud = Google::Cloud.new
46
+ # trace_client = gcloud.trace
47
+ #
48
+ # traces = trace_client.list_traces Time.now - 3600, Time.now
49
+ # traces.each do |trace|
50
+ # puts "Retrieved trace ID: #{trace.trace_id}"
51
+ # end
52
+ #
53
+ def trace scope: nil, timeout: nil, client_config: nil
54
+ Google::Cloud.trace @project, @keyfile, scope: scope,
55
+ timeout: (timeout || @timeout),
56
+ client_config: client_config
57
+ end
58
+
59
+ ##
60
+ # Creates a new object for connecting to the Stackdriver Trace service.
61
+ # Each call creates a new connection.
62
+ #
63
+ # For more information on connecting to Google Cloud see the [Authentication
64
+ # Guide](https://googlecloudplatform.github.io/google-cloud-ruby/#/docs/guides/authentication).
65
+ #
66
+ # @param [String] project Project identifier for the Stackdriver Trace
67
+ # service.
68
+ # @param [String, Hash] keyfile Keyfile downloaded from Google Cloud:
69
+ # either the JSON data or the path to a readable file.
70
+ # @param [String, Array<String>] scope The OAuth 2.0 scopes controlling the
71
+ # set of resources and operations that the connection can access. See
72
+ # [Using OAuth 2.0 to Access Google
73
+ # APIs](https://developers.google.com/identity/protocols/OAuth2).
74
+ # The default scope is `https://www.googleapis.com/auth/cloud-platform`
75
+ # @param [Integer] timeout Default timeout to use in requests. Optional.
76
+ #
77
+ # @return [Google::Cloud::Trace::Project]
78
+ #
79
+ # @example
80
+ # require "google/cloud"
81
+ #
82
+ # trace_client = Google::Cloud.trace
83
+ #
84
+ # traces = trace_client.list_traces Time.now - 3600, Time.now
85
+ # traces.each do |trace|
86
+ # puts "Retrieved trace ID: #{trace.trace_id}"
87
+ # end
88
+ #
89
+ def self.trace project = nil, keyfile = nil, scope: nil, timeout: nil,
90
+ client_config: nil
91
+ require "google/cloud/trace"
92
+ Google::Cloud::Trace.new project: project, keyfile: keyfile,
93
+ scope: scope, timeout: timeout,
94
+ client_config: client_config
95
+ end
96
+ end
97
+ end
@@ -0,0 +1,340 @@
1
+ # Copyright 2016 Google Inc. All rights reserved.
2
+ #
3
+ # Licensed under the Apache License, Version 2.0 (the "License");
4
+ # you may not use this file except in compliance with the License.
5
+ # You may obtain a copy of the License at
6
+ #
7
+ # http://www.apache.org/licenses/LICENSE-2.0
8
+ #
9
+ # Unless required by applicable law or agreed to in writing, software
10
+ # distributed under the License is distributed on an "AS IS" BASIS,
11
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ # See the License for the specific language governing permissions and
13
+ # limitations under the License.
14
+
15
+
16
+ require "stackdriver/core/trace_context"
17
+ require "google-cloud-trace"
18
+ require "google/cloud/trace/version"
19
+ require "google/cloud/trace/credentials"
20
+ require "google/cloud/trace/label_key"
21
+ require "google/cloud/trace/middleware"
22
+ require "google/cloud/trace/notifications"
23
+ require "google/cloud/trace/project"
24
+ require "google/cloud/trace/result_set"
25
+ require "google/cloud/trace/service"
26
+ require "google/cloud/trace/span"
27
+ require "google/cloud/trace/span_kind"
28
+ require "google/cloud/trace/time_sampler"
29
+ require "google/cloud/trace/trace_record"
30
+ require "google/cloud/trace/utils"
31
+
32
+ module Google
33
+ module Cloud
34
+ ##
35
+ # # Stackdriver Trace
36
+ #
37
+ # The Stackdriver Trace service collects and stores latency data from your
38
+ # application and displays it in the Google Cloud Platform Console, giving
39
+ # you detailed near-real-time insight into application performance.
40
+ #
41
+ # The Stackdriver Trace Ruby library, `google-cloud-trace`, provides:
42
+ #
43
+ # * Easy-to-use trace instrumentation that collects and collates latency
44
+ # data for your Ruby application. If you just want latency trace data
45
+ # for your application to appear on the Google Cloud Platform Console,
46
+ # see the section on [instrumenting your app](#instrumenting-your-app).
47
+ # * An idiomatic Ruby API for querying, analyzing, and manipulating trace
48
+ # data in your Ruby application. For an introduction to the Trace API,
49
+ # see the section on the [Trace API](#stackdriver-trace-api).
50
+ #
51
+ # ## Instrumenting Your App
52
+ #
53
+ # This library integrates with Rack-based web frameworks such as Ruby On
54
+ # Rails to provide latency trace reports for your application.
55
+ # Specifcally, it:
56
+ #
57
+ # * Provides a Rack middleware that automatically reports latency traces
58
+ # for http requests handled by your application, and measures the
59
+ # latency of each request as a whole.
60
+ # * Integrates with `ActiveSupport::Notifications` to add important
61
+ # latency-affecting events such as ActiveRecord queries to the trace.
62
+ # * Provides a simple API for your application code to define and
63
+ # measure latency-affecting processes specific to your application.
64
+ #
65
+ # When this library is installed and configured in your running
66
+ # application, you can view your application's latency traces in real time
67
+ # by opening the Google Cloud Console in your web browser and navigating
68
+ # to the "Trace" section. It also integrates with Google App Engine
69
+ # Flexible and Google Container Engine to provide additional information
70
+ # for applications hosted in those environments.
71
+ #
72
+ # Note that not all requests will have traces. By default, the library will
73
+ # sample about one trace every ten seconds per Ruby process, to prevent
74
+ # heavily used applications from reporting too much data. It will also
75
+ # omit certain requests used by Google App Engine for health checking. See
76
+ # {Google::Cloud::Trace::TimeSampler} for more details.
77
+ #
78
+ # ### Using instrumentation with Ruby on Rails
79
+ #
80
+ # To install application instrumentation in your Ruby on Rails app, add
81
+ # this gem, `google-cloud-trace`, to your Gemfile and update your bundle.
82
+ # Then add the following line to your `config/application.rb` file:
83
+ #
84
+ # ```ruby
85
+ # require "google/cloud/trace/rails"
86
+ # ```
87
+ #
88
+ # This will install a Railtie that automatically integrates with the
89
+ # Rails framework, installing the middleware and the ActiveSupport
90
+ # integration for you. Your application traces, including basic request
91
+ # tracing, ActiveRecord query measurements, and view render measurements,
92
+ # should then start appearing in the Cloud Console.
93
+ #
94
+ # See the {Google::Cloud::Trace::Railtie} class for more information,
95
+ # including how to customize your application traces.
96
+ #
97
+ # ### Using instrumentation with Sinatra
98
+ #
99
+ # To install application instrumentation in your Sinatra app, add this gem,
100
+ # `google-cloud-trace`, to your Gemfile and update your bundle. Then add
101
+ # the following lines to your main application Ruby file:
102
+ #
103
+ # ```ruby
104
+ # require "google/cloud/trace"
105
+ # use Google::Cloud::Trace::Middleware
106
+ # ```
107
+ #
108
+ # This will install the trace middleware in your application, providing
109
+ # basic request tracing for your application. You may measure additional
110
+ # processes such as database queries or calls to external services using
111
+ # other classes in this library. See the {Google::Cloud::Trace::Middleware}
112
+ # documentation for more information.
113
+ #
114
+ # ### Using instrumentation with other Rack-based frameworks
115
+ #
116
+ # To install application instrumentation in an app using another Rack-based
117
+ # web framework, add this gem, `google-cloud-trace`, to your Gemfile and
118
+ # update your bundle. Then add install the trace middleware in your
119
+ # middleware stack. In most cases, this means adding these lines to your
120
+ # `config.ru` Rack configuration file:
121
+ #
122
+ # ```ruby
123
+ # require "google/cloud/trace"
124
+ # use Google::Cloud::Trace::Middleware
125
+ # ```
126
+ #
127
+ # Some web frameworks have an alternate mechanism for modifying the
128
+ # middleware stack. Consult your web framework's documentation for more
129
+ # information.
130
+ #
131
+ # ### The Stackdriver diagnostics suite
132
+ #
133
+ # The trace library is part of the Stackdriver diagnostics suite, which
134
+ # also includes error reporting and log analysis. If you include the
135
+ # `stackdriver` gem in your Gemfile, this trace library will be included
136
+ # automatically. In addition, if you include the `stackdriver` gem in an
137
+ # application using Ruby On Rails, the Railtie will be installed
138
+ # automatically; you will not need to write any code to view latency
139
+ # traces for your appl. See the documentation for the "stackdriver" gem
140
+ # for more details.
141
+ #
142
+ # ## Stackdriver Trace API
143
+ #
144
+ # This library also includes an easy to use Ruby client for the
145
+ # Stackdriver Trace API. This API provides calls to report and modify
146
+ # application traces, as well as to query and analyze existing traces.
147
+ #
148
+ # For further information on the trace API, see
149
+ # {Google::Cloud::Trace::Project}.
150
+ #
151
+ # ### Querying traces using the API
152
+ #
153
+ # Using the Stackdriver Trace API, your application can query and analyze
154
+ # its own traces and traces of other projects. Here is an example query
155
+ # for all traces in the past hour.
156
+ #
157
+ # ```ruby
158
+ # require "google/cloud/trace"
159
+ # trace_client = Google::Cloud::Trace.new
160
+ #
161
+ # traces = trace_client.list_traces Time.now - 3600, Time.now
162
+ # traces.each do |trace|
163
+ # puts "Retrieved trace ID: #{trace.trace_id}"
164
+ # end
165
+ # ```
166
+ #
167
+ # Each trace is an object of type {Google::Cloud::Trace::TraceRecord},
168
+ # which provides methods for analyzing tasks that took place during the
169
+ # request trace. See https://cloud.google.com/trace for more information
170
+ # on the kind of data you can capture in a trace.
171
+ #
172
+ # ### Reporting traces using the API
173
+ #
174
+ # Usually it is easiest to use this library's trace instrumentation
175
+ # features to collect and record application trace information. However,
176
+ # you may also use the trace API to update this data. Here is an example:
177
+ #
178
+ # ```ruby
179
+ # require "google/cloud/trace"
180
+ #
181
+ # trace_client = Google::Cloud::Trace.new
182
+ #
183
+ # trace = Google::Cloud::Trace.new
184
+ # trace.in_span "root_span" do
185
+ # # Do stuff...
186
+ # end
187
+ #
188
+ # trace_client.patch_traces trace
189
+ # ```
190
+ #
191
+ module Trace
192
+ THREAD_KEY = :__stackdriver_trace_span__
193
+
194
+ ##
195
+ # Creates a new object for connecting to the Stackdriver Trace service.
196
+ # Each call creates a new connection.
197
+ #
198
+ # For more information on connecting to Google Cloud see the
199
+ # [Authentication
200
+ # Guide](https://googlecloudplatform.github.io/google-cloud-ruby/#/docs/guides/authentication).
201
+ #
202
+ # @param [String] project Project identifier for the Stackdriver Trace
203
+ # service.
204
+ # @param [String, Hash] keyfile Keyfile downloaded from Google Cloud:
205
+ # either the JSON data or the path to a readable file.
206
+ # @param [String, Array<String>] scope The OAuth 2.0 scopes controlling
207
+ # the set of resources and operations that the connection can access.
208
+ # See [Using OAuth 2.0 to Access Google
209
+ # APIs](https://developers.google.com/identity/protocols/OAuth2).
210
+ # The default scope is `https://www.googleapis.com/auth/cloud-platform`
211
+ # @param [Integer] timeout Default timeout to use in requests. Optional.
212
+ #
213
+ # @return [Google::Cloud::Trace::Project]
214
+ #
215
+ # @example
216
+ # require "google/cloud/trace"
217
+ #
218
+ # trace_client = Google::Cloud::Trace.new
219
+ #
220
+ # traces = trace_client.list_traces Time.now - 3600, Time.now
221
+ # traces.each do |trace|
222
+ # puts "Retrieved trace ID: #{trace.trace_id}"
223
+ # end
224
+ #
225
+ def self.new project: nil, keyfile: nil, scope: nil, timeout: nil,
226
+ client_config: nil
227
+ project ||= Google::Cloud::Trace::Project.default_project
228
+ project = project.to_s # Always cast to a string
229
+ fail ArgumentError, "project is missing" if project.empty?
230
+
231
+ credentials =
232
+ Google::Cloud::Trace::Credentials.credentials_with_scope keyfile,
233
+ scope
234
+
235
+ Google::Cloud::Trace::Project.new(
236
+ Google::Cloud::Trace::Service.new(
237
+ project, credentials, timeout: timeout,
238
+ client_config: client_config))
239
+ end
240
+
241
+ ##
242
+ # Set the current trace span being measured for the current thread, or
243
+ # the current trace if no span is currently open. This may be used with
244
+ # web frameworks that assign a thread to each request, to track the
245
+ # trace instrumentation state for the request being handled. You may use
246
+ # {Google::Cloud::Trace.get} to retrieve the data.
247
+ #
248
+ # @param [Google::Cloud::Trace::TraceSpan,
249
+ # Google::Cloud::Trace::TraceRecord, nil] trace The current span
250
+ # being measured, the current trace object, or `nil` if none.
251
+ #
252
+ # @example
253
+ # require "google/cloud/trace"
254
+ #
255
+ # trace_client = Google::Cloud::Trace.new
256
+ # trace = trace_client.new_trace
257
+ # Google::Cloud::Trace.set trace
258
+ #
259
+ # # Later...
260
+ # Google::Cloud::Trace.get.create_span "my_span"
261
+ #
262
+ def self.set trace
263
+ trace_context = trace ? trace.trace_context : nil
264
+ Stackdriver::Core::TraceContext.set trace_context
265
+ Thread.current[THREAD_KEY] = trace
266
+ end
267
+
268
+ ##
269
+ # Retrieve the current trace span or trace object for the current thread.
270
+ # This data should previously have been set using
271
+ # {Google::Cloud::Trace.set}.
272
+ #
273
+ # @return [Google::Cloud::Trace::TraceSpan,
274
+ # Google::Cloud::Trace::TraceRecord, nil] The span or trace object,
275
+ # or `nil`.
276
+ #
277
+ # @example
278
+ # require "google/cloud/trace"
279
+ #
280
+ # trace_client = Google::Cloud::Trace.new
281
+ # trace = trace_client.new_trace
282
+ # Google::Cloud::Trace.set trace
283
+ #
284
+ # # Later...
285
+ # Google::Cloud::Trace.get.create_span "my_span"
286
+ #
287
+ def self.get
288
+ Thread.current[THREAD_KEY]
289
+ end
290
+
291
+ ##
292
+ # Open a new span for the current thread, instrumenting the given block.
293
+ # The span is created within the current thread's trace context as set by
294
+ # {Google::Cloud::Trace.set}. The context is updated so any further calls
295
+ # within the block will create subspans. The new span is also yielded to
296
+ # the block.
297
+ #
298
+ # Does nothing if there is no trace context for the current thread.
299
+ #
300
+ # @param [String] name Name of the span to create
301
+ # @param [Google::Cloud::Trace::SpanKind] kind Kind of span to create.
302
+ # Optional.
303
+ # @param [Hash{String => String}] labels Labels for the span
304
+ #
305
+ # @example
306
+ # require "google/cloud/trace"
307
+ #
308
+ # trace_client = Google::Cloud::Trace.new
309
+ # trace = trace_client.new_trace
310
+ # Google::Cloud::Trace.set trace
311
+ #
312
+ # Google::Cloud::Trace.in_span "my_span" do |span|
313
+ # span.labels["foo"] = "bar"
314
+ # # Do stuff...
315
+ #
316
+ # Google::Cloud::Trace.in_span "my_subspan" do |subspan|
317
+ # subspan.labels["foo"] = "sub-bar"
318
+ # # Do other stuff...
319
+ # end
320
+ # end
321
+ #
322
+ def self.in_span name, kind: Google::Cloud::Trace::SpanKind::UNSPECIFIED,
323
+ labels: {}
324
+ parent = get
325
+ if parent
326
+ parent.in_span name, kind: kind, labels: labels do |child|
327
+ set child
328
+ begin
329
+ yield child
330
+ ensure
331
+ set parent
332
+ end
333
+ end
334
+ else
335
+ yield nil
336
+ end
337
+ end
338
+ end
339
+ end
340
+ end