Skip to content

Commit 31b483e

Browse files
Add default resource generator for log entries
To accommodate invocations like: `syslog.entry({textPayload: 'test'})` Create a basic class which attempts to automatically establish resource values based on connectivity to the metadata service and environmental variables. This addition does not affect the existing API and does not modify or augment user supplied resources. The meta data class will attempt to determine if the resource is a cloud function, app engine application or compute engine instance. If it can't match on any of these configurations but can still locate a project id then it will produce a global resource id.
1 parent 8047652 commit 31b483e

7 files changed

Lines changed: 789 additions & 11 deletions

File tree

packages/logging/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@
5454
"dependencies": {
5555
"@google-cloud/common": "^0.8.0",
5656
"arrify": "^1.0.0",
57+
"duplexify": "^3.5.0",
5758
"extend": "^3.0.0",
5859
"google-gax": "^0.9.1",
5960
"google-proto-files": "^0.8.0",

packages/logging/src/index.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
'use strict';
2222

2323
var arrify = require('arrify');
24+
var duplexify = require('duplexify');
2425
var common = require('@google-cloud/common');
2526
var extend = require('extend');
2627
var format = require('string-format-obj');
@@ -46,6 +47,12 @@ var Log = require('./log.js');
4647
*/
4748
var Sink = require('./sink.js');
4849

50+
/**
51+
* @type {module:metdata}
52+
* @private
53+
*/
54+
var Metadata = require('./metadata.js');
55+
4956
/**
5057
* <p class="notice">
5158
* **This is a Beta release of Stackdriver Logging.** This API is not covered
@@ -87,6 +94,19 @@ function Logging(options) {
8794
};
8895

8996
common.GrpcService.call(this, config, options);
97+
var requestStream = duplexify();
98+
common.util.makeRequest({
99+
url: 'http://metadata.google.internal/computeMetadata/v1/project/' +
100+
'project-id',
101+
headers: {
102+
'Metadata-Flavor': 'Google'
103+
}
104+
},
105+
{
106+
stream: requestStream,
107+
method: 'GET'
108+
});
109+
this.metadata = new Metadata(requestStream);
90110
}
91111

92112
util.inherits(Logging, common.GrpcService);

packages/logging/src/log.js

Lines changed: 61 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ var Entry = require('./entry.js');
5252
function Log(logging, name) {
5353
this.formattedName_ = Log.formatName_(logging.projectId, name);
5454
this.name = this.formattedName_.split('/').pop();
55+
this.metadata_ = logging.metadata;
5556

5657
var methods = {
5758
/**
@@ -136,6 +137,27 @@ Log.formatName_ = function(projectId, name) {
136137
return path + name;
137138
};
138139

140+
Log.prototype.assignDefaultMetadata_ = function(entry, callback) {
141+
var entryMetadata = entry.metadata;
142+
// If the resource specifcally was not supplied attempt to determine it
143+
// automatically
144+
if (!is.object(entryMetadata) || !is.object(entryMetadata.resource)) {
145+
this.metadata_.getDefaultResource(function(err, resource) {
146+
if (err) {
147+
callback(err);
148+
return;
149+
}
150+
// Merge the entry's metadata with the auto generated resource; give
151+
// precedence to any entry originated metadata information
152+
entry.metadata = extend(true, {resource: resource}, entryMetadata);
153+
callback(null);
154+
});
155+
return;
156+
}
157+
// Otherwise don't modify or augment user supplied resource data
158+
callback(null);
159+
};
160+
139161
/**
140162
* Write a log entry with a severity of "ALERT".
141163
*
@@ -157,7 +179,10 @@ Log.formatName_ = function(projectId, name) {
157179
* });
158180
*/
159181
Log.prototype.alert = function(entry, options, callback) {
160-
this.write(Log.assignSeverityToEntries_(entry, 'ALERT'), options, callback);
182+
var self = this;
183+
this.assignDefaultMetadata_(entry, function() {
184+
self.write(Log.assignSeverityToEntries_(entry, 'ALERT'), options, callback);
185+
});
161186
};
162187

163188
/**
@@ -181,8 +206,11 @@ Log.prototype.alert = function(entry, options, callback) {
181206
* });
182207
*/
183208
Log.prototype.critical = function(entry, options, callback) {
184-
var entries = Log.assignSeverityToEntries_(entry, 'CRITICAL');
185-
this.write(entries, options, callback);
209+
var self = this;
210+
this.assignDefaultMetadata_(entry, function() {
211+
self.write(Log.assignSeverityToEntries_(entry, 'CRITICAL'), options,
212+
callback);
213+
});
186214
};
187215

188216
/**
@@ -206,7 +234,11 @@ Log.prototype.critical = function(entry, options, callback) {
206234
* });
207235
*/
208236
Log.prototype.debug = function(entry, options, callback) {
209-
this.write(Log.assignSeverityToEntries_(entry, 'DEBUG'), options, callback);
237+
var self = this;
238+
this.assignDefaultMetadata_(entry, function() {
239+
self.write(Log.assignSeverityToEntries_(entry, 'DEBUG'), options,
240+
callback);
241+
});
210242
};
211243

212244
/**
@@ -230,8 +262,11 @@ Log.prototype.debug = function(entry, options, callback) {
230262
* });
231263
*/
232264
Log.prototype.emergency = function(entry, options, callback) {
233-
var entries = Log.assignSeverityToEntries_(entry, 'EMERGENCY');
234-
this.write(entries, options, callback);
265+
var self = this;
266+
this.assignDefaultMetadata_(entry, function() {
267+
self.write(Log.assignSeverityToEntries_(entry, 'EMERGENCY'), options,
268+
callback);
269+
});
235270
};
236271

237272
/**
@@ -313,7 +348,11 @@ Log.prototype.entry = function(metadata, data) {
313348
* });
314349
*/
315350
Log.prototype.error = function(entry, options, callback) {
316-
this.write(Log.assignSeverityToEntries_(entry, 'ERROR'), options, callback);
351+
var self = this;
352+
this.assignDefaultMetadata_(entry, function() {
353+
self.write(Log.assignSeverityToEntries_(entry, 'ERROR'), options,
354+
callback);
355+
});
317356
};
318357

319358
/**
@@ -439,7 +478,11 @@ Log.prototype.getEntriesStream = function(options) {
439478
* });
440479
*/
441480
Log.prototype.info = function(entry, options, callback) {
442-
this.write(Log.assignSeverityToEntries_(entry, 'INFO'), options, callback);
481+
var self = this;
482+
this.assignDefaultMetadata_(entry, function() {
483+
self.write(Log.assignSeverityToEntries_(entry, 'INFO'), options,
484+
callback);
485+
});
443486
};
444487

445488
/**
@@ -463,7 +506,11 @@ Log.prototype.info = function(entry, options, callback) {
463506
* });
464507
*/
465508
Log.prototype.notice = function(entry, options, callback) {
466-
this.write(Log.assignSeverityToEntries_(entry, 'NOTICE'), options, callback);
509+
var self = this;
510+
this.assignDefaultMetadata_(entry, function() {
511+
self.write(Log.assignSeverityToEntries_(entry, 'NOTICE'), options,
512+
callback);
513+
});
467514
};
468515

469516
/**
@@ -487,7 +534,11 @@ Log.prototype.notice = function(entry, options, callback) {
487534
* });
488535
*/
489536
Log.prototype.warning = function(entry, options, callback) {
490-
this.write(Log.assignSeverityToEntries_(entry, 'WARNING'), options, callback);
537+
var self = this;
538+
this.assignDefaultMetadata_(entry, function() {
539+
self.write(Log.assignSeverityToEntries_(entry, 'WARNING'), options,
540+
callback);
541+
});
491542
};
492543

493544
/**

0 commit comments

Comments
 (0)