Skip to content

Commit 792d421

Browse files
fix(customMetrics): float values to Gauge metric type (#76)
If no type was declared for a particular metric and the value had a decimal number it was converted as string metric type instead of Gauge. The float64 type was missing alongside some test of this feature.
1 parent 9dc652f commit 792d421

3 files changed

Lines changed: 74 additions & 33 deletions

File tree

src/metrics/metrics.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ func CollectCustomConfig(ci connection.Info, cfg customMetricsConfig, pgIntegrat
171171

172172
func sanitizeValue(val interface{}) interface{} {
173173
switch v := val.(type) {
174-
case string, float32, int, int32, int64:
174+
case string, float32, float64, int, int32, int64:
175175
return v
176176
case []byte:
177177
return string(v)

src/metrics/metrics_test.go

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package metrics
22

33
import (
4+
"os"
5+
"path/filepath"
46
"testing"
57

68
"github.com/blang/semver/v4"
@@ -507,3 +509,42 @@ func TestPopulateMetrics(t *testing.T) {
507509

508510
PopulateMetrics(ci, dbList, instance, testIntegration, true, true, true, "")
509511
}
512+
513+
func TestPopulateCustomMetricsFromFile(t *testing.T) {
514+
t.Parallel()
515+
516+
testIntegration, _ := integration.New("test", "test")
517+
518+
ci := &connection.MockInfo{}
519+
520+
testConnection, mock := connection.CreateMockSQL(t)
521+
522+
ci.On("NewConnection", tmock.Anything).Return(testConnection, nil)
523+
524+
instanceRows := sqlmock.NewRows([]string{
525+
"int_metric",
526+
"float_metric",
527+
"string_metric",
528+
}).AddRow(25, 0.064, "test-string")
529+
530+
mock.ExpectQuery(".*testTable.*").
531+
WillReturnRows(instanceRows)
532+
533+
dir := t.TempDir()
534+
customQueryCfg := []byte(`---
535+
queries:
536+
- query: >-
537+
SELECT test FROM testTable;
538+
`)
539+
assert.NoError(t, os.WriteFile(filepath.Join(dir, "customQueryConfig.yaml"), customQueryCfg, 0600))
540+
541+
PopulateCustomMetricsFromFile(ci, filepath.Join(dir, "customQueryConfig.yaml"), testIntegration)
542+
543+
assert.Len(t, testIntegration.Entities, 1)
544+
assert.Len(t, testIntegration.Entities[0].Metrics, 1)
545+
metricSet := testIntegration.Entities[0].Metrics[0].Metrics
546+
547+
assert.Equal(t, float64(25), metricSet["int_metric"])
548+
assert.Equal(t, float64(0.064), metricSet["float_metric"])
549+
assert.Equal(t, "test-string", metricSet["string_metric"])
550+
}

tests/testdata/jsonschema92.json

Lines changed: 32 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -12641,7 +12641,7 @@
1264112641
"type": "string"
1264212642
},
1264312643
"table.bloatRatio": {
12644-
"type": "integer"
12644+
"type": "number"
1264512645
},
1264612646
"table.bloatSizeInBytes": {
1264712647
"type": "integer"
@@ -12784,7 +12784,7 @@
1278412784
"type": "string"
1278512785
},
1278612786
"table.bloatRatio": {
12787-
"type": "integer"
12787+
"type": "number"
1278812788
},
1278912789
"table.bloatSizeInBytes": {
1279012790
"type": "integer"
@@ -12927,7 +12927,7 @@
1292712927
"type": "string"
1292812928
},
1292912929
"table.bloatRatio": {
12930-
"type": "integer"
12930+
"type": "number"
1293112931
},
1293212932
"table.bloatSizeInBytes": {
1293312933
"type": "integer"
@@ -13070,7 +13070,7 @@
1307013070
"type": "string"
1307113071
},
1307213072
"table.bloatRatio": {
13073-
"type": "integer"
13073+
"type": "number"
1307413074
},
1307513075
"table.bloatSizeInBytes": {
1307613076
"type": "integer"
@@ -13213,7 +13213,7 @@
1321313213
"type": "string"
1321413214
},
1321513215
"table.bloatRatio": {
13216-
"type": "integer"
13216+
"type": "number"
1321713217
},
1321813218
"table.bloatSizeInBytes": {
1321913219
"type": "integer"
@@ -13356,7 +13356,7 @@
1335613356
"type": "string"
1335713357
},
1335813358
"table.bloatRatio": {
13359-
"type": "integer"
13359+
"type": "number"
1336013360
},
1336113361
"table.bloatSizeInBytes": {
1336213362
"type": "integer"
@@ -13499,7 +13499,7 @@
1349913499
"type": "string"
1350013500
},
1350113501
"table.bloatRatio": {
13502-
"type": "integer"
13502+
"type": "number"
1350313503
},
1350413504
"table.bloatSizeInBytes": {
1350513505
"type": "integer"
@@ -13642,7 +13642,7 @@
1364213642
"type": "string"
1364313643
},
1364413644
"table.bloatRatio": {
13645-
"type": "integer"
13645+
"type": "number"
1364613646
},
1364713647
"table.bloatSizeInBytes": {
1364813648
"type": "integer"
@@ -13785,7 +13785,7 @@
1378513785
"type": "string"
1378613786
},
1378713787
"table.bloatRatio": {
13788-
"type": "integer"
13788+
"type": "number"
1378913789
},
1379013790
"table.bloatSizeInBytes": {
1379113791
"type": "integer"
@@ -13928,7 +13928,7 @@
1392813928
"type": "string"
1392913929
},
1393013930
"table.bloatRatio": {
13931-
"type": "integer"
13931+
"type": "number"
1393213932
},
1393313933
"table.bloatSizeInBytes": {
1393413934
"type": "integer"
@@ -14071,7 +14071,7 @@
1407114071
"type": "string"
1407214072
},
1407314073
"table.bloatRatio": {
14074-
"type": "integer"
14074+
"type": "number"
1407514075
},
1407614076
"table.bloatSizeInBytes": {
1407714077
"type": "integer"
@@ -14214,7 +14214,7 @@
1421414214
"type": "string"
1421514215
},
1421614216
"table.bloatRatio": {
14217-
"type": "integer"
14217+
"type": "number"
1421814218
},
1421914219
"table.bloatSizeInBytes": {
1422014220
"type": "integer"
@@ -14357,7 +14357,7 @@
1435714357
"type": "string"
1435814358
},
1435914359
"table.bloatRatio": {
14360-
"type": "integer"
14360+
"type": "number"
1436114361
},
1436214362
"table.bloatSizeInBytes": {
1436314363
"type": "integer"
@@ -14500,7 +14500,7 @@
1450014500
"type": "string"
1450114501
},
1450214502
"table.bloatRatio": {
14503-
"type": "integer"
14503+
"type": "number"
1450414504
},
1450514505
"table.bloatSizeInBytes": {
1450614506
"type": "integer"
@@ -14643,7 +14643,7 @@
1464314643
"type": "string"
1464414644
},
1464514645
"table.bloatRatio": {
14646-
"type": "integer"
14646+
"type": "number"
1464714647
},
1464814648
"table.bloatSizeInBytes": {
1464914649
"type": "integer"
@@ -14786,7 +14786,7 @@
1478614786
"type": "string"
1478714787
},
1478814788
"table.bloatRatio": {
14789-
"type": "integer"
14789+
"type": "number"
1479014790
},
1479114791
"table.bloatSizeInBytes": {
1479214792
"type": "integer"
@@ -15072,7 +15072,7 @@
1507215072
"type": "string"
1507315073
},
1507415074
"table.bloatRatio": {
15075-
"type": "integer"
15075+
"type": "number"
1507615076
},
1507715077
"table.bloatSizeInBytes": {
1507815078
"type": "integer"
@@ -15215,7 +15215,7 @@
1521515215
"type": "string"
1521615216
},
1521715217
"table.bloatRatio": {
15218-
"type": "integer"
15218+
"type": "number"
1521915219
},
1522015220
"table.bloatSizeInBytes": {
1522115221
"type": "integer"
@@ -15358,7 +15358,7 @@
1535815358
"type": "string"
1535915359
},
1536015360
"table.bloatRatio": {
15361-
"type": "integer"
15361+
"type": "number"
1536215362
},
1536315363
"table.bloatSizeInBytes": {
1536415364
"type": "integer"
@@ -15501,7 +15501,7 @@
1550115501
"type": "string"
1550215502
},
1550315503
"table.bloatRatio": {
15504-
"type": "integer"
15504+
"type": "number"
1550515505
},
1550615506
"table.bloatSizeInBytes": {
1550715507
"type": "integer"
@@ -15644,7 +15644,7 @@
1564415644
"type": "string"
1564515645
},
1564615646
"table.bloatRatio": {
15647-
"type": "integer"
15647+
"type": "number"
1564815648
},
1564915649
"table.bloatSizeInBytes": {
1565015650
"type": "integer"
@@ -15787,7 +15787,7 @@
1578715787
"type": "string"
1578815788
},
1578915789
"table.bloatRatio": {
15790-
"type": "integer"
15790+
"type": "number"
1579115791
},
1579215792
"table.bloatSizeInBytes": {
1579315793
"type": "integer"
@@ -15930,7 +15930,7 @@
1593015930
"type": "string"
1593115931
},
1593215932
"table.bloatRatio": {
15933-
"type": "integer"
15933+
"type": "number"
1593415934
},
1593515935
"table.bloatSizeInBytes": {
1593615936
"type": "integer"
@@ -16073,7 +16073,7 @@
1607316073
"type": "string"
1607416074
},
1607516075
"table.bloatRatio": {
16076-
"type": "integer"
16076+
"type": "number"
1607716077
},
1607816078
"table.bloatSizeInBytes": {
1607916079
"type": "integer"
@@ -16216,7 +16216,7 @@
1621616216
"type": "string"
1621716217
},
1621816218
"table.bloatRatio": {
16219-
"type": "integer"
16219+
"type": "number"
1622016220
},
1622116221
"table.bloatSizeInBytes": {
1622216222
"type": "integer"
@@ -16359,7 +16359,7 @@
1635916359
"type": "string"
1636016360
},
1636116361
"table.bloatRatio": {
16362-
"type": "integer"
16362+
"type": "number"
1636316363
},
1636416364
"table.bloatSizeInBytes": {
1636516365
"type": "integer"
@@ -16502,7 +16502,7 @@
1650216502
"type": "string"
1650316503
},
1650416504
"table.bloatRatio": {
16505-
"type": "integer"
16505+
"type": "number"
1650616506
},
1650716507
"table.bloatSizeInBytes": {
1650816508
"type": "integer"
@@ -16645,7 +16645,7 @@
1664516645
"type": "string"
1664616646
},
1664716647
"table.bloatRatio": {
16648-
"type": "integer"
16648+
"type": "number"
1664916649
},
1665016650
"table.bloatSizeInBytes": {
1665116651
"type": "integer"
@@ -16788,7 +16788,7 @@
1678816788
"type": "string"
1678916789
},
1679016790
"table.bloatRatio": {
16791-
"type": "integer"
16791+
"type": "number"
1679216792
},
1679316793
"table.bloatSizeInBytes": {
1679416794
"type": "integer"
@@ -16931,7 +16931,7 @@
1693116931
"type": "string"
1693216932
},
1693316933
"table.bloatRatio": {
16934-
"type": "integer"
16934+
"type": "number"
1693516935
},
1693616936
"table.bloatSizeInBytes": {
1693716937
"type": "integer"
@@ -17074,7 +17074,7 @@
1707417074
"type": "string"
1707517075
},
1707617076
"table.bloatRatio": {
17077-
"type": "integer"
17077+
"type": "number"
1707817078
},
1707917079
"table.bloatSizeInBytes": {
1708017080
"type": "integer"
@@ -17217,7 +17217,7 @@
1721717217
"type": "string"
1721817218
},
1721917219
"table.bloatRatio": {
17220-
"type": "integer"
17220+
"type": "number"
1722117221
},
1722217222
"table.bloatSizeInBytes": {
1722317223
"type": "integer"

0 commit comments

Comments
 (0)