forked from kiali/kiali
/
client.go
84 lines (74 loc) · 2.69 KB
/
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
package jaeger
import (
"errors"
"net/http"
"net/url"
"time"
"github.com/kiali/kiali/config"
"github.com/kiali/kiali/log"
"github.com/kiali/kiali/util/httputil"
)
// ClientInterface for mocks (only mocked function are necessary here)
type ClientInterface interface {
GetSpans(namespace, service, startMicros, endMicros string) ([]Span, error)
GetTraces(namespace string, service string, rawQuery string) (traces *JaegerResponse, err error)
GetTraceDetail(traceId string) (trace *JaegerResponse, err error)
GetErrorTraces(ns, srv string, duration time.Duration) (errorTraces int, err error)
}
// Client for Jaeger API.
// It hides the way we query Prometheus offering a layer with a high level defined API.
type Client struct {
ClientInterface
client http.Client
endpoint *url.URL
}
func NewClient(token string) (*Client, error) {
cfg := config.Get()
cfgTracing := cfg.ExternalServices.Tracing
if !cfgTracing.Enabled {
return nil, errors.New("jaeger is not available")
} else {
auth := cfgTracing.Auth
if auth.UseKialiToken {
auth.Token = token
}
u, errParse := url.Parse(cfgTracing.InClusterURL)
if !cfg.InCluster {
u, errParse = url.Parse(cfgTracing.URL)
}
if errParse != nil {
log.Errorf("Error parse Jaeger URL: %s", errParse)
return nil, errParse
}
timeout := time.Duration(5000 * time.Millisecond)
transport, err := httputil.AuthTransport(&auth, &http.Transport{})
if err != nil {
return nil, err
}
client := http.Client{Transport: transport, Timeout: timeout}
return &Client{client: client, endpoint: u}, nil
}
}
// GetSpans fetches Jaeger traces of a service and extract related spans
// Returns (spans, error)
func (in *Client) GetSpans(namespace, service, startMicros, endMicros string) ([]Span, error) {
return getSpans(in.client, in.endpoint, namespace, service, startMicros, endMicros)
}
// GetTraces Jaeger to fetch traces of a service
// requests for traces of a service
// Returns (traces, code, error)
func (in *Client) GetTraces(namespace string, service string, rawQuery string) (traces *JaegerResponse, err error) {
return getTraces(in.client, in.endpoint, namespace, service, rawQuery)
}
// GetTraceDetail jaeger to fetch a specific trace
// requests for a specific trace detail
// Returns (traces, code, error)
func (in *Client) GetTraceDetail(traceId string) (trace *JaegerResponse, err error) {
return getTraceDetail(in.client, in.endpoint, traceId)
}
// GetErrorTraces jaeger to fetch a traces of a specific service
// requests for errors traces
// Returns (errorTraces, error)
func (in *Client) GetErrorTraces(ns, srv string, duration time.Duration) (errorTraces int, err error) {
return getErrorTraces(in.client, in.endpoint, ns, srv, duration)
}