github.com/netdata/go.d.plugin@v0.58.1/modules/fluentd/apiclient.go (about)

     1  // SPDX-License-Identifier: GPL-3.0-or-later
     2  
     3  package fluentd
     4  
     5  import (
     6  	"encoding/json"
     7  	"fmt"
     8  	"io"
     9  	"net/http"
    10  	"net/url"
    11  	"path"
    12  
    13  	"github.com/netdata/go.d.plugin/pkg/web"
    14  )
    15  
    16  const pluginsPath = "/api/plugins.json"
    17  
    18  type pluginsInfo struct {
    19  	Payload []pluginData `json:"plugins"`
    20  }
    21  
    22  type pluginData struct {
    23  	ID                    string `json:"plugin_id"`
    24  	Type                  string `json:"type"`
    25  	Category              string `json:"plugin_category"`
    26  	RetryCount            *int64 `json:"retry_count"`
    27  	BufferTotalQueuedSize *int64 `json:"buffer_total_queued_size"`
    28  	BufferQueueLength     *int64 `json:"buffer_queue_length"`
    29  }
    30  
    31  func (p pluginData) hasCategory() bool {
    32  	return p.RetryCount != nil
    33  }
    34  
    35  func (p pluginData) hasBufferQueueLength() bool {
    36  	return p.BufferQueueLength != nil
    37  }
    38  
    39  func (p pluginData) hasBufferTotalQueuedSize() bool {
    40  	return p.BufferTotalQueuedSize != nil
    41  }
    42  
    43  func newAPIClient(client *http.Client, request web.Request) *apiClient {
    44  	return &apiClient{httpClient: client, request: request}
    45  }
    46  
    47  type apiClient struct {
    48  	httpClient *http.Client
    49  	request    web.Request
    50  }
    51  
    52  func (a apiClient) getPluginsInfo() (*pluginsInfo, error) {
    53  	req, err := a.createRequest(pluginsPath)
    54  	if err != nil {
    55  		return nil, fmt.Errorf("error on creating request : %v", err)
    56  	}
    57  
    58  	resp, err := a.doRequestOK(req)
    59  	defer closeBody(resp)
    60  	if err != nil {
    61  		return nil, err
    62  	}
    63  
    64  	var info pluginsInfo
    65  	if err = json.NewDecoder(resp.Body).Decode(&info); err != nil {
    66  		return nil, fmt.Errorf("error on decoding response from %s : %v", req.URL, err)
    67  	}
    68  
    69  	return &info, nil
    70  }
    71  
    72  func (a apiClient) doRequestOK(req *http.Request) (*http.Response, error) {
    73  	resp, err := a.httpClient.Do(req)
    74  	if err != nil {
    75  		return nil, fmt.Errorf("error on request: %v", err)
    76  	}
    77  
    78  	if resp.StatusCode != http.StatusOK {
    79  		return resp, fmt.Errorf("%s returned HTTP status %d", req.URL, resp.StatusCode)
    80  	}
    81  	return resp, nil
    82  }
    83  
    84  func (a apiClient) createRequest(urlPath string) (*http.Request, error) {
    85  	req := a.request.Copy()
    86  	u, err := url.Parse(req.URL)
    87  	if err != nil {
    88  		return nil, err
    89  	}
    90  
    91  	u.Path = path.Join(u.Path, urlPath)
    92  	req.URL = u.String()
    93  	return web.NewHTTPRequest(req)
    94  }
    95  
    96  func closeBody(resp *http.Response) {
    97  	if resp != nil && resp.Body != nil {
    98  		_, _ = io.Copy(io.Discard, resp.Body)
    99  		_ = resp.Body.Close()
   100  	}
   101  }