/
influxdb_writer.go
235 lines (213 loc) · 7.22 KB
/
influxdb_writer.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
package internal
import (
"bytes"
"context"
"database/sql"
"errors"
"fmt"
"io"
"net/http"
"sync"
"time"
"github.com/golang/groupcache/lru"
"github.com/influxdata/line-protocol/v2/lineprotocol"
"github.com/jaegertracing/jaeger/model"
"github.com/jaegertracing/jaeger/storage/spanstore"
"go.uber.org/zap"
"github.com/influxdata/influxdb-observability/common"
)
var _ spanstore.Writer = (*influxdbWriterNoop)(nil)
var _ spanstore.Writer = (*influxdbWriterArchive)(nil)
type influxdbWriterNoop struct {
logger *zap.Logger
}
func (iwn *influxdbWriterNoop) WriteSpan(_ context.Context, _ *model.Span) error {
iwn.logger.Debug("no-op WriteSpan called")
return errors.New("WriteSpan is not implemented in this context")
}
type influxdbWriterArchive struct {
logger *zap.Logger
executeQuery func(ctx context.Context, db *sql.DB, query string, f func(record map[string]interface{}) error) error
recentTraces *lru.Cache
recentTracesMu sync.Mutex
httpClient *http.Client
authToken string
dbSrc *sql.DB
bucketNameSrc, tableSpansSrc, tableLogsSrc, tableSpanLinksSrc string
writeURLArchive string
bucketNameArchive, tableSpansArchive, tableLogsArchive, tableSpanLinksArchive string
}
func (iwa *influxdbWriterArchive) WriteSpan(ctx context.Context, span *model.Span) error {
iwa.recentTracesMu.Lock()
if _, found := iwa.recentTraces.Get(span.TraceID.High ^ span.TraceID.Low); found {
iwa.recentTracesMu.Unlock()
return nil
}
iwa.recentTraces.Add(span.TraceID.High^span.TraceID.Low, struct{}{})
iwa.recentTracesMu.Unlock()
lpEncoder := new(lineprotocol.Encoder)
lpEncoder.SetLax(true)
lpEncoder.SetPrecision(lineprotocol.Nanosecond)
// trace spans
err := iwa.executeQuery(ctx, iwa.dbSrc, queryGetTraceSpans(iwa.tableSpansSrc, span.TraceID),
func(row map[string]interface{}) error {
lpEncoder.StartLine(iwa.tableSpansArchive)
var tagCount int
for _, k := range []string{common.AttributeTraceID, common.AttributeSpanID} {
if stringValue, ok := row[k].(string); ok {
lpEncoder.AddTag(k, stringValue)
tagCount++
} else {
iwa.logger.Sugar().Warn("expected column %s to have type string but got %T", k, row[k])
}
}
if tagCount != 2 {
return fmt.Errorf("expected 2 tags, but got %d; should find columns %s, %s",
tagCount, common.AttributeTraceID, common.AttributeSpanID)
}
for k, v := range row {
switch k {
case common.AttributeTraceID, common.AttributeSpanID, common.AttributeTime:
default:
if v == nil {
continue
}
if fieldValue, ok := lineprotocol.NewValue(v); ok {
lpEncoder.AddField(k, fieldValue)
} else {
iwa.logger.Sugar().Warn("failed to cast column %s (%T) to line protocol field value", k, v)
}
}
}
foundTime := false
if v, ok := row[common.AttributeTime]; ok && v != nil {
if timeValue, ok := v.(time.Time); ok {
foundTime = true
lpEncoder.EndLine(timeValue)
} else {
iwa.logger.Sugar().Warn("expected column %s to have type time but got %T", common.AttributeTime, v)
}
}
if !foundTime {
return fmt.Errorf("time value not found in row")
}
return nil
})
if err != nil {
return fmt.Errorf("failed to query spans table: %w", err)
}
// trace events
err = iwa.executeQuery(ctx, iwa.dbSrc, queryGetTraceEvents(iwa.tableLogsSrc, span.TraceID),
func(row map[string]interface{}) error {
lpEncoder.StartLine(iwa.tableLogsArchive)
var tagCount int
for _, k := range []string{common.AttributeTraceID, common.AttributeSpanID} {
if stringValue, ok := row[k].(string); ok {
lpEncoder.AddTag(k, stringValue)
tagCount++
} else {
iwa.logger.Sugar().Warn("expected column %s to have type string but got %T", k, row[k])
}
}
if tagCount != 2 {
return fmt.Errorf("expected 2 tags, but got %d; should find columns %s, %s",
tagCount, common.AttributeTraceID, common.AttributeSpanID)
}
for k, v := range row {
switch k {
case common.AttributeTraceID, common.AttributeSpanID, common.AttributeTime:
default:
if v == nil {
continue
}
if fieldValue, ok := lineprotocol.NewValue(v); ok {
lpEncoder.AddField(k, fieldValue)
} else {
iwa.logger.Sugar().Warn("failed to cast column %s (%T) to line protocol field value", k, v)
}
}
}
foundTime := false
if v, ok := row[common.AttributeTime]; ok && v != nil {
if timeValue, ok := v.(time.Time); ok {
foundTime = true
lpEncoder.EndLine(timeValue)
} else {
iwa.logger.Sugar().Warn("expected column %s to have type time but got %T", common.AttributeTime, v)
}
}
if !foundTime {
return fmt.Errorf("time value not found in row")
}
return nil
})
if err != nil {
iwa.logger.Error("failed to query logs (span events) table", zap.Error(err))
}
// trace span links
err = iwa.executeQuery(ctx, iwa.dbSrc, queryGetTraceLinks(iwa.tableSpanLinksSrc, span.TraceID),
func(row map[string]interface{}) error {
lpEncoder.StartLine(iwa.tableSpanLinksArchive)
var tagCount int
for _, k := range []string{common.AttributeTraceID, common.AttributeSpanID, common.AttributeLinkedTraceID, common.AttributeLinkedSpanID} {
if stringValue, ok := row[k].(string); ok {
lpEncoder.AddTag(k, stringValue)
tagCount++
} else {
iwa.logger.Sugar().Warn("expected column %s to have type string but got %T", k, row[k])
}
}
if tagCount != 4 {
return fmt.Errorf("expected 4 tags, but got %d; should find columns %s, %s, %s, %s",
tagCount, common.AttributeTraceID, common.AttributeSpanID, common.AttributeLinkedTraceID, common.AttributeLinkedSpanID)
}
for k, v := range row {
switch k {
case common.AttributeTraceID, common.AttributeSpanID, common.AttributeLinkedTraceID, common.AttributeLinkedSpanID, common.AttributeTime:
default:
if v == nil {
continue
}
if fieldValue, ok := lineprotocol.NewValue(v); ok {
lpEncoder.AddField(k, fieldValue)
} else {
iwa.logger.Sugar().Warn("failed to cast column %s (%T) to line protocol field value", k, v)
}
}
}
foundTime := false
if v, ok := row[common.AttributeTime]; ok && v != nil {
if timeValue, ok := v.(time.Time); ok {
foundTime = true
lpEncoder.EndLine(timeValue)
} else {
iwa.logger.Sugar().Warn("expected column %s to have type time but got %T", common.AttributeTime, v)
}
}
if !foundTime {
return fmt.Errorf("time value not found in row")
}
return nil
})
if err != nil {
iwa.logger.Error("failed to query span links table", zap.Error(err))
}
if err = lpEncoder.Err(); err != nil {
return err
}
req, err := http.NewRequestWithContext(ctx, http.MethodPost, iwa.writeURLArchive, bytes.NewReader(lpEncoder.Bytes()))
if err != nil {
return err
}
req.Header.Set("Authorization", fmt.Sprintf("Token %s", iwa.authToken))
if res, err := iwa.httpClient.Do(req); err != nil {
return err
} else if body, err := io.ReadAll(res.Body); err != nil {
return err
} else if err = res.Body.Close(); err != nil {
return err
} else if res.StatusCode/100 != 2 {
return fmt.Errorf("line protocol write returned %q %q", res.Status, string(body))
}
return nil
}