google-cloud-trace 0.25.0 → 0.26.1

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 537ea43256a80488f659a7b6ed8f41f23f935e13
4
- data.tar.gz: 4e0d0e17d29ffac6d6323eaebf2838332f14c072
3
+ metadata.gz: 3a4e0e04c7f4fe293e6bad7a5624670383578de2
4
+ data.tar.gz: 59c06ae1afb28e86a65045533de8a3be2f93ec44
5
5
  SHA512:
6
- metadata.gz: b874bb6c50a4b10f634311b69b45055551061b21f75125823edec6f78c181d87a0efb8ae241eb4e47a3fb7c28f259e2b6dc27c47b047332784a0e758505c7e56
7
- data.tar.gz: 451c43e68ad1ca3561306c9f4a30a060ce33838d0fff9a11d303065aa0dd2f985bfe57c2c53115d0ab41230900b380ed79a8026d734deac164f809eb6222eb2b
6
+ metadata.gz: b79c75d65ba9a052dc70605e1b75110a73be6df69599655f742bcc45dd598b485715d827f4a7c54b709ab1665024c05a0809df548790f2eeb807fed660001a3e
7
+ data.tar.gz: 6043aee0288c6efd69c48044abeab5445077df1b4b854491009e0530b2495ecb40329d69d7395f83e882cf96bc66ea8f2dda1923d0a3ab05f65a73924aa605c3
@@ -0,0 +1,124 @@
1
+ # Copyright 2017 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/async_actor"
17
+
18
+ module Google
19
+ module Cloud
20
+ module Trace
21
+ ##
22
+ # # AsyncReporter
23
+ #
24
+ # @private Used by the {Google::Cloud::Trace::Middleware} to
25
+ # asynchronously update traces to Stackdriver Trace service when used in
26
+ # a Rack-based application.
27
+ class AsyncReporter
28
+ include Stackdriver::Core::AsyncActor
29
+
30
+ ##
31
+ # @private Default Maximum blacklog size for the job queue
32
+ DEFAULT_MAX_QUEUE_SIZE = 1000
33
+
34
+ ##
35
+ # The {Google::Cloud::Trace::Service} object to patch traces
36
+ attr_accessor :service
37
+
38
+ ##
39
+ # The max number of items the queue holds
40
+ attr_accessor :max_queue_size
41
+
42
+ ##
43
+ # @private Construct a new instance of AsyncReporter
44
+ def initialize service, max_queue_size = DEFAULT_MAX_QUEUE_SIZE
45
+ super()
46
+
47
+ @service = service
48
+ @max_queue_size = max_queue_size
49
+ @queue = []
50
+ @queue_resource = new_cond
51
+ end
52
+
53
+ ##
54
+ # Add the traces to the queue to be reported to Stackdriver Trace
55
+ # asynchronously. Signal the child thread to start processing the queue.
56
+ def patch_traces traces
57
+ ensure_thread
58
+
59
+ synchronize do
60
+ @queue.push traces
61
+ @queue_resource.broadcast
62
+
63
+ while @max_queue_size && @queue.size > max_queue_size
64
+ @queue_resource.wait 1
65
+
66
+ @queue.pop while @queue.size > max_queue_size
67
+ end
68
+ end
69
+ end
70
+
71
+ ##
72
+ # @private Callback function for AsyncActor module to process the queue
73
+ # in a loop
74
+ def run_backgrounder
75
+ traces = wait_next_item
76
+ return if traces.nil?
77
+
78
+ begin
79
+ service.patch_traces traces
80
+ rescue => e
81
+ @last_exception = e
82
+ end
83
+ end
84
+
85
+ ##
86
+ # @private Callback function when the async actor thread state changes
87
+ def on_async_state_change
88
+ synchronize do
89
+ @queue_resource.broadcast
90
+ end
91
+ end
92
+
93
+ ##
94
+ # Get the project id from underlying service object.
95
+ def project
96
+ service.project
97
+ end
98
+
99
+ private
100
+
101
+ ##
102
+ # @private Wait for the next item from the reporter queue. If there are
103
+ # no more items, it blocks the child thread until an item is enqueued.
104
+ def wait_next_item
105
+ synchronize do
106
+ @queue_resource.wait_while do
107
+ async_suspended? || (async_running? && @queue.empty?)
108
+ end
109
+ @queue.pop
110
+ end
111
+ end
112
+
113
+ ##
114
+ # @private Override the #backgrounder_stoppable? method from AsyncActor
115
+ # module. The actor can be gracefully stopped when queue is empty.
116
+ def backgrounder_stoppable?
117
+ synchronize do
118
+ @queue.empty?
119
+ end
120
+ end
121
+ end
122
+ end
123
+ end
124
+ end
@@ -0,0 +1,90 @@
1
+ # Copyright 2017 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 "faraday"
17
+ require "google/cloud/trace"
18
+
19
+ module Google
20
+ module Cloud
21
+ module Trace
22
+ class FaradayMiddleware < Faraday::Middleware
23
+ ##
24
+ # Create a Trace span with the HTTP request/response information.
25
+ def call env
26
+ Google::Cloud::Trace.in_span "faraday_request" do |span|
27
+ add_request_labels span, env if span
28
+
29
+ response = @app.call env
30
+
31
+ add_response_labels span, env if span
32
+
33
+ response
34
+ end
35
+ end
36
+
37
+ protected
38
+
39
+ ##
40
+ # @private Set Trace span labels from request object
41
+ def add_request_labels span, env
42
+ labels = span.labels
43
+ label_keys = Google::Cloud::Trace::LabelKey
44
+
45
+ set_label labels, label_keys::HTTP_METHOD, env.method
46
+ set_label labels, label_keys::HTTP_URL, env.url.to_s
47
+
48
+ # Only sets request size if request is not sent yet.
49
+ unless env.status
50
+ request_body = env.body || ""
51
+ set_label labels, label_keys::RPC_REQUEST_SIZE,
52
+ request_body.bytesize.to_s
53
+ end
54
+ end
55
+
56
+ ##
57
+ # @private Set Trace span labels from response
58
+ def add_response_labels span, env
59
+ labels = span.labels
60
+ label_keys = Google::Cloud::Trace::LabelKey
61
+
62
+ response = env.response
63
+ response_body = response.body || ""
64
+ response_status = response.status
65
+ response_url = response.headers[:location]
66
+
67
+ set_label labels, label_keys::RPC_RESPONSE_SIZE,
68
+ response_body.bytesize.to_s
69
+ set_label labels, label_keys::HTTP_STATUS_CODE, response_status.to_s
70
+
71
+ if 300 <= response_status && response_status < 400 && response_url
72
+ set_label labels, label_keys::HTTP_REDIRECTED_URL, response_url
73
+ end
74
+ end
75
+
76
+ ##
77
+ # Sets the given label if the given value is a proper string.
78
+ #
79
+ # @private
80
+ # @param [Hash] labels The labels hash.
81
+ # @param [String] key The key of the label to set.
82
+ # @param [Object] value The value to set.
83
+ #
84
+ def set_label labels, key, value
85
+ labels[key] = value if value.is_a? ::String
86
+ end
87
+ end
88
+ end
89
+ end
90
+ end
@@ -70,6 +70,9 @@ module Google
70
70
  GAE_MEMCACHE_SIZE = "g.co/gae/memcache/size"
