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(http): add functionality to retrieve/store telegraf config as toml #16132

Merged
merged 39 commits into from
Dec 20, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
39 commits
Select commit Hold shift + click to select a range
ac35751
Start refactoring telegraf API to support toml editing of all the plu…
glinton Nov 27, 2019
3517c8f
Add currently available plugins
glinton Nov 27, 2019
c2e6a87
Handle complete telegraf config files
glinton Dec 3, 2019
9e53077
Expose list of telegraf plugins
glinton Dec 3, 2019
4d3c71e
Update swagger and add os to plugin spec
glinton Dec 4, 2019
e2d6b02
Update plugin sample configs
glinton Dec 4, 2019
c7bc861
Fix http telegraf tests
glinton Dec 4, 2019
cab760d
Fix telegraf tests
glinton Dec 4, 2019
1a0e7ce
Cleanup kv testing lib
glinton Dec 4, 2019
03ebe0d
Remove entry from go.mod
glinton Dec 4, 2019
3862491
Fix staticcheck
glinton Dec 5, 2019
6126bbb
Centralize available plugin logic
glinton Dec 5, 2019
974355c
Address review feedback
glinton Dec 5, 2019
ad34a6f
Merge branch 'master' into feat/5399
glinton Dec 6, 2019
9cdef65
Skip cypress tests of legacy relations
glinton Dec 6, 2019
b0e435e
Fix tests affected from merging master
glinton Dec 7, 2019
c07c956
Re-tie buckets to telegraf configs
glinton Dec 10, 2019
908bff2
Re-add buckets to telegraf list
glinton Dec 11, 2019
88987f6
Merge branch 'master' into feat/5399
glinton Dec 11, 2019
8a3fe19
Fix tests post merge
glinton Dec 13, 2019
998887b
Update test
glinton Dec 13, 2019
00adcf1
Start trying to fix js types
glinton Dec 13, 2019
86828c2
Try fixing 'label' type issues
glinton Dec 16, 2019
f2db62f
Revert "Start trying to fix js types"
glinton Dec 17, 2019
3854621
Fix type issuesecho 🚀 🚀
glinton Dec 17, 2019
1a30123
make fmt
glinton Dec 17, 2019
4745834
Merge branch 'master' into feat/5399
glinton Dec 17, 2019
00115f5
Merge branch 'master' into feat/5399
glinton Dec 17, 2019
b6ebfc9
Cleanup post merge
glinton Dec 18, 2019
5889c93
feat: moving the filtering down a level
Dec 18, 2019
fca777d
fix: forgot some props
Dec 18, 2019
c92e8f2
Merge branch 'master' into feat/5399
glinton Dec 18, 2019
7767e4e
fix: that test did nothing but fail
Dec 18, 2019
1b543b0
Address feedback
glinton Dec 19, 2019
f6b5ca6
Tidy the thing
glinton Dec 19, 2019
793fd64
Address some more feedbacks
glinton Dec 19, 2019
422b315
Export only filteredCollectorList
glinton Dec 20, 2019
58c99d8
Merge branch 'master' into feat/5399
glinton Dec 20, 2019
49ebf48
Manually add mod things to match circle checktidy
glinton Dec 20, 2019
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
5 changes: 3 additions & 2 deletions bolt/lookup_service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -187,8 +187,9 @@ func TestClient_Name(t *testing.T) {
},
init: func(ctx context.Context, s *bolt.Client) error {
return s.CreateTelegrafConfig(ctx, &platform.TelegrafConfig{
OrgID: platformtesting.MustIDBase16("0000000000000009"),
Name: "telegraf1",
OrgID: platformtesting.MustIDBase16("0000000000000009"),
Name: "telegraf1",
Config: "[agent]",
}, testID)
},
},
Expand Down
40 changes: 21 additions & 19 deletions cmd/influxd/launcher/pkger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,7 @@ func TestLauncher_Pkger(t *testing.T) {
assert.Equal(t, l.Org.ID, teles[0].TelegrafConfig.OrgID)
assert.Equal(t, "first_tele_config", teles[0].TelegrafConfig.Name)
assert.Equal(t, "desc", teles[0].TelegrafConfig.Description)
assert.Len(t, teles[0].TelegrafConfig.Plugins, 2)
assert.Equal(t, telConf, teles[0].TelegrafConfig.Config)

vars := sum1.Variables
require.Len(t, vars, 1)
Expand Down Expand Up @@ -525,7 +525,22 @@ func newPkg(t *testing.T) *pkger.Pkg {
return pkg
}

const pkgYMLStr = `apiVersion: 0.1.0
const telConf = `[agent]
interval = "10s"
glinton marked this conversation as resolved.
Show resolved Hide resolved
metric_batch_size = 1000
metric_buffer_limit = 10000
collection_jitter = "0s"
flush_interval = "10s"
[[outputs.influxdb_v2]]
urls = ["http://localhost:9999"]
token = "$INFLUX_TOKEN"
organization = "rg"
bucket = "rucket_3"
[[inputs.cpu]]
percpu = true
`

var pkgYMLStr = fmt.Sprintf(`apiVersion: 0.1.0
kind: Package
meta:
pkgName: pkg_name
Expand Down Expand Up @@ -576,20 +591,7 @@ spec:
associations:
- kind: Label
name: label_1
config: |
[agent]
interval = "10s"
metric_batch_size = 1000
metric_buffer_limit = 10000
collection_jitter = "0s"
flush_interval = "10s"
[[outputs.influxdb_v2]]
urls = ["http://localhost:9999"]
token = "$INFLUX_TOKEN"
organization = "rg"
bucket = "rucket_3"
[[inputs.cpu]]
percpu = true
config: %+q
- kind: Notification_Endpoint_HTTP
name: http_none_auth_notification_endpoint
type: none
Expand All @@ -598,8 +600,8 @@ spec:
url: https://www.example.com/endpoint/noneauth
status: inactive
associations:
- kind: Label
name: label_1
- kind: Label
name: label_1
- kind: Check_Threshold
name: check_0
every: 1m
Expand Down Expand Up @@ -664,7 +666,7 @@ spec:
associations:
- kind: Label
name: label_1
`
`, telConf)

