google-cloud-logging 0.20.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (34) hide show
  1. checksums.yaml +7 -0
  2. data/lib/google-cloud-logging.rb +119 -0
  3. data/lib/google/cloud/logging.rb +293 -0
  4. data/lib/google/cloud/logging/credentials.rb +31 -0
  5. data/lib/google/cloud/logging/entry.rb +469 -0
  6. data/lib/google/cloud/logging/entry/http_request.rb +145 -0
  7. data/lib/google/cloud/logging/entry/list.rb +180 -0
  8. data/lib/google/cloud/logging/entry/operation.rb +92 -0
  9. data/lib/google/cloud/logging/logger.rb +309 -0
  10. data/lib/google/cloud/logging/metric.rb +172 -0
  11. data/lib/google/cloud/logging/metric/list.rb +175 -0
  12. data/lib/google/cloud/logging/project.rb +650 -0
  13. data/lib/google/cloud/logging/resource.rb +86 -0
  14. data/lib/google/cloud/logging/resource_descriptor.rb +139 -0
  15. data/lib/google/cloud/logging/resource_descriptor/list.rb +179 -0
  16. data/lib/google/cloud/logging/service.rb +270 -0
  17. data/lib/google/cloud/logging/sink.rb +230 -0
  18. data/lib/google/cloud/logging/sink/list.rb +173 -0
  19. data/lib/google/cloud/logging/v2.rb +17 -0
  20. data/lib/google/cloud/logging/v2/config_service_v2_api.rb +331 -0
  21. data/lib/google/cloud/logging/v2/config_service_v2_client_config.json +53 -0
  22. data/lib/google/cloud/logging/v2/logging_service_v2_api.rb +351 -0
  23. data/lib/google/cloud/logging/v2/logging_service_v2_client_config.json +57 -0
  24. data/lib/google/cloud/logging/v2/metrics_service_v2_api.rb +330 -0
  25. data/lib/google/cloud/logging/v2/metrics_service_v2_client_config.json +53 -0
  26. data/lib/google/cloud/logging/version.rb +22 -0
  27. data/lib/google/logging/v2/log_entry_pb.rb +44 -0
  28. data/lib/google/logging/v2/logging_config_pb.rb +59 -0
  29. data/lib/google/logging/v2/logging_config_services_pb.rb +52 -0
  30. data/lib/google/logging/v2/logging_metrics_pb.rb +51 -0
  31. data/lib/google/logging/v2/logging_metrics_services_pb.rb +51 -0
  32. data/lib/google/logging/v2/logging_pb.rb +59 -0
  33. data/lib/google/logging/v2/logging_services_pb.rb +56 -0
  34. metadata +260 -0
