opencensus-stackdriver 0.2.0 → 0.3.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +7 -0
- data/README.md +2 -1
- data/lib/opencensus/stackdriver.rb +4 -1
- data/lib/opencensus/stackdriver/version.rb +1 -1
- data/lib/opencensus/stats/exporters/stackdriver.rb +338 -0
- data/lib/opencensus/stats/exporters/stackdriver/converter.rb +258 -0
- metadata +25 -10
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: fccacbc5dbd93388ade17c168d77b0a31cd0369b952b27fa1cb4fbc707f21c01
|
4
|
+
data.tar.gz: 12a2e44508d7b3ee0a74e126931be8e1775bc3ee7e1e3473082291de1ae5240b
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 885148fe5c54cca14e707a2ba774b9d1fa41df6e9f051ca106643149122f02f12e76ba53dec10b380f6662aa2b85ccbe28b7aa1687f7500925a41bc129180457
|
7
|
+
data.tar.gz: fc1572aa7fee3656ccc05632cd668efca389208bbcc6c0228907cd63e217a8ac1ecddc6efc9b0597268e6046f845de24180397d3500336c36b56e6347f753fdf
|
data/CHANGELOG.md
CHANGED
@@ -1,5 +1,12 @@
|
|
1
1
|
# Release History
|
2
2
|
|
3
|
+
### 0.3.0 / 2019-10-14
|
4
|
+
|
5
|
+
This release requires version 0.5 or later of the opencensus gem. It includes
|
6
|
+
experimental support for exporting OpenCensus Stats to the Stackdriver
|
7
|
+
Monitoring service. Note that Stats support is incomplete and there are known
|
8
|
+
issues.
|
9
|
+
|
3
10
|
### 0.2.0 / 2018-10-22
|
4
11
|
|
5
12
|
This release requires version 0.4 or later of the opencensus gem.
|
data/README.md
CHANGED
@@ -8,7 +8,8 @@ that exports data to [Stackdriver](https://cloud.google.com/stackdriver/).
|
|
8
8
|
OpenCensus is a platform- and provider-agnostic framework for distributed
|
9
9
|
tracing and stats collection. For more information, see https://opencensus.io.
|
10
10
|
|
11
|
-
This library is in an alpha stage, and the API is subject to change.
|
11
|
+
This library is in an alpha stage, and the API is subject to change. In
|
12
|
+
particular, support for the Stats API is currently incomplete and experimental.
|
12
13
|
|
13
14
|
## Quick Start
|
14
15
|
|
@@ -21,7 +21,8 @@ module OpenCensus
|
|
21
21
|
# stats information to Stackdriver Trace and Monitoring services.
|
22
22
|
#
|
23
23
|
# The Stackdriver Trace plugin for OpenCensus is implemented in the
|
24
|
-
# {OpenCensus::Trace::Exporters::Stackdriver} class
|
24
|
+
# {OpenCensus::Trace::Exporters::Stackdriver} class and
|
25
|
+
# {OpenCensus::Stats::Exporters::Stackdriver}
|
25
26
|
#
|
26
27
|
module Stackdriver
|
27
28
|
end
|
@@ -30,3 +31,5 @@ end
|
|
30
31
|
require "opencensus/stackdriver/version"
|
31
32
|
require "opencensus/trace/exporters/stackdriver"
|
32
33
|
require "opencensus/trace/exporters/stackdriver/converter"
|
34
|
+
require "opencensus/stats/exporters/stackdriver"
|
35
|
+
require "opencensus/stats/exporters/stackdriver/converter"
|
@@ -0,0 +1,338 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
# Copyright 2019 OpenCensus Authors
|
4
|
+
#
|
5
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
6
|
+
# you may not use this file except in compliance with the License.
|
7
|
+
# You may obtain a copy of the License at
|
8
|
+
#
|
9
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
10
|
+
#
|
11
|
+
# Unless required by applicable law or agreed to in writing, software
|
12
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
13
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
14
|
+
# See the License for the specific language governing permissions and
|
15
|
+
# limitations under the License.
|
16
|
+
|
17
|
+
|
18
|
+
gem "google-cloud-monitoring"
|
19
|
+
gem "concurrent-ruby"
|
20
|
+
|
21
|
+
require "concurrent"
|
22
|
+
require "google/cloud/monitoring"
|
23
|
+
require "google/cloud/monitoring/v3"
|
24
|
+
|
25
|
+
module OpenCensus
|
26
|
+
##
|
27
|
+
# OpenCensus Stats provides a standard interface for distributed stats
|
28
|
+
# recording.
|
29
|
+
#
|
30
|
+
module Stats
|
31
|
+
##
|
32
|
+
# The exporters module is a namespace for stats exporters.
|
33
|
+
#
|
34
|
+
module Exporters
|
35
|
+
##
|
36
|
+
# The Stackdriver exporter for OpenCensus Stats exports captured stats
|
37
|
+
# to a Google Monitoring project. It calls the Monitoring API in
|
38
|
+
# a background thread pool.
|
39
|
+
#
|
40
|
+
class Stackdriver
|
41
|
+
# Default custom opencensus domain name
|
42
|
+
# @return [String]
|
43
|
+
CUSTOM_OPENCENSUS_DOMAIN = "custom.googleapis.com/opencensus".freeze
|
44
|
+
|
45
|
+
# Default metric resouce type.
|
46
|
+
# @return [String]
|
47
|
+
GLOBAL_RESOURCE_TYPE = "global".freeze
|
48
|
+
|
49
|
+
# The project ID
|
50
|
+
# @return [String]
|
51
|
+
#
|
52
|
+
attr_reader :project_id
|
53
|
+
|
54
|
+
# Metric prefix
|
55
|
+
# @return [String]
|
56
|
+
attr_reader :metric_prefix
|
57
|
+
|
58
|
+
# Metric resource type
|
59
|
+
# @return [String]
|
60
|
+
attr_reader :resource_type
|
61
|
+
|
62
|
+
# Metric resource labels
|
63
|
+
# @return [Hash<String,String>]
|
64
|
+
attr_reader :resource_labels
|
65
|
+
|
66
|
+
# Create a Stackdriver exporter.
|
67
|
+
#
|
68
|
+
# @param [String] project_id The project identifier for the Stackdriver
|
69
|
+
# Monitoring service you are connecting to. If you are running on
|
70
|
+
# Google
|
71
|
+
# Cloud hosting (e.g. Compute Engine, Kubernetes Engine, or App
|
72
|
+
# Engine), this parameter is optional and will default to the
|
73
|
+
# hosting project. Otherwise, it is required.
|
74
|
+
# @param [String, Hash, Google::Auth::Credentials] credentials The
|
75
|
+
# Stackdriver API credentials, which can be a path to a keyfile as
|
76
|
+
# a String, the contents of a keyfile as a Hash, or a
|
77
|
+
# Google::Auth::Credentials object. If you are running on Google
|
78
|
+
# Cloud hosting (e.g. Compute Engine, Kubernetes Engine, or App
|
79
|
+
# Engine), this parameter is optional and will default to the
|
80
|
+
# credentials provided by the hosting project. Otherwise, it is
|
81
|
+
# required.
|
82
|
+
# @param [String, Array<String>] scope The OAuth 2.0 scopes controlling
|
83
|
+
# the set of resources and operations the API client can access.
|
84
|
+
# Optional. Most applications can leave this set to the default.
|
85
|
+
# @param [Integer] timeout The default timeout for API requests, in
|
86
|
+
# seconds. Optional.
|
87
|
+
# @param [Hash] client_config An optional set of additional
|
88
|
+
# configuration values for the API connection.
|
89
|
+
# @param [Integer] max_queue The maximum number of API requests that
|
90
|
+
# can be queued for background operation. If the queue exceeds this
|
91
|
+
# value, additional requests will be run in the calling thread
|
92
|
+
# rather than in the background. Set to 0 to allow the queue to
|
93
|
+
# grow indefinitely. Default is 1000.
|
94
|
+
# @param [Integer] max_threads The maximum number of threads that can
|
95
|
+
# be spun up to handle API requests. Default is 1. If set to 0,
|
96
|
+
# backgrounding will be disabled and all requests will run in the
|
97
|
+
# calling thread.
|
98
|
+
# @param [Integer] auto_terminate_time The time in seconds allotted to
|
99
|
+
# complete any pending background requests when Ruby is exiting.
|
100
|
+
# @param [String] metric_prefix Prefix for stackdriver metric.
|
101
|
+
# Default value set to {CUSTOM_OPENCENSUS_DOMAIN}
|
102
|
+
# @param [String] resource_type Metric resource type.
|
103
|
+
# Default value set to {GLOBAL_RESOURCE_TYPE}
|
104
|
+
# @param [Hash<String,String>] resource_labels Metric resource labels.
|
105
|
+
# Default value set to { "project_id" => project_id }
|
106
|
+
#
|
107
|
+
def initialize \
|
108
|
+
project_id: nil,
|
109
|
+
credentials: nil,
|
110
|
+
scope: nil,
|
111
|
+
timeout: nil,
|
112
|
+
client_config: nil,
|
113
|
+
max_queue: 1000,
|
114
|
+
max_threads: 1,
|
115
|
+
auto_terminate_time: 10,
|
116
|
+
mock_client: nil,
|
117
|
+
metric_prefix: nil,
|
118
|
+
resource_type: nil,
|
119
|
+
resource_labels: nil
|
120
|
+
@project_id = final_project_id project_id
|
121
|
+
@metric_prefix = metric_prefix || CUSTOM_OPENCENSUS_DOMAIN
|
122
|
+
@resource_type = resource_type || GLOBAL_RESOURCE_TYPE
|
123
|
+
@resource_labels = resource_labels || {
|
124
|
+
"project_id" => @project_id
|
125
|
+
}
|
126
|
+
@executor = create_executor max_threads, max_queue
|
127
|
+
|
128
|
+
if auto_terminate_time
|
129
|
+
terminate_at_exit! @executor, auto_terminate_time
|
130
|
+
end
|
131
|
+
|
132
|
+
if mock_client
|
133
|
+
@client_promise =
|
134
|
+
Concurrent::Promise.fulfill mock_client, executor: @executor
|
135
|
+
else
|
136
|
+
credentials = final_credentials credentials, scope
|
137
|
+
@client_promise = create_client_promise \
|
138
|
+
@executor, credentials, scope, client_config, timeout
|
139
|
+
end
|
140
|
+
|
141
|
+
@converter = Converter.new @project_id
|
142
|
+
@project_path = Google::Cloud::Monitoring::V3:: \
|
143
|
+
MetricServiceClient.project_path @project_id
|
144
|
+
end
|
145
|
+
|
146
|
+
# Export stats to Monitoring service asynchronously.
|
147
|
+
#
|
148
|
+
# @param [Array<OpenCensus::Stats::ViewData>] views_data The captured
|
149
|
+
# stats data
|
150
|
+
#
|
151
|
+
def export views_data
|
152
|
+
raise "Exporter is no longer running" unless @executor.running?
|
153
|
+
|
154
|
+
return if views_data.nil? || views_data.empty?
|
155
|
+
|
156
|
+
@client_promise.execute
|
157
|
+
export_promise = @client_promise.then do |client|
|
158
|
+
export_as_batch(client, views_data)
|
159
|
+
end
|
160
|
+
export_promise.on_error do |reason|
|
161
|
+
warn "Unable to export to Monitering service because: #{reason}"
|
162
|
+
end
|
163
|
+
|
164
|
+
nil
|
165
|
+
end
|
166
|
+
|
167
|
+
##
|
168
|
+
# Returns true if this exporter is running and will accept further
|
169
|
+
# export requests. Returns false once the exporter begins shutting down.
|
170
|
+
#
|
171
|
+
# @return [boolean]
|
172
|
+
#
|
173
|
+
def running?
|
174
|
+
@executor.running?
|
175
|
+
end
|
176
|
+
|
177
|
+
##
|
178
|
+
# Returns true if this exporter has finished shutting down and all
|
179
|
+
# pending stats have been sent.
|
180
|
+
#
|
181
|
+
# @return [boolean]
|
182
|
+
#
|
183
|
+
def shutdown?
|
184
|
+
@executor.shutdown?
|
185
|
+
end
|
186
|
+
|
187
|
+
##
|
188
|
+
# Returns true if this exporter has begun shutting down and is no
|
189
|
+
# longer accepting export requests, but is still running queued
|
190
|
+
# requests in the background.
|
191
|
+
#
|
192
|
+
# @return [boolean]
|
193
|
+
#
|
194
|
+
def shuttingdown?
|
195
|
+
@executor.shuttingdown?
|
196
|
+
end
|
197
|
+
|
198
|
+
##
|
199
|
+
# Begin shutting down the exporter gracefully. After this operation is
|
200
|
+
# performed, the exporter will no longer accept export requests, but
|
201
|
+
# will finish any pending requests in the background.
|
202
|
+
#
|
203
|
+
def shutdown
|
204
|
+
@executor.shutdown
|
205
|
+
self
|
206
|
+
end
|
207
|
+
|
208
|
+
##
|
209
|
+
# Begin shutting down the exporter forcefully. After this operation is
|
210
|
+
# performed, the exporter will no longer accept export requests, and
|
211
|
+
# will finish any currently running export requests, but will cancel
|
212
|
+
# all requests that are still pending in the queue.
|
213
|
+
#
|
214
|
+
def kill
|
215
|
+
@executor.kill
|
216
|
+
self
|
217
|
+
end
|
218
|
+
|
219
|
+
##
|
220
|
+
# Wait for the exporter to finish shutting down.
|
221
|
+
#
|
222
|
+
# @param [Integer, nil] timeout A timeout in seconds, or nil for no
|
223
|
+
# timeout.
|
224
|
+
# @return [boolean] true if the exporter is shut down, or false if the
|
225
|
+
# wait timed out.
|
226
|
+
#
|
227
|
+
def wait_for_termination timeout = nil
|
228
|
+
@executor.wait_for_termination timeout
|
229
|
+
end
|
230
|
+
|
231
|
+
# Create a metric descriptor
|
232
|
+
#
|
233
|
+
# An error will be raised if there is
|
234
|
+
# already a metric descriptor created with the same name
|
235
|
+
# but it has a different aggregation or keys.
|
236
|
+
#
|
237
|
+
# @param [OpenCensus::Stats::View] view
|
238
|
+
# @return [Google::Api::MetricDescriptor]
|
239
|
+
#
|
240
|
+
def create_metric_descriptor view
|
241
|
+
metric_descriptor = @converter.convert_metric_descriptor(
|
242
|
+
view,
|
243
|
+
metric_prefix
|
244
|
+
)
|
245
|
+
metric_name = Google::Cloud::Monitoring::V3:: \
|
246
|
+
MetricServiceClient.metric_descriptor_path(
|
247
|
+
project_id,
|
248
|
+
metric_descriptor.type
|
249
|
+
)
|
250
|
+
|
251
|
+
@client_promise.execute
|
252
|
+
descriptor_create_promise = @client_promise.then do |client|
|
253
|
+
client.create_metric_descriptor metric_name, metric_descriptor
|
254
|
+
end
|
255
|
+
descriptor_create_promise.value!
|
256
|
+
end
|
257
|
+
|
258
|
+
private
|
259
|
+
|
260
|
+
# Create the executor
|
261
|
+
def create_executor max_threads, max_queue
|
262
|
+
if max_threads >= 1
|
263
|
+
Concurrent::ThreadPoolExecutor.new \
|
264
|
+
min_threads: 1, max_threads: max_threads,
|
265
|
+
max_queue: max_queue, fallback_policy: :caller_runs,
|
266
|
+
auto_terminate: false
|
267
|
+
else
|
268
|
+
Concurrent::ImmediateExecutor.new
|
269
|
+
end
|
270
|
+
end
|
271
|
+
|
272
|
+
# Create the client promise.
|
273
|
+
# We create the client lazily so grpc doesn't get initialized until
|
274
|
+
# we actually need it. This is important because if it is intialized
|
275
|
+
# too early, before a fork, it can go into a bad state.
|
276
|
+
def create_client_promise executor, credentials, scopes, client_config,
|
277
|
+
timeout
|
278
|
+
Concurrent::Promise.new executor: executor do
|
279
|
+
Google::Cloud::Monitoring::Metric.new(
|
280
|
+
credentials: credentials,
|
281
|
+
scopes: scopes,
|
282
|
+
client_config: client_config,
|
283
|
+
timeout: timeout,
|
284
|
+
lib_name: "opencensus",
|
285
|
+
lib_version: OpenCensus::Stackdriver::VERSION
|
286
|
+
)
|
287
|
+
end
|
288
|
+
end
|
289
|
+
|
290
|
+
# Set up an at_exit hook that shuts the exporter down.
|
291
|
+
def terminate_at_exit! executor, timeout
|
292
|
+
at_exit do
|
293
|
+
executor.shutdown
|
294
|
+
unless executor.wait_for_termination timeout
|
295
|
+
executor.kill
|
296
|
+
executor.wait_for_termination timeout
|
297
|
+
end
|
298
|
+
end
|
299
|
+
end
|
300
|
+
|
301
|
+
# Fall back to default project ID
|
302
|
+
def final_project_id project_id
|
303
|
+
project_id ||
|
304
|
+
Google::Cloud.configure.project_id ||
|
305
|
+
Google::Cloud.env.project_id
|
306
|
+
end
|
307
|
+
|
308
|
+
# Fall back to default credentials, and wrap in a creds object
|
309
|
+
def final_credentials credentials, scope
|
310
|
+
credentials ||=
|
311
|
+
Google::Cloud.configure.credentials ||
|
312
|
+
Google::Cloud::Monitoring::V3::Credentials.default(scope: scope)
|
313
|
+
unless credentials.is_a? Google::Auth::Credentials
|
314
|
+
credentials = Google::Cloud::Monitoring::V3::Credentials.new(
|
315
|
+
credentials,
|
316
|
+
scope: scope
|
317
|
+
)
|
318
|
+
end
|
319
|
+
credentials
|
320
|
+
end
|
321
|
+
|
322
|
+
# Export a list of stats in the current thread
|
323
|
+
def export_as_batch client, views_data
|
324
|
+
time_series = views_data.map do |view_data|
|
325
|
+
@converter.convert_time_series(
|
326
|
+
metric_prefix,
|
327
|
+
resource_type,
|
328
|
+
resource_labels,
|
329
|
+
view_data
|
330
|
+
)
|
331
|
+
end
|
332
|
+
|
333
|
+
client.create_time_series @project_path, time_series.flatten!
|
334
|
+
end
|
335
|
+
end
|
336
|
+
end
|
337
|
+
end
|
338
|
+
end
|
@@ -0,0 +1,258 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
# Copyright 2019 OpenCensus Authors
|
4
|
+
#
|
5
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
6
|
+
# you may not use this file except in compliance with the License.
|
7
|
+
# You may obtain a copy of the License at
|
8
|
+
#
|
9
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
10
|
+
#
|
11
|
+
# Unless required by applicable law or agreed to in writing, software
|
12
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
13
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
14
|
+
# See the License for the specific language governing permissions and
|
15
|
+
# limitations under the License.
|
16
|
+
|
17
|
+
|
18
|
+
module OpenCensus
|
19
|
+
module Stats
|
20
|
+
module Exporters
|
21
|
+
class Stackdriver
|
22
|
+
##
|
23
|
+
# An object that converts OpenCensus stats data objects to Monitoring
|
24
|
+
# service protos
|
25
|
+
#
|
26
|
+
# @private
|
27
|
+
#
|
28
|
+
class Converter
|
29
|
+
##
|
30
|
+
# Create a converter
|
31
|
+
#
|
32
|
+
# @param [String] project_id Google project ID
|
33
|
+
#
|
34
|
+
def initialize project_id
|
35
|
+
@project_id = project_id
|
36
|
+
end
|
37
|
+
|
38
|
+
# Convert view to metric descriptor
|
39
|
+
#
|
40
|
+
# @param [OpenCensus::Stats:View] view Stats view
|
41
|
+
# @param [String] metric_prefix Metric prefix name
|
42
|
+
# @return [Google::Api::MetricDescriptor]
|
43
|
+
#
|
44
|
+
def convert_metric_descriptor view, metric_prefix
|
45
|
+
descriptor = Google::Api::MetricDescriptor.new(
|
46
|
+
type: make_metric_type(metric_prefix, view.name),
|
47
|
+
display_name: view.measure.name,
|
48
|
+
metric_kind: convert_metric_kind(view.aggregation),
|
49
|
+
value_type: convert_metric_value_type(view),
|
50
|
+
unit: view.measure.unit,
|
51
|
+
labels: convert_labels(view.columns)
|
52
|
+
)
|
53
|
+
|
54
|
+
descriptor.description = view.description if view.description
|
55
|
+
descriptor
|
56
|
+
end
|
57
|
+
|
58
|
+
# Conver to lables
|
59
|
+
#
|
60
|
+
# @param [Array<String>] names
|
61
|
+
# @return [Array<Google::Api::LabelDescriptor>]
|
62
|
+
def convert_labels names
|
63
|
+
names.map do |name|
|
64
|
+
Google::Api::LabelDescriptor.new(
|
65
|
+
key: name,
|
66
|
+
value_type: Google::Api::LabelDescriptor::ValueType::STRING
|
67
|
+
)
|
68
|
+
end
|
69
|
+
end
|
70
|
+
|
71
|
+
# Convert to metric view type.
|
72
|
+
#
|
73
|
+
# @param [OpenCensus::Stats:View] view Stats view
|
74
|
+
# @return [Symbol] Metric value type
|
75
|
+
#
|
76
|
+
def convert_metric_value_type view
|
77
|
+
case view.aggregation
|
78
|
+
when OpenCensus::Stats::Aggregation::Distribution
|
79
|
+
Google::Api::MetricDescriptor::ValueType::DISTRIBUTION
|
80
|
+
when OpenCensus::Stats::Aggregation::Count
|
81
|
+
Google::Api::MetricDescriptor::ValueType::INT64
|
82
|
+
when OpenCensus::Stats::Aggregation::Sum,
|
83
|
+
OpenCensus::Stats::Aggregation::LastValue
|
84
|
+
if view.measure.int64?
|
85
|
+
Google::Api::MetricDescriptor::ValueType::INT64
|
86
|
+
else
|
87
|
+
Google::Api::MetricDescriptor::ValueType::DOUBLE
|
88
|
+
end
|
89
|
+
end
|
90
|
+
end
|
91
|
+
|
92
|
+
# Convert to metric kind
|
93
|
+
#
|
94
|
+
# @param [OpenCensus::Stats:Aggregation::LastValue,
|
95
|
+
# OpenCensus::Stats:Aggregation::Sum,
|
96
|
+
# OpenCensus::Stats:Aggregation::Count,
|
97
|
+
# OpenCensus::Stats:Aggregation::Distribution] aggregation
|
98
|
+
# Aggregation type
|
99
|
+
# @return [Symbol] Metric kind type
|
100
|
+
#
|
101
|
+
def convert_metric_kind aggregation
|
102
|
+
last_value_class = OpenCensus::Stats::Aggregation::LastValue
|
103
|
+
|
104
|
+
if aggregation.instance_of? last_value_class
|
105
|
+
return Google::Api::MetricDescriptor::MetricKind::GAUGE
|
106
|
+
end
|
107
|
+
|
108
|
+
Google::Api::MetricDescriptor::MetricKind::CUMULATIVE
|
109
|
+
end
|
110
|
+
|
111
|
+
# Convert view data to time series list
|
112
|
+
#
|
113
|
+
# @param [String] metric_prefix Metric prefix name
|
114
|
+
# @param [String] resource_type Metric resource type
|
115
|
+
# @param [Hash<String,String>] resource_labels Metric resource labels
|
116
|
+
# @param [OpenCensus::Stats::ViewData] view_data Stats view data
|
117
|
+
# @return [Array[Google::Monitoring::V3::TimeSeries]]
|
118
|
+
#
|
119
|
+
def convert_time_series metric_prefix, resource_type, resource_labels,
|
120
|
+
view_data
|
121
|
+
view = view_data.view
|
122
|
+
|
123
|
+
view_data.data.map do |tag_values, aggr_data|
|
124
|
+
series = Google::Monitoring::V3::TimeSeries.new(
|
125
|
+
metric: {
|
126
|
+
type: make_metric_type(metric_prefix, view.name),
|
127
|
+
labels: Hash[view.columns.zip tag_values]
|
128
|
+
},
|
129
|
+
resource: {
|
130
|
+
type: resource_type,
|
131
|
+
labels: resource_labels
|
132
|
+
},
|
133
|
+
metric_kind: convert_metric_kind(view.aggregation),
|
134
|
+
value_type: convert_metric_value_type(view)
|
135
|
+
)
|
136
|
+
|
137
|
+
series.points << convert_point(
|
138
|
+
view_data.start_time,
|
139
|
+
aggr_data.time,
|
140
|
+
view.measure,
|
141
|
+
aggr_data
|
142
|
+
)
|
143
|
+
|
144
|
+
series
|
145
|
+
end
|
146
|
+
end
|
147
|
+
|
148
|
+
# Convert aggr data to time series point proto
|
149
|
+
#
|
150
|
+
# @param [Time] start_time Start time
|
151
|
+
# @param [Time] end_time Start time
|
152
|
+
# @param [OpenCensus::Stats:Measure] measure Measure details
|
153
|
+
# @param [OpenCensus::Stats:AggregationData] aggr_data Aggregated data
|
154
|
+
# @raise [TypeError] If invalid aggr data type.
|
155
|
+
# @return [Google::Monitoring::V3::Point]
|
156
|
+
def convert_point start_time, end_time, measure, aggr_data
|
157
|
+
case aggr_data
|
158
|
+
when OpenCensus::Stats::AggregationData::Distribution
|
159
|
+
create_distribution_point start_time, end_time, aggr_data
|
160
|
+
when OpenCensus::Stats::AggregationData::LastValue
|
161
|
+
create_number_point(
|
162
|
+
start_time,
|
163
|
+
start_time,
|
164
|
+
aggr_data.value,
|
165
|
+
measure
|
166
|
+
)
|
167
|
+
when OpenCensus::Stats::AggregationData::Sum,
|
168
|
+
OpenCensus::Stats::AggregationData::Count
|
169
|
+
create_number_point(
|
170
|
+
start_time,
|
171
|
+
end_time,
|
172
|
+
aggr_data.value,
|
173
|
+
measure
|
174
|
+
)
|
175
|
+
else
|
176
|
+
raise TypeError, "invalid aggregation type : #{aggr_data.class}"
|
177
|
+
end
|
178
|
+
end
|
179
|
+
|
180
|
+
# Create a distribution point
|
181
|
+
# @param [Time] start_time Start time
|
182
|
+
# @param [Time] end_time Start time
|
183
|
+
# @param [OpenCensus::Stats::AggregationData::Distribution] aggr_data
|
184
|
+
# @return [Google::Monitoring::V3::Point]
|
185
|
+
#
|
186
|
+
def create_distribution_point start_time, end_time, aggr_data
|
187
|
+
value = {
|
188
|
+
count: aggr_data.count,
|
189
|
+
mean: aggr_data.mean,
|
190
|
+
sum_of_squared_deviation: aggr_data.sum_of_squared_deviation,
|
191
|
+
bucket_options: {
|
192
|
+
explicit_buckets: {
|
193
|
+
bounds: [0].concat(aggr_data.buckets)
|
194
|
+
}
|
195
|
+
},
|
196
|
+
bucket_counts: [0].concat(aggr_data.bucket_counts)
|
197
|
+
}
|
198
|
+
|
199
|
+
Google::Monitoring::V3::Point.new(
|
200
|
+
interval: {
|
201
|
+
start_time: convert_time(start_time),
|
202
|
+
end_time: convert_time(end_time)
|
203
|
+
},
|
204
|
+
value: {
|
205
|
+
distribution_value: value
|
206
|
+
}
|
207
|
+
)
|
208
|
+
end
|
209
|
+
|
210
|
+
# Create a number point
|
211
|
+
# @param [Time] start_time Start time
|
212
|
+
# @param [Time] end_time Start time
|
213
|
+
# @param [Integer, Float] value
|
214
|
+
# @param [OpenCensus::Stats::Measure] measure Measure defination
|
215
|
+
# @return [Google::Monitoring::V3::Point]
|
216
|
+
#
|
217
|
+
def create_number_point start_time, end_time, value, measure
|
218
|
+
value = if measure.int64?
|
219
|
+
{ int64_value: value }
|
220
|
+
else
|
221
|
+
{ double_value: value }
|
222
|
+
end
|
223
|
+
|
224
|
+
Google::Monitoring::V3::Point.new(
|
225
|
+
interval: {
|
226
|
+
start_time: convert_time(start_time),
|
227
|
+
end_time: convert_time(end_time)
|
228
|
+
},
|
229
|
+
value: value
|
230
|
+
)
|
231
|
+
end
|
232
|
+
|
233
|
+
# Convert time object to protobuf timestamp
|
234
|
+
#
|
235
|
+
# @param [Time] time Ruby Time object
|
236
|
+
# @return [Google::Protobuf::Timestamp] The generated proto
|
237
|
+
#
|
238
|
+
def convert_time time
|
239
|
+
proto = Google::Protobuf::Timestamp.new
|
240
|
+
proto.from_time(time)
|
241
|
+
proto
|
242
|
+
end
|
243
|
+
|
244
|
+
##
|
245
|
+
# Make make metric type
|
246
|
+
#
|
247
|
+
# @param [String] metric_prefix The metric prefix
|
248
|
+
# @param [String] name The name of the mertic view
|
249
|
+
# @return [String] The metric type path
|
250
|
+
#
|
251
|
+
def make_metric_type metric_prefix, name
|
252
|
+
"#{metric_prefix}/#{name}"
|
253
|
+
end
|
254
|
+
end
|
255
|
+
end
|
256
|
+
end
|
257
|
+
end
|
258
|
+
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: opencensus-stackdriver
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.3.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Daniel Azuma
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2019-10-14 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: concurrent-ruby
|
@@ -44,28 +44,42 @@ dependencies:
|
|
44
44
|
requirements:
|
45
45
|
- - "~>"
|
46
46
|
- !ruby/object:Gem::Version
|
47
|
-
version: '0.
|
47
|
+
version: '0.5'
|
48
48
|
type: :runtime
|
49
49
|
prerelease: false
|
50
50
|
version_requirements: !ruby/object:Gem::Requirement
|
51
51
|
requirements:
|
52
52
|
- - "~>"
|
53
53
|
- !ruby/object:Gem::Version
|
54
|
-
version: '0.
|
54
|
+
version: '0.5'
|
55
55
|
- !ruby/object:Gem::Dependency
|
56
|
-
name:
|
56
|
+
name: google-cloud-monitoring
|
57
57
|
requirement: !ruby/object:Gem::Requirement
|
58
58
|
requirements:
|
59
59
|
- - "~>"
|
60
60
|
- !ruby/object:Gem::Version
|
61
|
-
version:
|
62
|
-
type: :
|
61
|
+
version: 0.29.2
|
62
|
+
type: :runtime
|
63
63
|
prerelease: false
|
64
64
|
version_requirements: !ruby/object:Gem::Requirement
|
65
65
|
requirements:
|
66
66
|
- - "~>"
|
67
67
|
- !ruby/object:Gem::Version
|
68
|
-
version:
|
68
|
+
version: 0.29.2
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: bundler
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - ">="
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '1.17'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - ">="
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '1.17'
|
69
83
|
- !ruby/object:Gem::Dependency
|
70
84
|
name: faraday
|
71
85
|
requirement: !ruby/object:Gem::Requirement
|
@@ -195,6 +209,8 @@ files:
|
|
195
209
|
- lib/opencensus-stackdriver.rb
|
196
210
|
- lib/opencensus/stackdriver.rb
|
197
211
|
- lib/opencensus/stackdriver/version.rb
|
212
|
+
- lib/opencensus/stats/exporters/stackdriver.rb
|
213
|
+
- lib/opencensus/stats/exporters/stackdriver/converter.rb
|
198
214
|
- lib/opencensus/trace/exporters/stackdriver.rb
|
199
215
|
- lib/opencensus/trace/exporters/stackdriver/converter.rb
|
200
216
|
homepage: https://github.com/census-instrumentation/ruby-stackdriver-exporter
|
@@ -216,8 +232,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
216
232
|
- !ruby/object:Gem::Version
|
217
233
|
version: '0'
|
218
234
|
requirements: []
|
219
|
-
|
220
|
-
rubygems_version: 2.7.6
|
235
|
+
rubygems_version: 3.0.3
|
221
236
|
signing_key:
|
222
237
|
specification_version: 4
|
223
238
|
summary: Stackdriver exporter for OpenCensus
|