-
Notifications
You must be signed in to change notification settings - Fork 2.5k
/
Copy pathclient.go
105 lines (87 loc) · 2.53 KB
/
client.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0
package couchdbreceiver // import "github.com/open-telemetry/opentelemetry-collector-contrib/receiver/couchdbreceiver"
import (
"context"
"encoding/json"
"fmt"
"io"
"net/http"
"strconv"
"go.opentelemetry.io/collector/component"
"go.uber.org/zap"
)
// client defines the basic HTTP client interface.
type client interface {
Get(path string) ([]byte, error)
GetStats(nodeName string) (map[string]any, error)
}
var _ client = (*couchDBClient)(nil)
type couchDBClient struct {
client *http.Client
cfg *Config
logger *zap.Logger
}
// newCouchDBClient creates a new client to make requests for the CouchDB receiver.
func newCouchDBClient(ctx context.Context, cfg *Config, host component.Host, settings component.TelemetrySettings) (client, error) {
client, err := cfg.ToClient(ctx, host, settings)
if err != nil {
return nil, fmt.Errorf("failed to create HTTP Client: %w", err)
}
return &couchDBClient{
client: client,
cfg: cfg,
logger: settings.Logger,
}, nil
}
// Get issues an authorized Get requests to the specified url.
func (c *couchDBClient) Get(path string) ([]byte, error) {
req, err := c.buildReq(path)
if err != nil {
return nil, err
}
resp, err := c.client.Do(req)
if err != nil {
return nil, err
}
defer func() {
if err = resp.Body.Close(); err != nil {
c.logger.Warn("failed to close response body", zap.Error(err))
}
}()
if resp.StatusCode != http.StatusOK {
if resp.StatusCode >= 400 {
c.logger.Error("couchdb", zap.Error(err), zap.String("status_code", strconv.Itoa(resp.StatusCode)))
}
return nil, fmt.Errorf("request GET %s failed - %q", req.URL.String(), resp.Status)
}
body, err := io.ReadAll(resp.Body)
if err != nil {
return nil, fmt.Errorf("failed to read response body %w", err)
}
return body, nil
}
// GetStats gets couchdb stats at a specific node name endpoint.
func (c *couchDBClient) GetStats(nodeName string) (map[string]any, error) {
path := fmt.Sprintf("/_node/%s/_stats/couchdb", nodeName)
body, err := c.Get(path)
if err != nil {
return nil, err
}
var stats map[string]any
err = json.Unmarshal(body, &stats)
if err != nil {
return nil, err
}
return stats, nil
}
func (c *couchDBClient) buildReq(path string) (*http.Request, error) {
url := c.cfg.Endpoint + path
req, err := http.NewRequest(http.MethodGet, url, nil)
if err != nil {
return nil, err
}
req.Header.Set("Content-Type", "application/json")
req.SetBasicAuth(c.cfg.Username, string(c.cfg.Password))
return req, nil
}