const updatePkgYMLStr = `apiVersion: 0.1.0
kind: Package
Expand Down
14 changes: 7 additions & 7 deletions http/swagger.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9307,15 +9307,15 @@ components:
type: string
description:
type: string
agent:
metadata:
type: object
properties:
collectionInterval:
type: integer
plugins:
type: array
items:
$ref: "#/components/schemas/TelegrafRequestPlugin"
buckets:
type: array
items:
type: string
config:
type: string
orgID:
type: string
TelegrafRequestPlugin:
Expand Down
67 changes: 8 additions & 59 deletions http/telegraf.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,55 +135,6 @@ type telegrafLinks struct {
Owners string `json:"owners"`
}

// MarshalJSON implement the json.Marshaler interface.
// TODO: remove this hack and make labels and links return.
// see: https://github.com/influxdata/influxdb/issues/12457
func (r *telegrafResponse) MarshalJSON() ([]byte, error) {
// telegrafPluginEncode is the helper struct for json encoding.
type telegrafPluginEncode struct {
// Name of the telegraf plugin, exp "docker"
Name string `json:"name"`
Type plugins.Type `json:"type"`
Comment string `json:"comment"`
Config plugins.Config `json:"config"`
}

// telegrafConfigEncode is the helper struct for json encoding.
type telegrafConfigEncode struct {
ID platform.ID `json:"id"`
OrgID platform.ID `json:"orgID,omitempty"`
Name string `json:"name"`
Description string `json:"description"`
Agent platform.TelegrafAgentConfig `json:"agent"`
Plugins []telegrafPluginEncode `json:"plugins"`
Labels []platform.Label `json:"labels"`
Links telegrafLinks `json:"links"`
}

tce := new(telegrafConfigEncode)
*tce = telegrafConfigEncode{
ID: r.ID,
OrgID: r.OrgID,
Name: r.Name,
Description: r.Description,
Agent: r.Agent,
Plugins: make([]telegrafPluginEncode, len(r.Plugins)),
Labels: r.Labels,
glinton marked this conversation as resolved.
Show resolved Hide resolved
Links: r.Links,
}

for k, p := range r.Plugins {
tce.Plugins[k] = telegrafPluginEncode{
Name: p.Config.PluginName(),
Type: p.Config.Type(),
Comment: p.Comment,
Config: p.Config,
}
}

return json.Marshal(tce)
}

type telegrafResponse struct {
*platform.TelegrafConfig
Labels []platform.Label `json:"labels"`
Expand Down Expand Up @@ -306,7 +257,7 @@ func (h *TelegrafHandler) handleGetTelegraf(w http.ResponseWriter, r *http.Reque
w.Header().Set("Content-Type", "application/octet-stream")
w.Header().Set("Content-Disposition", fmt.Sprintf("attachment; filename=\"%s.toml\"", strings.Replace(strings.TrimSpace(tc.Name), " ", "_", -1)))
w.WriteHeader(http.StatusOK)
w.Write([]byte(tc.TOML()))
w.Write([]byte(tc.Config))
case "application/json":
labels, err := h.LabelService.FindResourceLabels(ctx, platform.LabelMappingFilter{ResourceID: tc.ID})
if err != nil {
Expand All @@ -321,7 +272,7 @@ func (h *TelegrafHandler) handleGetTelegraf(w http.ResponseWriter, r *http.Reque
case "application/toml":
w.Header().Set("Content-Type", "application/toml; charset=utf-8")
w.WriteHeader(http.StatusOK)
w.Write([]byte(tc.TOML()))
w.Write([]byte(tc.Config))
}
}

Expand Down Expand Up @@ -349,12 +300,6 @@ func decodeTelegrafConfigFilter(ctx context.Context, r *http.Request) (*platform
return f, err
}

func decodePostTelegrafRequest(r *http.Request) (*platform.TelegrafConfig, error) {
var tc platform.TelegrafConfig
err := json.NewDecoder(r.Body).Decode(&tc)
return &tc, err
}

func decodePutTelegrafRequest(ctx context.Context, r *http.Request) (*platform.TelegrafConfig, error) {
tc := new(platform.TelegrafConfig)
if err := json.NewDecoder(r.Body).Decode(tc); err != nil {
Expand All @@ -379,12 +324,14 @@ func decodePutTelegrafRequest(ctx context.Context, r *http.Request) (*platform.T
// handlePostTelegraf is the HTTP handler for the POST /api/v2/telegrafs route.
func (h *TelegrafHandler) handlePostTelegraf(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
tc, err := decodePostTelegrafRequest(r)
if err != nil {

tc := new(platform.TelegrafConfig)
if err := json.NewDecoder(r.Body).Decode(tc); err != nil {
h.log.Debug("Failed to decode request", zap.Error(err))
h.HandleHTTPError(ctx, err, w)
return
}

auth, err := pctx.GetAuthorizer(ctx)
if err != nil {
h.HandleHTTPError(ctx, err, w)
Expand All @@ -406,12 +353,14 @@ func (h *TelegrafHandler) handlePostTelegraf(w http.ResponseWriter, r *http.Requ
// handlePutTelegraf is the HTTP handler for the POST /api/v2/telegrafs route.
func (h *TelegrafHandler) handlePutTelegraf(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()

tc, err := decodePutTelegrafRequest(ctx, r)
if err != nil {
h.log.Debug("Failed to decode request", zap.Error(err))
h.HandleHTTPError(ctx, err, w)
return
}

auth, err := pctx.GetAuthorizer(ctx)
if err != nil {
h.HandleHTTPError(ctx, err, w)
Expand Down
Loading