| 1 | // SPDX-License-Identifier: GPL-3.0-or-later |
| 2 | |
| 3 | package logstash |
| 4 | |
| 5 | import ( |
| 6 | "fmt" |
| 7 | |
| 8 | "github.com/netdata/netdata/go/plugins/pkg/stm" |
| 9 | "github.com/netdata/netdata/go/plugins/pkg/web" |
| 10 | ) |
| 11 | |
| 12 | const urlPathNodeStatsAPI = "/_node/stats" |
| 13 | |
| 14 | func (c *Collector) collect() (map[string]int64, error) { |
| 15 | stats, err := c.queryNodeStats() |
| 16 | if err != nil { |
| 17 | return nil, err |
| 18 | } |
| 19 | |
| 20 | c.updateCharts(stats.Pipelines) |
| 21 | |
| 22 | return stm.ToMap(stats), nil |
| 23 | } |
| 24 | |
| 25 | func (c *Collector) updateCharts(pipelines map[string]pipelineStats) { |
| 26 | seen := make(map[string]bool) |
| 27 | |
| 28 | for id := range pipelines { |
| 29 | seen[id] = true |
| 30 | if !c.pipelines[id] { |
| 31 | c.pipelines[id] = true |
| 32 | c.addPipelineCharts(id) |
| 33 | } |
| 34 | } |
| 35 | |
| 36 | for id := range c.pipelines { |
| 37 | if !seen[id] { |
| 38 | delete(c.pipelines, id) |
| 39 | c.removePipelineCharts(id) |
| 40 | } |
| 41 | } |
| 42 | } |
| 43 | |
| 44 | func (c *Collector) queryNodeStats() (*nodeStats, error) { |
| 45 | req, err := web.NewHTTPRequestWithPath(c.RequestConfig, urlPathNodeStatsAPI) |
| 46 | if err != nil { |
| 47 | return nil, fmt.Errorf("failed to create HTTP request: %w", err) |
| 48 | } |
| 49 | |
| 50 | var stats nodeStats |
| 51 | |
| 52 | if err := web.DoHTTP(c.httpClient).RequestJSON(req, &stats); err != nil { |
| 53 | return nil, err |
| 54 | } |
| 55 | |
| 56 | return &stats, nil |
| 57 | } |