-
Notifications
You must be signed in to change notification settings - Fork 6
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: add mysql connection metrics #118
Merged
Changes from 19 commits
Commits
Show all changes
22 commits
Select commit
Hold shift + click to select a range
7c6b052
feat: add connection metrics
Reasno a3b4718
fix: comment
Reasno 5250caa
fix: change metrics label
Reasno 7eda2d0
fix: data race
Reasno 1c2296f
fix: data race
Reasno cafc124
fix: data race
Reasno 0cf13cd
fix: data race
Reasno c32c763
Merge branch 'master' into metrics
Reasno 424c944
fix: data race
Reasno 85977f9
fix: add default duration
Reasno 0faaf76
fix: test
Reasno ff421d3
fix: test flakes on slow server
Reasno 25e46ad
fix: test flakes
Reasno 51e66fa
fix: test flakes when callback twice
Reasno 32f5a3d
fix: test flakes when callback twice
Reasno c44a3df
fix: test flakes when callback twice
Reasno 8b456e1
fix(otgorm): metrics missing labels (#124)
GGXXLL de85c78
Merge branch 'master' into metrics
Reasno 679b72e
Update otgorm.go
Reasno e9dd90b
Update observability.go
Reasno 48a834b
reame moduleIn.make to maker
GGXXLL 19f7e0b
fix: func renamed
GGXXLL File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,30 +1,65 @@ | ||
package observability | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/DoNewsCode/core" | ||
"github.com/DoNewsCode/core/config" | ||
"github.com/DoNewsCode/core/otgorm" | ||
"github.com/go-kit/kit/log" | ||
"github.com/knadh/koanf/parsers/yaml" | ||
"github.com/knadh/koanf/providers/rawbytes" | ||
"github.com/stretchr/testify/assert" | ||
"gorm.io/gorm" | ||
"testing" | ||
) | ||
|
||
func TestProvide(t *testing.T) { | ||
func TestProvideOpentracing(t *testing.T) { | ||
conf, _ := config.NewConfig(config.WithProviderLayer(rawbytes.Provider([]byte(sample)), yaml.Parser())) | ||
Out, cleanup, err := provide(in{ | ||
Conf: conf, | ||
Logger: log.NewNopLogger(), | ||
AppName: config.AppName("foo"), | ||
Env: config.EnvTesting, | ||
}) | ||
Out, cleanup, err := ProvideOpentracing( | ||
config.AppName("foo"), | ||
config.EnvTesting, | ||
ProvideJaegerLogAdapter(log.NewNopLogger()), | ||
conf, | ||
) | ||
assert.NoError(t, err) | ||
assert.NotNil(t, Out.Tracer) | ||
assert.NotNil(t, Out.Hist) | ||
assert.NotNil(t, Out) | ||
cleanup() | ||
} | ||
|
||
func Test_provideConfig(t *testing.T) { | ||
Conf := provideConfig() | ||
func TestProvideHistogramMetrics(t *testing.T) { | ||
Out := ProvideHistogramMetrics( | ||
config.AppName("foo"), | ||
config.EnvTesting, | ||
) | ||
assert.NotNil(t, Out) | ||
} | ||
|
||
func TestProvideGORMMetrics(t *testing.T) { | ||
c := core.New() | ||
c.ProvideEssentials() | ||
c.Provide(Providers()) | ||
c.Provide(otgorm.Providers()) | ||
c.Invoke(func(db *gorm.DB, g *otgorm.Gauges) { | ||
d, err := db.DB() | ||
if err != nil { | ||
t.Error(err) | ||
} | ||
stats := d.Stats() | ||
withValues := []string{"dbname", "default", "driver", db.Name()} | ||
g.Idle. | ||
With(withValues...). | ||
Set(float64(stats.Idle)) | ||
|
||
g.InUse. | ||
With(withValues...). | ||
Set(float64(stats.InUse)) | ||
|
||
g.Open. | ||
With(withValues...). | ||
Set(float64(stats.OpenConnections)) | ||
}) | ||
} | ||
|
||
func TestExportedConfigs(t *testing.T) { | ||
Conf := exportConfig() | ||
assert.NotEmpty(t, Conf.Config) | ||
} |
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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Other package export config func name is provideConfig, unified? Which one should we use?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
provideConfig. As this is a private function, we can always change it at any point.