forked from googleapis/google-cloud-node
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
577 lines (513 loc) · 16 KB
/
index.js
File metadata and controls
577 lines (513 loc) · 16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
/*!
* Copyright 2015 Google Inc. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/*!
* @module logging
*/
'use strict';
var arrify = require('arrify');
var common = require('@google-cloud/common');
var extend = require('extend');
var format = require('string-format-obj');
var googleProtoFiles = require('google-proto-files');
var is = require('is');
var util = require('util');
/**
* @type {module:logging/entry}
* @private
*/
var Entry = require('./entry.js');
/**
* @type {module:logging/log}
* @private
*/
var Log = require('./log.js');
/**
* @type {module:logging/sink}
* @private
*/
var Sink = require('./sink.js');
var PKG = require('../package.json');
/**
* <p class="notice">
* **This is a Beta release of Google Cloud Logging.** This API is not covered
* by any SLA or deprecation policy and may be subject to
* backward-incompatible changes.
* </p>
*
* [Google Cloud Logging](https://cloud.google.com/logging/docs) collects and
* stores logs from applications and services on the Google Cloud Platform:
*
* - Export your logs to Google Cloud Storage, Google BigQuery, or Google
* Cloud Pub/Sub.
* - Integrate third-party logs from your virtual machine instances by
* installing the logging agent, `google-fluentd`.
*
* @constructor
* @alias module:logging
* @resource [What is Google Cloud Logging?]{@link https://cloud.google.com/logging/docs}
* @resource [Introduction to the Cloud Logging API]{@link https://cloud.google.com/logging/docs/api}
*
* @param {object} options - [Configuration object](#/docs).
*/
function Logging(options) {
if (!(this instanceof Logging)) {
options = common.util.normalizeArguments(this, options);
return new Logging(options);
}
var config = {
baseUrl: 'logging.googleapis.com',
service: 'logging',
apiVersion: 'v2',
protoServices: {
ConfigServiceV2:
googleProtoFiles('logging', 'v2', 'logging_config.proto'),
LoggingServiceV2: googleProtoFiles.logging.v2
},
scopes: [
'https://www.googleapis.com/auth/cloud-platform'
],
userAgent: PKG.name + '/' + PKG.version
};
common.GrpcService.call(this, config, options);
}
util.inherits(Logging, common.GrpcService);
// jscs:disable maximumLineLength
/**
* Create a sink.
*
* @resource [Sink Overview]{@link https://cloud.google.com/logging/docs/api/ref_v2beta1/rest/v2beta1/projects.sinks}
* @resource [Advanced Logs Filters]{@link https://cloud.google.com/logging/docs/view/advanced_filters}
* @resource [projects.sinks.create API Documentation]{@link https://cloud.google.com/logging/docs/api/ref_v2beta1/rest/v2beta1/projects.sinks/create}
*
* @throws {Error} if a name is not provided.
* @throws {Error} if a config object is not provided.
*
* @param {string} name - Name of the sink.
* @param {object} config - See a
* [Sink resource](https://cloud.google.com/logging/docs/api/ref_v2beta1/rest/v2beta1/projects.sinks#LogSink).
* @param {module:storage/bucket|module:bigquery/dataset|module:pubsub/topic} config.destination -
* The destination. The proper ACL scopes will be granted to the provided
* destination.
* @param {string=} config.filter - An advanced logs filter. Only log entries
* matching the filter are written.
* @param {function} callback - The callback function.
* @param {?error} callback.err - An error returned while making this request.
* @param {module:logging/sink} callback.sink - The created Sink object.
* @param {object} callback.apiResponse - The full API response.
*
* @example
* var gcs = require('@google-cloud/storage')({
* projectId: 'grape-spaceship-123'
* });
*
* var config = {
* destination: gcs.bucket('logging-bucket'),
* filter: 'severity = ALERT'
* };
*
* function callback(err, sink, apiResponse) {
* // `sink` is a Sink object.
* }
*
* logging.createSink('new-sink-name', config, callback);
*/
Logging.prototype.createSink = function(name, config, callback) {
// jscs:enable maximumLineLength
var self = this;
if (!is.string(name)) {
throw new Error('A sink name must be provided.');
}
if (!is.object(config)) {
throw new Error('A sink configuration object must be provided.');
}
if (common.util.isCustomType(config.destination, 'bigquery/dataset')) {
this.setAclForDataset_(name, config, callback);
return;
}
if (common.util.isCustomType(config.destination, 'pubsub/topic')) {
this.setAclForTopic_(name, config, callback);
return;
}
if (common.util.isCustomType(config.destination, 'storage/bucket')) {
this.setAclForBucket_(name, config, callback);
return;
}
var protoOpts = {
service: 'ConfigServiceV2',
method: 'createSink'
};
var reqOpts = {
parent: 'projects/' + this.projectId,
sink: extend({}, config, { name: name })
};
this.request(protoOpts, reqOpts, function(err, resp) {
if (err) {
callback(err, null, resp);
return;
}
var sink = self.sink(resp.name);
sink.metadata = resp;
callback(null, sink, resp);
});
};
/**
* Create an entry object.
*
* Note that using this method will not itself make any API requests. You will
* use the object returned in other API calls, such as
* {module:logging/log#write}.
*
* @resource [LogEntry JSON representation]{@link https://cloud.google.com/logging/docs/api/ref_v2beta1/rest/v2beta1/LogEntry}
*
* @param {object=|string=} resource - See a
* [Monitored Resource](https://cloud.google.com/logging/docs/api/ref_v2beta1/rest/v2beta1/MonitoredResource).
* @param {object|string} data - The data to use as the value for this log
* entry.
* @return {module:logging/entry}
*
* @example
* var resource = {
* type: 'gce_instance',
* labels: {
* zone: 'global',
* instance_id: '3'
* }
* };
*
* var entry = logging.entry(resource, {
* delegate: 'my_username'
* });
*
* entry.toJSON();
* // {
* // resource: {
* // type: 'gce_instance',
* // labels: {
* // zone: 'global',
* // instance_id: '3'
* // }
* // },
* // jsonPayload: {
* // delegate: 'my_username'
* // }
* // }
*/
Logging.prototype.entry = function(resource, data) {
return new Entry(resource, data);
};
/**
* List the entries in your logs.
*
* @resource [entries.list API Documentation]{@link https://cloud.google.com/logging/docs/api/ref_v2beta1/rest/v2beta1/entries/list}
*
* @param {object=} options - Filtering options.
* @param {boolean} options.autoPaginate - Have pagination handled
* automatically. Default: true.
* @param {string} options.filter - An
* [advanced logs filter](https://cloud.google.com/logging/docs/view/advanced_filters).
* An empty filter matches all log entries.
* @param {number} options.maxApiCalls - Maximum number of API calls to make.
* @param {number} options.maxResults - Maximum number of results to return.
* @param {string} options.orderBy - How the results should be sorted,
* `timestamp` (oldest first) and `timestamp desc` (newest first,
* **default**).
* @param {number} options.pageSize - Maximum number of logs to return.
* @param {string} options.pageToken - A previously-returned page token
* representing part of the larger set of results to view.
* @param {function} callback - The callback function.
* @param {?error} callback.err - An error returned while making this request.
* @param {module:logging/entry[]} callback.entries - Entries from your logs.
* @param {?object} callback.nextQuery - If present, query with this object to
* check for more results.
* @param {object} callback.apiResponse - The full API response.
*
* @example
* logging.getEntries(function(err, entries) {
* // `entries` is an array of Cloud Logging entry objects.
* // See the `data` property to read the data from the entry.
* });
*
* //-
* // To control how many API requests are made and page through the results
* // manually, set `autoPaginate` to `false`.
* //-
* function callback(err, entries, nextQuery, apiResponse) {
* if (nextQuery) {
* // More results exist.
* logging.getEntries(nextQuery, callback);
* }
* }
*
* logging.getEntries({
* autoPaginate: false
* }, callback);
*
* //-
* // Get the entries from your project as a readable object stream.
* //-
* logging.getEntries()
* .on('error', console.error)
* .on('data', function(entry) {
* // `entry` is a Cloud Logging entry object.
* // See the `data` property to read the data from the entry.
* })
* .on('end', function() {
* // All entries retrieved.
* });
*
* //-
* // If you anticipate many results, you can end a stream early to prevent
* // unnecessary processing and API requests.
* //-
* logging.getEntries()
* .on('data', function(entry) {
* this.end();
* });
*/
Logging.prototype.getEntries = function(options, callback) {
if (is.fn(options)) {
callback = options;
options = {};
}
var protoOpts = {
service: 'LoggingServiceV2',
method: 'listLogEntries'
};
var reqOpts = extend({
orderBy: 'timestamp desc'
}, options);
reqOpts.projectIds = arrify(reqOpts.projectIds);
reqOpts.projectIds.push(this.projectId);
this.request(protoOpts, reqOpts, function(err, resp) {
if (err) {
callback(err, null, null, resp);
return;
}
var nextQuery = null;
if (resp.nextPageToken) {
nextQuery = extend({}, reqOpts, {
pageToken: resp.nextPageToken
});
}
var entries = arrify(resp.entries).map(Entry.fromApiResponse_);
callback(null, entries, nextQuery, resp);
});
};
/**
* Get the sinks associated with this project.
*
* @resource [projects.sinks.list API Documentation]{@link https://cloud.google.com/logging/docs/api/ref_v2beta1/rest/v2beta1/projects.sinks/list}
*
* @param {object=} options - Configuration object.
* @param {boolean} options.autoPaginate - Have pagination handled
* automatically. Default: true.
* @param {number} options.maxApiCalls - Maximum number of API calls to make.
* @param {number} options.maxResults - Maximum number of results to return.
* @param {function} callback - The callback function.
* @param {?error} callback.err - An error returned while making this request.
* @param {module:logging/sink[]} callback.sinks - Sink objects.
* @param {object} callback.apiResponse - The full API response.
*
* @example
* logging.getSinks(function(err, sinks) {
* // sinks is an array of Sink objects.
* });
*
* //-
* // Get the sinks from your project as a readable object stream.
* //-
* logging.getSinks()
* .on('error', console.error)
* .on('data', function(sink) {
* // `sink` is a Sink object.
* })
* .on('end', function() {
* // All sinks retrieved.
* });
*
* //-
* // If you anticipate many results, you can end a stream early to prevent
* // unnecessary processing and API requests.
* //-
* logging.getSinks()
* .on('data', function(sink) {
* this.end();
* });
*/
Logging.prototype.getSinks = function(options, callback) {
var self = this;
if (is.fn(options)) {
callback = options;
options = {};
}
var protoOpts = {
service: 'ConfigServiceV2',
method: 'listSinks'
};
var reqOpts = extend({}, options, {
parent: 'projects/' + this.projectId
});
this.request(protoOpts, reqOpts, function(err, resp) {
if (err) {
callback(err, null, null, resp);
return;
}
var nextQuery = null;
if (resp.nextPageToken) {
nextQuery = extend({}, options, {
pageToken: resp.nextPageToken
});
}
var sinks = arrify(resp.sinks).map(function(sink) {
var sinkInstance = self.sink(sink.name);
sinkInstance.metadata = sink;
return sinkInstance;
});
callback(null, sinks, nextQuery, resp);
});
};
/**
* Get a reference to a Cloud Logging log.
*
* @resource [Log Overview]{@link https://cloud.google.com/logging/docs/api/ref_v2beta1/rest/v2beta1/projects.logs}
*
* @param {string} name - Name of the existing log.
* @return {module:logging/log}
*
* @example
* var log = logging.log('my-log');
*/
Logging.prototype.log = function(name) {
return new Log(this, name);
};
/**
* Get a reference to a Cloud Logging sink.
*
* @resource [Sink Overview]{@link https://cloud.google.com/logging/docs/api/ref_v2beta1/rest/v2beta1/projects.sinks}
*
* @param {string} name - Name of the existing sink.
* @return {module:logging/sink}
*
* @example
* var sink = logging.sink('my-sink');
*/
Logging.prototype.sink = function(name) {
return new Sink(this, name);
};
/**
* This method is called when creating a sink with a Bucket destination. The
* bucket must first grant proper ACL access to the Cloud Logging account.
*
* The parameters are the same as what {module:logging#createSink} accepts.
*
* @private
*/
Logging.prototype.setAclForBucket_ = function(name, config, callback) {
var self = this;
var bucket = config.destination;
bucket.acl.owners.addGroup('cloud-logs@google.com', function(err, apiResp) {
if (err) {
callback(err, null, apiResp);
return;
}
config.destination = 'storage.googleapis.com/' + bucket.name;
self.createSink(name, config, callback);
});
};
/**
* This method is called when creating a sink with a Dataset destination. The
* dataset must first grant proper ACL access to the Cloud Logging account.
*
* The parameters are the same as what {module:logging#createSink} accepts.
*
* @private
*/
Logging.prototype.setAclForDataset_ = function(name, config, callback) {
var self = this;
var dataset = config.destination;
dataset.getMetadata(function(err, metadata, apiResp) {
if (err) {
callback(err, null, apiResp);
return;
}
var access = [].slice.call(arrify(metadata.access));
access.push({
role: 'WRITER',
groupByEmail: 'cloud-logs@google.com'
});
dataset.setMetadata({
access: access
}, function(err, apiResp) {
if (err) {
callback(err, null, apiResp);
return;
}
config.destination = format('{baseUrl}/projects/{pId}/datasets/{dId}', {
baseUrl: 'bigquery.googleapis.com',
pId: dataset.parent.projectId,
dId: dataset.id
});
self.createSink(name, config, callback);
});
});
};
/**
* This method is called when creating a sink with a Topic destination. The
* topic must first grant proper ACL access to the Cloud Logging account.
*
* The parameters are the same as what {module:logging#createSink} accepts.
*
* @private
*/
Logging.prototype.setAclForTopic_ = function(name, config, callback) {
var self = this;
var topic = config.destination;
topic.iam.getPolicy(function(err, policy, apiResp) {
if (err) {
callback(err, null, apiResp);
return;
}
policy.bindings = arrify(policy.bindings);
policy.bindings.push({
role: 'roles/pubsub.publisher',
members: [
'serviceAccount:cloud-logs@system.gserviceaccount.com'
]
});
topic.iam.setPolicy(policy, function(err, policy, apiResp) {
if (err) {
callback(err, null, apiResp);
return;
}
config.destination = format('{baseUrl}/{topicName}', {
baseUrl: 'pubsub.googleapis.com',
topicName: topic.name
});
self.createSink(name, config, callback);
});
});
};
/*! Developer Documentation
*
* These methods can be used with either a callback or as a readable object
* stream. `streamRouter` is used to add this dual behavior.
*/
common.streamRouter.extend(Logging, ['getEntries', 'getSinks']);
Logging.Entry = Entry;
Logging.Log = Log;
Logging.Logging = Logging;
Logging.Sink = Sink;
module.exports = Logging;