Skip to content

Commit 08cdfe0

Browse files
ofrobotsstephenplusplus
authored andcommitted
logging: omit resource from each log entry (#2309)
1 parent b1fbd2e commit 08cdfe0

6 files changed

Lines changed: 77 additions & 166 deletions

File tree

packages/logging/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,6 @@
5555
"@google-cloud/common": "^0.13.0",
5656
"@google-cloud/common-grpc": "^0.3.0",
5757
"arrify": "^1.0.0",
58-
"async": "^2.1.4",
5958
"eventid": "^0.1.0",
6059
"extend": "^3.0.0",
6160
"google-auto-auth": "^0.7.0",
@@ -71,6 +70,7 @@
7170
"@google-cloud/bigquery": "*",
7271
"@google-cloud/pubsub": "*",
7372
"@google-cloud/storage": "*",
73+
"async": "^2.1.4",
7474
"methmeth": "^1.0.0",
7575
"mocha": "^3.0.1",
7676
"propprop": "^0.3.0",

packages/logging/src/log.js

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

2323
var arrify = require('arrify');
24-
var async = require('async');
2524
var common = require('@google-cloud/common');
2625
var extend = require('extend');
2726
var is = require('is');
@@ -589,12 +588,27 @@ Log.prototype.write = function(entry, options, callback) {
589588
options = {};
590589
}
591590

592-
this.decorateEntries_(arrify(entry), function(err, decoratedEntries) {
593-
// Ignore errors (the API will speak up if it has an issue).
591+
if (!options.resource) {
592+
this.metadata_.getDefaultResource(function(err, resource) {
593+
// Ignore errors (the API will speak up if it has an issue).
594+
writeWithResource(resource);
595+
});
596+
} else {
597+
writeWithResource(options.resource);
598+
}
599+
600+
function writeWithResource(resource) {
601+
var decoratedEntries;
602+
try {
603+
decoratedEntries = self.decorateEntries_(arrify(entry));
604+
} catch (err) {
605+
// Ignore errors (the API will speak up if it has an issue).
606+
}
594607

595608
var reqOpts = extend({
596609
logName: self.formattedName_,
597-
entries: decoratedEntries
610+
entries: decoratedEntries,
611+
resource: resource
598612
}, options);
599613

600614
delete reqOpts.gaxOptions;
@@ -605,41 +619,30 @@ Log.prototype.write = function(entry, options, callback) {
605619
reqOpts: reqOpts,
606620
gaxOpts: options.gaxOptions
607621
}, callback);
608-
});
622+
}
609623
};
610624

