Skip to content

Commit

Permalink
SDK upgraded to v3
Browse files Browse the repository at this point in the history
  • Loading branch information
varas committed Nov 7, 2018
1 parent c4ca3db commit b3564a1
Show file tree
Hide file tree
Showing 29 changed files with 1,901 additions and 665 deletions.
2 changes: 1 addition & 1 deletion src/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"database/sql"

_ "github.com/go-sql-driver/mysql"
"gopkg.in/newrelic/infra-integrations-sdk.v2/log"
"github.com/newrelic/infra-integrations-sdk/log"
)

type dataSource interface {
Expand Down
14 changes: 7 additions & 7 deletions src/metrics-parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ package main
import (
"strconv"

"gopkg.in/newrelic/infra-integrations-sdk.v2/log"
"gopkg.in/newrelic/infra-integrations-sdk.v2/metric"
"gopkg.in/newrelic/infra-integrations-sdk.v2/sdk"
"github.com/newrelic/infra-integrations-sdk/data/inventory"
"github.com/newrelic/infra-integrations-sdk/data/metric"
"github.com/newrelic/infra-integrations-sdk/log"
)

const (
Expand Down Expand Up @@ -61,13 +61,13 @@ func getRawData(db dataSource) (map[string]interface{}, map[string]interface{},
return inventory, metrics, nil
}

func populateInventory(inventory sdk.Inventory, rawData map[string]interface{}) {
func populateInventory(inventory *inventory.Inventory, rawData map[string]interface{}) {
for name, value := range rawData {
inventory.SetItem(name, "value", value)
}
}

func populateMetrics(sample *metric.MetricSet, rawMetrics map[string]interface{}) {
func populateMetrics(sample *metric.Set, rawMetrics map[string]interface{}) {
if rawMetrics["node_type"] != "slave" {
delete(defaultMetrics, "cluster.slaveRunning")
}
Expand All @@ -89,7 +89,7 @@ func populateMetrics(sample *metric.MetricSet, rawMetrics map[string]interface{}
}

}
func populatePartialMetrics(sample *metric.MetricSet, metrics map[string]interface{}, metricsDefinition map[string][]interface{}) {
func populatePartialMetrics(ms *metric.Set, metrics map[string]interface{}, metricsDefinition map[string][]interface{}) {
for metricName, metricConf := range metricsDefinition {
rawSource := metricConf[0]
metricType := metricConf[1].(metric.SourceType)
Expand All @@ -114,7 +114,7 @@ func populatePartialMetrics(sample *metric.MetricSet, metrics map[string]interfa
continue
}

err := sample.SetMetric(metricName, rawMetric, metricType)
err := ms.SetMetric(metricName, rawMetric, metricType)

if err != nil {
log.Warn("Error setting value: %s", err)
Expand Down
2 changes: 1 addition & 1 deletion src/metrics.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package main

import (
"gopkg.in/newrelic/infra-integrations-sdk.v2/metric"
"github.com/newrelic/infra-integrations-sdk/data/metric"
)

var defaultMetrics = map[string][]interface{}{
Expand Down
21 changes: 11 additions & 10 deletions src/mysql.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ package main
import (
"fmt"

sdk_args "gopkg.in/newrelic/infra-integrations-sdk.v2/args"
"gopkg.in/newrelic/infra-integrations-sdk.v2/log"
"gopkg.in/newrelic/infra-integrations-sdk.v2/sdk"
sdk_args "github.com/newrelic/infra-integrations-sdk/args"
"github.com/newrelic/infra-integrations-sdk/integration"
"github.com/newrelic/infra-integrations-sdk/log"
)

const (
Expand All @@ -32,11 +32,12 @@ func generateDSN(args argumentList) string {
var args argumentList

func main() {
integration, err := sdk.NewIntegration(integrationName, integrationVersion, &args)
i, err := integration.New(integrationName, integrationVersion, integration.Args(&args))
fatalIfErr(err)
log.SetupLogging(args.Verbose)

sample := integration.NewMetricSet("MysqlSample")
e := i.LocalEntity()
ms := e.NewMetricSet("MysqlSample")

db, err := openDB(generateDSN(args))
fatalIfErr(err)
Expand All @@ -45,15 +46,15 @@ func main() {
rawInventory, rawMetrics, err := getRawData(db)
fatalIfErr(err)

if args.All || args.Inventory {
populateInventory(integration.Inventory, rawInventory)
if args.HasInventory() {
populateInventory(e.Inventory, rawInventory)
}

if args.All || args.Metrics {
populateMetrics(sample, rawMetrics)
if args.HasMetrics() {
populateMetrics(ms, rawMetrics)
}

fatalIfErr(integration.Publish())
fatalIfErr(i.Publish())
}

func fatalIfErr(err error) {
Expand Down
55 changes: 21 additions & 34 deletions src/mysql_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@ package main
import (
"testing"

"gopkg.in/newrelic/infra-integrations-sdk.v2/metric"
"gopkg.in/newrelic/infra-integrations-sdk.v2/sdk"
"github.com/newrelic/infra-integrations-sdk/data/inventory"
"github.com/newrelic/infra-integrations-sdk/data/metric"
"github.com/stretchr/testify/assert"
)

func TestAsValue(t *testing.T) {
Expand Down Expand Up @@ -61,29 +62,15 @@ func TestPopulatePartialMetrics(t *testing.T) {
"functionSource": {functionSource, metric.GAUGE},
}

var sample = metric.NewMetricSet("eventType")
populatePartialMetrics(&sample, rawMetrics, metricDefinition)

if sample["rawMetric1"] != 1 {
t.Error()
}
if sample["rawMetric2"] != 2 {
t.Error()
}
if sample["rawMetric3"] != "foo" {
t.Error()
}

if sample["unknownMetric"] != nil {
t.Error()
}
if sample["badRawSource"] != nil {
t.Error()
}
if sample["functionSource"] != float64(3) {
t.Error()
}
var ms = metric.NewSet("eventType", nil)
populatePartialMetrics(ms, rawMetrics, metricDefinition)

assert.Equal(t, 1., ms.Metrics["rawMetric1"])
assert.Equal(t, 2., ms.Metrics["rawMetric2"])
assert.Equal(t, "foo", ms.Metrics["rawMetric3"])
assert.Nil(t, ms.Metrics["unknownMetric"])
assert.Nil(t, ms.Metrics["badRawSource"])
assert.Equal(t, 3., ms.Metrics["functionSource"])
}

func TestPopulateInventory(t *testing.T) {
Expand All @@ -93,12 +80,12 @@ func TestPopulateInventory(t *testing.T) {
"key_3": "foo",
}

inventory := make(sdk.Inventory)
populateInventory(inventory, rawInventory)
i := inventory.New()
populateInventory(i, rawInventory)
for key, value := range rawInventory {
if inventory[key]["value"] != value {
t.Error()
}
v, exists := i.Item(key)
assert.True(t, exists)
assert.Equal(t, value, v["value"])
}
}

Expand Down Expand Up @@ -158,16 +145,16 @@ func TestPopulateMetricsWithZeroValuesInData(t *testing.T) {
"Key_cache_block_size": 0,
"Key_buffer_size": 0,
}
ms := metric.NewMetricSet("eventType")
populatePartialMetrics(&ms, rawMetrics, defaultMetrics)
populatePartialMetrics(&ms, rawMetrics, extendedMetrics)
populatePartialMetrics(&ms, rawMetrics, myisamMetrics)
ms := metric.NewSet("eventType", nil)
populatePartialMetrics(ms, rawMetrics, defaultMetrics)
populatePartialMetrics(ms, rawMetrics, extendedMetrics)
populatePartialMetrics(ms, rawMetrics, myisamMetrics)

testMetrics := []string{"db.qCacheUtilization", "db.qCacheHitRatio", "db.threadCacheMissRate", "db.myisam.keyCacheUtilization"}

expected := float64(0)
for _, metricName := range testMetrics {
actual, _ := ms[metricName]
actual, _ := ms.Metrics[metricName]
if actual != expected {
t.Errorf("For metric '%s', expected value: %f. Actual value: %f", metricName, expected, actual)
}
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit b3564a1

Please sign in to comment.