-
Notifications
You must be signed in to change notification settings - Fork 2.5k
/
Copy pathclient.go
214 lines (180 loc) · 5.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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0
package saphanareceiver // import "github.com/open-telemetry/opentelemetry-collector-contrib/receiver/saphanareceiver"
import (
"context"
"database/sql"
"database/sql/driver"
"errors"
"fmt"
sapdriver "github.com/SAP/go-hdb/driver"
"go.opentelemetry.io/collector/scraper/scrapererror"
)
// Interface for a SAP HANA client. Implementation can be faked for testing.
type client interface {
Connect(ctx context.Context) error
collectDataFromQuery(ctx context.Context, query *monitoringQuery) ([]map[string]string, error)
Close() error
}
// Wraps the result of a query so that it can be mocked in tests
type resultWrapper interface {
Scan(dest ...any) error
Close() error
Next() bool
}
// Wraps the sqlDB interface so that it can be mocked in tests
type dbWrapper interface {
PingContext(ctx context.Context) error
Close() error
QueryContext(ctx context.Context, query string) (resultWrapper, error)
}
type standardResultWrapper struct {
rows *sql.Rows
}
func (w *standardResultWrapper) Next() bool {
return w.rows.Next()
}
func (w *standardResultWrapper) Scan(dest ...any) error {
return w.rows.Scan(dest...)
}
func (w *standardResultWrapper) Close() error {
return w.rows.Close()
}
type standardDBWrapper struct {
db *sql.DB
}
func (w *standardDBWrapper) Close() error {
return w.db.Close()
}
func (w *standardDBWrapper) PingContext(ctx context.Context) error {
return w.db.PingContext(ctx)
}
func (w *standardDBWrapper) QueryContext(ctx context.Context, query string) (resultWrapper, error) {
rows, err := w.db.QueryContext(ctx, query)
if err != nil {
return nil, err
}
resultWrapper := standardResultWrapper{rows}
return &resultWrapper, nil
}
// Wraps the creation of a sqlDB so that it can be mocked in tests
type sapHanaConnectionFactory interface {
getConnection(c driver.Connector) dbWrapper
}
type defaultConnectionFactory struct{}
func (f *defaultConnectionFactory) getConnection(c driver.Connector) dbWrapper {
wrapper := standardDBWrapper{db: sql.OpenDB((c))}
return &wrapper
}
// Wraps a SAP HANA database connection, implements `client` interface.
type sapHanaClient struct {
receiverConfig *Config
connectionFactory sapHanaConnectionFactory
client dbWrapper
}
var _ client = (*sapHanaClient)(nil)
// Creates a SAP HANA database client
func newSapHanaClient(cfg *Config, factory sapHanaConnectionFactory) client {
return &sapHanaClient{
receiverConfig: cfg,
connectionFactory: factory,
}
}
func (c *sapHanaClient) Connect(ctx context.Context) error {
connector, err := sapdriver.NewDSNConnector(fmt.Sprintf("hdb://%s:%s@%s", c.receiverConfig.Username, string(c.receiverConfig.Password), c.receiverConfig.TCPAddrConfig.Endpoint))
if err != nil {
return fmt.Errorf("error generating DSN for SAP HANA connection: %w", err)
}
tls, err := c.receiverConfig.ClientConfig.LoadTLSConfig(ctx)
if err != nil {
return fmt.Errorf("error generating TLS config for SAP HANA connection: %w", err)
}
connector.SetTLSConfig(tls)
connector.SetApplicationName("OpenTelemetry Collector")
client := c.connectionFactory.getConnection(connector)
err = client.PingContext(ctx)
if err == nil {
c.client = client
} else {
client.Close()
}
return err
}
func (c *sapHanaClient) Close() error {
if c.client != nil {
client := c.client
c.client = nil
return client.Close()
}
return nil
}
func (c *sapHanaClient) collectDataFromQuery(ctx context.Context, query *monitoringQuery) ([]map[string]string, error) {
rows, err := c.client.QueryContext(ctx, query.query)
if err != nil {
return nil, err
}
defer rows.Close()
errors := scrapererror.ScrapeErrors{}
var data []map[string]string
ROW_ITERATOR:
for rows.Next() {
expectedFields := len(query.orderedMetricLabels) + len(query.orderedResourceLabels) + len(query.orderedStats)
rowFields := make([]any, expectedFields)
// Build a list of addresses that rows.Scan will load column data into
for i := range rowFields {
rowFields[i] = new(sql.NullString)
}
if err := rows.Scan(rowFields...); err != nil {
return nil, err
}
values := map[string]string{}
for _, label := range query.orderedResourceLabels {
v, err := convertInterfaceToString(rowFields[0])
if err != nil {
errors.AddPartial(0, err)
continue ROW_ITERATOR
}
// If value was null, we can't use this row
if !v.Valid {
errors.AddPartial(0, fmt.Errorf("database row NULL value for required resource label %s", label))
continue ROW_ITERATOR
}
values[label] = v.String
rowFields = rowFields[1:]
}
for _, label := range query.orderedMetricLabels {
v, err := convertInterfaceToString(rowFields[0])
if err != nil {
errors.AddPartial(0, err)
continue ROW_ITERATOR
}
// If value was null, we can't use this row
if !v.Valid {
errors.AddPartial(0, fmt.Errorf("database row NULL value for required metric label %s", label))
continue ROW_ITERATOR
}
values[label] = v.String
rowFields = rowFields[1:]
}
for _, stat := range query.orderedStats {
v, err := convertInterfaceToString(rowFields[0])
if err != nil {
errors.AddPartial(0, err)
continue
}
// Only report stat if value was not NULL
if v.Valid {
values[stat.key] = v.String
}
rowFields = rowFields[1:]
}
data = append(data, values)
}
return data, errors.Combine()
}
func convertInterfaceToString(input any) (sql.NullString, error) {
if val, ok := input.(*sql.NullString); ok {
return *val, nil
}
return sql.NullString{}, errors.New("issue converting interface into string")
}