Skip to content

Commit

Permalink
FAB-12804 - Add metrics for couchdb
Browse files Browse the repository at this point in the history
Metric added that will record the time a request to couch db takes to
complete.

Change-Id: Ie2068bb7b97a7103306093a2a6618ed0caed6b4b
Signed-off-by: Saad Karim <skarim@us.ibm.com>
Signed-off-by: Matthew Sykes <sykesmat@us.ibm.com>
  • Loading branch information
Saad Karim authored and sykesm committed Nov 29, 2018
1 parent 430e65e commit 4cd15d5
Show file tree
Hide file tree
Showing 12 changed files with 144 additions and 40 deletions.
2 changes: 1 addition & 1 deletion core/chaincode/exectransaction_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ func finitPeer(lis net.Listener, chainIDs ...string) {
requestTimeout := viper.GetDuration("ledger.state.couchDBConfig.requestTimeout")
createGlobalChangesDB := viper.GetBool("ledger.state.couchDBConfig.createGlobalChangesDB")

couchInstance, _ := couchdb.CreateCouchInstance(connectURL, username, password, maxRetries, maxRetriesOnStartup, requestTimeout, createGlobalChangesDB)
couchInstance, _ := couchdb.CreateCouchInstance(connectURL, username, password, maxRetries, maxRetriesOnStartup, requestTimeout, createGlobalChangesDB, &disabled.Provider{})
db := couchdb.CouchDatabase{CouchInstance: couchInstance, DBName: chainID}
//drop the test database
db.DropDatabase()
Expand Down
2 changes: 1 addition & 1 deletion core/ledger/kvledger/kv_ledger_provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ func (provider *Provider) Initialize(initializer *ledger.Initializer) error {
provider.stateListeners = stateListeners
provider.collElgNotifier = collElgNotifier
provider.bookkeepingProvider = bookkeeping.NewProvider()
provider.vdbProvider, err = privacyenabledstate.NewCommonStorageDBProvider(provider.bookkeepingProvider)
provider.vdbProvider, err = privacyenabledstate.NewCommonStorageDBProvider(provider.bookkeepingProvider, initializer.MetricsProvider)
if err != nil {
return err
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"strings"

"github.com/hyperledger/fabric/common/flogging"
"github.com/hyperledger/fabric/common/metrics"
"github.com/hyperledger/fabric/core/common/ccprovider"
"github.com/hyperledger/fabric/core/ledger/cceventmgmt"
"github.com/hyperledger/fabric/core/ledger/kvledger/bookkeeping"
Expand All @@ -37,11 +38,11 @@ type CommonStorageDBProvider struct {
}

// NewCommonStorageDBProvider constructs an instance of DBProvider
func NewCommonStorageDBProvider(bookkeeperProvider bookkeeping.Provider) (DBProvider, error) {
func NewCommonStorageDBProvider(bookkeeperProvider bookkeeping.Provider, metricsProvider metrics.Provider) (DBProvider, error) {
var vdbProvider statedb.VersionedDBProvider
var err error
if ledgerconfig.IsCouchDBEnabled() {
if vdbProvider, err = statecouchdb.NewVersionedDBProvider(); err != nil {
if vdbProvider, err = statecouchdb.NewVersionedDBProvider(metricsProvider); err != nil {
return nil, err
}
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"testing"
"time"

"github.com/hyperledger/fabric/common/metrics/disabled"
"github.com/hyperledger/fabric/core/ledger/kvledger/bookkeeping"
"github.com/hyperledger/fabric/core/ledger/kvledger/txmgmt/statedb/statecouchdb"
"github.com/hyperledger/fabric/core/ledger/ledgerconfig"
Expand Down Expand Up @@ -47,7 +48,7 @@ func (env *LevelDBCommonStorageTestEnv) Init(t testing.TB) {
viper.Set("ledger.state.stateDatabase", "")
removeDBPath(t)
env.bookkeeperTestEnv = bookkeeping.NewTestEnv(t)
dbProvider, err := NewCommonStorageDBProvider(env.bookkeeperTestEnv.TestProvider)
dbProvider, err := NewCommonStorageDBProvider(env.bookkeeperTestEnv.TestProvider, &disabled.Provider{})
assert.NoError(t, err)
env.t = t
env.provider = dbProvider
Expand Down Expand Up @@ -112,7 +113,7 @@ func (env *CouchDBCommonStorageTestEnv) Init(t testing.TB) {
viper.Set("ledger.state.couchDBConfig.requestTimeout", time.Second*35)

env.bookkeeperTestEnv = bookkeeping.NewTestEnv(t)
dbProvider, err := NewCommonStorageDBProvider(env.bookkeeperTestEnv.TestProvider)
dbProvider, err := NewCommonStorageDBProvider(env.bookkeeperTestEnv.TestProvider, &disabled.Provider{})
assert.NoError(t, err)
env.t = t
env.provider = dbProvider
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"sync"

"github.com/hyperledger/fabric/common/flogging"
"github.com/hyperledger/fabric/common/metrics"
"github.com/hyperledger/fabric/core/common/ccprovider"
"github.com/hyperledger/fabric/core/ledger/kvledger/txmgmt/statedb"
"github.com/hyperledger/fabric/core/ledger/kvledger/txmgmt/version"
Expand All @@ -35,11 +36,11 @@ type VersionedDBProvider struct {
}

// NewVersionedDBProvider instantiates VersionedDBProvider
func NewVersionedDBProvider() (*VersionedDBProvider, error) {
func NewVersionedDBProvider(metricsProvider metrics.Provider) (*VersionedDBProvider, error) {
logger.Debugf("constructing CouchDB VersionedDBProvider")
couchDBDef := couchdb.GetCouchDBDefinition()
couchInstance, err := couchdb.CreateCouchInstance(couchDBDef.URL, couchDBDef.Username, couchDBDef.Password,
couchDBDef.MaxRetries, couchDBDef.MaxRetriesOnStartup, couchDBDef.RequestTimeout, couchDBDef.CreateGlobalChangesDB)
couchDBDef.MaxRetries, couchDBDef.MaxRetriesOnStartup, couchDBDef.RequestTimeout, couchDBDef.CreateGlobalChangesDB, metricsProvider)
if err != nil {
return nil, err
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ package statecouchdb
import (
"testing"

"github.com/hyperledger/fabric/common/metrics/disabled"
"github.com/hyperledger/fabric/core/ledger/kvledger/txmgmt/statedb"
"github.com/stretchr/testify/assert"
)
Expand All @@ -22,7 +23,7 @@ type TestVDBEnv struct {
func NewTestVDBEnv(t testing.TB) *TestVDBEnv {
t.Logf("Creating new TestVDBEnv")

dbProvider, _ := NewVersionedDBProvider()
dbProvider, _ := NewVersionedDBProvider(&disabled.Provider{})
testVDBEnv := &TestVDBEnv{t, dbProvider}
// No cleanup for new test environment. Need to cleanup per test for each DB used in the test.
return testVDBEnv
Expand Down
6 changes: 6 additions & 0 deletions core/ledger/util/couchdb/couchdb.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,7 @@ type CouchConnectionDef struct {
type CouchInstance struct {
conf CouchConnectionDef //connection configuration
client *http.Client // a client to connect to this instance
stats *stats
}

//CouchDatabase represents a database within a CouchDB instance
Expand Down Expand Up @@ -1645,6 +1646,7 @@ func (couchInstance *CouchInstance) handleRequest(method, dbName, functionName s
var resp *http.Response
var errResp error
couchDBReturn := &DBReturn{}
defer couchInstance.recordMetric(time.Now(), dbName, couchDBReturn, functionName)

//set initial wait duration for retries
waitDuration := retryWaitTime * time.Millisecond
Expand Down Expand Up @@ -1832,6 +1834,10 @@ func (couchInstance *CouchInstance) handleRequest(method, dbName, functionName s
return resp, couchDBReturn, nil
}

func (ci *CouchInstance) recordMetric(startTime time.Time, connectURL string, couchDBReturn *DBReturn, api string) {
ci.stats.observeProcessingTime(startTime, connectURL, strconv.Itoa(couchDBReturn.StatusCode), api)
}

//invalidCouchDBResponse checks to make sure either a valid response or error is returned
func invalidCouchDBReturn(resp *http.Response, errResp error) bool {
if resp == nil && errResp == nil {
Expand Down
Loading

0 comments on commit 4cd15d5

Please sign in to comment.