71
71
  GAE_REQUEST_LOG_ID = "g.co/gae/request_log_id"
72
72
 
73
+ RPC_REQUEST_SIZE = "/rpc/request/size"
74
+ RPC_RESPONSE_SIZE = "/rpc/response/size"
75
+
73
76
  ##
74
77
  # Set the stack trace label in the given labels hash. The current call
75
78
  # stack is formatted so the Stackdriver UI will display it.
@@ -14,6 +14,7 @@
14
14
 
15
15
 
16
16
  require "google/cloud/env"
17
+ require "google/cloud/trace/async_reporter"
17
18
  require "stackdriver/core/trace_context"
18
19
 
19
20
  module Google
@@ -112,8 +113,8 @@ module Google
112
113
  # Create a new Middleware for traces
113
114
  #
114
115
  # @param [Rack Application] app Rack application
115
- # @param [Google::Cloud::Trace::Service] service The service object.
116
- # Optional if running on GCE.
116
+ # @param [Google::Cloud::Trace::Service, AsyncReporter] service
117
+ # The service object to update traces. Optional if running on GCE.
117
118
  # @param [Hash] *kwargs Hash of configuration settings. Used for
118
119
  # backward API compatibility. See the [Configuration
119
120
  # Guide](https://googlecloudplatform.github.io/google-cloud-ruby/#/docs/stackdriver/guides/instrumentation_configuration)
