diff --git a/dbstats_go1.11.go b/dbstats_go1.11.go index c32656d..b1a35e3 100644 --- a/dbstats_go1.11.go +++ b/dbstats_go1.11.go @@ -21,20 +21,41 @@ func RecordStats(db *sql.DB, interval time.Duration) (fnStop func()) { done = make(chan struct{}) ) + var ( + lastWaitCount int64 + lastWaitDuration time.Duration + ) + go func() { for { select { case <-ticker.C: dbStats := db.Stats() + + lastWindowWaitCount := dbStats.WaitCount - lastWaitCount + lastWindowWaitDuration := dbStats.WaitDuration - lastWaitDuration + + var avgWaitPerConnection float64 + if lastWindowWaitCount > 0 { + avgWaitPerConnection = float64(lastWindowWaitDuration.Milliseconds()) / float64(lastWindowWaitCount) + } + + percentActive := 100 * float64(dbStats.InUse) / float64(dbStats.MaxOpenConnections) + stats.Record(ctx, MeasureOpenConnections.M(int64(dbStats.OpenConnections)), MeasureIdleConnections.M(int64(dbStats.Idle)), MeasureActiveConnections.M(int64(dbStats.InUse)), + MeasurePercentActiveConnections.M(int64(percentActive)), MeasureWaitCount.M(dbStats.WaitCount), MeasureWaitDuration.M(float64(dbStats.WaitDuration.Nanoseconds())/1e6), + MeasureAvgWaitPerConnection.M(avgWaitPerConnection), MeasureIdleClosed.M(dbStats.MaxIdleClosed), MeasureLifetimeClosed.M(dbStats.MaxLifetimeClosed), ) + + lastWaitCount = dbStats.WaitCount + lastWaitDuration = dbStats.WaitDuration case <-done: ticker.Stop() return diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..6834076 --- /dev/null +++ b/go.mod @@ -0,0 +1,5 @@ +module github.com/opencensus-integrations/ocsql + +go 1.15 + +require go.opencensus.io v0.22.4 diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..87c4aa4 --- /dev/null +++ b/go.sum @@ -0,0 +1,49 @@ +cloud.google.com/go v0.26.0/go.mod h1:aQUYkXzVsufM+DwF1aE+0xfcU+56JwCaLick0ClmMTw= +github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU= +github.com/client9/misspell v0.3.4/go.mod h1:qj6jICC3Q7zFZvVWo7KLAzC3yx5G7kyvSDkc90ppPyw= +github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b/go.mod h1:SBH7ygxi8pfUlaOkMMuAQtPIUF8ecWP5IEl/CR7VP2Q= +github.com/golang/groupcache v0.0.0-20190702054246-869f871628b6 h1:ZgQEtGgCBiWRM39fZuwSd1LwSqqSW0hOdXCYYDX0R3I= +github.com/golang/groupcache v0.0.0-20190702054246-869f871628b6/go.mod h1:cIg4eruTrX1D+g88fzRXU5OdNfaM+9IcxsU14FzY7Hc= +github.com/golang/mock v1.1.1/go.mod h1:oTYuIxOrZwtPieC+H1uAHpcLFnEyAGVDL/k47Jfbm0A= +github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U= +github.com/golang/protobuf v1.3.1/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U= +github.com/google/go-cmp v0.3.0 h1:crn/baboCvb5fXaQ0IJ1SGTsTVrWpDsCWC8EGETZijY= +github.com/google/go-cmp v0.3.0/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU= +github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= +github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4= +go.opencensus.io v0.22.4 h1:LYy1Hy3MJdrCdMwwzxA/dRok4ejH+RwNGbuoD9fCjto= +go.opencensus.io v0.22.4/go.mod h1:yxeiOL68Rb0Xd1ddK5vPZ/oVn4vY4Ynel7k9FzqtOIw= +golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= +golang.org/x/exp v0.0.0-20190121172915-509febef88a4/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA= +golang.org/x/lint v0.0.0-20181026193005-c67002cb31c3/go.mod h1:UVdnD1Gm6xHRNCYTkRU2/jEulfH38KcIWyp/GAMgvoE= +golang.org/x/lint v0.0.0-20190227174305-5b3e6a55c961/go.mod h1:wehouNa3lNwaWXcvxsM5YxQ5yQlVC4a0KAMCusXpPoU= +golang.org/x/lint v0.0.0-20190313153728-d0100b6bd8b3/go.mod h1:6SW0HCj/g11FgYtHlgUYUwCkIfeOF89ocIRzGO/8vkc= +golang.org/x/net v0.0.0-20180724234803-3673e40ba225/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= +golang.org/x/net v0.0.0-20180826012351-8a410e7b638d/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= +golang.org/x/net v0.0.0-20190213061140-3a22650c66bd/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= +golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= +golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= +golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U= +golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sync v0.0.0-20181108010431-42b317875d0f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sync v0.0.0-20190227155943-e225da77a7e6/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sys v0.0.0-20180830151530-49385e6e1522/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= +golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= +golang.org/x/sys v0.0.0-20190502145724-3ef323f4f1fd/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= +golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk= +golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= +golang.org/x/tools v0.0.0-20190114222345-bf090417da8b/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= +golang.org/x/tools v0.0.0-20190226205152-f727befe758c/go.mod h1:9Yl7xja0Znq3iFh3HoIrodX9oNMXvdceNzlUR8zjMvY= +golang.org/x/tools v0.0.0-20190311212946-11955173bddd/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs= +google.golang.org/appengine v1.1.0/go.mod h1:EbEs0AVv82hx2wNQdGPgUI5lhzA/G0D9YwlJXL52JkM= +google.golang.org/appengine v1.4.0/go.mod h1:xpcJRLb0r/rnEns0DIKYYv+WjYCduHsrkT7/EB5XEv4= +google.golang.org/genproto v0.0.0-20180817151627-c66870c02cf8/go.mod h1:JiN7NxoALGmiZfu7CAH4rXhgtRTLTxftemlI0sWmxmc= +google.golang.org/genproto v0.0.0-20190425155659-357c62f0e4bb/go.mod h1:VzzqZJRnGkLBvHegQrXjBqPurQTc5/KpmUdxsrq26oE= +google.golang.org/grpc v1.19.0/go.mod h1:mqu4LbDTu4XGKhr4mRzUsmM4RtVoemTSY81AxZiDr8c= +google.golang.org/grpc v1.20.1/go.mod h1:10oTOabMzJvdu6/UiuZezV6QK5dSlG84ov/aaiqXj38= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= +honnef.co/go/tools v0.0.0-20190102054323-c2f93a96b099/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4= diff --git a/observability.go b/observability.go index 817b623..af3b4a1 100644 --- a/observability.go +++ b/observability.go @@ -26,14 +26,16 @@ var ( // The following measures are supported for use in custom views. var ( - MeasureLatencyMs = stats.Float64("go.sql/latency", "The latency of calls in milliseconds", stats.UnitMilliseconds) - MeasureOpenConnections = stats.Int64("go.sql/connections/open", "Count of open connections in the pool", stats.UnitDimensionless) - MeasureIdleConnections = stats.Int64("go.sql/connections/idle", "Count of idle connections in the pool", stats.UnitDimensionless) - MeasureActiveConnections = stats.Int64("go.sql/connections/active", "Count of active connections in the pool", stats.UnitDimensionless) - MeasureWaitCount = stats.Int64("go.sql/connections/wait_count", "The total number of connections waited for", stats.UnitDimensionless) - MeasureWaitDuration = stats.Float64("go.sql/connections/wait_duration", "The total time blocked waiting for a new connection", stats.UnitMilliseconds) - MeasureIdleClosed = stats.Int64("go.sql/connections/idle_closed", "The total number of connections closed due to SetMaxIdleConns", stats.UnitDimensionless) - MeasureLifetimeClosed = stats.Int64("go.sql/connections/lifetime_closed", "The total number of connections closed due to SetConnMaxLifetime", stats.UnitDimensionless) + MeasureLatencyMs = stats.Float64("go.sql/latency", "The latency of calls in milliseconds", stats.UnitMilliseconds) + MeasureOpenConnections = stats.Int64("go.sql/connections/open", "Count of open connections in the pool", stats.UnitDimensionless) + MeasureIdleConnections = stats.Int64("go.sql/connections/idle", "Count of idle connections in the pool", stats.UnitDimensionless) + MeasureActiveConnections = stats.Int64("go.sql/connections/active", "Count of active connections in the pool", stats.UnitDimensionless) + MeasurePercentActiveConnections = stats.Int64("go.sql/connections/percent_active", "Percent active of max open connections in the pool", stats.UnitDimensionless) + MeasureWaitCount = stats.Int64("go.sql/connections/wait_count", "The total number of connections waited for", stats.UnitDimensionless) + MeasureWaitDuration = stats.Float64("go.sql/connections/wait_duration", "The total time blocked waiting for a new connection", stats.UnitMilliseconds) + MeasureAvgWaitPerConnection = stats.Float64("go.sql/connections/avg_wait_per_connection", "The average time blocked waiting per connection during the last sampling window", stats.UnitMilliseconds) + MeasureIdleClosed = stats.Int64("go.sql/connections/idle_closed", "The total number of connections closed due to SetMaxIdleConns", stats.UnitDimensionless) + MeasureLifetimeClosed = stats.Int64("go.sql/connections/lifetime_closed", "The total number of connections closed due to SetConnMaxLifetime", stats.UnitDimensionless) ) // Default distributions used by views in this package @@ -116,6 +118,14 @@ var ( TagKeys: []tag.Key{GoSQLInstance}, } + SQLClientPercentActiveConnectionsView = &view.View{ + Name: "go.sql/db/connections/percent_active", + Description: "The percentage active out of max connections", + Measure: MeasurePercentActiveConnections, + Aggregation: view.LastValue(), + TagKeys: []tag.Key{GoSQLInstance}, + } + SQLClientWaitCountView = &view.View{ Name: "go.sql/db/connections/wait_count", Description: "The total number of connections waited for", @@ -132,6 +142,14 @@ var ( TagKeys: []tag.Key{GoSQLInstance}, } + SQLClientAvgWaitPerConnectionView = &view.View{ + Name: "go.sql/db/connections/avg_wait_per_connection", + Description: "The average time blocked waiting per connection during the last sampling window", + Measure: MeasureAvgWaitPerConnection, + Aggregation: view.LastValue(), + TagKeys: []tag.Key{GoSQLInstance}, + } + SQLClientIdleClosedView = &view.View{ Name: "go.sql/db/connections/idle_closed_count", Description: "The total number of connections closed due to SetMaxIdleConns", @@ -150,8 +168,8 @@ var ( DefaultViews = []*view.View{ SQLClientLatencyView, SQLClientCallsView, SQLClientOpenConnectionsView, - SQLClientIdleConnectionsView, SQLClientActiveConnectionsView, - SQLClientWaitCountView, SQLClientWaitDurationView, + SQLClientIdleConnectionsView, SQLClientActiveConnectionsView, SQLClientPercentActiveConnectionsView, + SQLClientWaitCountView, SQLClientWaitDurationView, SQLClientAvgWaitPerConnectionView, SQLClientIdleClosedView, SQLClientLifetimeClosedView, } )