Skip to content
This repository was archived by the owner on Mar 8, 2020. It is now read-only.

Commit 4cc9d5a

Browse files
authored
[0.19.x] Fix TxId logging for performance logging (#4536)
This doesn’t fix it for any other logging as all trace points in composer-runtime will need to be changed, also somehow the context would have to be passed to apis in composer-common as well. Signed-off-by: Dave Kelsey <d_kelsey@uk.ibm.com>
1 parent 1e67fbf commit 4cc9d5a

File tree

19 files changed

+155
-105
lines changed

19 files changed

+155
-105
lines changed

packages/composer-common/lib/log/logger.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -318,6 +318,8 @@ class Logger {
318318
const timeTaken = (Date.now() - startTime).toFixed(2);
319319
if (txId && txId.getTransactionID) {
320320
this.intlog('verbose', method, `[${txId.getTransactionID().substring(0, 8)}] ${msg} ${timeTaken}ms`);
321+
} else if (txId && txId.length > 0) {
322+
this.intlog('verbose', method, `[${txId.substring(0, 8)}] ${msg} ${timeTaken}ms`);
321323
} else {
322324
this.intlog('verbose', method, `[NO TXID ] ${msg} ${timeTaken}ms`);
323325
}

packages/composer-common/test/log/logger.js

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -282,13 +282,20 @@ describe('Logger', () => {
282282
sinon.assert.calledWith(logger.intlog, 'verbose', 'Method','Message','Data');
283283
});
284284

285-
it('perf method should call verbose level, no args', () => {
285+
it('perf method should call verbose level, handling a transaction id object', () => {
286286
logger.perf('Method', 'Perf message', {getTransactionID: () => {return 'txid';}}, new Date());
287287
sinon.assert.calledOnce(logger.intlog);
288288
sinon.assert.calledWith(logger.intlog, 'verbose', 'Method', sinon.match.string);
289289
});
290290

291-
it('perf method should call verbose level and work without a txid, no args', () => {
291+
it('perf method should call verbose level, handling a transaction id string', () => {
292+
logger.perf('Method', 'Perf message', 'A-TxId-value', new Date());
293+
sinon.assert.calledOnce(logger.intlog);
294+
sinon.assert.calledWith(logger.intlog, 'verbose', 'Method', sinon.match.string);
295+
});
296+
297+
298+
it('perf method should call verbose level and work without a txid', () => {
292299
logger.perf('Method', 'Perf message', null, new Date());
293300
sinon.assert.calledOnce(logger.intlog);
294301
sinon.assert.calledWith(logger.intlog, 'verbose', 'Method', sinon.match.string);

packages/composer-runtime-hlfv1/lib/composer.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -97,12 +97,12 @@ class Composer {
9797
let nodeContext = this._createContext(engine, stub);
9898
await engine.init(nodeContext, fcn, params);
9999
LOG.exit(method);
100-
LOG.verbose('@PERF ' + method, 'Total (ms) duration for txnID [' + stub.getTxID() + ']: ' + (Date.now() - t0).toFixed(2));
100+
LOG.perf(method, 'Total (ms) duration for txnID [' + stub.getTxID() + ']: ', stub.getTxID(), t0);
101101
return shim.success();
102102
}
103103
catch(err) {
104104
LOG.error(method, err);
105-
LOG.verbose('@PERF ' + method, 'Total (ms) duration for txnID [' + stub.getTxID() + ']: ' + (Date.now() - t0).toFixed(2));
105+
LOG.perf(method, 'Total (ms) duration for txnID [' + stub.getTxID() + ']: ', stub.getTxID(), t0);
106106
return shim.error(err);
107107
}
108108
}
@@ -127,16 +127,16 @@ class Composer {
127127
let payload = await engine.invoke(nodeContext, fcn, params);
128128
if (payload !== null && payload !== undefined) {
129129
LOG.exit(method, payload);
130-
LOG.verbose('@PERF ' + method, 'Total (ms) duration for txnID [' + stub.getTxID() + ']: ' + (Date.now() - t0).toFixed(2));
130+
LOG.perf(method, 'Total (ms) duration for txnID [' + stub.getTxID() + ']: ', stub.getTxID(), t0);
131131
return shim.success(Buffer.from(JSON.stringify(payload)));
132132
}
133133
LOG.exit(method);
134-
LOG.verbose('@PERF ' + method, 'Total (ms) duration for txnID [' + stub.getTxID() + ']: ' + (Date.now() - t0).toFixed(2));
134+
LOG.perf(method, 'Total (ms) duration for txnID [' + stub.getTxID() + ']: ', stub.getTxID(), t0);
135135
return shim.success();
136136
}
137137
catch(err) {
138138
LOG.error(method, err);
139-
LOG.verbose('@PERF ' + method, 'Total (ms) duration for txnID [' + stub.getTxID() + ']: ' + (Date.now() - t0).toFixed(2));
139+
LOG.perf(method, 'Total (ms) duration for txnID [' + stub.getTxID() + ']: ', stub.getTxID(), t0);
140140
return shim.error(err);
141141
}
142142
}

packages/composer-runtime-hlfv1/lib/nodecontext.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ class NodeContext extends Context {
4343
this.stub = stub;
4444
this.dataService = new NodeDataService(this.stub);
4545
this.identityService = new NodeIdentityService(this.stub);
46+
this.setContextId(stub.getTxID());
4647
LOG.exit(method);
4748
}
4849

packages/composer-runtime-hlfv1/lib/nodedatacollection.js

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,9 @@ class NodeDataCollection extends DataCollection {
5050
const t0 = Date.now();
5151

5252
let iterator = await this.stub.getStateByPartialCompositeKey(this.collectionID, []);
53-
let results = await NodeUtils.getAllResults(iterator);
53+
let results = await NodeUtils.getAllResults(iterator, this.stub);
5454
LOG.exit(method, results);
55-
LOG.verbose('@PERF ' + method, 'Total (ms) duration: ' + (Date.now() - t0).toFixed(2));
55+
LOG.perf(method, 'Total (ms) duration: ', this.stub.getTxID(), t0);
5656
return results;
5757
}
5858

@@ -72,12 +72,12 @@ class NodeDataCollection extends DataCollection {
7272
if (value.length === 0) {
7373
const newErr = new Error(`Object with ID '${id}' in collection with ID '${this.collectionID}' does not exist`);
7474
LOG.error(method, newErr);
75-
LOG.verbose('@PERF ' + method, 'Total (ms) duration: ' + (Date.now() - t0).toFixed(2));
75+
LOG.perf(method, 'Total (ms) duration: ', this.stub.getTxID(), t0);
7676
throw newErr;
7777
}
7878
let retVal = JSON.parse(value.toString('utf8'));
7979
LOG.exit(method, retVal);
80-
LOG.verbose('@PERF ' + method, 'Total (ms) duration: ' + (Date.now() - t0).toFixed(2));
80+
LOG.perf(method, 'Total (ms) duration: ', this.stub.getTxID(), t0);
8181
return retVal;
8282
}
8383

@@ -96,7 +96,7 @@ class NodeDataCollection extends DataCollection {
9696
let value = await this.stub.getState(key);
9797
let retVal = value.length !== 0;
9898
LOG.exit(method, retVal);
99-
LOG.verbose('@PERF ' + method, 'Total (ms) duration: ' + (Date.now() - t0).toFixed(2));
99+
LOG.perf(method, 'Total (ms) duration: ', this.stub.getTxID(), t0);
100100
return retVal;
101101
}
102102

@@ -120,14 +120,14 @@ class NodeDataCollection extends DataCollection {
120120
if (value.length !== 0) {
121121
const newErr = new Error(`Failed to add object with ID '${id}' in collection with ID '${this.collectionID}' as the object already exists`);
122122
LOG.error(method, newErr);
123-
LOG.verbose('@PERF ' + method, 'Total (ms) duration: ' + (Date.now() - t0).toFixed(2));
123+
LOG.perf(method, 'Total (ms) duration: ', this.stub.getTxID(), t0);
124124
throw newErr;
125125
}
126126
}
127127
await this.stub.putState(key, Buffer.from(JSON.stringify(object)));
128128

129129
LOG.exit(method);
130-
LOG.verbose('@PERF ' + method, 'Total (ms) duration: ' + (Date.now() - t0).toFixed(2));
130+
LOG.perf(method, 'Total (ms) duration: ', this.stub.getTxID(), t0);
131131
}
132132

133133
/**
@@ -148,12 +148,12 @@ class NodeDataCollection extends DataCollection {
148148
if (value.length === 0) {
149149
const newErr = new Error(`Failed to update object with ID '${id}' in collection with ID '${this.collectionID}' as the object does not exist`);
150150
LOG.error(method, newErr);
151-
LOG.verbose('@PERF ' + method, 'Total (ms) duration: ' + (Date.now() - t0).toFixed(2));
151+
LOG.perf(method, 'Total (ms) duration: ', this.stub.getTxID(), t0);
152152
throw newErr;
153153
}
154154
await this.stub.putState(key, Buffer.from(JSON.stringify(object)));
155155
LOG.exit(method);
156-
LOG.verbose('@PERF ' + method, 'Total (ms) duration: ' + (Date.now() - t0).toFixed(2));
156+
LOG.perf(method, 'Total (ms) duration: ', this.stub.getTxID(), t0);
157157
}
158158

159159
/**
@@ -170,12 +170,12 @@ class NodeDataCollection extends DataCollection {
170170
if (value.length === 0) {
171171
const newErr = new Error(`Failed to delete object with ID '${id}' in collection with ID '${this.collectionID}' as the object does not exist`);
172172
LOG.error(method, newErr);
173-
LOG.verbose('@PERF ' + method, 'Total (ms) duration: ' + (Date.now() - t0).toFixed(2));
173+
LOG.perf(method, 'Total (ms) duration: ', this.stub.getTxID(), t0);
174174
throw newErr;
175175
}
176176
await this.stub.deleteState(key);
177177
LOG.exit(method);
178-
LOG.verbose('@PERF ' + method, 'Total (ms) duration: ' + (Date.now() - t0).toFixed(2));
178+
LOG.perf(method, 'Total (ms) duration: ', this.stub.getTxID(), t0);
179179
}
180180
}
181181

packages/composer-runtime-hlfv1/lib/nodedataservice.js

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -57,17 +57,17 @@ class NodeDataService extends DataService {
5757
if (value.length === 0) {
5858
let result = await this._storeCollection(key, id);
5959
LOG.exit(method, result);
60-
LOG.verbose('@PERF ' + method, 'Total (ms) duration: ' + (Date.now() - t0).toFixed(2));
60+
LOG.perf(method, 'Total (ms) duration: ', this.stub.getTxID(), t0);
6161
return result;
6262
}
6363
else {
64-
LOG.verbose('@PERF ' + method, 'Total (ms) duration: ' + (Date.now() - t0).toFixed(2));
64+
LOG.perf(method, 'Total (ms) duration: ', this.stub.getTxID(), t0);
6565
throw new Error(`Failed to create collection with ID ${id} as it already exists`);
6666
}
6767
} else {
6868
let result = await this._storeCollection(key, id);
6969
LOG.exit(method, result);
70-
LOG.verbose('@PERF ' + method, 'Total (ms) duration: ' + (Date.now() - t0).toFixed(2));
70+
LOG.perf(method, 'Total (ms) duration: ', this.stub.getTxID(), t0);
7171
return result;
7272
}
7373
}
@@ -88,7 +88,7 @@ class NodeDataService extends DataService {
8888
await this.stub.putState(key, Buffer.from(JSON.stringify({'id': id})));
8989
let retVal = new NodeDataCollection(this, this.stub, id);
9090
LOG.exit(method, retVal);
91-
LOG.verbose('@PERF ' + method, 'Total (ms) duration: ' + (Date.now() - t0).toFixed(2));
91+
LOG.perf(method, 'Total (ms) duration: ', this.stub.getTxID(), t0);
9292
return retVal;
9393
}
9494

@@ -104,13 +104,13 @@ class NodeDataService extends DataService {
104104
let key = this.stub.createCompositeKey(collectionObjectType, [id]);
105105
let exists = await this.existsCollection(id);
106106
if (!exists) {
107-
LOG.verbose('@PERF ' + method, 'Total (ms) duration: ' + (Date.now() - t0).toFixed(2));
107+
LOG.perf(method, 'Total (ms) duration: ', this.stub.getTxID(), t0);
108108
throw new Error(`Collection with ID ${id} does not exist`);
109109
}
110110
await this.clearCollection(id);
111111
await this.stub.deleteState(key);
112112
LOG.exit(method);
113-
LOG.verbose('@PERF ' + method, 'Total (ms) duration: ' + (Date.now() - t0).toFixed(2));
113+
LOG.perf(method, 'Total (ms) duration: ', this.stub.getTxID(), t0);
114114
}
115115

116116
/**
@@ -128,18 +128,18 @@ class NodeDataService extends DataService {
128128
if (bypass) {
129129
let retVal = new NodeDataCollection(this, this.stub, id);
130130
LOG.exit(method, retVal);
131-
LOG.verbose('@PERF ' + method, 'Total (ms) duration: ' + (Date.now() - t0).toFixed(2));
131+
LOG.perf(method, 'Total (ms) duration: ', this.stub.getTxID(), t0);
132132
return retVal;
133133
} else {
134134
let key = this.stub.createCompositeKey(collectionObjectType, [id]);
135135
let value = await this.stub.getState(key);
136136
if (value.length === 0) {
137-
LOG.verbose('@PERF ' + method, 'Total (ms) duration: ' + (Date.now() - t0).toFixed(2));
137+
LOG.perf(method, 'Total (ms) duration: ', this.stub.getTxID(), t0);
138138
throw new Error(`Collection with ID ${id} does not exist`);
139139
}
140140
let retVal = new NodeDataCollection(this, this.stub, id);
141141
LOG.exit(method, retVal);
142-
LOG.verbose('@PERF ' + method, 'Total (ms) duration: ' + (Date.now() - t0).toFixed(2));
142+
LOG.perf(method, 'Total (ms) duration: ', this.stub.getTxID(), t0);
143143
return retVal;
144144
}
145145
}
@@ -159,7 +159,7 @@ class NodeDataService extends DataService {
159159
let value = await this.stub.getState(key);
160160
let retVal = value.length !== 0;
161161
LOG.exit(method, retVal);
162-
LOG.verbose('@PERF ' + method, 'Total (ms) duration: ' + (Date.now() - t0).toFixed(2));
162+
LOG.perf(method, 'Total (ms) duration: ', this.stub.getTxID(), t0);
163163
return retVal;
164164
}
165165

@@ -176,9 +176,9 @@ class NodeDataService extends DataService {
176176
const t0 = Date.now();
177177

178178
let iterator = await this.stub.getQueryResult(query);
179-
let results = await NodeUtils.getAllResults(iterator);
179+
let results = await NodeUtils.getAllResults(iterator, this.stub);
180180
LOG.exit(method, results);
181-
LOG.verbose('@PERF ' + method, 'Total (ms) duration: ' + (Date.now() - t0).toFixed(2));
181+
LOG.perf(method, 'Total (ms) duration: ', this.stub.getTxID(), t0);
182182
return results;
183183
}
184184

@@ -194,7 +194,7 @@ class NodeDataService extends DataService {
194194
let iterator = await this.stub.getStateByPartialCompositeKey(id, []);
195195
await NodeUtils.deleteAllResults(iterator, this.stub);
196196
LOG.exit(method);
197-
LOG.verbose('@PERF ' + method, 'Total (ms) duration: ' + (Date.now() - t0).toFixed(2));
197+
LOG.perf(method, 'Total (ms) duration: ', this.stub.getTxID(), t0);
198198
}
199199
}
200200

packages/composer-runtime-hlfv1/lib/nodeloggingservice.js

Lines changed: 26 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,13 @@
1717
const LoggingService = require('composer-runtime').LoggingService;
1818
const LOGLEVEL_KEY = 'ComposerLogCfg';
1919
const Logger = require('composer-common').Logger;
20+
21+
Logger.setCallBack(function(logLevel) {
22+
const timestamp = new Date().toISOString();
23+
return `${timestamp} ${logLevel.toUpperCase().padEnd(8)} `;
24+
});
25+
26+
2027
/**
2128
* Base class representing the logging service provided by a {@link Container}.
2229
* @protected
@@ -29,6 +36,7 @@ class NodeLoggingService extends LoggingService {
2936
constructor() {
3037
super();
3138
this.stub = null;
39+
this.loggerInitialized = false;
3240
}
3341

3442
/**
@@ -41,15 +49,19 @@ class NodeLoggingService extends LoggingService {
4149
async initLogging(stub) {
4250
this.stub = stub;
4351

44-
let logCFG = await this.getLoggerCfg();
45-
Logger.setLoggerCfg(logCFG, true);
46-
47-
Logger.setCallBack(function(logLevel) {
48-
const timestamp = new Date().toISOString();
49-
const shortTxId = stub.getTxID().substring(0, 8);
50-
return `${timestamp} [${shortTxId}] ${logLevel.toUpperCase().padEnd(8)} `;
51-
});
52-
52+
// we only want to do this once for the first request that comes
53+
// in so we can look at the initial state of the logger and set it
54+
// appropriately. Any change to the logger state is handled so doesn't
55+
// need to be checked at initLogging. The theory is that as this is
56+
// called constantly in interleaved requests it stops some log points
57+
// being logged.
58+
if (!this.loggerInitialized) {
59+
// set the initialized flag first to stop another interleaved request
60+
// coming in trying to do the same thing.
61+
this.loggerInitialized = true;
62+
let logCFG = await this.getLoggerCfg();
63+
Logger.setLoggerCfg(logCFG, true);
64+
}
5365
}
5466

5567
/**
@@ -66,14 +78,14 @@ class NodeLoggingService extends LoggingService {
6678
*
6779
* @returns {Object} configuration
6880
*/
69-
async getLoggerCfg(){
81+
async getLoggerCfg() {
7082
let result = await this.stub.getState(LOGLEVEL_KEY);
7183
if (result.length === 0) {
7284
let defCfg = this.getDefaultCfg();
7385
return defCfg;
7486
} else {
7587
let json = JSON.parse(result.toString());
76-
if( json.origin && json.origin==='default-logger-module'){
88+
if( json.origin && json.origin === 'default-logger-module'){
7789
json = this.getDefaultCfg();
7890
}
7991
return json;
@@ -83,7 +95,7 @@ class NodeLoggingService extends LoggingService {
8395
/**
8496
* @return {Object} the default cfg
8597
*/
86-
getDefaultCfg(){
98+
getDefaultCfg() {
8799

88100
let envVariable = process.env.CORE_CHAINCODE_LOGGING_LEVEL;
89101
let debugString = this.mapFabricDebug(envVariable);
@@ -97,7 +109,7 @@ class NodeLoggingService extends LoggingService {
97109
},
98110
'debug' : debugString,
99111
'logger': './consolelogger.js',
100-
'origin':'default-runtime-hlfv1'
112+
'origin': 'default-runtime-hlfv1'
101113
};
102114
}
103115

@@ -109,7 +121,7 @@ class NodeLoggingService extends LoggingService {
109121
* @param {String} string input value to process
110122
* @return {String} clean string that can be used for setting up logging.
111123
*/
112-
mapCfg(string){
124+
mapCfg(string) {
113125
let DEFAULT = 'composer[warn]:*';
114126
// first split it up into elements based on ,
115127
let details = string.split(/[\s,]+/);

packages/composer-runtime-hlfv1/lib/nodeutils.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,10 @@ class NodeUtils {
2828
*
2929
* @static
3030
* @param {any} iterator the chaincode iterator
31+
* @param {any} stub the stub for this invocation
3132
* @returns {promise} a promise that is resolved with the results or rejected or error
3233
*/
33-
static async getAllResults(iterator) {
34+
static async getAllResults(iterator, stub) {
3435
const method = 'getAllResults';
3536
LOG.entry(method, iterator);
3637
const t0 = Date.now();
@@ -56,7 +57,7 @@ class NodeUtils {
5657
LOG.warn(warnMsg);
5758
}
5859
LOG.exit(method);
59-
LOG.verbose('@PERF ' + method, 'Total (ms) duration: ' + (Date.now() - t0).toFixed(2));
60+
LOG.perf(method, 'Total (ms) duration: ', stub.getTxID(), t0);
6061
return results;
6162
}
6263
}
@@ -94,7 +95,7 @@ class NodeUtils {
9495
LOG.warn(warnMsg);
9596
}
9697
LOG.exit(method);
97-
LOG.verbose('@PERF ' + method, 'Total (ms) duration: ' + (Date.now() - t0).toFixed(2));
98+
LOG.perf(method, 'Total (ms) duration: ', stub.getTxID(), t0);
9899
return results;
99100
}
100101
}

0 commit comments

Comments
 (0)