-
Notifications
You must be signed in to change notification settings - Fork 71
/
azureappinsights.go
374 lines (308 loc) 路 8.63 KB
/
azureappinsights.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
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
package datastores
import (
"context"
"encoding/json"
"fmt"
"strings"
"time"
"github.com/Azure/azure-sdk-for-go/sdk/azcore"
"github.com/Azure/azure-sdk-for-go/sdk/azcore/policy"
"github.com/Azure/azure-sdk-for-go/sdk/azidentity"
"github.com/Azure/azure-sdk-for-go/sdk/monitor/azquery"
"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"
)
var (
azureLogUrl = "https://api.loganalytics.azure.com"
)
type azureAppInsightsDB struct {
realDataStore
resourceArmId string
credentials azcore.TokenCredential
client *azquery.LogsClient
}
var _ DataStore = &azureAppInsightsDB{}
func NewAzureAppInsightsDB(config *datastore.AzureAppInsightsConfig) (DataStore, error) {
var credentials azcore.TokenCredential
var err error
if config.UseAzureActiveDirectoryAuth {
credentials, err = azidentity.NewDefaultAzureCredential(nil)
if err != nil {
return nil, err
}
} else {
creds := []azcore.TokenCredential{
&tokenCredentials{accessToken: config.AccessToken},
}
credentials, err = azidentity.NewChainedTokenCredential(creds, nil)
if err != nil {
return nil, err
}
}
return &azureAppInsightsDB{
resourceArmId: config.ResourceArmId,
credentials: credentials,
}, nil
}
func (db *azureAppInsightsDB) Connect(ctx context.Context) error {
client, err := azquery.NewLogsClient(db.credentials, nil)
if err != nil {
return err
}
db.client = client
return nil
}
func (db *azureAppInsightsDB) Close() error {
return nil
}
func (db *azureAppInsightsDB) Ready() bool {
return db.credentials != nil && db.client != nil
}
func (db *azureAppInsightsDB) GetEndpoints() string {
return azureLogUrl
}
func (db *azureAppInsightsDB) TestConnection(ctx context.Context) model.ConnectionResult {
url := azureLogUrl
tester := connection.NewTester(
connection.WithConnectivityTest(connection.ConnectivityStep(model.ProtocolHTTP, url)),
connection.WithPollingTest(connection.TracePollingTestStep(db)),
connection.WithAuthenticationTest(connection.NewTestStep(func(ctx context.Context) (string, error) {
_, err := db.GetTraceByID(ctx, db.GetTraceID().String())
if err != nil && strings.Contains(strings.ToLower(err.Error()), "403") {
return `Tracetest tried to execute an Azure API request but it failed due to authentication issues`, err
}
return "Tracetest managed to authenticate with the Azure Services", nil
})),
)
return tester.TestConnection(ctx)
}
func (db *azureAppInsightsDB) GetTraceByID(ctx context.Context, traceID string) (traces.Trace, error) {
query := fmt.Sprintf("union * | where operation_Id == '%s'", traceID)
body := azquery.Body{
Query: &query,
}
res, err := db.client.QueryResource(ctx, db.resourceArmId, body, nil)
if err != nil {
return traces.Trace{}, err
}
table := res.Tables[0]
if len(table.Rows) == 0 {
return traces.Trace{}, connection.ErrTraceNotFound
}
return parseAzureAppInsightsTrace(traceID, table)
}
type spanTable struct {
rows []spanRow
}
func (st *spanTable) Spans() []spanRow {
output := make([]spanRow, 0)
for _, row := range st.rows {
if row.Type() != "trace" {
output = append(output, row)
}
}
return output
}
func (st *spanTable) Events() []spanRow {
output := make([]spanRow, 0)
for _, row := range st.rows {
if row.Type() == "trace" {
output = append(output, row)
}
}
return output
}
type spanRow struct {
values map[string]any
}
func (sr *spanRow) Get(name string) any {
return sr.values[name]
}
func (sr *spanRow) Type() string {
return sr.values["itemType"].(string)
}
func (sr *spanRow) ParentID() string {
return sr.values["operation_ParentId"].(string)
}
func (sr *spanRow) SpanID() string {
return sr.values["id"].(string)
}
func newSpanTable(table *azquery.Table) spanTable {
spanRows := make([]spanRow, 0, len(table.Rows))
for _, row := range table.Rows {
spanRows = append(spanRows, newSpanRow(row, table.Columns))
}
return spanTable{spanRows}
}
func newSpanRow(row azquery.Row, columns []*azquery.Column) spanRow {
values := make(map[string]any)
for i, column := range columns {
name := *column.Name
if value := row[i]; value != nil {
values[name] = value
}
}
return spanRow{values}
}
func parseAzureAppInsightsTrace(traceID string, table *azquery.Table) (traces.Trace, error) {
spans, err := parseSpans(table)
if err != nil {
return traces.Trace{}, err
}
return traces.NewTrace(traceID, spans), nil
}
func parseSpans(table *azquery.Table) ([]traces.Span, error) {
spanTable := newSpanTable(table)
spanRows := spanTable.Spans()
eventRows := spanTable.Events()
spanEventsMap := make(map[string][]spanRow)
for _, eventRow := range eventRows {
spanEventsMap[eventRow.ParentID()] = append(spanEventsMap[eventRow.ParentID()], eventRow)
}
spanMap := make(map[string]*traces.Span)
for _, spanRow := range spanRows {
span, err := parseRowToSpan(spanRow)
if err != nil {
return []traces.Span{}, err
}
spanMap[span.ID.String()] = &span
}
for _, eventRow := range eventRows {
parentSpan := spanMap[eventRow.ParentID()]
event, err := parseEvent(eventRow)
if err != nil {
return []traces.Span{}, err
}
parentSpan.Events = append(parentSpan.Events, event)
}
spans := make([]traces.Span, 0, len(spanMap))
for _, span := range spanMap {
spans = append(spans, *span)
}
return spans, nil
}
func parseEvent(row spanRow) (traces.SpanEvent, error) {
event := traces.SpanEvent{
Name: row.Get("message").(string),
}
timestamp, err := time.Parse(time.RFC3339Nano, row.Get("timestamp").(string))
if err != nil {
return event, fmt.Errorf("could not parse event timestamp: %w", err)
}
event.Timestamp = timestamp
attributes := make(traces.Attributes, 0)
rawAttributes := row.Get("customDimensions").(string)
err = json.Unmarshal([]byte(rawAttributes), &attributes)
if err != nil {
return event, fmt.Errorf("could not unmarshal event attributes: %w", err)
}
event.Attributes = attributes
return event, nil
}
func parseRowToSpan(row spanRow) (traces.Span, error) {
attributes := make(traces.Attributes, 0)
span := traces.Span{
Attributes: attributes,
}
var duration time.Duration
for name, value := range row.values {
switch name {
case "id":
err := parseSpanID(&span, value)
if err != nil {
return span, err
}
case "customDimensions":
err := parseAttributes(&span, value)
if err != nil {
return span, err
}
case "operation_ParentId":
err := parseParentID(&span, value)
if err != nil {
return span, err
}
case "name":
err := parseName(&span, value)
if err != nil {
return span, err
}
case "timestamp":
err := parseStartTime(&span, value)
if err != nil {
return span, err
}
case "duration":
timeDuration, err := parseDuration(value)
if err != nil {
return span, err
}
duration = timeDuration
}
}
span.EndTime = span.StartTime.Add(duration)
return span, nil
}
func parseSpanID(span *traces.Span, value any) error {
spanID, err := trace.SpanIDFromHex(value.(string))
if err != nil {
return fmt.Errorf("failed to parse spanId: %w", err)
}
span.ID = spanID
return nil
}
func parseAttributes(span *traces.Span, value any) error {
attributes := make(traces.Attributes, 0)
rawAttributes := value.(string)
err := json.Unmarshal([]byte(rawAttributes), &attributes)
if err != nil {
return fmt.Errorf("failed to parse attributes: %w", err)
}
for key, value := range attributes {
span.Attributes[key] = value
}
return nil
}
func parseParentID(span *traces.Span, value any) error {
rawParentID, ok := value.(string)
if ok {
span.Attributes[traces.TracetestMetadataFieldParentID] = rawParentID
} else {
span.Attributes[traces.TracetestMetadataFieldParentID] = ""
}
return nil
}
func parseName(span *traces.Span, value any) error {
rawName, ok := value.(string)
if ok {
span.Name = rawName
} else {
span.Name = ""
}
return nil
}
func parseStartTime(span *traces.Span, value any) error {
rawStartTime := value.(string)
startTime, err := time.Parse(time.RFC3339Nano, rawStartTime)
if err != nil {
return fmt.Errorf("failed to parse startTime: %w", err)
}
span.StartTime = startTime
return nil
}
func parseDuration(value any) (time.Duration, error) {
rawDuration, ok := value.(float64)
if !ok {
return time.Duration(0), fmt.Errorf("failed to parse duration")
}
return time.Duration(rawDuration), nil
}
type tokenCredentials struct {
accessToken string
}
func (c *tokenCredentials) GetToken(ctx context.Context, opts policy.TokenRequestOptions) (azcore.AccessToken, error) {
return azcore.AccessToken{Token: c.accessToken}, nil
}