Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

logging-bunyan: HttpRequest support #2325

Merged
merged 3 commits into from
May 25, 2017
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions packages/logging-bunyan/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,29 @@ var loggingBunyan = require('@google-cloud/logging-bunyan')({
// ...you're good to go!
```

## Formatting Request Logs

To format your request logs you can provide a `httpRequest` property on the bunyan metadata you provide along with the log message. We will format this as the [`HttpRequest`][http-request-message] message and Stackdriver logging will show this as a request log. Example:

This comment was marked as spam.

This comment was marked as spam.


![Request Log Example](/doc/images/request-log.png)

```js
logger.info({
httpRequest: {
status: res.statusCode,
requestUrl: req.url,
requestMethod: req.method,
remoteIp: req.connection.remoteAddress,
// etc.
}
}, req.path);
```

The `httpRequest` proprety must be a properly formatted [`HttpRequest`][http-request-message] message.


[bunyan]: https://github.com/trentm/node-bunyan
[@google-cloud/logging]: https://www.npmjs.com/package/@google-cloud/logging
[gce-how-to]: https://cloud.google.com/compute/docs/authentication#using
[http-request-message]: https://cloud.google.com/logging/docs/reference/v2/rest/v2/LogEntry#HttpRequest
[dev-console]: https://console.developers.google.com/project
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
16 changes: 13 additions & 3 deletions packages/logging-bunyan/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -141,13 +141,12 @@ LoggingBunyan.prototype.formatEntry_ = function(record) {
);
}

record = extend({}, record);

// Stackdriver Log Viewer picks up the summary line from the 'message' field
// of the payload. Unless the user has provided a 'message' property also,
// move the 'msg' to 'message'.
if (!record.message) {
// Clone the object before modifying it.
record = extend({}, record);

// If this is an error, report the full stack trace. This allows Stackdriver
// Error Reporting to pick up errors automatically (for severity 'error' or
// higher). In this case we leave the 'msg' property intact.
Expand All @@ -171,6 +170,17 @@ LoggingBunyan.prototype.formatEntry_ = function(record) {
severity: BUNYAN_TO_STACKDRIVER[record.level]
};

// If the record contains a httpRequest property, provide it on the entry
// metadata. This allows Stackdriver to use request log formatting.
// https://cloud.google.com/logging/docs/reference/v2/rest/v2/LogEntry#HttpRequest
// Note that the httpRequest field must properly validate as a HttpRequest
// proto message, or the log entry would be rejected by the API. We do no
// validation here.
if (record.httpRequest) {
entryMetadata.httpRequest = record.httpRequest;
delete record.httpRequest;
}

return this.log_.entry(entryMetadata, record);
};

Expand Down
22 changes: 22 additions & 0 deletions packages/logging-bunyan/test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,28 @@ describe('logging-bunyan', function() {

loggingBunyan.formatEntry_(record);
});

it('should promote the httpRequest property to metadata', function(done) {
var HTTP_REQUEST = {
statusCode: 418
};
var recordWithRequest = extend({
httpRequest: HTTP_REQUEST,
}, RECORD);

loggingBunyan.log_.entry = function(entryMetadata, record) {
assert.deepStrictEqual(entryMetadata, {
resource: loggingBunyan.resource_,
timestamp: RECORD.time,
severity: LoggingBunyan.BUNYAN_TO_STACKDRIVER[RECORD.level],
httpRequest: HTTP_REQUEST
});
assert.deepStrictEqual(record, RECORD);
done();
}

loggingBunyan.formatEntry_(recordWithRequest);
});
});

describe('_write', function() {
Expand Down