-
Notifications
You must be signed in to change notification settings - Fork 2.4k
/
client.go
114 lines (91 loc) · 2.99 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
106
107
108
109
110
111
112
113
114
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0
package riakreceiver // import "github.com/open-telemetry/opentelemetry-collector-contrib/receiver/riakreceiver"
import (
"context"
"encoding/json"
"fmt"
"io"
"net/http"
"go.opentelemetry.io/collector/component"
"go.uber.org/zap"
"github.com/open-telemetry/opentelemetry-collector-contrib/receiver/riakreceiver/internal/model"
)
// statsPath is the path to stats endpoint
const statsPath = "/stats"
type client interface {
// GetStats calls "/stats" endpoint to get list of stats for the target node
GetStats(ctx context.Context) (*model.Stats, error)
}
var _ client = (*riakClient)(nil)
type riakClient struct {
client *http.Client
hostEndpoint string
creds riakCredentials
logger *zap.Logger
}
type riakCredentials struct {
username string
password string
}
func newClient(ctx context.Context, cfg *Config, host component.Host, settings component.TelemetrySettings, logger *zap.Logger) (client, error) {
httpClient, err := cfg.ToClient(ctx, host, settings)
if err != nil {
return nil, fmt.Errorf("failed to create HTTP Client: %w", err)
}
return &riakClient{
client: httpClient,
hostEndpoint: cfg.Endpoint,
creds: riakCredentials{
username: cfg.Username,
password: string(cfg.Password),
},
logger: logger,
}, nil
}
func (c *riakClient) GetStats(ctx context.Context) (*model.Stats, error) {
var stats *model.Stats
if err := c.get(ctx, statsPath, &stats); err != nil {
c.logger.Debug("Failed to retrieve stats", zap.Error(err))
return nil, err
}
return stats, nil
}
func (c *riakClient) get(ctx context.Context, path string, respObj any) error {
// Construct endpoint and create request
url := c.hostEndpoint + path
req, err := http.NewRequestWithContext(ctx, http.MethodGet, url, http.NoBody)
if err != nil {
return fmt.Errorf("failed to create get request for path %s: %w", path, err)
}
// Set user/pass authentication
req.SetBasicAuth(c.creds.username, c.creds.password)
// Make request
resp, err := c.client.Do(req)
if err != nil {
return fmt.Errorf("failed to make http request: %w", err)
}
// Defer body close
defer func() {
if closeErr := resp.Body.Close(); closeErr != nil {
c.logger.Warn("failed to close response body", zap.Error(closeErr))
}
}()
// Check for OK status code
if resp.StatusCode != http.StatusOK {
c.logger.Debug("riak API non-200", zap.Error(err), zap.Int("status_code", resp.StatusCode))
// Attempt to extract the error payload
payloadData, err := io.ReadAll(resp.Body)
if err != nil {
c.logger.Debug("failed to read payload error message", zap.Error(err))
} else {
c.logger.Debug("riak API Error", zap.ByteString("api_error", payloadData))
}
return fmt.Errorf("non 200 code returned %d", resp.StatusCode)
}
// Decode the payload into the passed in response object
if err := json.NewDecoder(resp.Body).Decode(respObj); err != nil {
return fmt.Errorf("failed to decode response payload: %w", err)
}
return nil
}