-
Notifications
You must be signed in to change notification settings - Fork 473
/
http_client.go
317 lines (276 loc) · 9.77 KB
/
http_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
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
package tempo
import (
"encoding/json"
"errors"
"fmt"
"io"
"net/http"
"net/url"
"path"
"strconv"
"strings"
"github.com/kiali/kiali/config"
"github.com/kiali/kiali/log"
"github.com/kiali/kiali/models"
"github.com/kiali/kiali/tracing/jaeger/model"
jaegerModels "github.com/kiali/kiali/tracing/jaeger/model/json"
otel "github.com/kiali/kiali/tracing/otel/model"
"github.com/kiali/kiali/tracing/otel/model/converter"
otelModels "github.com/kiali/kiali/tracing/otel/model/json"
"github.com/kiali/kiali/util"
)
type OtelHTTPClient struct {
ClusterTag bool
}
// New client
func NewOtelClient(client http.Client, baseURL *url.URL) (otelClient *OtelHTTPClient, err error) {
url := *baseURL
url.Path = path.Join(url.Path, "/api/search/tags")
tags := false
r, status, _ := makeRequest(client, url.String(), nil)
if status != 200 {
log.Debugf("Error getting Tempo tags for tracing. Tags will be disabled. %s", r)
} else {
var response otel.TagsResponse
if errMarshal := json.Unmarshal(r, &response); errMarshal != nil {
log.Errorf("Error unmarshalling Tempo API response: %s [URL: %v]", errMarshal, url)
return nil, errMarshal
}
if util.InSlice(response.TagNames, "cluster") {
tags = true
}
}
return &OtelHTTPClient{ClusterTag: tags}, nil
}
// GetAppTracesHTTP search traces
func (oc OtelHTTPClient) GetAppTracesHTTP(client http.Client, baseURL *url.URL, serviceName string, q models.TracingQuery) (response *model.TracingResponse, err error) {
url := *baseURL
url.Path = path.Join(url.Path, "/api/search")
oc.prepareTraceQL(&url, serviceName, q)
r, err := oc.queryTracesHTTP(client, &url, q.Tags["error"])
if r != nil {
r.TracingServiceName = serviceName
}
return r, err
}
// GetTraceDetailHTTP get one trace by trace ID
func (oc OtelHTTPClient) GetTraceDetailHTTP(client http.Client, endpoint *url.URL, traceID string) (*model.TracingSingleTrace, error) {
u := *endpoint
u.Path = path.Join(u.Path, "/api/traces/", traceID)
resp, code, reqError := makeRequest(client, u.String(), nil)
if reqError != nil {
log.Errorf("API Tempo query error: %s [code: %d, URL: %v]", reqError, code, u)
return nil, reqError
}
if code != 200 {
errorMsg := fmt.Sprintf("Error returning traces: %s", resp)
log.Errorf(errorMsg)
var errorTrace []model.StructuredError
errorTrace = append(errorTrace, model.StructuredError{TraceID: traceID, Code: code, Msg: errorMsg})
return &model.TracingSingleTrace{Errors: errorTrace}, errors.New(errorMsg)
}
if len(resp) == 0 {
return nil, errors.New("empty body response")
}
responseOtel, _ := unmarshalSingleTrace(resp, &u)
response, err := convertSingleTrace(responseOtel, traceID)
if err != nil {
return nil, err
}
if len(response.Data) == 0 {
return &model.TracingSingleTrace{Errors: response.Errors}, nil
}
return &model.TracingSingleTrace{
Data: response.Data[0],
Errors: response.Errors,
}, nil
}
// GetServiceStatusHTTP get service status
func (oc OtelHTTPClient) GetServiceStatusHTTP(client http.Client, baseURL *url.URL) (bool, error) {
var u url.URL
healthCheckUrl := config.Get().ExternalServices.Tracing.HealthCheckUrl
if healthCheckUrl != "" {
url, err := u.Parse(healthCheckUrl)
if err != nil {
return false, fmt.Errorf("Error %s incorrect healthCheckUrl", err)
}
u = *url
} else {
u = *baseURL
u.Path = path.Join(u.Path, "/status/services")
}
_, status, reqError := makeRequest(client, u.String(), nil)
if status != 200 {
return false, fmt.Errorf("Error %d getting status services", status)
}
return reqError == nil, reqError
}
// queryTracesHTTP
func (oc OtelHTTPClient) queryTracesHTTP(client http.Client, u *url.URL, error string) (*model.TracingResponse, error) {
// HTTP and GRPC requests co-exist, but when minDuration is present, for HTTP it requires a unit (ms)
// https://github.com/kiali/kiali/issues/3939
minDuration := u.Query().Get("minDuration")
if minDuration != "" && !strings.HasSuffix(minDuration, "ms") {
query := u.Query()
query.Set("minDuration", minDuration)
u.RawQuery = query.Encode()
}
resp, code, reqError := makeRequest(client, u.String(), nil)
if reqError != nil {
log.Errorf("Tempo API query error: %s [code: %d, URL: %v]", reqError, code, u)
return &model.TracingResponse{}, reqError
}
if code != 200 {
errorMsg := fmt.Sprintf("Tempo API query error: %s [code: %d, URL: %v]", resp, code, u)
log.Errorf(errorMsg)
return &model.TracingResponse{}, errors.New(errorMsg)
}
limit, err := strconv.Atoi(u.Query().Get("limit"))
if err != nil {
limit = 0
}
response, _ := unmarshal(resp, u)
return oc.transformTrace(response, error, limit)
}
// transformTrace processes every trace ID and make a request to get all the spans for that trace
func (oc OtelHTTPClient) transformTrace(traces *otel.Traces, error string, limit int) (*model.TracingResponse, error) {
var response model.TracingResponse
serviceName := ""
if traces != nil {
for i, trace := range traces.Traces {
if limit != 0 && i >= limit {
break
}
serviceName = getServiceName(trace.SpanSet.Spans[0].Attributes)
if error == "true" {
if !hasErrors(trace) {
continue
}
}
batchTrace, err := convertBatchTrace(trace, serviceName)
if err != nil {
log.Errorf("Error getting trace detail for %s: %s", trace.TraceID, err.Error())
} else {
response.Data = append(response.Data, batchTrace)
}
}
}
response.TracingServiceName = serviceName
return &response, nil
}
func unmarshal(r []byte, u *url.URL) (*otel.Traces, error) {
var response otel.Traces
if errMarshal := json.Unmarshal(r, &response); errMarshal != nil {
log.Errorf("Error unmarshalling Tempo API response: %s [URL: %v]", errMarshal, u)
return nil, errMarshal
}
return &response, nil
}
func unmarshalSingleTrace(r []byte, u *url.URL) (*otelModels.Data, error) {
var response otelModels.Data
if errMarshal := json.Unmarshal(r, &response); errMarshal != nil {
log.Errorf("Error unmarshalling Tempo API Single trace response: %s [URL: %v]", errMarshal, u)
return nil, errMarshal
}
return &response, nil
}
// convertBatchTrace Convert a trace returned by TraceQL query into a jaeger Trace
func convertBatchTrace(trace otel.Trace, serviceName string) (jaegerModels.Trace, error) {
var jaegerModel jaegerModels.Trace
jaegerModel.TraceID = converter.ConvertId(trace.TraceID)
for _, span := range trace.SpanSet.Spans {
jaegerModel.Spans = append(jaegerModel.Spans, converter.ConvertSpanSet(span, serviceName, trace.TraceID, trace.RootTraceName)...)
}
jaegerModel.Matched = trace.SpanSet.Matched
jaegerModel.Processes = map[jaegerModels.ProcessID]jaegerModels.Process{}
jaegerModel.Warnings = []string{}
return jaegerModel, nil
}
// convertSingleTrace Convert a single trace returned by the TraceQL search endpoint
func convertSingleTrace(traces *otelModels.Data, id string) (*model.TracingResponse, error) {
var response model.TracingResponse
var jaegerModel jaegerModels.Trace
tracingServiceName := ""
jaegerModel.TraceID = converter.ConvertId(id)
if traces != nil {
tracingServiceName = getServiceName(traces.Batches[0].Resource.Attributes)
for _, batch := range traces.Batches {
serviceName := getServiceName(batch.Resource.Attributes)
jaegerModel.Spans = append(jaegerModel.Spans, converter.ConvertSpans(batch.ScopeSpans[0].Spans, serviceName, id)...)
}
jaegerModel.Matched = len(jaegerModel.Spans)
jaegerModel.Processes = map[jaegerModels.ProcessID]jaegerModels.Process{}
jaegerModel.Warnings = []string{}
}
response.Data = append(response.Data, jaegerModel)
response.TracingServiceName = tracingServiceName
return &response, nil
}
// prepareTraceQL returns a query in TraceQL format
func (oc OtelHTTPClient) prepareTraceQL(u *url.URL, tracingServiceName string, query models.TracingQuery) {
q := url.Values{}
q.Set("start", fmt.Sprintf("%d", query.Start.Unix()))
q.Set("end", fmt.Sprintf("%d", query.End.Unix()))
queryPart := TraceQL{operator1: ".service.name", operand: EQUAL, operator2: tracingServiceName}
if len(query.Tags) > 0 {
for k, v := range query.Tags {
if k != "cluster" && oc.ClusterTag {
tag := TraceQL{operator1: "." + k, operand: EQUAL, operator2: v}
queryPart = TraceQL{operator1: queryPart, operand: AND, operator2: tag}
}
}
}
selects := []string{"status", ".service_name", ".node_id", ".component", ".upstream_cluster", ".http.method", ".response_flags", "resource.hostname"}
trace := TraceQL{operator1: Subquery{queryPart}, operand: AND, operator2: Subquery{}}
queryQL := fmt.Sprintf("%s| %s", printOperator(trace), printSelect(selects))
q.Set("q", queryQL)
if query.MinDuration > 0 {
q.Set("minDuration", fmt.Sprintf("%dms", query.MinDuration.Milliseconds()))
}
// By default, the number of spans returned is 3. All are needed to calculate avg and heatmap
q.Set("spss", "10")
if query.Limit > 0 {
q.Set("limit", strconv.Itoa(query.Limit))
}
u.RawQuery = q.Encode()
log.Debugf("Prepared Tempo API query: %v", u)
}
func makeRequest(client http.Client, endpoint string, body io.Reader) (response []byte, status int, err error) {
response = nil
status = 0
req, err := http.NewRequest(http.MethodGet, endpoint, body)
if err != nil {
return
}
req.Header.Add("Accept", "application/json")
req.Header.Add("Content-Type", "application/json")
resp, err := client.Do(req)
if err != nil {
return
}
defer resp.Body.Close()
response, err = io.ReadAll(resp.Body)
status = resp.StatusCode
return
}
func hasErrors(trace otel.Trace) bool {
for _, span := range trace.SpanSet.Spans {
for _, atb := range span.Attributes {
if atb.Key == "status" && atb.Value.StringValue == "error" {
return true
}
}
if span.Status.Code == "STATUS_CODE_ERROR" {
return true
}
}
return false
}
func getServiceName(attributes []otelModels.Attribute) string {
for _, attb := range attributes {
if attb.Key == "service.name" {
return attb.Value.StringValue
}
}
return ""
}