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,172 @@
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/logging/metric/list"
17
+
18
+ module Google
19
+ module Cloud
20
+ module Logging
21
+ ##
22
+ # # Metric
23
+ #
24
+ # A logs-based [Google Cloud
25
+ # Monitoring](https://cloud.google.com/monitoring/docs) metric. A metric
26
+ # is a measured value that can be used to assess a system. The basis of a
27
+ # logs-based metric is the collection of log entries that match a logs
28
+ # filter.
29
+ #
30
+ # @see https://cloud.google.com/logging/docs/view/logs_based_metrics
31
+ # Logs-based Metrics
32
+ # @see https://cloud.google.com/monitoring/docs Google Cloud Monitoring
33
+ #
34
+ # @example
35
+ # require "google/cloud"
36
+ #
37
+ # gcloud = Google::Cloud.new
38
+ # logging = gcloud.logging
39
+ # metric = logging.create_metric "errors", "severity>=ERROR"
40
+ #
41
+ class Metric
42
+ ##
43
+ # @private The gRPC Service object.
44
+ attr_accessor :service
45
+
46
+ ##
47
+ # @private The gRPC Google::Logging::V2::LogMetric object.
48
+ attr_accessor :grpc
49
+
50
+ ##
51
+ # @private Create an empty Metric object.
52
+ def initialize
53
+ @service = nil
54
+ @grpc = Google::Logging::V2::LogMetric.new
55
+ end
56
+
57
+ ##
58
+ # The client-assigned metric identifier. Metric identifiers are limited
59
+ # to 1000 characters and can include only the following characters:
60
+ # `A-Z`, `a-z`, `0-9`, and the special characters `_-.,+!*',()%/\`. The
61
+ # forward-slash character (`/`) denotes a hierarchy of name pieces, and
62
+ # it cannot be the first character of the name.
63
+ def name
64
+ grpc.name
65
+ end
66
+
67
+ ##
68
+ # The description of this metric, which is used in documentation.
69
+ def description
70
+ grpc.description
71
+ end
72
+
73
+ ##
74
+ # Updates the description of this metric, which is used in
75
+ # documentation.
76
+ def description= description
77
+ grpc.description = description
78
+ end
79
+
80
+ ##
81
+ # An [advanced logs
82
+ # filter](https://cloud.google.com/logging/docs/view/advanced_filters).
83
+ def filter
84
+ grpc.filter
85
+ end
86
+
87
+ ##
88
+ # Updates the [advanced logs
89
+ # filter](https://cloud.google.com/logging/docs/view/advanced_filters).
90
+ def filter= filter
91
+ grpc.filter = filter
92
+ end
93
+
94
+ ##
95
+ # Updates the logs-based metric.
96
+ #
97
+ # @example
98
+ # require "google/cloud"
99
+ #
100
+ # gcloud = Google::Cloud.new
101
+ # logging = gcloud.logging
102
+ # metric = logging.metric "severe_errors"
103
+ # metric.filter = "logName:syslog AND severity>=ERROR"
104
+ # metric.save
105
+ #
106
+ def save
107
+ ensure_service!
108
+ @grpc = service.update_metric name, description, filter
109
+ true
110
+ end
111
+
112
+ ##
113
+ # Reloads the logs-based metric with current data from the Logging
114
+ # service.
115
+ #
116
+ # @example
117
+ # require "google/cloud"
118
+ #
119
+ # gcloud = Google::Cloud.new
120
+ # logging = gcloud.logging
121
+ # metric = logging.metric "severe_errors"
122
+ # metric.filter = "Unwanted value"
123
+ # metric.reload!
124
+ # metric.filter #=> "logName:syslog"
125
+ #
126
+ def reload!
127
+ ensure_service!
128
+ @grpc = service.get_metric name
129
+ true
130
+ end
131
+ alias_method :refresh!, :reload!
132
+
133
+ ##
134
+ # Permanently deletes the logs-based metric.
135
+ #
136
+ # @return [Boolean] Returns `true` if the metric was deleted.
137
+ #
138
+ # @example
139
+ # require "google/cloud"
140
+ #
141
+ # gcloud = Google::Cloud.new
142
+ # logging = gcloud.logging
143
+ # metric = logging.metric "severe_errors"
144
+ # metric.delete
145
+ #
146
+ def delete
147
+ ensure_service!
148
+ service.delete_metric name
149
+ true
150
+ end
151
+
152
+ ##
153
+ # @private New Metric from a gRPC object.
154
+ def self.from_grpc grpc, service
155
+ new.tap do |m|
156
+ m.grpc = grpc
157
+ m.service = service
158
+ end
159
+ end
160
+
161
+ protected
162
+
163
+ ##
164
+ # @private Raise an error unless an active connection to the service is
165
+ # available.
166
+ def ensure_service!
167
+ fail "Must have active connection to service" unless service
168
+ end
169
+ end
170
+ end
171
+ end
172
+ end
@@ -0,0 +1,175 @@
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 "delegate"
17
+
18
+ module Google
19
+ module Cloud
20
+ module Logging
21
+ class Metric
22
+ ##
23
+ # Metric::List is a special case Array with additional values.
24
+ class List < DelegateClass(::Array)
25
+ ##
26
+ # If not empty, indicates that there are more records that match
27
+ # the request and this value should be passed to continue.
28
+ attr_accessor :token
29
+
30
+ ##
31
+ # @private Create a new Metric::List with an array of Metric
32
+ # instances.
33
+ def initialize arr = []
34
+ super arr
35
+ end
36
+
37
+ ##
38
+ # Whether there is a next page of metrics.
39
+ #
40
+ # @return [Boolean]
41
+ #
42
+ # @example
43
+ # require "google/cloud"
44
+ #
45
+ # gcloud = Google::Cloud.new
46
+ # logging = gcloud.logging
47
+ #
48
+ # metrics = logging.metrics
49
+ # if metrics.next?
50
+ # next_metrics = metrics.next
51
+ # end
52
+ #
53
+ def next?
54
+ !token.nil?
55
+ end
56
+
57
+ ##
58
+ # Retrieve the next page of metrics.
59
+ #
60
+ # @return [Sink::List]
61
+ #
62
+ # @example
63
+ # require "google/cloud"
64
+ #
65
+ # gcloud = Google::Cloud.new
66
+ # logging = gcloud.logging
67
+ #
68
+ # metrics = dataset.metrics
69
+ # if metrics.next?
70
+ # next_metrics = metrics.next
71
+ # end
72
+ #
73
+ def next
74
+ return nil unless next?
75
+ ensure_service!
76
+ grpc = @service.list_metrics token: token, max: @max
77
+ self.class.from_grpc grpc, @service
78
+ end
79
+
80
+ ##
81
+ # Retrieves all metrics by repeatedly loading {#next} until {#next?}
82
+ # returns `false`. Calls the given block once for each metric, which
83
+ # is passed as the parameter.
84
+ #
85
+ # An Enumerator is returned if no block is given.
86
+ #
87
+ # This method may make several API calls until all metrics are
88
+ # retrieved. Be sure to use as narrow a search criteria as possible.
89
+ # Please use with caution.
90
+ #
91
+ # @param [Integer] request_limit The upper limit of API requests to
92
+ # make to load all metrics. Default is no limit.
93
+ # @yield [metric] The block for accessing each metric.
94
+ # @yieldparam [Metric] metric The metric object.
95
+ #
96
+ # @return [Enumerator]
97
+ #
98
+ # @example Iterating each metric by passing a block:
99
+ # require "google/cloud"
100
+ #
101
+ # gcloud = Google::Cloud.new
102
+ # logging = gcloud.logging
103
+ # metrics = logging.metrics
104
+ #
105
+ # metrics.all do |metric|
106
+ # puts "#{metric.name}: #{metric.filter}"
107
+ # end
108
+ #
109
+ # @example Using the enumerator by not passing a block:
110
+ # require "google/cloud"
111
+ #
112
+ # gcloud = Google::Cloud.new
113
+ # logging = gcloud.logging
114
+ # metrics = logging.metrics
115
+ #
116
+ # all_names = metrics.all.map do |metric|
117
+ # metric.name
118
+ # end
119
+ #
120
+ # @example Limit the number of API calls made:
121
+ # require "google/cloud"
122
+ #
123
+ # gcloud = Google::Cloud.new
124
+ # logging = gcloud.logging
125
+ # metrics = logging.metrics
126
+ #
127
+ # metrics.all(request_limit: 10) do |metric|
128
+ # puts "#{metric.name}: #{metric.filter}"
129
+ # end
130
+ #
131
+ def all request_limit: nil
132
+ request_limit = request_limit.to_i if request_limit
133
+ unless block_given?
134
+ return enum_for(:all, request_limit: request_limit)
135
+ end
136
+ results = self
137
+ loop do
138
+ results.each { |r| yield r }
139
+ if request_limit
140
+ request_limit -= 1
141
+ break if request_limit < 0
142
+ end
143
+ break unless results.next?
144
+ results = results.next
145
+ end
146
+ end
147
+
148
+ ##
149
+ # @private New Metric::List from a
150
+ # Google::Logging::V2::ListLogMetricsResponse object.
151
+ def self.from_grpc grpc_list, service, max = nil
152
+ metrics = new(Array(grpc_list.metrics).map do |grpc_metric|
153
+ Metric.from_grpc grpc_metric, service
154
+ end)
155
+ token = grpc_list.next_page_token
156
+ token = nil if token == ""
157
+ metrics.instance_variable_set "@token", token
158
+ metrics.instance_variable_set "@service", service
159
+ metrics.instance_variable_set "@max", max
160
+ metrics
161
+ end
162
+
163
+ protected
164
+
165
+ ##
166
+ # @private Raise an error unless an active connection to the service
167
+ # is available.
168
+ def ensure_service!
169
+ fail "Must have active connection to service" unless @service
170
+ end
171
+ end
172
+ end
173
+ end
174
+ end
175
+ end
@@ -0,0 +1,650 @@
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/errors"
17
+ require "google/cloud/core/gce"
18
+ require "google/cloud/logging/service"
19
+ require "google/cloud/logging/credentials"
20
+ require "google/cloud/logging/entry"
21
+ require "google/cloud/logging/resource_descriptor"
22
+ require "google/cloud/logging/sink"
23
+ require "google/cloud/logging/metric"
24
+ require "google/cloud/logging/logger"
25
+
26
+ module Google
27
+ module Cloud
28
+ module Logging
29
+ ##
30
+ # # Project
31
+ #
32
+ # Projects are top-level containers in Google Cloud Platform. They store
33
+ # information about billing and authorized users, and they control access
34
+ # to Stackdriver Logging resources. Each project has a friendly name and a
35
+ # unique ID. Projects can be created only in the [Google Developers
36
+ # Console](https://console.developers.google.com). See
37
+ # {Google::Cloud#logging}.
38
+ #
39
+ # @example
40
+ # require "google/cloud"
41
+ #
42
+ # gcloud = Google::Cloud.new
43
+ # logging = gcloud.logging
44
+ # entries = logging.entries
45
+ #
46
+ # See Google::Cloud#logging
47
+ class Project
48
+ ##
49
+ # @private The gRPC Service object.
50
+ attr_accessor :service
51
+
52
+ ##
53
+ # @private Creates a new Connection instance.
54
+ def initialize service
55
+ @service = service
56
+ end
57
+
58
+ ##
59
+ # The ID of the current project.
60
+ #
61
+ # @return [String] the Google Cloud project ID
62
+ #
63
+ # @example
64
+ # require "google/cloud"
65
+ #
66
+ # gcloud = Google::Cloud.new "my-project", "/path/to/keyfile.json"
67
+ # logging = gcloud.logging
68
+ #
69
+ # logging.project #=> "my-project"
70
+ #
71
+ def project
72
+ service.project
73
+ end
74
+
75
+ ##
76
+ # @private Default project.
77
+ def self.default_project
78
+ ENV["LOGGING_PROJECT"] ||
79
+ ENV["GOOGLE_CLOUD_PROJECT"] ||
80
+ ENV["GCLOUD_PROJECT"] ||
81
+ Google::Cloud::Core::GCE.project_id
82
+ end
83
+
84
+ ##
85
+ # Lists log entries. Use this method to retrieve log entries from Cloud
86
+ # Logging.
87
+ #
88
+ # @param [String, Array] projects One or more project IDs or project
89
+ # numbers from which to retrieve log entries. If `nil`, the ID of the
90
+ # receiving project instance will be used.
91
+ # @param [String] filter An [advanced logs
92
+ # filter](https://cloud.google.com/logging/docs/view/advanced_filters).
93
+ # The filter is compared against all log entries in the projects
94
+ # specified by `projects`. Only entries that match the filter are
95
+ # retrieved. An empty filter matches all log entries.
96
+ # @param [String] order How the results should be sorted. Presently, the
97
+ # only permitted values are "timestamp" (default) and "timestamp
98
+ # desc".
99
+ # @param [String] token A previously-returned page token representing
100
+ # part of the larger set of results to view.
101
+ # @param [Integer] max Maximum number of entries to return.
102
+ #
103
+ # @return [Array<Google::Cloud::Logging::Entry>] (See
104
+ # {Google::Cloud::Logging::Entry::List})
105
+ #
106
+ # @example
107
+ # require "google/cloud"
108
+ #
109
+ # gcloud = Google::Cloud.new
110
+ # logging = gcloud.logging
111
+ # entries = logging.entries
112
+ # entries.each do |e|
113
+ # puts "[#{e.timestamp}] #{e.log_name} #{e.payload.inspect}"
114
+ # end
115
+ #
116
+ # @example You can use a filter to narrow results to a single log.
117
+ # require "google/cloud"
118
+ #
119
+ # gcloud = Google::Cloud.new
120
+ # logging = gcloud.logging
121
+ # entries = logging.entries filter: "log:syslog"
122
+ # entries.each do |e|
123
+ # puts "[#{e.timestamp}] #{e.payload.inspect}"
124
+ # end
125
+ #
126
+ # @example You can also order the results by timestamp.
127
+ # require "google/cloud"
128
+ #
129
+ # gcloud = Google::Cloud.new
130
+ # logging = gcloud.logging
131
+ # entries = logging.entries order: "timestamp desc"
132
+ # entries.each do |e|
133
+ # puts "[#{e.timestamp}] #{e.log_name} #{e.payload.inspect}"
134
+ # end
135
+ #
136
+ # @example Retrieve all log entries: (See {Entry::List#all})
137
+ # require "google/cloud"
138
+ #
139
+ # gcloud = Google::Cloud.new
140
+ # logging = gcloud.logging
141
+ # entries = logging.entries
142
+ #
143
+ # entries.all do |e|
144
+ # puts "[#{e.timestamp}] #{e.log_name} #{e.payload.inspect}"
145
+ # end
146
+ #
147
+ def entries projects: nil, filter: nil, order: nil, token: nil, max: nil
148
+ ensure_service!
149
+ list_grpc = service.list_entries projects: projects, filter: filter,
150
+ order: order, token: token, max: max
151
+ Entry::List.from_grpc list_grpc, service,
152
+ projects: projects, max: max,
153
+ filter: filter, order: order
154
+ end
155
+ alias_method :find_entries, :entries
156
+
157
+ ##
158
+ # Creates an new Entry instance that may be populated and written to the
159
+ # Stackdriver Logging service. The {Entry#resource} attribute is
160
+ # pre-populated with a new {Google::Cloud::Logging::Resource} instance.
161
+ # Equivalent to calling `Google::Cloud::Logging::Entry.new`.
162
+ #
163
+ # @param [String] log_name The resource name of the log to which this
164
+ # log entry belongs. See also {Entry#log_name=}.
165
+ # @param [Resource] resource The monitored resource associated with this
166
+ # log entry. See also {Entry#resource}.
167
+ # @param [Time] timestamp The time the event described by the log entry
168
+ # occurred. If omitted, Stackdriver Logging will use the time the log
169
+ # entry is written. See also {Entry#timestamp}.
170
+ # @param [Symbol] severity The severity level of the log entry. The
171
+ # default value is `DEFAULT`. See also {Entry#severity}.
172
+ # @param [String] insert_id A unique ID for the log entry. If you
173
+ # provide this field, the logging service considers other log entries
174
+ # in the same log with the same ID as duplicates which can be removed.
175
+ # If omitted, Stackdriver Logging will generate a unique ID for this
176
+ # log entry. See also {Entry#insert_id}.
177
+ # @param [Hash{Symbol,String => String}] labels A hash of user-defined
178
+ # `key:value` pairs that provide additional information about the log
179
+ # entry. See also {Entry#labels=}.
180
+ # @param [String, Hash] payload The log entry payload, represented as
181
+ # either a string, a hash (JSON), or a hash (protocol buffer). See
182
+ # also {Entry#payload}.
183
+ #
184
+ # @return [Google::Cloud::Logging::Entry] a new Entry instance
185
+ #
186
+ # @example
187
+ # require "google/cloud"
188
+ #
189
+ # gcloud = Google::Cloud.new
190
+ # logging = gcloud.logging
191
+ #
192
+ # entry = logging.entry severity: :INFO, payload: "Job started."
193
+ #
194
+ # logging.write_entries entry
195
+ #
196
+ def entry log_name: nil, resource: nil, timestamp: nil, severity: nil,
197
+ insert_id: nil, labels: nil, payload: nil
198
+ e = Entry.new
199
+ e.log_name = log_name if log_name
200
+ e.resource = resource if resource
201
+ e.timestamp = timestamp if timestamp
202
+ e.severity = severity if severity
203
+ e.insert_id = insert_id if insert_id
204
+ e.labels = labels if labels
205
+ e.payload = payload if payload
206
+ e
207
+ end
208
+ alias_method :new_entry, :entry
209
+
210
+ ##
211
+ # Writes log entries to the Stackdriver Logging service.
212
+ #
213
+ # If you write a collection of log entries, you can provide the log
214
+ # name, resource, and/or labels hash to be used for all of the entries,
215
+ # and omit these values from the individual entries.
216
+ #
217
+ # @param [Google::Cloud::Logging::Entry,
218
+ # Array<Google::Cloud::Logging::Entry>] entries One or more entry
219
+ # objects to write. The log entries must have values for all required
220
+ # fields.
221
+ # @param [String] log_name A default log ID for those log entries in
222
+ # `entries` that do not specify their own `log_name`. See also
223
+ # {Entry#log_name=}.
224
+ # @param [Resource] resource A default monitored resource for those log
225
+ # entries in entries that do not specify their own resource. See also
226
+ # {Entry#resource}.
227
+ # @param [Hash{Symbol,String => String}] labels User-defined `key:value`
228
+ # items that are added to the `labels` field of each log entry in
229
+ # `entries`, except when a log entry specifies its own `key:value`
230
+ # item with the same key. See also {Entry#labels=}.
231
+ #
232
+ # @return [Boolean] Returns `true` if the entries were written.
233
+ #
234
+ # @example
235
+ # require "google/cloud"
236
+ #
237
+ # gcloud = Google::Cloud.new
238
+ # logging = gcloud.logging
239
+ #
240
+ # entry = logging.entry payload: "Job started.",
241
+ # log_name: "my_app_log"
242
+ # entry.resource.type = "gae_app"
243
+ # entry.resource.labels[:module_id] = "1"
244
+ # entry.resource.labels[:version_id] = "20150925t173233"
245
+ #
246
+ # logging.write_entries entry
247
+ #
248
+ # @example Optionally pass log name, resource, and labels for entries.
249
+ # require "google/cloud"
250
+ #
251
+ # gcloud = Google::Cloud.new
252
+ # logging = gcloud.logging
253
+ #
254
+ # entry1 = logging.entry payload: "Job started."
255
+ # entry2 = logging.entry payload: "Job completed."
256
+ #
257
+ # labels = { job_size: "large", job_code: "red" }
258
+ # resource = logging.resource "gae_app",
259
+ # "module_id" => "1",
260
+ # "version_id" => "20150925t173233"
261
+ #
262
+ # logging.write_entries [entry1, entry2],
263
+ # log_name: "my_app_log",
264
+ # resource: resource,
265
+ # labels: labels
266
+ #
267
+ def write_entries entries, log_name: nil, resource: nil, labels: nil
268
+ ensure_service!
269
+ service.write_entries Array(entries).map(&:to_grpc),
270
+ log_name: log_name, resource: resource,
271
+ labels: labels
272
+ true
273
+ end
274
+
275
+ ##
276
+ # Creates a logger instance that is API-compatible with Ruby's standard
277
+ # library [Logger](http://ruby-doc.org/stdlib/libdoc/logger/rdoc).
278
+ #
279
+ # @param [String] log_name A log resource name to be associated with the
280
+ # written log entries.
281
+ # @param [Google::Cloud::Logging::Resource] resource The monitored
282
+ # resource to be associated with written log entries.
283
+ # @param [Hash] labels A set of user-defined data to be associated with
284
+ # written log entries.
285
+ #
286
+ # @return [Google::Cloud::Logging::Logger] a Logger object that can be
287
+ # used in place of a ruby standard library logger object.
288
+ #
289
+ # @example
290
+ # require "google/cloud"
291
+ #
292
+ # gcloud = Google::Cloud.new
293
+ # logging = gcloud.logging
294
+ #
295
+ # resource = logging.resource "gae_app",
296
+ # module_id: "1",
297
+ # version_id: "20150925t173233"
298
+ #
299
+ # logger = logging.logger "my_app_log", resource, env: :production
300
+ # logger.info "Job started."
301
+ #
302
+ def logger log_name, resource, labels = {}
303
+ Logger.new self, log_name, resource, labels
304
+ end
305
+
306
+ ##
307
+ # Deletes a log and all its log entries. The log will reappear if it
308
+ # receives new entries.
309
+ #
310
+ # @param [String] name The name of the log, which may be the full path
311
+ # including the project ID (`projects/<project-id>/logs/<log-id>`), or
312
+ # just the short name (`<log-id>`), in which case the beginning of the
313
+ # path will be automatically prepended, using the ID of the current
314
+ # project.
315
+ #
316
+ # @return [Boolean] Returns `true` if the log and all its log entries
317
+ # were deleted.
318
+ #
319
+ # @example
320
+ # require "google/cloud"
321
+ #
322
+ # gcloud = Google::Cloud.new
323
+ # logging = gcloud.logging
324
+ # logging.delete_log "my_app_log"
325
+ #
326
+ def delete_log name
327
+ ensure_service!
328
+ service.delete_log name
329
+ true
330
+ end
331
+
332
+ ##
333
+ # Retrieves the list of monitored resource descriptors that are used by
334
+ # Stackdriver Logging.
335
+ #
336
+ # @see https://cloud.google.com/logging/docs/api/introduction_v2#monitored_resources
337
+ # Monitored Resources
338
+ #
339
+ # @param [String] token A previously-returned page token representing
340
+ # part of the larger set of results to view.
341
+ # @param [Integer] max Maximum number of resource descriptors to return.
342
+ #
343
+ # @return [Array<Google::Cloud::Logging::ResourceDescriptor>] (See
344
+ # {Google::Cloud::Logging::ResourceDescriptor::List})
345
+ #
346
+ # @example
347
+ # require "google/cloud"
348
+ #
349
+ # gcloud = Google::Cloud.new
350
+ # logging = gcloud.logging
351
+ # resource_descriptors = logging.resource_descriptors
352
+ # resource_descriptors.each do |rd|
353
+ # label_keys = rd.labels.map(&:key).join(", ")
354
+ # puts "#{rd.type} (#{label_keys})"
355
+ # end
356
+ #
357
+ # @example Pagination:
358
+ # require "google/cloud"
359
+ #
360
+ # gcloud = Google::Cloud.new
361
+ # logging = gcloud.logging
362
+ # resource_descriptors = logging.resource_descriptors
363
+ #
364
+ # resource_descriptors.all do |rd|
365
+ # puts rd.type
366
+ # end
367
+ #
368
+ def resource_descriptors token: nil, max: nil
369
+ ensure_service!
370
+ list_grpc = service.list_resource_descriptors token: token, max: max
371
+ ResourceDescriptor::List.from_grpc list_grpc, service, max
372
+ end
373
+ alias_method :find_resource_descriptors, :resource_descriptors
374
+
375
+ ##
376
+ # Creates a new monitored resource instance.
377
+ #
378
+ # @return [Google::Cloud::Logging::Resource]
379
+ #
380
+ # @example
381
+ # require "google/cloud"
382
+ #
383
+ # gcloud = Google::Cloud.new
384
+ # logging = gcloud.logging
385
+ #
386
+ # resource = logging.resource "gae_app",
387
+ # "module_id" => "1",
388
+ # "version_id" => "20150925t173233"
389
+ #
390
+ def resource type, labels = {}
391
+ Resource.new.tap do |r|
392
+ r.type = type
393
+ r.labels = labels
394
+ end
395
+ end
396
+ alias_method :new_resource, :resource
397
+
398
+ ##
399
+ # Retrieves the list of sinks belonging to the project.
400
+ #
401
+ # @param [String] token A previously-returned page token representing
402
+ # part of the larger set of results to view.
403
+ # @param [Integer] max Maximum number of sinks to return.
404
+ #
405
+ # @return [Array<Google::Cloud::Logging::Sink>] (See
406
+ # {Google::Cloud::Logging::Sink::List})
407
+ #
408
+ # @example
409
+ # require "google/cloud"
410
+ #
411
+ # gcloud = Google::Cloud.new
412
+ # logging = gcloud.logging
413
+ # sinks = logging.sinks
414
+ # sinks.each do |s|
415
+ # puts "#{s.name}: #{s.filter} -> #{s.destination}"
416
+ # end
417
+ #
418
+ # @example Retrieve all sinks: (See {Sink::List#all})
419
+ # require "google/cloud"
420
+ #
421
+ # gcloud = Google::Cloud.new
422
+ # logging = gcloud.logging
423
+ # sinks = logging.sinks
424
+ #
425
+ # sinks.all do |s|
426
+ # puts "#{s.name}: #{s.filter} -> #{s.destination}"
427
+ # end
428
+ #
429
+ def sinks token: nil, max: nil
430
+ ensure_service!
431
+ list_grpc = service.list_sinks token: token, max: max
432
+ Sink::List.from_grpc list_grpc, service, max
433
+ end
434
+ alias_method :find_sinks, :sinks
435
+
436
+ ##
437
+ # Creates a new project sink. When you create a sink, only new log
438
+ # entries that match the sink's filter are exported. Stackdriver Logging
439
+ # does not send previously-ingested log entries to the sink's
440
+ # destination.
441
+ #
442
+ # Before creating the sink, ensure that you have granted
443
+ # `cloud-logs@google.com` permission to write logs to the destination.
444
+ # See [Permissions for writing exported
445
+ # logs](https://cloud.google.com/logging/docs/export/configure_export#setting_product_name_short_permissions_for_writing_exported_logs).
446
+ #
447
+ # @see https://cloud.google.com/logging/docs/api/tasks/exporting-logs
448
+ # Exporting Logs With Sinks
449
+ # @see https://cloud.google.com/logging/docs/api/introduction_v2#kinds_of_log_sinks
450
+ # Kinds of log sinks (API V2)
451
+ # @see https://cloud.google.com/logging/docs/api/#sinks Sinks (API V1)
452
+ # @see https://cloud.google.com/logging/docs/export/configure_export#setting_product_name_short_permissions_for_writing_exported_logs
453
+ # Permissions for writing exported logs
454
+ #
455
+ # @param [String] name The client-assigned sink identifier. Sink
456
+ # identifiers are limited to 1000 characters and can include only the
457
+ # following characters: `A-Z`, `a-z`, `0-9`, and the special
458
+ # characters `_-.`.
459
+ # @param [String] destination The resource name of the export
460
+ # destination. See [About
461
+ # sinks](https://cloud.google.com/logging/docs/api/tasks/exporting-logs#about_sinks)
462
+ # for examples.
463
+ # @param [String, nil] filter An [advanced logs
464
+ # filter](https://cloud.google.com/logging/docs/view/advanced_filters)
465
+ # that defines the log entries to be exported. The filter must be
466
+ # consistent with the log entry format designed by the `version`
467
+ # parameter, regardless of the format of the log entry that was
468
+ # originally written to Stackdriver Logging.
469
+ # @param [Symbol] version The log entry version used when exporting log
470
+ # entries from this sink. This version does not have to correspond to
471
+ # the version of the log entry when it was written to Stackdriver
472
+ # Logging. Accepted values are `:unspecified`, `:v2`, and `:v1`.
473
+ # Version 2 is currently the preferred format. An unspecified version
474
+ # format currently defaults to V2 in the service. The default value is
475
+ # `:unspecified`.
476
+ #
477
+ # @return [Google::Cloud::Logging::Sink] a project sink
478
+ #
479
+ # @example
480
+ # require "google/cloud"
481
+ #
482
+ # gcloud = Google::Cloud.new
483
+ # logging = gcloud.logging
484
+ # storage = gcloud.storage
485
+ #
486
+ # bucket = storage.create_bucket "my-logs-bucket"
487
+ #
488
+ # # Grant owner permission to Stackdriver Logging service
489
+ # email = "cloud-logs@google.com"
490
+ # bucket.acl.add_owner "group-#{email}"
491
+ #
492
+ # sink = logging.create_sink "my-sink",
493
+ # "storage.googleapis.com/#{bucket.id}"
494
+ #
495
+ def create_sink name, destination, filter: nil, version: :unspecified
496
+ version = Sink.resolve_version version
497
+ ensure_service!
498
+ grpc = service.create_sink name, destination, filter, version
499
+ Sink.from_grpc grpc, service
500
+ end
501
+ alias_method :new_sink, :create_sink
502
+
503
+ ##
504
+ # Retrieves a sink by name.
505
+ #
506
+ # @param [String] sink_name Name of a sink.
507
+ #
508
+ # @return [Google::Cloud::Logging::Sink, nil] Returns `nil` if the sink
509
+ # does not exist.
510
+ #
511
+ # @example
512
+ # require "google/cloud"
513
+ #
514
+ # gcloud = Google::Cloud.new
515
+ # logging = gcloud.logging
516
+ # sink = logging.sink "existing-sink"
517
+ #
518
+ # @example By default `nil` will be returned if the sink does not exist.
519
+ # require "google/cloud"
520
+ #
521
+ # gcloud = Google::Cloud.new
522
+ # logging = gcloud.logging
523
+ # sink = logging.sink "non-existing-sink" #=> nil
524
+ #
525
+ def sink sink_name
526
+ ensure_service!
527
+ grpc = service.get_sink sink_name
528
+ Sink.from_grpc grpc, service
529
+ rescue Google::Cloud::NotFoundError
530
+ nil
531
+ end
532
+ alias_method :get_sink, :sink
533
+ alias_method :find_sink, :sink
534
+
535
+ ##
536
+ # Retrieves the list of metrics belonging to the project.
537
+ #
538
+ # @param [String] token A previously-returned page token representing
539
+ # part of the larger set of results to view.
540
+ # @param [Integer] max Maximum number of metrics to return.
541
+ #
542
+ # @return [Array<Google::Cloud::Logging::Metric>] (See
543
+ # {Google::Cloud::Logging::Metric::List})
544
+ #
545
+ # @example
546
+ # require "google/cloud"
547
+ #
548
+ # gcloud = Google::Cloud.new
549
+ # logging = gcloud.logging
550
+ # metrics = logging.metrics
551
+ # metrics.each do |m|
552
+ # puts "#{m.name}: #{m.filter}"
553
+ # end
554
+ #
555
+ # @example Retrieve all metrics: (See {Metric::List#all})
556
+ # require "google/cloud"
557
+ #
558
+ # gcloud = Google::Cloud.new
559
+ # logging = gcloud.logging
560
+ # metrics = logging.metrics
561
+ #
562
+ # metrics.all do |m|
563
+ # puts "#{m.name}: #{m.filter}"
564
+ # end
565
+ #
566
+ def metrics token: nil, max: nil
567
+ ensure_service!
568
+ grpc = service.list_metrics token: token, max: max
569
+ Metric::List.from_grpc grpc, service, max
570
+ end
571
+ alias_method :find_metrics, :metrics
572
+
573
+ ##
574
+ # Creates a new logs-based metric for Google Cloud Monitoring.
575
+ #
576
+ # @see https://cloud.google.com/logging/docs/view/logs_based_metrics
577
+ # Logs-based Metrics
578
+ # @see https://cloud.google.com/monitoring/docs Google Cloud Monitoring
579
+ #
580
+ # @param [String] name The client-assigned metric identifier. Metric
581
+ # identifiers are limited to 1000 characters and can include only the
582
+ # following characters: `A-Z`, `a-z`, `0-9`, and the special
583
+ # characters `_-.,+!*',()%/\`. The forward-slash character (`/`)
584
+ # denotes a hierarchy of name pieces, and it cannot be the first
585
+ # character of the name.
586
+ # @param [String] filter An [advanced logs
587
+ # filter](https://cloud.google.com/logging/docs/view/advanced_filters).
588
+ # @param [String, nil] description A description of this metric, which
589
+ # is used in documentation.
590
+ #
591
+ # @return [Google::Cloud::Logging::Metric]
592
+ #
593
+ # @example
594
+ # require "google/cloud"
595
+ #
596
+ # gcloud = Google::Cloud.new
597
+ # logging = gcloud.logging
598
+ # metric = logging.create_metric "errors", "severity>=ERROR"
599
+ #
600
+ def create_metric name, filter, description: nil
601
+ ensure_service!
602
+ grpc = service.create_metric name, filter, description
603
+ Metric.from_grpc grpc, service
604
+ end
605
+ alias_method :new_metric, :create_metric
606
+
607
+ ##
608
+ # Retrieves metric by name.
609
+ #
610
+ # @param [String] name Name of a metric.
611
+ #
612
+ # @return [Google::Cloud::Logging::Metric, nil] Returns `nil` if metric
613
+ # does not exist.
614
+ #
615
+ # @example
616
+ # require "google/cloud"
617
+ #
618
+ # gcloud = Google::Cloud.new
619
+ # logging = gcloud.logging
620
+ # metric = logging.metric "existing_metric"
621
+ #
622
+ # @example By default `nil` will be returned if metric does not exist.
623
+ # require "google/cloud"
624
+ #
625
+ # gcloud = Google::Cloud.new
626
+ # logging = gcloud.logging
627
+ # metric = logging.metric "non_existing_metric" #=> nil
628
+ #
629
+ def metric name
630
+ ensure_service!
631
+ grpc = service.get_metric name
632
+ Metric.from_grpc grpc, service
633
+ rescue Google::Cloud::NotFoundError
634
+ nil
635
+ end
636
+ alias_method :get_metric, :metric
637
+ alias_method :find_metric, :metric
638
+
639
+ protected
640
+
641
+ ##
642
+ # @private Raise an error unless an active connection to the service is
643
+ # available.
644
+ def ensure_service!
645
+ fail "Must have active connection to service" unless service
646
+ end
647
+ end
648
+ end
649
+ end
650
+ end