Skip to content

Commit

Permalink
test - new test for metrics
Browse files Browse the repository at this point in the history
  • Loading branch information
nstrappazzonc committed Dec 6, 2024
1 parent 30daad1 commit 0205a35
Show file tree
Hide file tree
Showing 6 changed files with 71 additions and 26 deletions.
10 changes: 8 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,23 @@ module github.com/debeando/agent-mysql

go 1.23.2

require github.com/debeando/go-common v0.4.15
require (
github.com/debeando/go-common v0.4.15
github.com/influxdata/influxdb-client-go/v2 v2.14.0
github.com/stretchr/testify v1.9.0
)

require (
filippo.io/edwards25519 v1.1.0 // indirect
github.com/apapsch/go-jsonmerge/v2 v2.0.0 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/go-sql-driver/mysql v1.8.1 // indirect
github.com/google/uuid v1.6.0 // indirect
github.com/influxdata/influxdb-client-go/v2 v2.14.0 // indirect
github.com/influxdata/line-protocol v0.0.0-20200327222509-2487e7298839 // indirect
github.com/oapi-codegen/runtime v1.0.0 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/sirupsen/logrus v1.9.3 // indirect
golang.org/x/net v0.23.0 // indirect
golang.org/x/sys v0.26.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
11 changes: 3 additions & 8 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,8 @@ github.com/bmatcuk/doublestar v1.1.1/go.mod h1:UD6OnuiIn0yFxxA2le/rnRU1G4RaI4UvF
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
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/debeando/go-common v0.4.14 h1:uLfNnmL06pMvSEukv7dHAd6swRssvP157jMiB+LfBlQ=
github.com/debeando/go-common v0.4.14/go.mod h1:VB9jy+4LKkB5rGvdwvq5MZobK8DUPWc3dL3HPlAW2hw=
github.com/debeando/go-common v0.4.15 h1:ReOX4HF3trrtEBmh26gijZFPNXMvbpK91U2y3C6ZLrA=
github.com/debeando/go-common v0.4.15/go.mod h1:W8uo1WxtES3CS42OaIFjgDolhtCoqhGgrFq9INi0sek=
github.com/go-sql-driver/mysql v1.7.1 h1:lUIinVbN1DY0xBg0eMOzmmtGoHwWBbvnWubQUrtU8EI=
github.com/go-sql-driver/mysql v1.7.1/go.mod h1:OXbVy3sEdcQ2Doequ6Z5BW6fXNQTmx+9S1MCJN5yJMI=
github.com/go-sql-driver/mysql v1.8.1 h1:LedoTUt/eveggdHS9qUFC1EFSa8bU2+1pZjSRpvNJ1Y=
github.com/go-sql-driver/mysql v1.8.1/go.mod h1:wEBSXgmK//2ZFJyE+qWnIsVGmvmEKlqwuVSjsCm7DZg=
github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
Expand All @@ -32,15 +28,14 @@ github.com/spkg/bom v0.0.0-20160624110644-59b7046e48ad/go.mod h1:qLr4V1qq6nMqFKk
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
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/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
golang.org/x/net v0.23.0 h1:7EYJ93RZ9vYSZAIb2x3lnuvqO5zneoD6IvWjuhfxjTs=
golang.org/x/net v0.23.0/go.mod h1:JKghWKKOSdJwpW2GEx0Ja7fmaKnMsbu+MWVZTokSYmg=
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.14.0 h1:Vz7Qs629MkJkGyHxUlRHizWJRG2j8fbQKjELVSNhy7Q=
golang.org/x/sys v0.14.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/sys v0.26.0 h1:KHjCJyddX0LoSTb3J+vWpupP9p0oznkqVk/IfjymZbo=
golang.org/x/sys v0.26.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
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.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
Expand Down
11 changes: 6 additions & 5 deletions influxdb.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"os"
"time"

"github.com/debeando/agent-mysql/metrics"
"github.com/debeando/go-common/env"
"github.com/debeando/go-common/log"
"github.com/influxdata/influxdb-client-go/v2"
Expand Down Expand Up @@ -49,14 +50,14 @@ func (i *InfluxDB) New() {
)
}

