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:
parent
2771a03727
commit
7f40378ecc
16 changed files with 286 additions and 35 deletions
|
@ -20,6 +20,7 @@ require('@google-cloud/trace-agent').start();
|
|||
const path = require('path');
|
||||
const grpc = require('grpc');
|
||||
const leftPad = require('left-pad');
|
||||
const pino = require('pino');
|
||||
|
||||
const PROTO_PATH = path.join(__dirname, './proto/demo.proto');
|
||||
const PORT = 7000;
|
||||
|
@ -28,6 +29,13 @@ const shopProto = grpc.load(PROTO_PATH).hipstershop;
|
|||
const client = new shopProto.CurrencyService(`localhost:${PORT}`,
|
||||
grpc.credentials.createInsecure());
|
||||
|
||||
const logger = pino({
|
||||
name: 'currencyservice-client',
|
||||
messageKey: 'message',
|
||||
changeLevelName: 'severity',
|
||||
useLevelLabels: true
|
||||
});
|
||||
|
||||
const request = {
|
||||
from: {
|
||||
currency_code: 'CHF',
|
||||
|
@ -43,16 +51,16 @@ function _moneyToString (m) {
|
|||
|
||||
client.getSupportedCurrencies({}, (err, response) => {
|
||||
if (err) {
|
||||
console.error(`Error in getSupportedCurrencies: ${err}`);
|
||||
logger.error(`Error in getSupportedCurrencies: ${err}`);
|
||||
} else {
|
||||
console.log(`Currency codes: ${response.currency_codes}`);
|
||||
logger.info(`Currency codes: ${response.currency_codes}`);
|
||||
}
|
||||
});
|
||||
|
||||
client.convert(request, (err, response) => {
|
||||
if (err) {
|
||||
console.error(`Error in convert: ${err}`);
|
||||
logger.error(`Error in convert: ${err}`);
|
||||
} else {
|
||||
console.log(`Convert: ${_moneyToString(request.from)} to ${_moneyToString(response)}`);
|
||||
logger.log(`Convert: ${_moneyToString(request.from)} to ${_moneyToString(response)}`);
|
||||
}
|
||||
});
|
||||
|
|
62
src/currencyservice/package-lock.json
generated
62
src/currencyservice/package-lock.json
generated
|
@ -663,16 +663,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",
|
||||
|
@ -1570,6 +1590,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",
|
||||
|
@ -1599,6 +1639,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",
|
||||
|
@ -1609,6 +1658,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",
|
||||
|
@ -1706,6 +1760,14 @@
|
|||
"resolved": "https://registry.npmjs.org/shimmer/-/shimmer-1.2.0.tgz",
|
||||
"integrity": "sha512-xTCx2vohXC2EWWDqY/zb4+5Mu28D+HYNSOuFzsyRDRvI/e1ICb69afwaUwfjr+25ZXldbOLyp+iDUZHq8UnTag=="
|
||||
},
|
||||
"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",
|
||||
|
|
|
@ -17,6 +17,7 @@
|
|||
"google-protobuf": "^3.0.0",
|
||||
"grpc": "^1.0.0",
|
||||
"left-pad": "^1.3.0",
|
||||
"pino": "^5.6.2",
|
||||
"request": "^2.87.0",
|
||||
"xml2js": "^0.4.19"
|
||||
},
|
||||
|
|
|
@ -32,6 +32,7 @@ const path = require('path');
|
|||
const grpc = require('grpc');
|
||||
const request = require('request');
|
||||
const xml2js = require('xml2js');
|
||||
const pino = require('pino');
|
||||
const protoLoader = require('@grpc/proto-loader');
|
||||
|
||||
const MAIN_PROTO_PATH = path.join(__dirname, './proto/demo.proto');
|
||||
|
@ -43,6 +44,13 @@ const DATA_URL = 'http://www.ecb.europa.eu/stats/eurofxref/eurofxref-daily.xml';
|
|||
const shopProto = _loadProto(MAIN_PROTO_PATH).hipstershop;
|
||||
const healthProto = _loadProto(HEALTH_PROTO_PATH).grpc.health.v1;
|
||||
|
||||
const logger = pino({
|
||||
name: 'currencyservice-server',
|
||||
messageKey: 'message',
|
||||
changeLevelName: 'severity',
|
||||
useLevelLabels: true
|
||||
});
|
||||
|
||||
/**
|
||||
* Helper function that loads a protobuf file.
|
||||
*/
|
||||
|
@ -67,7 +75,7 @@ function _loadProto (path) {
|
|||
let _data;
|
||||
function _getCurrencyData (callback) {
|
||||
if (!_data) {
|
||||
console.log('Fetching currency data...');
|
||||
logger.info('Fetching currency data...');
|
||||
request(DATA_URL, (err, res) => {
|
||||
if (err) {
|
||||
throw new Error(`Error getting data: ${err}`);
|
||||
|
@ -108,7 +116,7 @@ function _carry (amount) {
|
|||
* Lists the supported currencies
|
||||
*/
|
||||
function getSupportedCurrencies (call, callback) {
|
||||
console.log('Getting supported currencies...');
|
||||
logger.info('Getting supported currencies...');
|
||||
_getCurrencyData((data) => {
|
||||
callback(null, {currency_codes: Object.keys(data)});
|
||||
});
|
||||
|
@ -118,7 +126,7 @@ function getSupportedCurrencies (call, callback) {
|
|||
* Converts between currencies
|
||||
*/
|
||||
function convert (call, callback) {
|
||||
console.log('received conversion request');
|
||||
logger.info('received conversion request');
|
||||
try {
|
||||
_getCurrencyData((data) => {
|
||||
const request = call.request;
|
||||
|
@ -142,12 +150,11 @@ function convert (call, callback) {
|
|||
result.nanos = Math.floor(result.nanos);
|
||||
result.currency_code = request.to_code;
|
||||
|
||||
console.log(`conversion request successful`);
|
||||
logger.info(`conversion request successful`);
|
||||
callback(null, result);
|
||||
});
|
||||
} catch (err) {
|
||||
console.error('conversion request failed.');
|
||||
console.error(err);
|
||||
logger.error(`conversion request failed: ${err}`);
|
||||
callback(err.message);
|
||||
}
|
||||
}
|
||||
|
@ -164,7 +171,7 @@ function check (call, callback) {
|
|||
* CurrencyConverter service at the sample server port
|
||||
*/
|
||||
function main () {
|
||||
console.log(`Starting gRPC server on port ${PORT}...`);
|
||||
logger.info(`Starting gRPC server on port ${PORT}...`);
|
||||
const server = new grpc.Server();
|
||||
server.addService(shopProto.CurrencyService.service, {getSupportedCurrencies, convert});
|
||||
server.addService(healthProto.Health.service, {check});
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue