-
Notifications
You must be signed in to change notification settings - Fork 179
/
otlp.go
347 lines (305 loc) · 11.4 KB
/
otlp.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
// Copyright (c) 2021-2024 SigScalr, Inc.
//
// This file is part of SigLens Observability Solution
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
package otlp
import (
"bytes"
"compress/gzip"
"encoding/hex"
"encoding/json"
"fmt"
"io"
"github.com/siglens/siglens/pkg/es/writer"
"github.com/siglens/siglens/pkg/grpc"
"github.com/siglens/siglens/pkg/hooks"
"github.com/siglens/siglens/pkg/utils"
log "github.com/sirupsen/logrus"
"github.com/valyala/fasthttp"
coltracepb "go.opentelemetry.io/proto/otlp/collector/trace/v1"
commonpb "go.opentelemetry.io/proto/otlp/common/v1"
tracepb "go.opentelemetry.io/proto/otlp/trace/v1"
"google.golang.org/genproto/googleapis/rpc/status"
"google.golang.org/protobuf/proto"
)
func ProcessTraceIngest(ctx *fasthttp.RequestCtx) {
if hook := hooks.GlobalHooks.OverrideIngestRequestHook; hook != nil {
alreadyHandled := hook(ctx, 0 /* TODO */, grpc.INGEST_FUNC_OTLP_TRACES, false)
if alreadyHandled {
return
}
}
// All requests and responses should be protobufs.
ctx.Response.Header.Set("Content-Type", "application/x-protobuf")
if string(ctx.Request.Header.Peek("Content-Type")) != "application/x-protobuf" {
log.Infof("ProcessTraceIngest: got a non-protobuf request. Got Content-Type: %s", string(ctx.Request.Header.Peek("Content-Type")))
setFailureResponse(ctx, fasthttp.StatusBadRequest, "Expected a protobuf request")
return
}
// Get the data from the request.
data := ctx.PostBody()
if requiresGzipDecompression(ctx) {
reader, err := gzip.NewReader(bytes.NewReader(data))
if err != nil {
setFailureResponse(ctx, fasthttp.StatusBadRequest, "Unable to gzip decompress the data")
return
}
data, err = io.ReadAll(reader)
if err != nil {
setFailureResponse(ctx, fasthttp.StatusBadRequest, "Unable to gzip decompress the data")
return
}
}
// Unmarshal the data.
request, err := unmarshalTraceRequest(data)
if err != nil {
log.Errorf("ProcessTraceIngest: failed to unpack Data: %s with err %v", string(data), err)
setFailureResponse(ctx, fasthttp.StatusBadRequest, "Unable to unmarshal traces")
return
}
// Setup ingestion parameters.
now := utils.GetCurrentTimeInMs()
indexName := "traces"
shouldFlush := false
localIndexMap := make(map[string]string)
orgId := uint64(0)
// Go through the request data and ingest each of the spans.
numSpans := 0 // The total number of spans sent in this request.
numFailedSpans := 0 // The number of spans that we could not ingest.
for _, resourceSpans := range request.ResourceSpans {
// Find the service name.
var service string
if resourceSpans.Resource != nil {
for _, keyvalue := range resourceSpans.Resource.Attributes {
if keyvalue.Key == "service.name" {
service = keyvalue.Value.GetStringValue()
}
}
}
// Ingest each of these spans.
for _, scopeSpans := range resourceSpans.ScopeSpans {
numSpans += len(scopeSpans.Spans)
for _, span := range scopeSpans.Spans {
jsonData, err := spanToJson(span, service)
if err != nil {
log.Errorf("ProcessTraceIngest: failed to marshal span %s: %v. Service name: %s", span, err, service)
numFailedSpans++
continue
}
lenJsonData := uint64(len(jsonData))
err = writer.ProcessIndexRequest(jsonData, now, indexName, lenJsonData, shouldFlush, localIndexMap, orgId, 0 /* TODO */)
if err != nil {
log.Errorf("ProcessTraceIngest: failed to process ingest request with err: %v. JSON Data: %s", err, string(jsonData))
numFailedSpans++
continue
}
}
}
}
log.Debugf("ProcessTraceIngest: %v spans in the request and failed to ingest %v of them", numSpans, numFailedSpans)
// Send the appropriate response.
handleTraceIngestionResponse(ctx, numSpans, numFailedSpans)
}
func requiresGzipDecompression(ctx *fasthttp.RequestCtx) bool {
encoding := string(ctx.Request.Header.Peek("Content-Encoding"))
if encoding == "gzip" {
return true
}
if encoding != "" && encoding != "none" {
log.Errorf("requiresGzipDecompression: invalid content encoding: %s. Request headers: %v", encoding, ctx.Request.Header.String())
}
return false
}
func unmarshalTraceRequest(data []byte) (*coltracepb.ExportTraceServiceRequest, error) {
var trace coltracepb.ExportTraceServiceRequest
err := proto.Unmarshal(data, &trace)
if err != nil {
log.Errorf("unmarshalTraceRequest: failed to unmarshal trace request. err: %v data: %v", err, string(data))
return nil, err
}
return &trace, nil
}
func spanToJson(span *tracepb.Span, service string) ([]byte, error) {
result := make(map[string]interface{})
result["trace_id"] = hex.EncodeToString(span.TraceId)
result["span_id"] = hex.EncodeToString(span.SpanId)
result["parent_span_id"] = hex.EncodeToString(span.ParentSpanId)
result["service"] = service
result["trace_state"] = span.TraceState
result["name"] = span.Name
result["kind"] = span.Kind.String()
result["start_time"] = span.StartTimeUnixNano
result["end_time"] = span.EndTimeUnixNano
result["duration"] = span.EndTimeUnixNano - span.StartTimeUnixNano
result["dropped_attributes_count"] = uint64(span.DroppedAttributesCount)
result["dropped_events_count"] = uint64(span.DroppedEventsCount)
result["dropped_links_count"] = uint64(span.DroppedLinksCount)
if span.Status != nil {
result["status"] = span.Status.Code.String()
} else {
result["status"] = "Unknown"
}
// Make a column for each attribute key.
for _, keyvalue := range span.Attributes {
key, value, err := extractKeyValue(keyvalue)
if err != nil {
return nil, fmt.Errorf("spanToJson: failed to extract KeyValue: %v", err)
}
result[key] = value
}
eventsJson, err := json.Marshal(span.Events)
if err != nil {
log.Errorf("spanToJson: failed to marshal span with ID %s: %v", hex.EncodeToString(span.SpanId), err)
return nil, err
}
result["events"] = string(eventsJson)
linksJson, err := linksToJson(span.Links)
if err != nil {
return nil, err
}
result["links"] = string(linksJson)
bytes, err := json.Marshal(result)
return bytes, err
}
func extractKeyValue(keyvalue *commonpb.KeyValue) (string, interface{}, error) {
value, err := extractAnyValue(keyvalue.Value)
if err != nil {
log.Errorf("extractKeyValue: failed to extract value for key %s: %v", keyvalue.Key, err)
return "", nil, err
}
return keyvalue.Key, value, nil
}
func extractAnyValue(anyValue *commonpb.AnyValue) (interface{}, error) {
switch anyValue.Value.(type) {
case *commonpb.AnyValue_StringValue:
return anyValue.GetStringValue(), nil
case *commonpb.AnyValue_IntValue:
return anyValue.GetIntValue(), nil
case *commonpb.AnyValue_DoubleValue:
return anyValue.GetDoubleValue(), nil
case *commonpb.AnyValue_BoolValue:
return anyValue.GetBoolValue(), nil
case *commonpb.AnyValue_ArrayValue:
arrayValue := anyValue.GetArrayValue().Values
value := make([]interface{}, len(arrayValue))
for i := range arrayValue {
var err error
value[i], err = extractAnyValue(arrayValue[i])
if err != nil {
return nil, err
}
}
return value, nil
default:
return nil, fmt.Errorf("extractAnyValue: unsupported value type: %T", anyValue.Value)
}
}
func linksToJson(spanLinks []*tracepb.Span_Link) ([]byte, error) {
// Links have SpanId and TraceId fields that we want to display has hex, so
// we need custom JSON marshalling.
type Link struct {
TraceId string `json:"trace_id,omitempty"`
SpanId string `json:"span_id,omitempty"`
TraceState string `json:"trace_state,omitempty"`
Attributes map[string]interface{} `json:"attributes,omitempty"`
}
links := make([]Link, len(spanLinks))
for i, link := range spanLinks {
attributes := make(map[string]interface{})
for _, keyvalue := range link.Attributes {
key, value, err := extractKeyValue(keyvalue)
if err != nil {
log.Errorf("linksToJson: failed to extract link attribute for key-value pair %v. err: %v", keyvalue, err)
return nil, err
}
attributes[key] = value
}
links[i] = Link{
TraceId: string(link.TraceId),
SpanId: string(link.SpanId),
TraceState: link.TraceState,
Attributes: attributes,
}
}
jsonLinks, err := json.Marshal(links)
if err != nil {
log.Errorf("linksToJson: failed to marshal links to JSON. links: %v err: %v", links, err)
return nil, err
}
return jsonLinks, nil
}
func setFailureResponse(ctx *fasthttp.RequestCtx, statusCode int, message string) {
ctx.SetStatusCode(statusCode)
failureStatus := status.Status{
Code: int32(statusCode),
Message: message,
}
bytes, err := proto.Marshal(&failureStatus)
if err != nil {
log.Errorf("setFailureResponse: failed to marshal failure status. err: %v. Status: %+v", err, &failureStatus)
}
_, err = ctx.Write(bytes)
if err != nil {
log.Errorf("sendFailureResponse: failed to write failure status: %v", err)
}
}
func handleTraceIngestionResponse(ctx *fasthttp.RequestCtx, numSpans int, numFailedSpans int) {
if numFailedSpans == 0 {
// This request was successful.
response, err := proto.Marshal(&coltracepb.ExportTraceServiceResponse{})
if err != nil {
log.Errorf("handleTraceIngestionResponse: failed to marshal successful response. err: %v. NumSpans: %d", err, numSpans)
ctx.SetStatusCode(fasthttp.StatusInternalServerError)
return
}
_, err = ctx.Write(response)
if err != nil {
log.Errorf("handleTraceIngestionResponse: failed to write successful response. err: %v. NumSpans: %d", err, numSpans)
ctx.SetStatusCode(fasthttp.StatusInternalServerError)
return
}
ctx.SetStatusCode(fasthttp.StatusOK)
return
} else if numFailedSpans < numSpans {
// This request was partially successful.
traceResponse := coltracepb.ExportTraceServiceResponse{
PartialSuccess: &coltracepb.ExportTracePartialSuccess{
RejectedSpans: int64(numFailedSpans),
},
}
response, err := proto.Marshal(&traceResponse)
if err != nil {
log.Errorf("handleTraceIngestionResponse: failed to marshal partially successful response: %v. NumSpans: %d, NumFailedSpans: %d, Trace Response: %v", err, numSpans, numFailedSpans, &traceResponse)
ctx.SetStatusCode(fasthttp.StatusInternalServerError)
return
}
_, err = ctx.Write(response)
if err != nil {
log.Errorf("handleTraceIngestionResponse: failed to write partially successful response: %v. NumSpans: %d, NumFailedSpans: %d, response: %v", err, numSpans, numFailedSpans, response)
ctx.SetStatusCode(fasthttp.StatusInternalServerError)
return
}
ctx.SetStatusCode(fasthttp.StatusOK)
return
} else {
// Every span failed to be ingested.
if numFailedSpans > numSpans {
log.Errorf("handleTraceIngestionResponse: error in counting number of total and failed spans. Counted NumSpans: %d, Counted NumFailedSpans: %d", numSpans, numFailedSpans)
}
log.Errorf("handleTraceIngestionResponse: every span failed ingestion. NumSpans: %d, NumFailedSpans: %d", numSpans, numFailedSpans)
setFailureResponse(ctx, fasthttp.StatusInternalServerError, "Every span failed ingestion")
return
}
}