-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
176 additions
and
30 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
package persistence | ||
|
||
import ( | ||
"time" | ||
|
||
"github.com/ydb-platform/nbs/cloud/tasks/metrics" | ||
) | ||
|
||
//////////////////////////////////////////////////////////////////////////////// | ||
|
||
type healthCheck struct { | ||
queriesCount uint64 | ||
successQueriesCount uint64 | ||
registry metrics.Registry | ||
metricsCollectionInterval time.Duration | ||
} | ||
|
||
func (h *healthCheck) accountQuery(err error) { | ||
h.queriesCount++ | ||
if err == nil { | ||
h.successQueriesCount++ | ||
} | ||
} | ||
|
||
func (h *healthCheck) reportSuccessRate() { | ||
if h.queriesCount == 0 { | ||
h.registry.Gauge("successRate").Set(0) | ||
return | ||
} | ||
|
||
h.registry.Gauge("successRate").Set(float64(h.successQueriesCount) / float64(h.queriesCount)) | ||
} | ||
|
||
func newHealthCheck(componentName string, registry metrics.Registry) *healthCheck { | ||
subRegistry := registry.WithTags(map[string]string{ | ||
"component": componentName, | ||
}) | ||
|
||
h := healthCheck{ | ||
registry: subRegistry, | ||
metricsCollectionInterval: 15 * time.Second, // todo | ||
} | ||
|
||
go func() { | ||
ticker := time.NewTicker(h.metricsCollectionInterval) | ||
defer ticker.Stop() | ||
|
||
for range ticker.C { | ||
h.reportSuccessRate() | ||
} | ||
}() | ||
|
||
return &h | ||
} |
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,48 @@ | ||
package persistence | ||
|
||
import ( | ||
"sync" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/mock" | ||
"github.com/ydb-platform/nbs/cloud/tasks/errors" | ||
"github.com/ydb-platform/nbs/cloud/tasks/metrics/mocks" | ||
) | ||
|
||
//////////////////////////////////////////////////////////////////////////////// | ||
|
||
func TestHealthCheckMetric(t *testing.T) { | ||
registry := mocks.NewRegistryMock() | ||
healthCheck := newHealthCheck("test", registry) | ||
|
||
gaugeSetWg := sync.WaitGroup{} | ||
|
||
gaugeSetWg.Add(1) | ||
registry.GetGauge( | ||
"successRate", | ||
map[string]string{"component": "test"}, | ||
).On("Set", float64(0)).Once().Run( | ||
func(args mock.Arguments) { | ||
gaugeSetWg.Done() | ||
}, | ||
) | ||
gaugeSetWg.Wait() | ||
|
||
healthCheck.accountQuery(nil) | ||
healthCheck.accountQuery(nil) | ||
healthCheck.accountQuery(nil) | ||
healthCheck.accountQuery(errors.NewEmptyRetriableError()) | ||
|
||
gaugeSetWg.Add(1) | ||
registry.GetGauge( | ||
"successRate", | ||
map[string]string{"component": "test"}, | ||
).On("Set", float64(3.0/4.0)).Once().Run( | ||
func(args mock.Arguments) { | ||
gaugeSetWg.Done() | ||
}, | ||
) | ||
gaugeSetWg.Wait() | ||
|
||
registry.AssertAllExpectations(t) | ||
} |
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