feat: add default appType as config option to groundwork output (#11300)

This commit is contained in:
Vladislav Senkevich 2022-06-15 22:25:09 +03:00 committed by GitHub
parent 2424852aa7
commit 2d7f2094e9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 27 additions and 12 deletions

View File

@ -20,6 +20,9 @@ GW8+
username = "" username = ""
password = "" password = ""
## Default application type to use in GroundWork client
# default_app_type = "TELEGRAF"
## Default display name for the host with services(metrics). ## Default display name for the host with services(metrics).
# default_host = "telegraf" # default_host = "telegraf"

View File

@ -33,6 +33,7 @@ type Groundwork struct {
AgentID string `toml:"agent_id"` AgentID string `toml:"agent_id"`
Username string `toml:"username"` Username string `toml:"username"`
Password string `toml:"password"` Password string `toml:"password"`
DefaultAppType string `toml:"default_app_type"`
DefaultHost string `toml:"default_host"` DefaultHost string `toml:"default_host"`
DefaultServiceState string `toml:"default_service_state"` DefaultServiceState string `toml:"default_service_state"`
GroupTag string `toml:"group_tag"` GroupTag string `toml:"group_tag"`
@ -58,6 +59,9 @@ func (g *Groundwork) Init() error {
if g.Password == "" { if g.Password == "" {
return errors.New("no 'password' provided") return errors.New("no 'password' provided")
} }
if g.DefaultAppType == "" {
return errors.New("no 'default_app_type' provided")
}
if g.DefaultHost == "" { if g.DefaultHost == "" {
return errors.New("no 'default_host' provided") return errors.New("no 'default_host' provided")
} }
@ -70,7 +74,7 @@ func (g *Groundwork) Init() error {
g.client = clients.GWClient{ g.client = clients.GWClient{
AppName: "telegraf", AppName: "telegraf",
AppType: "TELEGRAF", AppType: g.DefaultAppType,
GWConnection: &clients.GWConnection{ GWConnection: &clients.GWConnection{
HostName: g.Server, HostName: g.Server,
UserName: g.Username, UserName: g.Username,
@ -172,7 +176,7 @@ func (g *Groundwork) Write(metrics []telegraf.Metric) error {
} }
requestJSON, err := json.Marshal(transit.ResourcesWithServicesRequest{ requestJSON, err := json.Marshal(transit.ResourcesWithServicesRequest{
Context: &transit.TracerContext{ Context: &transit.TracerContext{
AppType: "TELEGRAF", AppType: g.DefaultAppType,
AgentID: g.AgentID, AgentID: g.AgentID,
TraceToken: traceToken, TraceToken: traceToken,
TimeStamp: transit.NewTimestamp(), TimeStamp: transit.NewTimestamp(),
@ -200,6 +204,7 @@ func init() {
GroupTag: "group", GroupTag: "group",
ResourceTag: "host", ResourceTag: "host",
DefaultHost: "telegraf", DefaultHost: "telegraf",
DefaultAppType: "TELEGRAF",
DefaultServiceState: string(transit.ServiceOk), DefaultServiceState: string(transit.ServiceOk),
} }
}) })

View File

@ -18,6 +18,8 @@ import (
const ( const (
defaultTestAgentID = "ec1676cc-583d-48ee-b035-7fb5ed0fcf88" defaultTestAgentID = "ec1676cc-583d-48ee-b035-7fb5ed0fcf88"
defaultHost = "telegraf" defaultHost = "telegraf"
defaultAppType = "TELEGRAF"
customAppType = "SYSLOG"
) )
func TestWriteWithDefaults(t *testing.T) { func TestWriteWithDefaults(t *testing.T) {
@ -36,6 +38,7 @@ func TestWriteWithDefaults(t *testing.T) {
// Check if server gets valid metrics object // Check if server gets valid metrics object
require.Equal(t, defaultTestAgentID, obj.Context.AgentID) require.Equal(t, defaultTestAgentID, obj.Context.AgentID)
require.Equal(t, customAppType, obj.Context.AppType)
require.Equal(t, defaultHost, obj.Resources[0].Name) require.Equal(t, defaultHost, obj.Resources[0].Name)
require.Equal(t, "IntMetric", obj.Resources[0].Services[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, int64(42), obj.Resources[0].Services[0].Metrics[0].Value.IntegerValue)
@ -46,12 +49,13 @@ func TestWriteWithDefaults(t *testing.T) {
})) }))
i := Groundwork{ i := Groundwork{
Server: server.URL, Server: server.URL,
AgentID: defaultTestAgentID, AgentID: defaultTestAgentID,
DefaultHost: defaultHost, DefaultHost: defaultHost,
DefaultAppType: customAppType,
client: clients.GWClient{ client: clients.GWClient{
AppName: "telegraf", AppName: "telegraf",
AppType: "TELEGRAF", AppType: customAppType,
GWConnection: &clients.GWConnection{ GWConnection: &clients.GWConnection{
HostName: server.URL, HostName: server.URL,
}, },
@ -82,6 +86,7 @@ func TestWriteWithTags(t *testing.T) {
// Check if server gets valid metrics object // Check if server gets valid metrics object
require.Equal(t, defaultTestAgentID, obj.Context.AgentID) require.Equal(t, defaultTestAgentID, obj.Context.AgentID)
require.Equal(t, defaultAppType, obj.Context.AppType)
require.Equal(t, "Host01", obj.Resources[0].Name) require.Equal(t, "Host01", obj.Resources[0].Name)
require.Equal(t, "FloatMetric", 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, 1.0, obj.Resources[0].Services[0].Metrics[0].Value.DoubleValue)
@ -93,14 +98,15 @@ func TestWriteWithTags(t *testing.T) {
})) }))
i := Groundwork{ i := Groundwork{
Server: server.URL, Server: server.URL,
AgentID: defaultTestAgentID, AgentID: defaultTestAgentID,
DefaultHost: defaultHost, DefaultHost: defaultHost,
GroupTag: "group", DefaultAppType: defaultAppType,
ResourceTag: "host", GroupTag: "group",
ResourceTag: "host",
client: clients.GWClient{ client: clients.GWClient{
AppName: "telegraf", AppName: "telegraf",
AppType: "TELEGRAF", AppType: defaultAppType,
GWConnection: &clients.GWConnection{ GWConnection: &clients.GWConnection{
HostName: server.URL, HostName: server.URL,
}, },
@ -116,6 +122,7 @@ func TestWriteWithTags(t *testing.T) {
type groundworkObject struct { type groundworkObject struct {
Context struct { Context struct {
AgentID string `json:"agentId"` AgentID string `json:"agentId"`
AppType string `json:"appType"`
} `json:"context"` } `json:"context"`
Resources []struct { Resources []struct {
Name string `json:"name"` Name string `json:"name"`