func (i *InfluxDB) Write(metrics Metrics) {
func (i *InfluxDB) Write(items metrics.Metrics) {
writeAPI := i.Connection.WriteAPI("debeando", i.Bucket)

for _, metric := range metrics {
for _, item := range items {
point := influxdb2.NewPoint(
metric.Measurement,
metric.TagsToMap(),
metric.FieldsToMap(),
item.Measurement,
item.TagsToMap(),
item.FieldsToMap(),
time.Now(),
)

Expand Down
21 changes: 11 additions & 10 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"strings"
"time"

"github.com/debeando/agent-mysql/metrics"
"github.com/debeando/go-common/env"
"github.com/debeando/go-common/log"
"github.com/debeando/go-common/mysql"
Expand Down Expand Up @@ -39,12 +40,12 @@ func main() {
defer MySQL.Connection.Close()

for {
metrics := Metrics{}
items := metrics.Metrics{}

MySQL.Connection.Connect()

for _, query := range Queries {
metric := Metric{}
metric := metrics.Metric{}

if !query.IsTime(query.Interval) {
continue
Expand All @@ -59,41 +60,41 @@ func main() {

MySQL.Connection.FetchAll(query.Beautifier(), func(row map[string]string) {
metric.Measurement = query.Name
metric.AddTag(Tag{
metric.AddTag(metrics.Tag{
Name: "server",
Value: getServer(),
})

if query.UnPivot {
for column, value := range row {
if valueParsed, ok := mysql.ParseNumberValue(value); ok {
metric.AddField(Field{
metric.AddField(metrics.Field{
Name: column,
Value: valueParsed,
})
} else {
metric.AddTag(Tag{
metric.AddTag(metrics.Tag{
Name: column,
Value: value,
})
}
}
} else if valueParsed, ok := mysql.ParseNumberValue(row[query.Value]); ok {
metric.AddField(Field{
metric.AddField(metrics.Field{
Name: row[query.Key],
Value: valueParsed,
})
}
})

metrics.Add(metric)
items.Add(metric)
}

if metrics.Count() > 0 {
influxDB.Write(metrics)
if items.Count() > 0 {
influxDB.Write(items)
}

metrics.Reset()
items.Reset()
log.Debug("Wait until next collect metrics.")
time.Sleep(getInterval())
}
Expand Down
2 changes: 1 addition & 1 deletion metrics.go → metrics/metrics.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package main
package metrics

// Tag for metric.
type Tag struct {
Expand Down
42 changes: 42 additions & 0 deletions metrics/metrics_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package metrics_test

import (
"testing"

"github.com/debeando/agent-mysql/metrics"

"github.com/stretchr/testify/assert"
)

func TestEmpty(t *testing.T) {
m := metrics.Metric{}

assert.True(t, m.Empty())

m.AddTag(metrics.Tag{Name: "Test", Value: "test"})
m.AddField(metrics.Field{Name: "Test", Value: 0})

assert.False(t, m.Empty())
}

func TestCountTags(t *testing.T) {
m := metrics.Metric{}

assert.Equal(t, m.CountTags(), 0)

m.AddTag(metrics.Tag{Name: "Test1", Value: "test1"})
m.AddTag(metrics.Tag{Name: "Test2", Value: "test2"})

assert.Equal(t, m.CountTags(), 2)
}

func TestCountFields(t *testing.T) {
m := metrics.Metric{}

assert.Equal(t, m.CountFields(), 0)

m.AddField(metrics.Field{Name: "Test1", Value: "test1"})
m.AddField(metrics.Field{Name: "Test2", Value: "test2"})

assert.Equal(t, m.CountFields(), 2)
}

0 comments on commit 0205a35

Please sign in to comment.