-
Notifications
You must be signed in to change notification settings - Fork 0
/
jaeger_client.go
62 lines (57 loc) · 1.56 KB
/
jaeger_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
package tracing
import (
"context"
"io"
"time"
"github.com/jaegertracing/jaeger/model"
proto "github.com/jaegertracing/jaeger/proto-gen/api_v2"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials/insecure"
"google.golang.org/grpc/keepalive"
)
// JaegerClient is a client for query Jaeger
type JaegerClient struct {
conn *grpc.ClientConn
queryClient proto.QueryServiceClient
}
// NewJaegerClient create a new JaegerClient
//
//goland:noinspection GoUnusedExportedFunction
func NewJaegerClient(jaegerQueryAddr string) (*JaegerClient, error) {
conn, err := grpc.DialContext(context.Background(), jaegerQueryAddr,
grpc.WithTransportCredentials(insecure.NewCredentials()),
grpc.WithBlock(),
grpc.WithKeepaliveParams(keepalive.ClientParameters{
Time: 10 * time.Second,
Timeout: 100 * time.Millisecond,
PermitWithoutStream: true}),
)
if err != nil {
return nil, err
}
queryClient := proto.NewQueryServiceClient(conn)
return &JaegerClient{
conn: conn,
queryClient: queryClient,
}, nil
}
// QuerySpans query spans from Jaeger
func (c *JaegerClient) QuerySpans(ctx context.Context, query *proto.TraceQueryParameters) ([]model.Span, error) {
findTraceClient, err := c.queryClient.FindTraces(ctx, &proto.FindTracesRequest{
Query: query,
})
if err != nil {
return nil, err
}
var spans []model.Span
for {
spanChunk, err := findTraceClient.Recv()
if err == io.EOF { // EOF if finish
break
} else if err != nil {
return nil, err
}
spans = append(spans, spanChunk.Spans...)
}
return spans, nil
}