Skip to content

Commit

Permalink
Add interface to logstash client
Browse files Browse the repository at this point in the history
  • Loading branch information
kuskoman committed Feb 12, 2023
1 parent 621a526 commit 4cfa06a
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 8 deletions.
2 changes: 1 addition & 1 deletion collectors/collector_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func NewCollectorManager(endpoint string) *CollectorManager {
return &CollectorManager{collectors: collectors, scrapeDurations: scrapeDurations}
}

func getCollectors(client *logstashclient.Client) map[string]Collector {
func getCollectors(client logstashclient.Client) map[string]Collector {
collectors := make(map[string]Collector)
collectors["nodeinfo"] = nodestats.NewNodestatsCollector(client)
return collectors
Expand Down
4 changes: 2 additions & 2 deletions collectors/nodestats/nodeinfo_collector.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
)

type NodestatsCollector struct {
client *logstashclient.Client
client logstashclient.Client

NodeInfos *prometheus.Desc
BuildInfos *prometheus.Desc
Expand All @@ -24,7 +24,7 @@ type NodestatsCollector struct {
Status *prometheus.Desc
}

func NewNodestatsCollector(client *logstashclient.Client) *NodestatsCollector {
func NewNodestatsCollector(client logstashclient.Client) *NodestatsCollector {
const subsystem = "info"
namespace := config.PrometheusNamespace
descHelper := helpers.SimpleDescHelper{Namespace: namespace, Subsystem: subsystem}
Expand Down
25 changes: 25 additions & 0 deletions collectors/nodestats/nodeinfo_collector_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package nodestats

import (
"encoding/json"
"io/ioutil"

"github.com/kuskoman/logstash-exporter/fetcher/responses"
)

type mockClient struct{}

func (m *mockClient) GetNodeInfo() (*responses.NodeInfoResponse, error) {
b, err := ioutil.ReadFile("node_info.json")
if err != nil {
return nil, err
}

var nodeInfo responses.NodeInfoResponse
err = json.Unmarshal(b, &nodeInfo)
if err != nil {
return nil, err
}

return &nodeInfo, nil
}
15 changes: 11 additions & 4 deletions fetcher/logstash_client/client.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,21 @@
package logstashclient

import "github.com/kuskoman/logstash-exporter/httphandler"
import (
"github.com/kuskoman/logstash-exporter/fetcher/responses"
"github.com/kuskoman/logstash-exporter/httphandler"
)

type Client struct {
type DefaultClient struct {
httpClient httphandler.HTTPHandler
}

type Client interface {
GetNodeInfo() (*responses.NodeInfoResponse, error)
}

const defaultLogstashEndpoint = "http://localhost:9600"

func NewClient(httpClient httphandler.HTTPHandler) *Client {
func NewClient(httpClient httphandler.HTTPHandler) *DefaultClient {
var clientHandler httphandler.HTTPHandler

if httpClient == nil {
Expand All @@ -18,5 +25,5 @@ func NewClient(httpClient httphandler.HTTPHandler) *Client {
clientHandler = httpClient
}

return &Client{httpClient: clientHandler}
return &DefaultClient{httpClient: clientHandler}
}
2 changes: 1 addition & 1 deletion fetcher/logstash_client/queries.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"github.com/kuskoman/logstash-exporter/httphandler"
)

func (c *Client) GetNodeInfo() (*responses.NodeInfoResponse, error) {
func (c *DefaultClient) GetNodeInfo() (*responses.NodeInfoResponse, error) {
var nodeInfoResponse responses.NodeInfoResponse
err := httphandler.GetMetrics(c.httpClient, &nodeInfoResponse)
if err != nil {
Expand Down

0 comments on commit 4cfa06a

Please sign in to comment.