611625
/**
612-
* All entries are passed through here to make sure this log is attached to the
613-
* entry.
626+
* All entries are passed through here in order to get them serialized.
614627
*
615628
* @private
616629
*
617-
* @param {object} entry - An entry object.
630+
* @param {object[]} entries - Entry objects.
631+
* @return {object[]} Serialized entries.
632+
* @throws if there is an error during serialization.
618633
*/
619-
Log.prototype.decorateEntries_ = function(entries, callback) {
634+
Log.prototype.decorateEntries_ = function(entries) {
620635
var self = this;
621636

622-
async.map(entries, function(entry, callback) {
637+
return entries.map(function(entry) {
623638
if (!(entry instanceof Entry)) {
624639
entry = self.entry(entry);
625640
}
626641

627-
var decoratedEntry;
628-
629-
try {
630-
decoratedEntry = entry.toJSON({
631-
removeCircular: self.removeCircular_
632-
});
633-
} catch(e) {
634-
callback(e);
635-
return;
636-
}
637-
638-
self.metadata_.assignDefaultResource(decoratedEntry, function(err, entry) {
639-
// Ignore errors (the API will speak up if it has an issue).
640-
callback(null, entry || decoratedEntry);
642+
return entry.toJSON({
643+
removeCircular: self.removeCircular_
641644
});
642-
}, callback);
645+
});
643646
};
644647

645648
/*! Developer Documentation

packages/logging/src/metadata.js

Lines changed: 0 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -123,32 +123,6 @@ Metadata.getGlobalDescriptor = function(projectId) {
123123
};
124124
};
125125

126-
/**
127-
* Assigns an entry with a default resource object.
128-
*
129-
* @param {object} entryJson - The entry object to assign a resource to.
130-
* @param {function} callback - The callback function.
131-
*/
132-
Metadata.prototype.assignDefaultResource = function(entryJson, callback) {
133-
if (entryJson.resource) {
134-
setImmediate(function() {
135-
callback(null, entryJson);
136-
});
137-
return;
138-
}
139-
140-
this.getDefaultResource(function(err, resource) {
141-
if (err) {
142-
callback(err);
143-
return;
144-
}
145-
146-
entryJson.resource = resource;
147-
148-
callback(null, entryJson);
149-
});
150-
};
151-
152126
/**
153127
* Retrieve a resource object describing the current environment.
154128
*

packages/logging/system-test/logging.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -495,7 +495,7 @@ describe('Logging', function() {
495495
assert.deepEqual(entry.metadata.resource, {
496496
type: 'global',
497497
labels: {
498-
project_id: logging.projectId
498+
project_id: env.projectId
499499
}
500500
});
501501

packages/logging/test/log.js

Lines changed: 47 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -326,14 +326,40 @@ describe('Log', function() {
326326

327327
describe('write', function() {
328328
var ENTRY = {};
329-
var OPTIONS = {
330-
resource: {}
331-
};
329+
var OPTIONS = {};
330+
var FAKE_RESOURCE = 'fake-resource';
332331

333332
beforeEach(function() {
334-
log.decorateEntries_ = function(entries, callback) {
335-
callback(null, entries);
333+
log.decorateEntries_ = function(entries) {
334+
return entries;
335+
};
336+
log.metadata_.getDefaultResource = function(callback) {
337+
callback(null, FAKE_RESOURCE);
338+
};
339+
});
340+
341+
it('should forward options.resource to request', function(done) {
342+
var CUSTOM_RESOURCE = 'custom-resource';
343+
var optionsWithResource = extend({}, OPTIONS, {
344+
resource: CUSTOM_RESOURCE
345+
});
346+
347+
log.logging.request = function(config, callback) {
348+
assert.strictEqual(config.client, 'loggingServiceV2Client');
349+
assert.strictEqual(config.method, 'writeLogEntries');
350+
351+
assert.deepEqual(config.reqOpts, {
352+
logName: log.formattedName_,
353+
entries: [ENTRY],
354+
resource: CUSTOM_RESOURCE
355+
});
356+
357+
assert.strictEqual(config.gaxOpts, undefined);
358+
359+
callback();
336360
};
361+
362+
log.write(ENTRY, optionsWithResource, done);
337363
});
338364

339365
it('should make the correct API request', function(done) {
@@ -344,7 +370,7 @@ describe('Log', function() {
344370
assert.deepEqual(config.reqOpts, {
345371
logName: log.formattedName_,
346372
entries: [ENTRY],
347-
resource: {}
373+
resource: FAKE_RESOURCE
348374
});
349375

350376
assert.strictEqual(config.gaxOpts, undefined);
@@ -358,9 +384,9 @@ describe('Log', function() {
358384
it('should arrify & decorate the entries', function(done) {
359385
var decoratedEntries = [];
360386

361-
log.decorateEntries_ = function(entries, callback) {
387+
log.decorateEntries_ = function(entries) {
362388
assert.strictEqual(entries[0], ENTRY);
363-
callback(null, decoratedEntries);
389+
return decoratedEntries;
364390
};
365391

366392
log.logging.request = function(config) {
@@ -639,36 +665,30 @@ describe('Log', function() {
639665
};
640666
});
641667

642-
it('should create an Entry object if one is not provided', function(done) {
668+
it('should create an Entry object if one is not provided', function() {
643669
var entry = {};
644670

645671
log.entry = function(entry_) {
646672
assert.strictEqual(entry_, entry);
647673
return new FakeEntry();
648674
};
649675

650-
log.decorateEntries_([entry], function(err, decoratedEntries) {
651-
assert.ifError(err);
652-
assert.strictEqual(decoratedEntries[0], toJSONResponse);
653-
done();
654-
});
676+
var decoratedEntries = log.decorateEntries_([entry]);
677+
assert.strictEqual(decoratedEntries[0], toJSONResponse);
655678
});
656679

657-
it('should get JSON format from Entry object', function(done) {
680+
it('should get JSON format from Entry object', function() {
658681
log.entry = function() {
659-
done(); // will result in multiple done() calls and fail the test.
682+
throw new Error('should not be called');
660683
};
661684

662685
var entry = new Entry();
663686
entry.toJSON = function() {
664687
return toJSONResponse;
665688
};
666689

667-
log.decorateEntries_([entry], function(err, decoratedEntries) {
668-
assert.ifError(err);
669-
assert.strictEqual(decoratedEntries[0], toJSONResponse);
670-
done();
671-
});
690+
var decoratedEntries = log.decorateEntries_([entry]);
691+
assert.strictEqual(decoratedEntries[0], toJSONResponse);
672692
});
673693

674694
it('should pass log.removeCircular to toJSON', function(done) {
@@ -681,59 +701,22 @@ describe('Log', function() {
681701
return {};
682702
};
683703

684-
log.decorateEntries_([entry], assert.ifError);
704+
log.decorateEntries_([entry]);
685705
});
686706

687-
it('should exec callback with error from serialization', function(done) {
707+
it('should throw error from serialization', function() {
688708
var error = new Error('Error.');
689709

690710
var entry = new Entry();
691711
entry.toJSON = function() {
692712
throw error;
693713
};
694714

695-
log.decorateEntries_([entry], function(err) {
715+
try {
716+
log.decorateEntries_([entry]);
717+
} catch (err) {
696718
assert.strictEqual(err, error);
697-
done();
698-
});
699-
});
700-
701-
it('should return extended entry with default resource', function(done) {
702-
var entry = new FakeEntry();
703-
entry.toJSON = function() {
704-
return toJSONResponse;
705-
};
706-
707-
var entryWithDefaultResource = {};
708-
709-
log.metadata_.assignDefaultResource = function(entryJson, callback) {
710-
assert.strictEqual(entryJson, toJSONResponse);
711-
callback(null, entryWithDefaultResource);
712-
};
713-
714-
log.decorateEntries_([entry], function(err, decoratedEntries) {
715-
assert.ifError(err);
716-
assert.strictEqual(decoratedEntries[0], entryWithDefaultResource);
717-
done();
718-
});
719-
});
720-
721-
it('should return original entry without resource', function(done) {
722-
var entry = new Entry();
723-
entry.toJSON = function() {
724-
return toJSONResponse;
725-
};
726-
727-
log.metadata_.assignDefaultResource = function(entryJson, callback) {
728-
assert.strictEqual(entryJson, toJSONResponse);
729-
callback();
730-
};
731-
732-
log.decorateEntries_([entry], function(err, decoratedEntries) {
733-
assert.ifError(err);
734-
assert.strictEqual(decoratedEntries[0], toJSONResponse);
735-
done();
736-
});
719+
}
737720
});
738721
});
739722
});

packages/logging/test/metadata.js

Lines changed: 0 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -136,55 +136,6 @@ describe('metadata', function() {
136136
});
137137
});
138138

139-
describe('assignDefaultResource', function() {
140-
var ENTRY_JSON = {};
141-
142-
it('should return entry if it already has a resource', function(done) {
143-
var entryJson = { resource: {} };
144-
145-
metadata.assignDefaultResource(entryJson, function(err, entryJson_) {
146-
assert.ifError(err);
147-
assert.strictEqual(entryJson_, entryJson);
148-
done();
149-
});
150-
});
151-
152-
it('should get the default resource', function(done) {
153-
metadata.getDefaultResource = function() {
154-
done();
155-
};
156-
157-
metadata.assignDefaultResource(ENTRY_JSON, assert.ifError);
158-
});
159-
160-
it('should return error from getDefaultResource', function(done) {
161-
var error = new Error('Error.');
162-
163-
metadata.getDefaultResource = function(callback) {
164-
callback(error);
165-
};
166-
167-
metadata.assignDefaultResource(ENTRY_JSON, function(err) {
168-
assert.strictEqual(err, error);
169-
done();
170-
});
171-
});
172-
173-
it('should assign default resource to entry', function(done) {
174-
var defaultResource = {};
175-
176-
metadata.getDefaultResource = function(callback) {
177-
callback(null, defaultResource);
178-
};
179-
180-
metadata.assignDefaultResource(ENTRY_JSON, function(err, entryJson) {
181-
assert.ifError(err);
182-
assert.strictEqual(entryJson.resource, defaultResource);
183-
done();
184-
});
185-
});
186-
});
187-
188139
describe('getDefaultResource', function() {
189140
var RETURNED_PROJECT_ID = 'project-id';
190141

0 commit comments

Comments
 (0)