@@ -0,0 +1,469 @@
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 "google/cloud/core/grpc_utils"
17
+ require "google/cloud/logging/resource"
18
+ require "google/cloud/logging/entry/http_request"
19
+ require "google/cloud/logging/entry/operation"
20
+ require "google/cloud/logging/entry/list"
21
+
22
+ module Google
23
+ module Cloud
24
+ module Logging
25
+ ##
26
+ # # Entry
27
+ #
28
+ # An individual entry in a log.
29
+ #
30
+ # Each log entry is composed of metadata and a payload. The metadata
31
+ # includes standard information used by Stackdriver Logging, such as when
32
+ # the entry was created and where it came from. The payload is the event
33
+ # record. Traditionally this is a message string, but in Stackdriver
34
+ # Logging it can also be a JSON or protocol buffer object. A single log
35
+ # can have entries with different payload types.
36
+ #
37
+ # A log is a named collection of entries. Logs can be produced by Google
38
+ # Cloud Platform services, by third-party services, or by your
39
+ # applications. For example, the log `compute.googleapis.com/activity_log`
40
+ # is produced by Google Compute Engine. Logs are simply referenced by name
41
+ # in google-cloud. There is no `Log` type in google-cloud or `Log`
42
+ # resource in the Cloud Logging API.
43
+ #
44
+ # @see https://cloud.google.com/logging/docs/view/logs_index List of Log
45
+ # Types
46
+ #
47
+ # @example
48
+ # require "google/cloud"
49
+ #
50
+ # gcloud = Google::Cloud.new
51
+ # logging = gcloud.logging
52
+ #
53
+ # entry = logging.entry payload: "Job started.", log_name: "my_app_log"
54
+ # entry.resource.type = "gae_app"
55
+ # entry.resource.labels[:module_id] = "1"
56
+ # entry.resource.labels[:version_id] = "20150925t173233"
57
+ #
58
+ # logging.write_entries entry
59
+ #
60
+ class Entry
61
+ ##
62
+ # Create a new Entry instance. The {#resource} attribute is
63
+ # pre-populated with a new {Google::Cloud::Logging::Resource} instance.
64
+ # See also {Google::Cloud::Logging::Project#entry}.
65
+ def initialize
66
+ @labels = {}
67
+ @resource = Resource.new
68
+ @http_request = HttpRequest.new
69
+ @operation = Operation.new
70
+ @severity = :DEFAULT
71
+ end
72
+
73
+ ##
74
+ # The resource name of the log to which this log entry belongs. The
75
+ # format of the name is `projects/<project-id>/logs/<log-id>`. e.g.
76
+ # `projects/my-projectid/logs/my_app_log` and
77
+ # `projects/1234567890/logs/library.googleapis.com%2Fbook_log`
78
+ #
79
+ # The log ID part of resource name must be less than 512 characters long
80
+ # and can only include the following characters: upper and lower case
81
+ # alphanumeric characters: `[A-Za-z0-9]`; and punctuation characters:
82
+ # forward-slash (`/`), underscore (`_`), hyphen (`-`), and period (`.`).
83
+ # Forward-slash (`/`) characters in the log ID must be URL-encoded.
84
+ attr_accessor :log_name
85
+
86
+ ##
87
+ # The monitored resource associated with this log entry. Example: a log
88
+ # entry that reports a database error would be associated with the
89
+ # monitored resource designating the particular database that reported
90
+ # the error.
91
+ # @return [Google::Cloud::Logging::Resource]
92
+ attr_accessor :resource
93
+
94
+ ##
95
+ # The time the event described by the log entry occurred. If omitted,
96
+ # Stackdriver Logging will use the time the log entry is written.
97
+ # @return [Time]
98
+ attr_accessor :timestamp
99
+
100
+ ##
101
+ # The severity level of the log entry. The default value is `:DEFAULT`.
102
+ # @return [Symbol]
103
+ attr_accessor :severity
104
+
105
+ ##
106
+ # Returns `true` if the severity level is `:DEFAULT`.
107
+ def default?
108
+ severity == :DEFAULT
109
+ end
110
+
111
+ ##
112
+ # Sets the severity level to `:DEFAULT`.
113
+ #
114
+ # @example
115
+ # require "google/cloud"
116
+ #
117
+ # gcloud = Google::Cloud.new
118
+ # logging = gcloud.logging
119
+ #
120
+ # entry = logging.entry
121
+ # entry.severity = :DEBUG
122
+ # entry.default!
123
+ # entry.default? #=> true
124
+ # entry.severity #=> :DEFAULT
125
+ #
126
+ def default!
127
+ self.severity = :DEFAULT
128
+ end
129
+
130
+ ##
131
+ # Returns `true` if the severity level is `:DEBUG`.
132
+ def debug?
133
+ severity == :DEBUG
134
+ end
135
+
136
+ ##
137
+ # Sets the severity level to `:DEBUG`.
138
+ #
139
+ # @example
140
+ # require "google/cloud"
141
+ #
142
+ # gcloud = Google::Cloud.new
143
+ # logging = gcloud.logging
144
+ #
145
+ # entry = logging.entry
146
+ # entry.severity #=> :DEFAULT
147
+ # entry.debug!
148
+ # entry.debug? #=> true
149
+ # entry.severity #=> :DEBUG
150
+ #
151
+ def debug!
152
+ self.severity = :DEBUG
153
+ end
154
+
155
+ ##
156
+ # Returns `true` if the severity level is `:INFO`.
157
+ def info?
158
+ severity == :INFO
159
+ end
160
+
161
+ ##
162
+ # Sets the severity level to `:INFO`.
163
+ #
164
+ # @example
165
+ # require "google/cloud"
166
+ #
167
+ # gcloud = Google::Cloud.new
168
+ # logging = gcloud.logging
169
+ #
170
+ # entry = logging.entry
171
+ # entry.severity #=> :DEFAULT
172
+ # entry.info!
173
+ # entry.info? #=> true
174
+ # entry.severity #=> :INFO
175
+ #
176
+ def info!
177
+ self.severity = :INFO
178
+ end
179
+
180
+ ##
181
+ # Returns `true` if the severity level is `:NOTICE`.
182
+ def notice?
183
+ severity == :NOTICE
184
+ end
185
+
186
+ ##
187
+ # Sets the severity level to `:NOTICE`.
188
+ #
189
+ # @example
190
+ # require "google/cloud"
191
+ #
192
+ # gcloud = Google::Cloud.new
193
+ # logging = gcloud.logging
194
+ #
195
+ # entry = logging.entry
196
+ # entry.severity #=> :DEFAULT
197
+ # entry.notice!
198
+ # entry.notice? #=> true
199
+ # entry.severity #=> :NOTICE
200
+ #
201
+ def notice!
202
+ self.severity = :NOTICE
203
+ end
204
+
205
+ ##
206
+ # Returns `true` if the severity level is `:WARNING`.
207
+ def warning?
208
+ severity == :WARNING
209
+ end
210
+
211
+ ##
212
+ # Sets the severity level to `:WARNING`.
213
+ #
214
+ # @example
215
+ # require "google/cloud"
216
+ #
217
+ # gcloud = Google::Cloud.new
218
+ # logging = gcloud.logging
219
+ #
220
+ # entry = logging.entry
221
+ # entry.severity #=> :DEFAULT
222
+ # entry.warning!
223
+ # entry.warning? #=> true
224
+ # entry.severity #=> :WARNING
225
+ #
226
+ def warning!
227
+ self.severity = :WARNING
228
+ end
229
+
230
+ ##
231
+ # Returns `true` if the severity level is `:ERROR`.
232
+ def error?
233
+ severity == :ERROR
234
+ end
235
+
236
+ ##
237
+ # Sets the severity level to `:ERROR`.
238
+ #
239
+ # @example
240
+ # require "google/cloud"
241
+ #
242
+ # gcloud = Google::Cloud.new
243
+ # logging = gcloud.logging
244
+ #
245
+ # entry = logging.entry
246
+ # entry.severity #=> :DEFAULT
247
+ # entry.error!
248
+ # entry.error? #=> true
249
+ # entry.severity #=> :ERROR
250
+ #
251
+ def error!
252
+ self.severity = :ERROR
253
+ end
254
+
255
+ ##
256
+ # Returns `true` if the severity level is `:CRITICAL`.
257
+ def critical?
258
+ severity == :CRITICAL
259
+ end
260
+
261
+ ##
262
+ # Sets the severity level to `:CRITICAL`.
263
+ #
264
+ # @example
265
+ # require "google/cloud"
266
+ #
267
+ # gcloud = Google::Cloud.new
268
+ # logging = gcloud.logging
269
+ #
270
+ # entry = logging.entry
271
+ # entry.severity #=> :DEFAULT
272
+ # entry.critical!
273
+ # entry.critical? #=> true
274
+ # entry.severity #=> :CRITICAL
275
+ #
276
+ def critical!
277
+ self.severity = :CRITICAL
278
+ end
279
+
280
+ ##
281
+ # Returns `true` if the severity level is `:ALERT`.
282
+ def alert?
283
+ severity == :ALERT
284
+ end
285
+
286
+ ##
287
+ # Sets the severity level to `:ALERT`.
288
+ #
289
+ # @example
290
+ # require "google/cloud"
291
+ #
292
+ # gcloud = Google::Cloud.new
293
+ # logging = gcloud.logging
294
+ #
295
+ # entry = logging.entry
296
+ # entry.severity #=> :DEFAULT
297
+ # entry.alert!
298
+ # entry.alert? #=> true
299
+ # entry.severity #=> :ALERT
300
+ #
301
+ def alert!
302
+ self.severity = :ALERT
303
+ end
304
+
305
+ ##
306
+ # Returns `true` if the severity level is `:EMERGENCY`.
307
+ def emergency?
308
+ severity == :EMERGENCY
309
+ end
310
+
311
+ ##
312
+ # Sets the severity level to `:EMERGENCY`.
313
+ #
314
+ # @example
315
+ # require "google/cloud"
316
+ #
317
+ # gcloud = Google::Cloud.new
318
+ # logging = gcloud.logging
319
+ #
320
+ # entry = logging.entry
321
+ # entry.severity #=> :DEFAULT
322
+ # entry.emergency!
323
+ # entry.emergency? #=> true
324
+ # entry.severity #=> :EMERGENCY
325
+ #
326
+ def emergency!
327
+ self.severity = :EMERGENCY
328
+ end
329
+
330
+ ##
331
+ # A unique ID for the log entry. If you provide this field, the logging
332
+ # service considers other log entries in the same log with the same ID
333
+ # as duplicates which can be removed. If omitted, Stackdriver Logging
334
+ # will generate a unique ID for this log entry.
335
+ # @return [String]
336
+ attr_accessor :insert_id
337
+
338
+ ##
339
+ # A set of user-defined data that provides additional information about
340
+ # the log entry.
341
+ # @return [Hash]
342
+ attr_accessor :labels
343
+
344
+ ##
345
+ # The log entry payload, represented as either a string, a hash (JSON),
346
+ # or a hash (protocol buffer).
347
+ # @return [String, Hash]
348
+ attr_accessor :payload
349
+
350
+ ##
351
+ # Information about the HTTP request associated with this log entry, if
352
+ # applicable.
353
+ # @return [Google::Cloud::Logging::Entry::HttpRequest]
354
+ attr_reader :http_request
355
+
356
+ ##
357
+ # Information about an operation associated with the log entry, if
358
+ # applicable.
359
+ # @return [Google::Cloud::Logging::Entry::Operation]
360
+ attr_reader :operation
361
+
362
+ ##
363
+ # @private Determines if the Entry has any data.
364
+ def empty?
365
+ log_name.nil? &&
366
+ timestamp.nil? &&
367
+ insert_id.nil? &&
368
+ (labels.nil? || labels.empty?) &&
369
+ payload.nil? &&
370
+ resource.empty? &&
371
+ http_request.empty? &&
372
+ operation.empty?
373
+ end
374
+
375
+ ##
376
+ # @private Exports the Entry to a Google::Logging::V2::LogEntry object.
377
+ def to_grpc
378
+ grpc = Google::Logging::V2::LogEntry.new(
379
+ log_name: log_name.to_s,
380
+ timestamp: timestamp_grpc,
381
+ # TODO: verify severity is the correct type?
382
+ severity: severity,
383
+ insert_id: insert_id.to_s,
384
+ labels: labels_grpc,
385
+ resource: resource.to_grpc,
386
+ http_request: http_request.to_grpc,
387
+ operation: operation.to_grpc
388
+ )
389
+ # Add payload
390
+ append_payload grpc
391
+ grpc
392
+ end
393
+
394
+ ##
395
+ # @private New Entry from a Google::Logging::V2::LogEntry object.
396
+ def self.from_grpc grpc
397
+ return new if grpc.nil?
398
+ new.tap do |e|
399
+ e.log_name = grpc.log_name
400
+ e.timestamp = extract_timestamp(grpc)
401
+ e.severity = grpc.severity
402
+ e.insert_id = grpc.insert_id
403
+ e.labels = Core::GRPCUtils.map_to_hash(grpc.labels)
404
+ e.payload = extract_payload(grpc)
405
+ e.instance_variable_set "@resource",
406
+ Resource.from_grpc(grpc.resource)
407
+ e.instance_variable_set "@http_request",
408
+ HttpRequest.from_grpc(grpc.http_request)
409
+ e.instance_variable_set "@operation",
410
+ Operation.from_grpc(grpc.operation)
411
+ end
412
+ end
413
+
414
+ ##
415
+ # @private Formats the timestamp as a Google::Protobuf::Timestamp
416
+ # object.
417
+ def timestamp_grpc
418
+ return nil if timestamp.nil?
419
+ # TODO: ArgumentError if timestamp is not a Time object?
420
+ Google::Protobuf::Timestamp.new(
421
+ seconds: timestamp.to_i,
422
+ nanos: timestamp.nsec
423
+ )
424
+ end
425
+
426
+ ##
427
+ # @private Formats the labels so they can be saved to a
428
+ # Google::Logging::V2::LogEntry object.
429
+ def labels_grpc
430
+ # Coerce symbols to strings
431
+ Hash[labels.map do |k, v|
432
+ v = String(v) if v.is_a? Symbol
433
+ [String(k), v]
434
+ end]
435
+ end
436
+
437
+ ##
438
+ # @private Adds the payload data to a Google::Logging::V2::LogEntry
439
+ # object.
440
+ def append_payload grpc
441
+ grpc.proto_payload = nil
442
+ grpc.json_payload = nil
443
+ grpc.text_payload = nil
444
+
445
+ if payload.is_a? Google::Protobuf::Any
446
+ grpc.proto_payload = payload
447
+ elsif payload.respond_to? :to_hash
448
+ grpc.json_payload = Core::GRPCUtils.hash_to_struct payload.to_hash
449
+ else
450
+ grpc.text_payload = payload.to_s
451
+ end
452
+ end
453
+
454
+ ##
455
+ # @private Extract payload data from Google API Client object.
456
+ def self.extract_payload grpc
457
+ grpc.proto_payload || grpc.json_payload || grpc.text_payload
458
+ end
459
+
460
+ ##
461
+ # @private Get a Time object from a Google::Protobuf::Timestamp object.
462
+ def self.extract_timestamp grpc
463
+ return nil if grpc.timestamp.nil?
464
+ Time.at grpc.timestamp.seconds, grpc.timestamp.nanos/1000.0
465
+ end
466
+ end
467
+ end
468
+ end
469
+ end