@@ -128,8 +129,12 @@ module Google
128
129
  @service = service
129
130
  else
130
131
  project_id = configuration.project_id
132
+
131
133
  if project_id
132
- @service = Google::Cloud::Trace.new(project: project_id).service
134
+ keyfile = configuration.keyfile
135
+ tracer = Google::Cloud::Trace.new project: project_id,
136
+ keyfile: keyfile
137
+ @service = Google::Cloud::Trace::AsyncReporter.new tracer.service
133
138
  end
134
139
  end
135
140
  end
@@ -98,16 +98,9 @@ module Google
98
98
  #
99
99
  def self.init_middleware app
100
100
  trace_config = Trace.configure
101
- project_id = trace_config.project_id
102
- keyfile = trace_config.keyfile
103
101
 
104
- tracer = Google::Cloud::Trace.new project: project_id,
105
- keyfile: keyfile
106
-
107
- app.middleware.insert_before \
108
- Rack::Runtime,
109
- Google::Cloud::Trace::Middleware,
110
- service: tracer.service
102
+ app.middleware.insert_before Rack::Runtime,
103
+ Google::Cloud::Trace::Middleware
111
104
 
112
105
  trace_config.notifications.each do |type|
113
106
  Google::Cloud::Trace::Notifications.instrument \
@@ -6,9 +6,7 @@
6
6
  "DEADLINE_EXCEEDED",
7
7
  "UNAVAILABLE"
8
8
  ],
9
- "non_idempotent": [
10
- "UNAVAILABLE"
11
- ]
9
+ "non_idempotent": []
12
10
  },
13
11
  "retry_params": {
14
12
  "default": {
@@ -16,7 +16,7 @@
16
16
  module Google
17
17
  module Cloud
18
18
  module Trace
19
- VERSION = "0.25.0".freeze
19
+ VERSION = "0.26.1".freeze
20
20
  end
21
21
  end
22
22
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: google-cloud-trace
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.25.0
4
+ version: 0.26.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Daniel Azuma
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-05-26 00:00:00.000000000 Z
11
+ date: 2017-07-12 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: google-cloud-core
@@ -30,14 +30,14 @@ dependencies:
30
30
  requirements:
31
31
  - - "~>"
32
32
  - !ruby/object:Gem::Version
33
- version: '1.1'
33
+ version: '1.2'
34
34
  type: :runtime
35
35
  prerelease: false
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
38
38
  - - "~>"
39
39
  - !ruby/object:Gem::Version
40
- version: '1.1'
40
+ version: '1.2'
41
41
  - !ruby/object:Gem::Dependency
42
42
  name: google-gax
43
43
  requirement: !ruby/object:Gem::Requirement
@@ -122,6 +122,20 @@ dependencies:
122
122
  - - "~>"
123
123
  - !ruby/object:Gem::Version
124
124
  version: '1.1'
125
+ - !ruby/object:Gem::Dependency
126
+ name: faraday
127
+ requirement: !ruby/object:Gem::Requirement
128
+ requirements:
129
+ - - "~>"
130
+ - !ruby/object:Gem::Version
131
+ version: '0.8'
132
+ type: :development
133
+ prerelease: false
134
+ version_requirements: !ruby/object:Gem::Requirement
135
+ requirements:
136
+ - - "~>"
137
+ - !ruby/object:Gem::Version
138
+ version: '0.8'
125
139
  - !ruby/object:Gem::Dependency
126
140
  name: railties
127
141
  requirement: !ruby/object:Gem::Requirement
@@ -204,7 +218,9 @@ files:
204
218
  - README.md
205
219
  - lib/google-cloud-trace.rb
206
220
  - lib/google/cloud/trace.rb
221
+ - lib/google/cloud/trace/async_reporter.rb
207
222
  - lib/google/cloud/trace/credentials.rb
223
+ - lib/google/cloud/trace/faraday_middleware.rb
208
224
  - lib/google/cloud/trace/label_key.rb
209
225
  - lib/google/cloud/trace/middleware.rb
210
226
  - lib/google/cloud/trace/notifications.rb