Skip to content

Commit 4f40ad1

Browse files
sykesmdenyeart
authored andcommitted
[FAB-12303] info logging for chaincode invokes
Fabric operators need some minimal visibility into chaincode execution information, without turning on full peer debug. Message should include context information such as channel and txid, as well as execution time. This change adds the following two info messages per chaincode invoke: [endorser] callChaincode -> INFO 001 [channel][9b5836e1] Entry chaincode: name:"chaincode-name" version:"chaincode-version" [endorser] callChaincode -> INFO 002 [channel][9b5836e1] Exit chaincode: name:"chaincode-name" version:"chaincode-version" (5.003ms) Change-Id: I0d116628efb6291f564989a5ff51af5bc4cca6dd Signed-off-by: David Enyeart <enyeart@us.ibm.com> Signed-off-by: Matthew Sykes <sykesmat@us.ibm.com>
1 parent 342aab6 commit 4f40ad1

File tree

2 files changed

+41
-2
lines changed

2 files changed

+41
-2
lines changed

core/endorser/endorser.go

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ package endorser
99
import (
1010
"context"
1111
"fmt"
12+
"time"
1213

1314
"github.com/golang/protobuf/proto"
1415
"github.com/hyperledger/fabric/common/channelconfig"
@@ -25,6 +26,7 @@ import (
2526
"github.com/hyperledger/fabric/protos/transientstore"
2627
putils "github.com/hyperledger/fabric/protos/utils"
2728
"github.com/pkg/errors"
29+
"go.uber.org/zap"
2830
)
2931

3032
var endorserLogger = flogging.MustGetLogger("endorser")
@@ -125,8 +127,13 @@ func NewEndorserServer(privDist privateDataDistributor, s Support, pr *platforms
125127

126128
// call specified chaincode (system or user)
127129
func (e *Endorser) callChaincode(txParams *ccprovider.TransactionParams, version string, input *pb.ChaincodeInput, cid *pb.ChaincodeID) (*pb.Response, *pb.ChaincodeEvent, error) {
128-
endorserLogger.Debugf("[%s][%s] Entry chaincode: %s version: %s", txParams.ChannelID, txParams.TxID, cid, version)
129-
defer endorserLogger.Debugf("[%s][%s] Exit", txParams.ChannelID, txParams.TxID)
130+
endorserLogger.Infof("[%s][%s] Entry chaincode: %s", txParams.ChannelID, shorttxid(txParams.TxID), cid)
131+
defer func(start time.Time) {
132+
logger := endorserLogger.WithOptions(zap.AddCallerSkip(1))
133+
elapsedMilliseconds := time.Since(start).Round(time.Millisecond) / time.Millisecond
134+
logger.Infof("[%s][%s] Exit chaincode: %s (%dms)", txParams.ChannelID, shorttxid(txParams.TxID), cid, elapsedMilliseconds)
135+
}(time.Now())
136+
130137
var err error
131138
var res *pb.Response
132139
var ccevent *pb.ChaincodeEvent

core/endorser/endorser_test.go

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,10 @@ import (
1212
"os"
1313
"testing"
1414

15+
. "github.com/onsi/gomega"
16+
1517
"github.com/golang/protobuf/proto"
18+
"github.com/hyperledger/fabric/common/flogging"
1619
mc "github.com/hyperledger/fabric/common/mocks/config"
1720
"github.com/hyperledger/fabric/common/mocks/resourcesconfig"
1821
"github.com/hyperledger/fabric/common/util"
@@ -33,6 +36,7 @@ import (
3336
pb "github.com/hyperledger/fabric/protos/peer"
3437
"github.com/hyperledger/fabric/protos/transientstore"
3538
"github.com/hyperledger/fabric/protos/utils"
39+
"github.com/onsi/gomega/gbytes"
3640
"github.com/pkg/errors"
3741
"github.com/stretchr/testify/assert"
3842
"github.com/stretchr/testify/mock"
@@ -450,6 +454,34 @@ func TestEndorserGoodPath(t *testing.T) {
450454
assert.EqualValues(t, 200, pResp.Response.Status)
451455
}
452456

457+
func TestEndorserChaincodeCallLogging(t *testing.T) {
458+
gt := NewGomegaWithT(t)
459+
m := &mock.Mock{}
460+
m.On("Sign", mock.Anything).Return([]byte{1, 2, 3, 4, 5}, nil)
461+
m.On("Serialize").Return([]byte{1, 1, 1}, nil)
462+
m.On("GetTxSimulator", mock.Anything, mock.Anything).Return(newMockTxSim(), nil)
463+
support := &em.MockSupport{
464+
Mock: m,
465+
GetApplicationConfigBoolRv: true,
466+
GetApplicationConfigRv: &mc.MockApplication{CapabilitiesRv: &mc.MockApplicationCapabilities{}},
467+
GetTransactionByIDErr: errors.New(""),
468+
ChaincodeDefinitionRv: &ccprovider.ChaincodeData{Escc: "ESCC"},
469+
ExecuteResp: &pb.Response{Status: 200, Payload: utils.MarshalOrPanic(&pb.ProposalResponse{Response: &pb.Response{}})},
470+
}
471+
attachPluginEndorser(support)
472+
es := endorser.NewEndorserServer(pvtEmptyDistributor, support, platforms.NewRegistry(&golang.Platform{}))
473+
474+
buf := gbytes.NewBuffer()
475+
flogging.Global.SetWriter(buf)
476+
defer flogging.Global.SetWriter(os.Stderr)
477+
478+
es.ProcessProposal(context.Background(), getSignedProp("chaincode-name", "chaincode-version", t))
479+
480+
t.Logf("contents:\n%s", buf.Contents())
481+
gt.Eventually(buf).Should(gbytes.Say(`INFO.*\[testchainid\]\[[[:xdigit:]]{8}\] Entry chaincode: name:"chaincode-name" version:"chaincode-version"`))
482+
gt.Eventually(buf).Should(gbytes.Say(`INFO.*\[testchainid\]\[[[:xdigit:]]{8}\] Exit chaincode: name:"chaincode-name" version:"chaincode-version" (.*ms)`))
483+
}
484+
453485
func TestEndorserLSCC(t *testing.T) {
454486
m := &mock.Mock{}
455487
m.On("Sign", mock.Anything).Return([]byte{1, 2, 3, 4, 5}, nil)

0 commit comments

Comments
 (0)