Skip to content

Commit dd5a482

Browse files
stephenpluspluscallmehiphop
authored andcommitted
logging: change resource argument to metadata (#1666)
1 parent baf0986 commit dd5a482

7 files changed

Lines changed: 137 additions & 142 deletions

File tree

README.md

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1114,15 +1114,17 @@ loggingClient.createSink('my-new-sink', {
11141114
// Write a critical entry to a log.
11151115
var syslog = loggingClient.log('syslog');
11161116

1117-
var resource = {
1118-
type: 'gce_instance',
1119-
labels: {
1120-
zone: 'global',
1121-
instance_id: '3'
1117+
var metadata = {
1118+
resource: {
1119+
type: 'gce_instance',
1120+
labels: {
1121+
zone: 'global',
1122+
instance_id: '3'
1123+
}
11221124
}
11231125
};
11241126

1125-
var entry = syslog.entry(resource, {
1127+
var entry = syslog.entry(metadata, {
11261128
delegate: process.env.user
11271129
});
11281130

packages/logging/README.md

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,15 +27,17 @@ logging.createSink('my-new-sink', {
2727
// Write a critical entry to a log.
2828
var syslog = logging.log('syslog');
2929

30-
var resource = {
31-
type: 'gce_instance',
32-
labels: {
33-
zone: 'global',
34-
instance_id: '3'
30+
var metadata = {
31+
resource: {
32+
type: 'gce_instance',
33+
labels: {
34+
zone: 'global',
35+
instance_id: '3'
36+
}
3537
}
3638
};
3739

38-
var entry = syslog.entry(resource, {
40+
var entry = syslog.entry(metadata, {
3941
delegate: process.env.user
4042
});
4143

packages/logging/src/entry.js

Lines changed: 18 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,8 @@ var isCircular = require('is-circular');
3333
* @alias module:logging/entry
3434
* @constructor
3535
*
36-
* @param {object=|string=} resource - See a
37-
* [Monitored Resource](https://cloud.google.com/logging/docs/api/ref_v2beta1/rest/v2beta1/MonitoredResource).
36+
* @param {object=} metadata - See a
37+
* [LogEntry Resource](https://cloud.google.com/logging/docs/api/ref_v2beta1/rest/v2beta1/LogEntry).
3838
* @param {object|string} data - The data to use as the value for this log
3939
* entry.
4040
*
@@ -52,15 +52,17 @@ var isCircular = require('is-circular');
5252
* @example
5353
* var syslog = logging.log('syslog');
5454
*
55-
* var resource = {
56-
* type: 'gce_instance',
57-
* labels: {
58-
* zone: 'global',
59-
* instance_id: '3'
55+
* var metadata = {
56+
* resource: {
57+
* type: 'gce_instance',
58+
* labels: {
59+
* zone: 'global',
60+
* instance_id: '3'
61+
* }
6062
* }
6163
* };
6264
*
63-
* var entry = syslog.entry(resource, {
65+
* var entry = syslog.entry(metadata, {
6466
* delegate: 'my_username'
6567
* });
6668
*
@@ -80,13 +82,8 @@ var isCircular = require('is-circular');
8082
* }
8183
* });
8284
*/
83-
function Entry(resource, data) {
84-
if (!data) {
85-
this.data = resource;
86-
return;
87-
}
88-
89-
this.resource = resource;
85+
function Entry(metadata, data) {
86+
this.metadata = metadata;
9087
this.data = data;
9188
}
9289

@@ -106,12 +103,12 @@ Entry.fromApiResponse_ = function(entry) {
106103
data = common.GrpcService.structToObj_(data);
107104
}
108105

109-
var serializedEntry = extend(new Entry(entry.resource, data), entry);
106+
var serializedEntry = new Entry(entry, data);
110107

111-
if (serializedEntry.timestamp) {
112-
var ms = serializedEntry.timestamp.seconds * 1000;
113-
ms += serializedEntry.timestamp.nanos / 1e6;
114-
serializedEntry.timestamp = new Date(ms);
108+
if (serializedEntry.metadata.timestamp) {
109+
var ms = serializedEntry.metadata.timestamp.seconds * 1000;
110+
ms += serializedEntry.metadata.timestamp.nanos / 1e6;
111+
serializedEntry.metadata.timestamp = new Date(ms);
115112
}
116113

117114
return serializedEntry;
@@ -127,30 +124,7 @@ Entry.prototype.toJSON = function() {
127124
throw new Error('The JSON data for this entry has a circular reference.');
128125
}
129126

130-
var entry = extend(true, {}, this);
131-
132-
var whitelist = [
133-
'logName',
134-
'resource',
135-
'timestamp',
136-
'severity',
137-
'insertId',
138-
'httpRequest',
139-
'labels',
140-
'operation'
141-
];
142-
143-
for (var prop in entry) {
144-
if (whitelist.indexOf(prop) === -1) {
145-
delete entry[prop];
146-
}
147-
}
148-
149-
if (is.string(this.resource)) {
150-
entry.resource = {
151-
type: this.resource
152-
};
153-
}
127+
var entry = extend(true, {}, this.metadata);
154128

155129
if (is.object(this.data)) {
156130
entry.jsonPayload = common.GrpcService.objToStruct_(this.data, {

packages/logging/src/log.js

Lines changed: 25 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -101,8 +101,10 @@ util.inherits(Log, common.GrpcServiceObject);
101101
*/
102102
Log.assignSeverityToEntries_ = function(entries, severity) {
103103
return arrify(entries).map(function(entry) {
104-
return extend(new Entry(), entry, {
105-
severity: severity
104+
return extend(true, new Entry(), entry, {
105+
metadata: {
106+
severity: severity
107+
}
106108
});
107109
});
108110
};
@@ -206,22 +208,24 @@ Log.prototype.emergency = function(entry, options, callback) {
206208
*
207209
* @resource [LogEntry JSON representation]{@link https://cloud.google.com/logging/docs/api/ref_v2beta1/rest/v2beta1/LogEntry}
208210
*
209-
* @param {object=|string=} resource - See a
210-
* [Monitored Resource](https://cloud.google.com/logging/docs/api/ref_v2beta1/rest/v2beta1/MonitoredResource).
211+
* @param {object=} metadata - See a
212+
* [LogEntry Resource](https://cloud.google.com/logging/docs/api/ref_v2beta1/rest/v2beta1/LogEntry).
211213
* @param {object|string} data - The data to use as the value for this log
212214
* entry.
213215
* @return {module:logging/entry}
214216
*
215217
* @example
216-
* var resource = {
217-
* type: 'gce_instance',
218-
* labels: {
219-
* zone: 'global',
220-
* instance_id: '3'
218+
* var metadata = {
219+
* resource: {
220+
* type: 'gce_instance',
221+
* labels: {
222+
* zone: 'global',
223+
* instance_id: '3'
224+
* }
221225
* }
222226
* };
223227
*
224-
* var entry = log.entry(resource, {
228+
* var entry = log.entry(metadata, {
225229
* delegate: 'my_username'
226230
* });
227231
*
@@ -240,10 +244,17 @@ Log.prototype.emergency = function(entry, options, callback) {
240244
* // }
241245
* // }
242246
*/
243-
Log.prototype.entry = function(resource, data) {
244-
var entryInstance = this.parent.entry(resource, data);
245-
entryInstance.logName = this.formattedName_;
246-
return entryInstance;
247+
Log.prototype.entry = function(metadata, data) {
248+
if (!data) {
249+
data = metadata;
250+
metadata = {};
251+
}
252+
253+
metadata = extend({}, metadata, {
254+
logName: this.formattedName_
255+
});
256+
257+
return this.parent.entry(metadata, data);
247258
};
248259

249260
/**

packages/logging/system-test/logging.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ var assert = require('assert');
2020
var async = require('async');
2121
var BigQuery = require('@google-cloud/bigquery');
2222
var exec = require('methmeth');
23+
var extend = require('extend');
2324
var format = require('string-format-obj');
2425
var is = require('is');
2526
var prop = require('propprop');
@@ -375,6 +376,35 @@ describe('Logging', function() {
375376
});
376377
});
377378

379+
it('should write a log with metadata', function(done) {
380+
var metadata = extend({}, options, {
381+
severity: 'DEBUG'
382+
});
383+
384+
var data = {
385+
embeddedData: true
386+
};
387+
388+
var logEntry = log.entry(metadata, data);
389+
390+
log.write(logEntry, function(err) {
391+
assert.ifError(err);
392+
393+
setTimeout(function() {
394+
log.getEntries({ pageSize: 1 }, function(err, entries) {
395+
assert.ifError(err);
396+
397+
var entry = entries[0];
398+
399+
assert.strictEqual(entry.metadata.severity, metadata.severity);
400+
assert.deepEqual(entry.data, data);
401+
402+
done();
403+
});
404+
}, WRITE_CONSISTENCY_DELAY_MS);
405+
});
406+
});
407+
378408
it('should write to a log with alert helper', function(done) {
379409
log.alert(logEntries, options, done);
380410
});

packages/logging/test/entry.js

Lines changed: 9 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ describe('Entry', function() {
2727
var Entry;
2828
var entry;
2929

30-
var RESOURCE = {};
30+
var METADATA = {};
3131
var DATA = {};
3232

3333
before(function() {
@@ -40,23 +40,18 @@ describe('Entry', function() {
4040

4141
beforeEach(function() {
4242
extend(FakeGrpcService, GrpcService);
43-
entry = new Entry(RESOURCE, DATA);
43+
entry = new Entry(METADATA, DATA);
4444
});
4545

4646
describe('instantiation', function() {
47-
it('should treat resource as data if data is not provided', function() {
48-
var entry = new Entry(DATA);
49-
assert.strictEqual(entry.data, DATA);
50-
assert.strictEqual(entry.resource, undefined);
51-
});
52-
53-
it('should localize resource and data', function() {
54-
assert.strictEqual(entry.resource, RESOURCE);
47+
it('should localize metadata and data', function() {
48+
assert.strictEqual(entry.metadata, METADATA);
5549
assert.strictEqual(entry.data, DATA);
5650
});
5751
});
5852

5953
describe('fromApiResponse_', function() {
54+
var RESOURCE = {};
6055
var entry;
6156
var date = new Date();
6257

@@ -82,10 +77,10 @@ describe('Entry', function() {
8277

8378
it('should create an Entry', function() {
8479
assert(entry instanceof Entry);
85-
assert.strictEqual(entry.resource, RESOURCE);
80+
assert.strictEqual(entry.metadata.resource, RESOURCE);
8681
assert.strictEqual(entry.data, DATA);
87-
assert.strictEqual(entry.extraProperty, true);
88-
assert.deepEqual(entry.timestamp, date);
82+
assert.strictEqual(entry.metadata.extraProperty, true);
83+
assert.deepEqual(entry.metadata.timestamp, date);
8984
});
9085

9186
it('should extend the entry with proto data', function() {
@@ -123,52 +118,6 @@ describe('Entry', function() {
123118
assert.deepEqual(entryBefore, entryAfter);
124119
});
125120

126-
it('should only include correct properties', function() {
127-
var propertiesToInclude = [
128-
'logName',
129-
'resource',
130-
'timestamp',
131-
'severity',
132-
'insertId',
133-
'httpRequest',
134-
'labels',
135-
'operation'
136-
];
137-
138-
var value = 'value';
139-
140-
propertiesToInclude.forEach(function(property) {
141-
entry[property] = value;
142-
});
143-
144-
entry.extraProperty = true;
145-
146-
var json = entry.toJSON();
147-
148-
assert(propertiesToInclude.every(function(property) {
149-
if (property === 'resource') {
150-
return json[property].type === value;
151-
}
152-
153-
return json[property] === value;
154-
}));
155-
156-
// Was removed for JSON representation...
157-
assert.strictEqual(json.extraProperty, undefined);
158-
// ...but still exists on the Entry.
159-
assert.strictEqual(entry.extraProperty, true);
160-
});
161-
162-
it('should convert a string resource to an object', function() {
163-
entry.resource = 'resource-name';
164-
165-
var json = entry.toJSON();
166-
167-
assert.deepEqual(json.resource, {
168-
type: entry.resource
169-
});
170-
});
171-
172121
it('should convert data as a struct and assign to jsonPayload', function() {
173122
var input = {};
174123
var converted = {};
@@ -203,7 +152,7 @@ describe('Entry', function() {
203152

204153
it('should convert a date', function() {
205154
var date = new Date();
206-
entry.timestamp = date;
155+
entry.metadata.timestamp = date;
207156

208157
var json = entry.toJSON();
209158

0 commit comments

Comments
 (0)