-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add mysql connection metrics (#118)
* feat: add connection metrics * fix: comment * fix: change metrics label * fix: data race * fix: data race * fix: data race * fix: data race * fix: data race * fix: add default duration * fix: test * fix: test flakes on slow server * fix: test flakes * fix: test flakes when callback twice * fix: test flakes when callback twice * fix: test flakes when callback twice * fix(otgorm): metrics missing labels (#124) * fix(otgorm): metrics missing labels * fix: add with value, extend waiting time * Update otgorm.go * Update observability.go * reame moduleIn.make to maker * fix: func renamed Co-authored-by: Trock <35254251+GGXXLL@users.noreply.github.com>
- Loading branch information
Showing
14 changed files
with
398 additions
and
66 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
//go:generate mockgen -destination=./mocks/metrics.go github.com/go-kit/kit/metrics Gauge | ||
|
||
package otgorm | ||
|
||
import ( | ||
"time" | ||
|
||
"github.com/go-kit/kit/metrics" | ||
"gorm.io/gorm" | ||
) | ||
|
||
type collector struct { | ||
factory Factory | ||
gauges *Gauges | ||
interval time.Duration | ||
} | ||
|
||
// Gauges is a collection of metrics for database connection info. | ||
type Gauges struct { | ||
Idle metrics.Gauge | ||
InUse metrics.Gauge | ||
Open metrics.Gauge | ||
} | ||
|
||
// newCollector creates a new database wrapper containing the name of the database, | ||
// it's driver and the (sql) database itself. | ||
func newCollector(factory Factory, gauges *Gauges, interval time.Duration) *collector { | ||
return &collector{ | ||
factory: factory, | ||
gauges: gauges, | ||
interval: interval, | ||
} | ||
} | ||
|
||
// collectConnectionStats collects database connections for Prometheus to scrape. | ||
func (d *collector) collectConnectionStats() { | ||
for k, v := range d.factory.List() { | ||
conn := v.Conn.(*gorm.DB) | ||
db, _ := conn.DB() | ||
stats := db.Stats() | ||
withValues := []string{"dbname", k, "driver", conn.Name()} | ||
d.gauges.Idle. | ||
With(withValues...). | ||
Set(float64(stats.Idle)) | ||
|
||
d.gauges.InUse. | ||
With(withValues...). | ||
Set(float64(stats.InUse)) | ||
|
||
d.gauges.Open. | ||
With(withValues...). | ||
Set(float64(stats.OpenConnections)) | ||
} | ||
} |
Oops, something went wrong.