Skip to content

Commit

Permalink
chore: Fix linter findings for revive:exported in `plugins/inputs/[…
Browse files Browse the repository at this point in the history
…a-b]*`
  • Loading branch information
zak-pawel committed Sep 19, 2024
1 parent 24b7b91 commit f7d0dc7
Show file tree
Hide file tree
Showing 22 changed files with 494 additions and 489 deletions.
123 changes: 62 additions & 61 deletions plugins/inputs/activemq/activemq.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
//go:embed sample.conf
var sampleConfig string

// ActiveMQ Input Plugin
type ActiveMQ struct {
Server string `toml:"server" deprecated:"1.11.0;use 'url' instead"`
Port int `toml:"port" deprecated:"1.11.0;use 'url' instead"`
Expand Down Expand Up @@ -87,22 +88,6 @@ type stats struct {
DequeueCounter int `xml:"dequeueCounter,attr"`
}

func (a *ActiveMQ) createHTTPClient() (*http.Client, error) {
tlsCfg, err := a.ClientConfig.TLSConfig()
if err != nil {
return nil, err
}

client := &http.Client{
Transport: &http.Transport{
TLSClientConfig: tlsCfg,
},
Timeout: time.Duration(a.ResponseTimeout),
}

return client, nil
}

func (*ActiveMQ) SampleConfig() string {
return sampleConfig
}
Expand Down Expand Up @@ -138,7 +123,61 @@ func (a *ActiveMQ) Init() error {
return nil
}

func (a *ActiveMQ) GetMetrics(u string) ([]byte, error) {
func (a *ActiveMQ) Gather(acc telegraf.Accumulator) error {
dataQueues, err := a.getMetrics(a.queuesURL())
if err != nil {
return err
}
queues := queues{}
err = xml.Unmarshal(dataQueues, &queues)
if err != nil {
return fmt.Errorf("queues XML unmarshal error: %w", err)
}

dataTopics, err := a.getMetrics(a.topicsURL())
if err != nil {
return err
}
topics := topics{}
err = xml.Unmarshal(dataTopics, &topics)
if err != nil {
return fmt.Errorf("topics XML unmarshal error: %w", err)
}

dataSubscribers, err := a.getMetrics(a.subscribersURL())
if err != nil {
return err
}
subscribers := subscribers{}
err = xml.Unmarshal(dataSubscribers, &subscribers)
if err != nil {
return fmt.Errorf("subscribers XML unmarshal error: %w", err)
}

a.gatherQueuesMetrics(acc, queues)
a.gatherTopicsMetrics(acc, topics)
a.gatherSubscribersMetrics(acc, subscribers)

return nil
}

func (a *ActiveMQ) createHTTPClient() (*http.Client, error) {
tlsCfg, err := a.ClientConfig.TLSConfig()
if err != nil {
return nil, err
}

client := &http.Client{
Transport: &http.Transport{
TLSClientConfig: tlsCfg,
},
Timeout: time.Duration(a.ResponseTimeout),
}

return client, nil
}

func (a *ActiveMQ) getMetrics(u string) ([]byte, error) {
req, err := http.NewRequest("GET", u, nil)
if err != nil {
return nil, err
Expand All @@ -161,7 +200,7 @@ func (a *ActiveMQ) GetMetrics(u string) ([]byte, error) {
return io.ReadAll(resp.Body)
}

func (a *ActiveMQ) GatherQueuesMetrics(acc telegraf.Accumulator, queues queues) {
func (a *ActiveMQ) gatherQueuesMetrics(acc telegraf.Accumulator, queues queues) {
for _, queue := range queues.QueueItems {
records := make(map[string]interface{})
tags := make(map[string]string)
Expand All @@ -179,7 +218,7 @@ func (a *ActiveMQ) GatherQueuesMetrics(acc telegraf.Accumulator, queues queues)
}
}

func (a *ActiveMQ) GatherTopicsMetrics(acc telegraf.Accumulator, topics topics) {
func (a *ActiveMQ) gatherTopicsMetrics(acc telegraf.Accumulator, topics topics) {
for _, topic := range topics.TopicItems {
records := make(map[string]interface{})
tags := make(map[string]string)
Expand All @@ -197,7 +236,7 @@ func (a *ActiveMQ) GatherTopicsMetrics(acc telegraf.Accumulator, topics topics)
}
}

func (a *ActiveMQ) GatherSubscribersMetrics(acc telegraf.Accumulator, subscribers subscribers) {
func (a *ActiveMQ) gatherSubscribersMetrics(acc telegraf.Accumulator, subscribers subscribers) {
for _, subscriber := range subscribers.SubscriberItems {
records := make(map[string]interface{})
tags := make(map[string]string)
Expand All @@ -221,55 +260,17 @@ func (a *ActiveMQ) GatherSubscribersMetrics(acc telegraf.Accumulator, subscriber
}
}

func (a *ActiveMQ) Gather(acc telegraf.Accumulator) error {
dataQueues, err := a.GetMetrics(a.QueuesURL())
if err != nil {
return err
}
queues := queues{}
err = xml.Unmarshal(dataQueues, &queues)
if err != nil {
return fmt.Errorf("queues XML unmarshal error: %w", err)
}

dataTopics, err := a.GetMetrics(a.TopicsURL())
if err != nil {
return err
}
topics := topics{}
err = xml.Unmarshal(dataTopics, &topics)
if err != nil {
return fmt.Errorf("topics XML unmarshal error: %w", err)
}

dataSubscribers, err := a.GetMetrics(a.SubscribersURL())
if err != nil {
return err
}
subscribers := subscribers{}
err = xml.Unmarshal(dataSubscribers, &subscribers)
if err != nil {
return fmt.Errorf("subscribers XML unmarshal error: %w", err)
}

a.GatherQueuesMetrics(acc, queues)
a.GatherTopicsMetrics(acc, topics)
a.GatherSubscribersMetrics(acc, subscribers)

return nil
}

func (a *ActiveMQ) QueuesURL() string {
func (a *ActiveMQ) queuesURL() string {
ref := url.URL{Path: path.Join("/", a.Webadmin, "/xml/queues.jsp")}
return a.baseURL.ResolveReference(&ref).String()
}

func (a *ActiveMQ) TopicsURL() string {
func (a *ActiveMQ) topicsURL() string {
ref := url.URL{Path: path.Join("/", a.Webadmin, "/xml/topics.jsp")}
return a.baseURL.ResolveReference(&ref).String()
}

func (a *ActiveMQ) SubscribersURL() string {
func (a *ActiveMQ) subscribersURL() string {
ref := url.URL{Path: path.Join("/", a.Webadmin, "/xml/subscribers.jsp")}
return a.baseURL.ResolveReference(&ref).String()
}
Expand Down
6 changes: 3 additions & 3 deletions plugins/inputs/activemq/activemq_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ func TestGatherQueuesMetrics(t *testing.T) {
require.NoError(t, plugin.Init())

var acc testutil.Accumulator
plugin.GatherQueuesMetrics(&acc, queues)
plugin.gatherQueuesMetrics(&acc, queues)
acc.AssertContainsTaggedFields(t, "activemq_queues", records, tags)
}

Expand Down Expand Up @@ -98,7 +98,7 @@ func TestGatherTopicsMetrics(t *testing.T) {
require.NoError(t, plugin.Init())

var acc testutil.Accumulator
plugin.GatherTopicsMetrics(&acc, topics)
plugin.gatherTopicsMetrics(&acc, topics)
acc.AssertContainsTaggedFields(t, "activemq_topics", records, tags)
}

Expand Down Expand Up @@ -137,7 +137,7 @@ func TestGatherSubscribersMetrics(t *testing.T) {
require.NoError(t, plugin.Init())

var acc testutil.Accumulator
plugin.GatherSubscribersMetrics(&acc, subscribers)
plugin.gatherSubscribersMetrics(&acc, subscribers)
acc.AssertContainsTaggedFields(t, "activemq_subscribers", records, tags)
}

Expand Down
1 change: 1 addition & 0 deletions plugins/inputs/aerospike/aerospike.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
//go:embed sample.conf
var sampleConfig string

// Aerospike Input Plugin
type Aerospike struct {
Servers []string `toml:"servers"`

Expand Down
31 changes: 12 additions & 19 deletions plugins/inputs/aliyuncms/aliyuncms.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ type (
Period config.Duration `toml:"period"`
Delay config.Duration `toml:"delay"`
Project string `toml:"project"`
Metrics []*Metric `toml:"metrics"`
Metrics []*metric `toml:"metrics"`
RateLimit int `toml:"ratelimit"`

Log telegraf.Logger `toml:"-"`
Expand All @@ -57,8 +57,8 @@ type (
measurement string
}

// Metric describes what metrics to get
Metric struct {
// metric describes what metrics to get
metric struct {
ObjectsFilter string `toml:"objects_filter"`
MetricNames []string `toml:"names"`
Dimensions string `toml:"dimensions"` // String representation of JSON dimensions
Expand All @@ -74,11 +74,6 @@ type (

}

// Dimension describe how to get metrics
Dimension struct {
Value string `toml:"value"`
}

aliyuncmsClient interface {
DescribeMetricList(request *cms.DescribeMetricListRequest) (response *cms.DescribeMetricListResponse, err error)
}
Expand Down Expand Up @@ -113,7 +108,6 @@ func (*AliyunCMS) SampleConfig() string {
return sampleConfig
}

// Init perform checks of plugin inputs and initialize internals
func (s *AliyunCMS) Init() error {
if s.Project == "" {
return errors.New("project is not set")
Expand Down Expand Up @@ -216,7 +210,6 @@ func (s *AliyunCMS) Start(telegraf.Accumulator) error {
return nil
}

// Gather implements telegraf.Inputs interface
func (s *AliyunCMS) Gather(acc telegraf.Accumulator) error {
s.updateWindow(time.Now())

Expand All @@ -225,16 +218,16 @@ func (s *AliyunCMS) Gather(acc telegraf.Accumulator) error {
defer lmtr.Stop()

var wg sync.WaitGroup
for _, metric := range s.Metrics {
for _, m := range s.Metrics {
// Prepare internal structure with data from discovery
s.prepareTagsAndDimensions(metric)
wg.Add(len(metric.MetricNames))
for _, metricName := range metric.MetricNames {
s.prepareTagsAndDimensions(m)
wg.Add(len(m.MetricNames))
for _, metricName := range m.MetricNames {
<-lmtr.C
go func(metricName string, metric *Metric) {
go func(metricName string, m *metric) {
defer wg.Done()
acc.AddError(s.gatherMetric(acc, metricName, metric))
}(metricName, metric)
acc.AddError(s.gatherMetric(acc, metricName, m))
}(metricName, m)
}
wg.Wait()
}
Expand Down Expand Up @@ -269,7 +262,7 @@ func (s *AliyunCMS) updateWindow(relativeTo time.Time) {
}

// Gather given metric and emit error
func (s *AliyunCMS) gatherMetric(acc telegraf.Accumulator, metricName string, metric *Metric) error {
func (s *AliyunCMS) gatherMetric(acc telegraf.Accumulator, metricName string, metric *metric) error {
for _, region := range s.Regions {
req := cms.CreateDescribeMetricListRequest()
req.Period = strconv.FormatInt(int64(time.Duration(s.Period).Seconds()), 10)
Expand Down Expand Up @@ -372,7 +365,7 @@ func parseTag(tagSpec string, data interface{}) (tagKey, tagValue string, err er
return tagKey, tagValue, nil
}

func (s *AliyunCMS) prepareTagsAndDimensions(metric *Metric) {
func (s *AliyunCMS) prepareTagsAndDimensions(metric *metric) {
var (
newData bool
defaultTags = []string{"RegionId:RegionId"}
Expand Down
14 changes: 7 additions & 7 deletions plugins/inputs/aliyuncms/aliyuncms_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -240,15 +240,15 @@ func TestPluginMetricsInitialize(t *testing.T) {
expectedErrorString string
regions []string
discoveryRegions []string
metrics []*Metric
metrics []*metric
}{
{
name: "Valid project",
project: "acs_slb_dashboard",
regions: []string{"cn-shanghai"},
accessKeyID: "dummy",
accessKeySecret: "dummy",
metrics: []*Metric{
metrics: []*metric{
{
MetricNames: []string{},
Dimensions: `{"instanceId": "i-abcdefgh123456"}`,
Expand All @@ -261,7 +261,7 @@ func TestPluginMetricsInitialize(t *testing.T) {
regions: []string{"cn-shanghai"},
accessKeyID: "dummy",
accessKeySecret: "dummy",
metrics: []*Metric{
metrics: []*metric{
{
MetricNames: []string{},
Dimensions: `[{"instanceId": "p-example"},{"instanceId": "q-example"}]`,
Expand All @@ -275,7 +275,7 @@ func TestPluginMetricsInitialize(t *testing.T) {
accessKeyID: "dummy",
accessKeySecret: "dummy",
expectedErrorString: `cannot parse dimensions (neither obj, nor array) "[": unexpected end of JSON input`,
metrics: []*Metric{
metrics: []*metric{
{
MetricNames: []string{},
Dimensions: `[`,
Expand Down Expand Up @@ -343,7 +343,7 @@ func TestGatherMetric(t *testing.T) {
Regions: []string{"cn-shanghai"},
}

metric := &Metric{
metric := &metric{
MetricNames: []string{},
Dimensions: `"instanceId": "i-abcdefgh123456"`,
}
Expand Down Expand Up @@ -374,15 +374,15 @@ func TestGatherMetric(t *testing.T) {
}

func TestGather(t *testing.T) {
metric := &Metric{
m := &metric{
MetricNames: []string{},
Dimensions: `{"instanceId": "i-abcdefgh123456"}`,
}
plugin := &AliyunCMS{
AccessKeyID: "my_access_key_id",
AccessKeySecret: "my_access_key_secret",
Project: "acs_slb_dashboard",
Metrics: []*Metric{metric},
Metrics: []*metric{m},
RateLimit: 200,
measurement: formatMeasurement("acs_slb_dashboard"),
Regions: []string{"cn-shanghai"},
Expand Down
6 changes: 2 additions & 4 deletions plugins/inputs/aliyuncms/discovery.go
Original file line number Diff line number Diff line change
Expand Up @@ -395,8 +395,7 @@ func (dt *discoveryTool) getDiscoveryDataAcrossRegions(lmtr chan bool) (map[stri
return resultData, nil
}

// start the discovery pooling
// In case smth. new found it will be reported back through `DataChan`
// start the discovery pooling; in case something new is found, it will be reported back through `dataChan`
func (dt *discoveryTool) start() {
var (
err error
Expand Down Expand Up @@ -443,8 +442,7 @@ func (dt *discoveryTool) start() {
}()
}

// stop the discovery loop, making sure
// all data is read from 'dataChan'
// stop the discovery loop, making sure all data is read from 'dataChan'
func (dt *discoveryTool) stop() {
close(dt.done)

Expand Down
Loading

0 comments on commit f7d0dc7

Please sign in to comment.