-
Notifications
You must be signed in to change notification settings - Fork 0
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
0 parents
commit f6d0eb0
Showing
6 changed files
with
150 additions
and
0 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,11 @@ | ||
name: Test | ||
|
||
on: | ||
workflow_dispatch: | ||
push: | ||
|
||
jobs: | ||
test: | ||
uses: kneu-messenger-pigeon/github-workflows/.github/workflows/test.yaml@main | ||
secrets: inherit | ||
|
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,2 @@ | ||
[![Test](https://github.com/kneu-messenger-pigeon/victoria-metrics-init/actions/workflows/test.yaml/badge.svg)](https://github.com/kneu-messenger-pigeon/victoria-metrics-init/actions/workflows/test.yaml) | ||
[![codecov](https://codecov.io/gh/kneu-messenger-pigeon/victoria-metrics-init/branch/main/graph/badge.svg?token=72G31R0CBU)](https://codecov.io/gh/kneu-messenger-pigeon/victoria-metrics-init) |
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,19 @@ | ||
module github.com/kneu-messenger-pigeon/victoria-metrics-init | ||
|
||
go 1.20 | ||
|
||
require ( | ||
github.com/VictoriaMetrics/metrics v1.24.0 | ||
github.com/h2non/gock v1.2.0 | ||
github.com/stretchr/testify v1.8.4 | ||
) | ||
|
||
require ( | ||
github.com/davecgh/go-spew v1.1.1 // indirect | ||
github.com/h2non/parth v0.0.0-20190131123155-b4df798d6542 // indirect | ||
github.com/pmezard/go-difflib v1.0.0 // indirect | ||
github.com/valyala/fastrand v1.1.0 // indirect | ||
github.com/valyala/histogram v1.2.0 // indirect | ||
golang.org/x/sys v0.7.0 // indirect | ||
gopkg.in/yaml.v3 v3.0.1 // indirect | ||
) |
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,24 @@ | ||
github.com/VictoriaMetrics/metrics v1.24.0 h1:ILavebReOjYctAGY5QU2F9X0MYvkcrG3aEn2RKa1Zkw= | ||
github.com/VictoriaMetrics/metrics v1.24.0/go.mod h1:eFT25kvsTidQFHb6U0oa0rTrDRdz4xTYjpL8+UPohys= | ||
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= | ||
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= | ||
github.com/h2non/gock v1.2.0 h1:K6ol8rfrRkUOefooBC8elXoaNGYkpp7y2qcxGG6BzUE= | ||
github.com/h2non/gock v1.2.0/go.mod h1:tNhoxHYW2W42cYkYb1WqzdbYIieALC99kpYr7rH/BQk= | ||
github.com/h2non/parth v0.0.0-20190131123155-b4df798d6542 h1:2VTzZjLZBgl62/EtslCrtky5vbi9dd7HrQPQIx6wqiw= | ||
github.com/h2non/parth v0.0.0-20190131123155-b4df798d6542/go.mod h1:Ow0tF8D4Kplbc8s8sSb3V2oUCygFHVp8gC3Dn6U4MNI= | ||
github.com/nbio/st v0.0.0-20140626010706-e9e8d9816f32 h1:W6apQkHrMkS0Muv8G/TipAy/FJl/rCYT0+EuS8+Z0z4= | ||
github.com/nbio/st v0.0.0-20140626010706-e9e8d9816f32/go.mod h1:9wM+0iRr9ahx58uYLpLIr5fm8diHn0JbqRycJi6w0Ms= | ||
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= | ||
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= | ||
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk= | ||
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo= | ||
github.com/valyala/fastrand v1.1.0 h1:f+5HkLW4rsgzdNoleUOB69hyT9IlD2ZQh9GyDMfb5G8= | ||
github.com/valyala/fastrand v1.1.0/go.mod h1:HWqCzkrkg6QXT8V2EXWvXCoow7vLwOFN002oeRzjapQ= | ||
github.com/valyala/histogram v1.2.0 h1:wyYGAZZt3CpwUiIb9AU/Zbllg1llXyrtApRS815OLoQ= | ||
github.com/valyala/histogram v1.2.0/go.mod h1:Hb4kBwb4UxsaNbbbh+RRz8ZR6pdodR57tzWUS3BUzXY= | ||
golang.org/x/sys v0.7.0 h1:3jlCCIQZPdOYu1h8BkNvLz8Kgwtae2cagcG/VamtZRU= | ||
golang.org/x/sys v0.7.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= | ||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= | ||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= | ||
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= | ||
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= |
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,35 @@ | ||
package victoria_metrics_init | ||
|
||
import ( | ||
"fmt" | ||
"github.com/VictoriaMetrics/metrics" | ||
"os" | ||
"time" | ||
) | ||
|
||
const defaultHost = "victoria-metrics:8428" | ||
|
||
var defaultTimeInterval = 10 * time.Second | ||
|
||
func InitMetrics(instance string) { | ||
host := os.Getenv("VICTORIA_METRICS_HOST") | ||
if host == "" { | ||
host = defaultHost | ||
} | ||
|
||
timeInterval, err := time.ParseDuration(os.Getenv("VICTORIA_METRICS_INTERVAL")) | ||
if err != nil || timeInterval <= 0 { | ||
timeInterval = defaultTimeInterval | ||
} | ||
|
||
err = metrics.InitPush( | ||
fmt.Sprintf("http://%s/api/v1/import/prometheus", host), | ||
timeInterval, | ||
fmt.Sprintf(`instance="%s"`, instance), | ||
true, | ||
) | ||
|
||
if err != nil { | ||
_, _ = fmt.Fprintf(os.Stderr, "Failed to init metrics: %s\n", err) | ||
} | ||
} |
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,59 @@ | ||
package victoria_metrics_init | ||
|
||
import ( | ||
"github.com/h2non/gock" | ||
"github.com/stretchr/testify/assert" | ||
"os" | ||
"runtime" | ||
"testing" | ||
"time" | ||
) | ||
|
||
func TestInitMetrics(t *testing.T) { | ||
t.Run("request", func(t *testing.T) { | ||
testHost := "testHost:8428" | ||
|
||
_ = os.Setenv("VICTORIA_METRICS_HOST", testHost) | ||
_ = os.Setenv("VICTORIA_METRICS_INTERVAL", "1s") | ||
|
||
gock.New("http://" + testHost).Get("/api/v1/import/prometheus").Reply(200) | ||
|
||
InitMetrics("testInstance") | ||
time.Sleep(time.Second * 2) | ||
assert.True(t, gock.IsDone()) | ||
}) | ||
|
||
t.Run("defaults", func(t *testing.T) { | ||
defaultTimeInterval = 2 * time.Second | ||
|
||
_ = os.Setenv("VICTORIA_METRICS_HOST", "") | ||
_ = os.Setenv("VICTORIA_METRICS_INTERVAL", "") | ||
|
||
gock.New("http://" + defaultHost).Get("/api/v1/import/prometheus").Reply(200) | ||
|
||
InitMetrics("testInstance") | ||
runtime.Gosched() | ||
time.Sleep(defaultTimeInterval + time.Millisecond*500) | ||
assert.True(t, gock.IsDone()) | ||
}) | ||
|
||
t.Run("errors", func(t *testing.T) { | ||
stdErrFile, _ := os.CreateTemp("", "stderr.txt") | ||
origStderr := os.Stderr | ||
os.Stderr = stdErrFile | ||
|
||
defer (func() { | ||
_ = stdErrFile.Close() | ||
_ = os.Remove(stdErrFile.Name()) | ||
os.Stderr = origStderr | ||
})() | ||
|
||
_ = os.Setenv("VICTORIA_METRICS_HOST", "\n") | ||
_ = os.Setenv("VICTORIA_METRICS_INTERVAL", "") | ||
|
||
InitMetrics("testInstance") | ||
|
||
content, _ := os.ReadFile(stdErrFile.Name()) | ||
assert.Contains(t, string(content), "Failed to init metrics: ") | ||
}) | ||
} |