Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: process group tag for groundwork output plugin #10499

Merged
merged 2 commits into from
Jan 27, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
feat: add test to cover defaults
  • Loading branch information
Pavlo Sumkin committed Jan 26, 2022
commit 84458edb77a0221c50b92f2b5b8415327a67c7fc
3 changes: 3 additions & 0 deletions plugins/outputs/groundwork/groundwork.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ const sampleConfig = `

## The name of the tag that contains the hostname.
# resource_tag = "host"

## The name of the tag that contains the host group name.
# group_tag = "group"
`

type metricMeta struct {
Expand Down
56 changes: 50 additions & 6 deletions plugins/outputs/groundwork/groundwork_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,53 @@ const (
defaultHost = "telegraf"
)

func TestWrite(t *testing.T) {
func TestWriteWithDefaults(t *testing.T) {
// Generate test metric with default name to test Write logic
floatMetric := testutil.TestMetric(1.0, "Float")
intMetric := testutil.TestMetric(42, "IntMetric")

// Simulate Groundwork server that should receive custom metrics
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
body, err := ioutil.ReadAll(r.Body)
require.NoError(t, err)

// Decode body to use in assertions below
var obj groundworkObject
err = json.Unmarshal(body, &obj)
require.NoError(t, err)

// Check if server gets valid metrics object
require.Equal(t, defaultTestAgentID, obj.Context.AgentID)
require.Equal(t, defaultHost, obj.Resources[0].Name)
require.Equal(t, "IntMetric", obj.Resources[0].Services[0].Name)
require.Equal(t, int64(42), obj.Resources[0].Services[0].Metrics[0].Value.IntegerValue)
require.Equal(t, 0, len(obj.Groups))

_, err = fmt.Fprintln(w, `OK`)
require.NoError(t, err)
}))

i := Groundwork{
Server: server.URL,
AgentID: defaultTestAgentID,
DefaultHost: defaultHost,
client: clients.GWClient{
AppName: "telegraf",
AppType: "TELEGRAF",
GWConnection: &clients.GWConnection{
HostName: server.URL,
},
},
}

err := i.Write([]telegraf.Metric{intMetric})
require.NoError(t, err)

defer server.Close()
}

func TestWriteWithTags(t *testing.T) {
// Generate test metric with tags to test Write logic
floatMetric := testutil.TestMetric(1.0, "FloatMetric")
floatMetric.AddTag("host", "Host01")
floatMetric.AddTag("group", "Group01")

Expand All @@ -39,7 +83,7 @@ func TestWrite(t *testing.T) {
// Check if server gets valid metrics object
require.Equal(t, defaultTestAgentID, obj.Context.AgentID)
require.Equal(t, "Host01", obj.Resources[0].Name)
require.Equal(t, "Float", obj.Resources[0].Services[0].Name)
require.Equal(t, "FloatMetric", obj.Resources[0].Services[0].Name)
require.Equal(t, 1.0, obj.Resources[0].Services[0].Metrics[0].Value.DoubleValue)
require.Equal(t, "Group01", obj.Groups[0].GroupName)
require.Equal(t, "Host01", obj.Groups[0].Resources[0].Name)
Expand All @@ -51,9 +95,9 @@ func TestWrite(t *testing.T) {
i := Groundwork{
Server: server.URL,
AgentID: defaultTestAgentID,
DefaultHost: defaultHost,
GroupTag: "group",
ResourceTag: "host",
DefaultHost: "telegraf",
client: clients.GWClient{
AppName: "telegraf",
AppType: "TELEGRAF",
Expand All @@ -79,8 +123,8 @@ type groundworkObject struct {
Name string `json:"name"`
Metrics []struct {
Value struct {
StringValue string `json:"stringValue"`
DoubleValue float64 `json:"doubleValue"`
DoubleValue float64 `json:"doubleValue"`
IntegerValue int64 `json:"integerValue"`
} `json:"value"`
}
} `json:"services"`
Expand Down