-
Notifications
You must be signed in to change notification settings - Fork 69
/
elasticsearchdb.go
298 lines (259 loc) 路 7.55 KB
/
elasticsearchdb.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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
package datastores
import (
"context"
"crypto/tls"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"reflect"
"strconv"
"strings"
"time"
"github.com/elastic/go-elasticsearch/v8"
"github.com/elastic/go-elasticsearch/v8/esapi"
"github.com/kubeshop/tracetest/server/datastore"
"github.com/kubeshop/tracetest/server/model"
"github.com/kubeshop/tracetest/server/tracedb/connection"
"github.com/kubeshop/tracetest/server/traces"
"go.opentelemetry.io/otel/trace"
)
func elasticSearchDefaultPorts() []string {
return []string{"9200"}
}
type elasticsearchDB struct {
realDataStore
config *datastore.ElasticSearchConfig
client *elasticsearch.Client
}
func (db *elasticsearchDB) Connect(ctx context.Context) error {
return nil
}
func (db *elasticsearchDB) Close() error {
// No need to close this db
return nil
}
func (db *elasticsearchDB) GetEndpoints() string {
return strings.Join(db.config.Addresses, ", ")
}
func (db *elasticsearchDB) TestConnection(ctx context.Context) model.ConnectionResult {
tester := connection.NewTester(
connection.WithPortLintingTest(connection.PortLinter("ElasticSearch", elasticSearchDefaultPorts(), db.config.Addresses...)),
connection.WithConnectivityTest(connection.ConnectivityStep(model.ProtocolHTTP, db.config.Addresses...)),
connection.WithPollingTest(connection.TracePollingTestStep(db)),
connection.WithAuthenticationTest(connection.NewTestStep(func(ctx context.Context) (string, error) {
_, err := getClusterInfo(db.client)
if err != nil {
return "Tracetest tried to execute an ElasticSearch API request but it failed due to authentication issues", err
}
return "Tracetest managed to authenticate with ElasticSearch", nil
})),
)
return tester.TestConnection(ctx)
}
func (db *elasticsearchDB) Ready() bool {
return db.client != nil
}
func (db *elasticsearchDB) GetTraceByID(ctx context.Context, traceID string) (traces.Trace, error) {
if !db.Ready() {
return traces.Trace{}, fmt.Errorf("ElasticSearch dataStore not ready")
}
content := strings.NewReader(fmt.Sprintf(`{
"query": { "match": { "trace.id": "%s" } }
}`, traceID))
searchRequest := esapi.SearchRequest{
Index: []string{db.config.Index},
Body: content,
Pretty: true,
}
response, err := searchRequest.Do(ctx, db.client)
if err != nil {
return traces.Trace{}, fmt.Errorf("could not execute search request: %w", err)
}
defer response.Body.Close()
responseBody, err := ioutil.ReadAll(response.Body)
if err != nil {
return traces.Trace{}, fmt.Errorf("could not read response body")
}
var searchResponse searchResponse
err = json.Unmarshal(responseBody, &searchResponse)
if err != nil {
return traces.Trace{}, fmt.Errorf("could not unmarshal search response into struct: %w", err)
}
if len(searchResponse.Hits.Results) == 0 {
return traces.Trace{}, connection.ErrTraceNotFound
}
return convertElasticSearchFormatIntoTrace(traceID, searchResponse), nil
}
func newElasticSearchDB(cfg *datastore.ElasticSearchConfig) (DataStore, error) {
var caCert []byte
if cfg.Certificate != "" {
caCert = []byte(cfg.Certificate)
}
client, err := elasticsearch.NewClient(elasticsearch.Config{
Addresses: cfg.Addresses,
Username: cfg.Username,
Password: cfg.Password,
CACert: caCert,
Transport: &http.Transport{
TLSClientConfig: &tls.Config{
InsecureSkipVerify: cfg.InsecureSkipVerify,
},
},
})
if err != nil {
return nil, fmt.Errorf("could not create elasticsearch client: %w", err)
}
return &elasticsearchDB{
config: cfg,
client: client,
}, nil
}
func getClusterInfo(client *elasticsearch.Client) (string, error) {
var r map[string]interface{}
res, err := client.Info()
if err != nil {
return "", fmt.Errorf("error getting cluster info response: %s", err)
}
defer res.Body.Close()
// Check response status
if res.IsError() {
return "", fmt.Errorf("error getting cluster info response status: %s", res.String())
}
// Deserialize the response into a map
if err := json.NewDecoder(res.Body).Decode(&r); err != nil {
return "", fmt.Errorf("error parsing cluster info response: %s", err)
}
// Return client version number
info := fmt.Sprintf("Server: %s", r["version"].(map[string]interface{})["number"])
return info, nil
}
func convertElasticSearchFormatIntoTrace(traceID string, searchResponse searchResponse) traces.Trace {
spans := make([]traces.Span, 0)
for _, result := range searchResponse.Hits.Results {
span := convertElasticSearchSpanIntoSpan(result.Source)
spans = append(spans, span)
}
return traces.NewTrace(traceID, spans)
}
func convertElasticSearchSpanIntoSpan(input map[string]interface{}) traces.Span {
opts := &FlattenOptions{Delimiter: "."}
flatInput, _ := flatten(opts.Prefix, 0, input, opts)
// SpanId
transactionId := flatInput["transaction.id"]
spanId := flatInput["span.id"]
var id trace.SpanID
if transactionId != nil {
id, _ = trace.SpanIDFromHex((transactionId).(string))
}
if spanId != nil {
id, _ = trace.SpanIDFromHex((spanId).(string))
}
// SpanName
transactionName := flatInput["transaction.name"]
spanName := flatInput["span.name"]
var name string
if transactionName != nil {
name = transactionName.(string)
}
if spanName != nil {
name = spanName.(string)
}
// Duration
transactionDuration := flatInput["transaction.duration.us"]
spanDuration := flatInput["span.duration.us"]
var duration float64
if transactionDuration != nil {
duration = transactionDuration.(float64)
}
if spanDuration != nil {
duration = spanDuration.(float64)
}
// Timestamps
startTime, _ := time.Parse(time.RFC3339, flatInput["@timestamp"].(string))
endTime := startTime.Add(time.Microsecond * time.Duration(duration))
// Attributes
attributes := make(traces.Attributes, 0)
for attrName, attrValue := range flatInput {
name := attrName
name = strings.ReplaceAll(name, "transaction.", "")
name = strings.ReplaceAll(name, "span.", "")
attributes[name] = fmt.Sprintf("%v", attrValue)
}
// ParentId
parentId := flatInput["parent.id"]
if parentId != nil {
attributes[traces.TracetestMetadataFieldParentID] = flatInput["parent.id"].(string)
}
return traces.Span{
ID: id,
Name: name,
StartTime: startTime,
EndTime: endTime,
Attributes: attributes,
Parent: nil,
Children: []*traces.Span{},
}
}
type FlattenOptions struct {
Prefix string
Delimiter string
Safe bool
MaxDepth int
}
func flatten(prefix string, depth int, nested interface{}, opts *FlattenOptions) (flatmap map[string]interface{}, err error) {
flatmap = make(map[string]interface{})
switch nested := nested.(type) {
case map[string]interface{}:
if opts.MaxDepth != 0 && depth >= opts.MaxDepth {
flatmap[prefix] = nested
return
}
if reflect.DeepEqual(nested, map[string]interface{}{}) {
flatmap[prefix] = nested
return
}
for k, v := range nested {
// create new key
newKey := k
if prefix != "" {
newKey = prefix + opts.Delimiter + newKey
}
fm1, fe := flatten(newKey, depth+1, v, opts)
if fe != nil {
err = fe
return
}
update(flatmap, fm1)
}
case []interface{}:
if opts.Safe {
flatmap[prefix] = nested
return
}
if reflect.DeepEqual(nested, []interface{}{}) {
flatmap[prefix] = nested
return
}
for i, v := range nested {
newKey := strconv.Itoa(i)
if prefix != "" {
newKey = prefix + opts.Delimiter + newKey
}
fm1, fe := flatten(newKey, depth+1, v, opts)
if fe != nil {
err = fe
return
}
update(flatmap, fm1)
}
default:
flatmap[prefix] = nested
}
return
}
func update(to map[string]interface{}, from map[string]interface{}) {
for kt, vt := range from {
to[kt] = vt
}
}