Skip to content

Commit 0de66c1

Browse files
Move getCurrentTraceFromAgent out of logging package
1 parent 0e13f5a commit 0de66c1

6 files changed

Lines changed: 123 additions & 85 deletions

File tree

packages/logging-bunyan/src/index.js

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,30 @@ LoggingBunyan.prototype.formatEntry_ = function(record) {
186186
return this.log_.entry(entryMetadata, record);
187187
};
188188

189+
/**
190+
* Gets the current fully qualified trace ID when available from the
191+
* @google-cloud/trace-agent library in the LogEntry.trace field format of:
192+
* "projects/[PROJECT-ID]/traces/[TRACE-ID]".
193+
*/
194+
function getCurrentTraceFromAgent() {
195+
var agent = global._google_trace_agent;
196+
if (!agent || !agent.getCurrentContextId || !agent.getWriterProjectId) {
197+
return null;
198+
}
199+
200+
var traceId = agent.getCurrentContextId();
201+
if (!traceId) {
202+
return null;
203+
}
204+
205+
var traceProjectId = agent.getWriterProjectId();
206+
if (!traceProjectId) {
207+
return null;
208+
}
209+
210+
return `projects/${traceProjectId}/traces/${traceId}`;
211+
}
212+
189213
/**
190214
* Intercept log entries as they are written so we can attempt to add the trace
191215
* ID in the same continuation as the function that wrote the log, because the
@@ -197,7 +221,7 @@ LoggingBunyan.prototype.formatEntry_ = function(record) {
197221
LoggingBunyan.prototype.write = function(record, encoding, callback) {
198222
record = extend({}, record);
199223
if (!record[LOGGING_TRACE_KEY]) {
200-
var trace = logging.getCurrentTraceFromAgent();
224+
var trace = getCurrentTraceFromAgent();
201225
if (trace) {
202226
record[LOGGING_TRACE_KEY] = trace;
203227
}

packages/logging-bunyan/test/index.js

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -298,6 +298,7 @@ describe('logging-bunyan', function() {
298298
});
299299

300300
it('should leave prefixed trace property as is if set', function(done) {
301+
var oldTraceAgent = global._google_trace_agent;
301302
global._google_trace_agent = {
302303
getCurrentContextId: function() { return 'trace-from-agent'; },
303304
getWriterProjectId: function() { return 'project1'; }
@@ -315,9 +316,44 @@ describe('logging-bunyan', function() {
315316
};
316317

317318
loggingBunyan.write(recordWithTraceAlreadySet, '', assert.ifError);
319+
320+
global._google_trace_agent = oldTraceAgent;
318321
});
319322
});
320323

324+
it('should not set prefixed trace property if trace unavailable', function() {
325+
FakeWritable.prototype.write = function(record, encoding, callback) {
326+
assert.deepStrictEqual(record, RECORD);
327+
assert.strictEqual(encoding, '');
328+
assert.strictEqual(callback, assert.ifError);
329+
assert.strictEqual(this, loggingBunyan);
330+
};
331+
var oldTraceAgent = global._google_trace_agent;
332+
333+
global._google_trace_agent = {};
334+
loggingBunyan.write(RECORD, '', assert.ifError);
335+
336+
global._google_trace_agent = {
337+
getCurrentContextId: function() { return null; },
338+
getWriterProjectId: function() { return null; }
339+
};
340+
loggingBunyan.write(RECORD, '', assert.ifError);
341+
342+
global._google_trace_agent = {
343+
getCurrentContextId: function() { return null; },
344+
getWriterProjectId: function() { return 'project1'; }
345+
};
346+
loggingBunyan.write(RECORD, '', assert.ifError);
347+
348+
global._google_trace_agent = {
349+
getCurrentContextId: function() { return 'trace1'; },
350+
getWriterProjectId: function() { return null; }
351+
};
352+
loggingBunyan.write(RECORD, '', assert.ifError);
353+
354+
global._google_trace_agent = oldTraceAgent;
355+
});
356+
321357
describe('_write', function() {
322358
beforeEach(function() {
323359
fakeLogInstance.entry = function() {};

packages/logging-winston/src/index.js

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,30 @@ function LoggingWinston(options) {
130130
winston.transports.StackdriverLogging = LoggingWinston;
131131
util.inherits(LoggingWinston, winston.Transport);
132132

133+
/**
134+
* Gets the current fully qualified trace ID when available from the
135+
* @google-cloud/trace-agent library in the LogEntry.trace field format of:
136+
* "projects/[PROJECT-ID]/traces/[TRACE-ID]".
137+
*/
138+
function getCurrentTraceFromAgent() {
139+
var agent = global._google_trace_agent;
140+
if (!agent || !agent.getCurrentContextId || !agent.getWriterProjectId) {
141+
return null;
142+
}
143+
144+
var traceId = agent.getCurrentContextId();
145+
if (!traceId) {
146+
return null;
147+
}
148+
149+
var traceProjectId = agent.getWriterProjectId();
150+
if (!traceProjectId) {
151+
return null;
152+
}
153+
154+
return `projects/${traceProjectId}/traces/${traceId}`;
155+
}
156+
133157
/**
134158
* Relay a log entry to the logging agent. This is normally called by winston.
135159
*
@@ -207,7 +231,7 @@ LoggingWinston.prototype.log = function(levelName, msg, metadata, callback) {
207231
entryMetadata.trace = metadata[LOGGING_TRACE_KEY];
208232
delete data.metadata[LOGGING_TRACE_KEY];
209233
} else {
210-
var trace = logging.getCurrentTraceFromAgent();
234+
var trace = getCurrentTraceFromAgent();
211235
if (trace) {
212236
entryMetadata.trace = trace;
213237
}

packages/logging-winston/test/index.js

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -350,6 +350,43 @@ describe('logging-winston', function() {
350350
global._google_trace_agent = oldTraceAgent;
351351
});
352352

353+
it('should leave out trace metadata if trace unavailable', function() {
354+
loggingWinston.log_.entry = function(entryMetadata, data) {
355+
assert.deepStrictEqual(entryMetadata, {
356+
resource: loggingWinston.resource_,
357+
});
358+
assert.deepStrictEqual(data, {
359+
message: MESSAGE,
360+
metadata: METADATA
361+
});
362+
};
363+
364+
var oldTraceAgent = global._google_trace_agent;
365+
366+
global._google_trace_agent = {};
367+
loggingWinston.log(LEVEL, MESSAGE, METADATA, assert.ifError);
368+
369+
global._google_trace_agent = {
370+
getCurrentContextId: function() { return null; },
371+
getWriterProjectId: function() { return null; }
372+
};
373+
loggingWinston.log(LEVEL, MESSAGE, METADATA, assert.ifError);
374+
375+
global._google_trace_agent = {
376+
getCurrentContextId: function() { return null; },
377+
getWriterProjectId: function() { return 'project1'; }
378+
};
379+
loggingWinston.log(LEVEL, MESSAGE, METADATA, assert.ifError);
380+
381+
global._google_trace_agent = {
382+
getCurrentContextId: function() { return 'trace1'; },
383+
getWriterProjectId: function() { return null; }
384+
};
385+
loggingWinston.log(LEVEL, MESSAGE, METADATA, assert.ifError);
386+
387+
global._google_trace_agent = oldTraceAgent;
388+
});
389+
353390
it('should write to the log', function(done) {
354391
var entry = {};
355392

packages/logging/src/index.js

Lines changed: 0 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -773,41 +773,6 @@ Logging.prototype.setAclForTopic_ = function(name, config, callback) {
773773
});
774774
};
775775

776-
/**
777-
* Attempts to get the current fully qualified trace ID from the
778-
* @google-cloud/trace-agent (if installed) in the format
779-
* "projects/[PROJECT-ID]/traces/[TRACE-ID]".
780-
*
781-
* That is the specified format to set the LogEntry.trace field so that the
782-
* Cloud Console logs and trace viewer can correlate log entries and associate
783-
* them with traces.
784-
*
785-
* This returns null if the trace agent is not installed, there is no available
786-
* trace context or no trace writer project ID.
787-
*/
788-
function getCurrentTraceFromAgent() {
789-
var traceAgent = global._google_trace_agent;
790-
if (!traceAgent || !traceAgent.getCurrentContextId ||
791-
!traceAgent.getWriterProjectId) {
792-
// Trace agent is not installed or is an older version
793-
return null;
794-
}
795-
796-
var traceId = traceAgent.getCurrentContextId();
797-
if (!traceId) {
798-
// No trace context because request not sampled or context lost
799-
return null;
800-
}
801-
802-
var traceProjectId = traceAgent.getWriterProjectId();
803-
if (!traceProjectId) {
804-
// No project, possibly because async project auto-discovery not yet done
805-
return null;
806-
}
807-
808-
return `projects/${traceProjectId}/traces/${traceId}`;
809-
}
810-
811776
/*! Developer Documentation
812777
*
813778
* These methods can be auto-paginated.
@@ -832,7 +797,6 @@ Logging.Entry = Entry;
832797
Logging.Log = Log;
833798
Logging.Logging = Logging;
834799
Logging.Sink = Sink;
835-
Logging.getCurrentTraceFromAgent = getCurrentTraceFromAgent;
836800

837801
module.exports = Logging;
838802
module.exports.v2 = v2;

packages/logging/test/index.js

Lines changed: 0 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1379,51 +1379,4 @@ describe('Logging', function() {
13791379
});
13801380
});
13811381
});
1382-
1383-
describe('getCurrentTraceFromAgent', function() {
1384-
var oldTraceAgent;
1385-
1386-
beforeEach(function() {
1387-
oldTraceAgent = global._google_trace_agent;
1388-
});
1389-
1390-
afterEach(function() {
1391-
global._google_trace_agent = oldTraceAgent;
1392-
});
1393-
1394-
it('returns null if there is no trace agent', function() {
1395-
global._google_trace_agent = undefined;
1396-
assert.strictEqual(Logging.getCurrentTraceFromAgent(), null);
1397-
});
1398-
1399-
it('returns null if agent does not support context methods', function() {
1400-
global._google_trace_agent = {};
1401-
assert.strictEqual(Logging.getCurrentTraceFromAgent(), null);
1402-
});
1403-
1404-
it('returns null if context id is missing', function() {
1405-
global._google_trace_agent = {
1406-
getCurrentContextId: function() { return null; },
1407-
getWriterProjectId: function() { return 'project1'; }
1408-
};
1409-
assert.strictEqual(Logging.getCurrentTraceFromAgent(), null);
1410-
});
1411-
1412-
it('returns null if project id is missing', function() {
1413-
global._google_trace_agent = {
1414-
getCurrentContextId: function() { return 'trace1'; },
1415-
getWriterProjectId: function() { return null; }
1416-
};
1417-
assert.strictEqual(Logging.getCurrentTraceFromAgent(), null);
1418-
});
1419-
1420-
it('returns trace path if project and context available', function() {
1421-
global._google_trace_agent = {
1422-
getCurrentContextId: function() { return 'trace1'; },
1423-
getWriterProjectId: function() { return 'project1'; }
1424-
};
1425-
assert.strictEqual(Logging.getCurrentTraceFromAgent(),
1426-
'projects/project1/traces/trace1');
1427-
});
1428-
});
14291382
});

0 commit comments

Comments
 (0)