log: change log format to JSON payload for better log in Stackdriver (#66)

change the log format in Python and Node.js services.

Effected services are currencyservice, emailservice, paymentservice,
and recommendationservice. Loadgenerator is left as is because of
the diffculty to change the log format and log target in locust.

ref. #47
This commit is contained in:
Yoshi Yamaguchi 2018-10-06 03:23:45 +09:00 committed by Ahmet Alp Balkan
parent 2771a03727
commit 7f40378ecc
16 changed files with 286 additions and 35 deletions

View file

@ -14,6 +14,15 @@
const cardValidator = require('simple-card-validator');
const uuid = require('uuid/v4');
const pino = require('pino');
const logger = pino({
name: 'paymentservice-charge',
messageKey: 'message',
changeLevelName: 'severity',
useLevelLabels: true
});
class CreditCardError extends Error {
constructor (message) {
@ -67,7 +76,7 @@ module.exports = function charge (request) {
const { credit_card_expiration_year: year, credit_card_expiration_month: month } = creditCard;
if ((currentYear * 12 + currentMonth) > (year * 12 + month)) { throw new ExpiredCreditCard(cardNumber.replace('-', ''), month, year); }
console.log(`Transaction processed: ${cardType} ending ${cardNumber.substr(-4)} \
logger.info(`Transaction processed: ${cardType} ending ${cardNumber.substr(-4)} \
Amount: ${amount.currency_code}${amount.units}.${amount.nanos}`);
return { transaction_id: uuid() };

View file

@ -700,16 +700,36 @@
"resolved": "https://registry.npmjs.org/fast-deep-equal/-/fast-deep-equal-1.1.0.tgz",
"integrity": "sha1-wFNHeBfIa1HaqFPIHgWbcz0CNhQ="
},
"fast-json-parse": {
"version": "1.0.3",
"resolved": "https://registry.npmjs.org/fast-json-parse/-/fast-json-parse-1.0.3.tgz",
"integrity": "sha512-FRWsaZRWEJ1ESVNbDWmsAlqDk96gPQezzLghafp5J4GUKjbCz3OkAHuZs5TuPEtkbVQERysLp9xv6c24fBm8Aw=="
},
"fast-json-stable-stringify": {
"version": "2.0.0",
"resolved": "https://registry.npmjs.org/fast-json-stable-stringify/-/fast-json-stable-stringify-2.0.0.tgz",
"integrity": "sha1-1RQsDK7msRifh9OnYREGT4bIu/I="
},
"fast-redact": {
"version": "1.2.0",
"resolved": "https://registry.npmjs.org/fast-redact/-/fast-redact-1.2.0.tgz",
"integrity": "sha512-k/uSk9PtFmvYx0m7bRk5B2gZChQk4euWhrn7Mf3vYSmwZBLh7cGNuMuc/vhH1MKMPyVJMMtl9oMwPnwlKqs7CQ=="
},
"fast-safe-stringify": {
"version": "2.0.6",
"resolved": "https://registry.npmjs.org/fast-safe-stringify/-/fast-safe-stringify-2.0.6.tgz",
"integrity": "sha512-q8BZ89jjc+mz08rSxROs8VsrBBcn1SIw1kq9NjolL509tkABRk9io01RAjSaEv1Xb2uFLt8VtRiZbGp5H8iDtg=="
},
"findit2": {
"version": "2.2.3",
"resolved": "https://registry.npmjs.org/findit2/-/findit2-2.2.3.tgz",
"integrity": "sha1-WKRmaX34piBc39vzlVNri9d3pfY="
},
"flatstr": {
"version": "1.0.8",
"resolved": "https://registry.npmjs.org/flatstr/-/flatstr-1.0.8.tgz",
"integrity": "sha512-YXblbv/vc1zuVVUtnKl1hPqqk7TalZCppnKE7Pr8FI/Rp48vzckS/4SJ4Y9O9RNiI82Vcw/FydmtqdQOg1Dpqw=="
},
"follow-redirects": {
"version": "1.5.1",
"resolved": "https://registry.npmjs.org/follow-redirects/-/follow-redirects-1.5.1.tgz",
@ -1592,6 +1612,26 @@
"resolved": "https://registry.npmjs.org/pify/-/pify-3.0.0.tgz",
"integrity": "sha1-5aSs0sEB/fPZpNB/DbxNtJ3SgXY="
},
"pino": {
"version": "5.6.2",
"resolved": "https://registry.npmjs.org/pino/-/pino-5.6.2.tgz",
"integrity": "sha512-JVMYqJkE58b2u5+t85zJLbUDOhoWDjEQqrRY1eQzuR4Ub9RIyUSQJms4deT8Joy+C/QIdrrie7NffgCm+ao9xw==",
"requires": {
"fast-json-parse": "^1.0.3",
"fast-redact": "^1.2.0",
"fast-safe-stringify": "^2.0.6",
"flatstr": "^1.0.5",
"pino-std-serializers": "^2.2.1",
"pump": "^3.0.0",
"quick-format-unescaped": "^3.0.0",
"sonic-boom": "^0.6.1"
}
},
"pino-std-serializers": {
"version": "2.2.1",
"resolved": "https://registry.npmjs.org/pino-std-serializers/-/pino-std-serializers-2.2.1.tgz",
"integrity": "sha512-QqL7kkF7eMCpFG4hpZD8UPQga/kxkkh3E62HzMzTIL4OQyijyisAnBL8msBEAml8xcb/ioGhH7UUzGxuHqczhQ=="
},
"pretty-ms": {
"version": "3.2.0",
"resolved": "https://registry.npmjs.org/pretty-ms/-/pretty-ms-3.2.0.tgz",
@ -1621,6 +1661,15 @@
"resolved": "https://registry.npmjs.org/pseudomap/-/pseudomap-1.0.2.tgz",
"integrity": "sha1-8FKijacOYYkX7wqKw0wa5aaChrM="
},
"pump": {
"version": "3.0.0",
"resolved": "https://registry.npmjs.org/pump/-/pump-3.0.0.tgz",
"integrity": "sha512-LwZy+p3SFs1Pytd/jYct4wpv49HiYCqd9Rlc5ZVdk0V+8Yzv6jR5Blk3TRmPL1ft69TxP0IMZGJ+WPFU2BFhww==",
"requires": {
"end-of-stream": "^1.1.0",
"once": "^1.3.1"
}
},
"punycode": {
"version": "1.4.1",
"resolved": "https://registry.npmjs.org/punycode/-/punycode-1.4.1.tgz",
@ -1631,6 +1680,11 @@
"resolved": "https://registry.npmjs.org/qs/-/qs-6.5.2.tgz",
"integrity": "sha512-N5ZAX4/LxJmF+7wN74pUD6qAh9/wnvdQcjq9TZjevvXzSUo7bfmw91saqMjzGS2xq91/odN2dW/WOl7qQHNDGA=="
},
"quick-format-unescaped": {
"version": "3.0.0",
"resolved": "https://registry.npmjs.org/quick-format-unescaped/-/quick-format-unescaped-3.0.0.tgz",
"integrity": "sha512-XmIOc07VM2kPm6m3j/U6jgxyUgDm2Rgh2c1PPy0JUHoQRdoh86hOym0bHyF6G1T6sn+N5lildhvl/T59H5KVyA=="
},
"readable-stream": {
"version": "2.3.6",
"resolved": "https://registry.npmjs.org/readable-stream/-/readable-stream-2.3.6.tgz",
@ -1728,6 +1782,14 @@
"resolved": "https://registry.npmjs.org/simple-card-validator/-/simple-card-validator-1.1.0.tgz",
"integrity": "sha1-675uRp/q7Cy7SBX4Qyu+BMccNvk="
},
"sonic-boom": {
"version": "0.6.1",
"resolved": "https://registry.npmjs.org/sonic-boom/-/sonic-boom-0.6.1.tgz",
"integrity": "sha512-3qx6XXDeG+hPNa+jla1H6BMBLcjLl8L8NRERLVeIf/EuPqoqmq4K8owG29Xu7OypT/7/YT/0uKW6YitsKA+nLQ==",
"requires": {
"flatstr": "^1.0.5"
}
},
"source-map": {
"version": "0.6.1",
"resolved": "https://registry.npmjs.org/source-map/-/source-map-0.6.1.tgz",

View file

@ -16,6 +16,7 @@
"@google-cloud/trace-agent": "^2.11.0",
"@grpc/proto-loader": "^0.1.0",
"grpc": "^1.12.3",
"pino": "^5.6.2",
"simple-card-validator": "^1.1.0",
"uuid": "^3.2.1"
},

View file

@ -14,10 +14,18 @@
const path = require('path');
const grpc = require('grpc');
const pino = require('pino');
const protoLoader = require('@grpc/proto-loader');
const charge = require('./charge');
const logger = pino({
name: 'paymentservice-server',
messageKey: 'message',
changeLevelName: 'severity',
useLevelLabels: true
});
class HipsterShopServer {
constructor (protoRoot, port = HipsterShopServer.DEFAULT_PORT) {
this.port = port;
@ -38,7 +46,7 @@ class HipsterShopServer {
*/
static ChargeServiceHandler (call, callback) {
try {
console.log(`PaymentService#Charge invoked with request ${JSON.stringify(call.request)}`);
logger.info(`PaymentService#Charge invoked with request ${JSON.stringify(call.request)}`);
const response = charge(call.request);
callback(null, response);
} catch (err) {
@ -53,7 +61,7 @@ class HipsterShopServer {
listen () {
this.server.bind(`0.0.0.0:${this.port}`, grpc.ServerCredentials.createInsecure());
console.log(`PaymentService grpc server listening on ${this.port}`);
logger.info(`PaymentService grpc server listening on ${this.port}`);
this.server.start();
}