|
| 1 | +// Copyright (c) 2020 The Jaeger Authors. |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package query |
| 16 | + |
| 17 | +import ( |
| 18 | + "context" |
| 19 | + "fmt" |
| 20 | + "io" |
| 21 | + "time" |
| 22 | + |
| 23 | + "google.golang.org/grpc" |
| 24 | + "google.golang.org/grpc/status" |
| 25 | + |
| 26 | + "github.com/jaegertracing/jaeger/model" |
| 27 | + "github.com/jaegertracing/jaeger/proto-gen/api_v2" |
| 28 | + "github.com/jaegertracing/jaeger/storage/spanstore" |
| 29 | +) |
| 30 | + |
| 31 | +// Query represents a jaeger-query's query for trace-id |
| 32 | +type Query struct { |
| 33 | + client api_v2.QueryServiceClient |
| 34 | + conn *grpc.ClientConn |
| 35 | +} |
| 36 | + |
| 37 | +// New creates a Query object |
| 38 | +func New(addr string) (*Query, error) { |
| 39 | + ctx, cancel := context.WithTimeout(context.Background(), 1*time.Second) |
| 40 | + defer cancel() |
| 41 | + |
| 42 | + conn, err := grpc.DialContext(ctx, addr, grpc.WithInsecure()) |
| 43 | + if err != nil { |
| 44 | + return nil, fmt.Errorf("failed to connect with the jaeger-query service: %w", err) |
| 45 | + } |
| 46 | + |
| 47 | + return &Query{ |
| 48 | + client: api_v2.NewQueryServiceClient(conn), |
| 49 | + conn: conn, |
| 50 | + }, nil |
| 51 | +} |
| 52 | + |
| 53 | +// unwrapNotFoundErr is a conversion function |
| 54 | +func unwrapNotFoundErr(err error) error { |
| 55 | + if s, _ := status.FromError(err); s != nil { |
| 56 | + if s.Message() == spanstore.ErrTraceNotFound.Error() { |
| 57 | + return spanstore.ErrTraceNotFound |
| 58 | + } |
| 59 | + } |
| 60 | + return err |
| 61 | +} |
| 62 | + |
| 63 | +// QueryTrace queries for a trace and returns all spans inside it |
| 64 | +func (q *Query) QueryTrace(traceID string) ([]model.Span, error) { |
| 65 | + mTraceID, err := model.TraceIDFromString(traceID) |
| 66 | + if err != nil { |
| 67 | + return nil, fmt.Errorf("failed to convert the provided trace id: %w", err) |
| 68 | + } |
| 69 | + |
| 70 | + stream, err := q.client.GetTrace(context.Background(), &api_v2.GetTraceRequest{ |
| 71 | + TraceID: mTraceID, |
| 72 | + }) |
| 73 | + if err != nil { |
| 74 | + return nil, unwrapNotFoundErr(err) |
| 75 | + } |
| 76 | + |
| 77 | + var spans []model.Span |
| 78 | + for received, err := stream.Recv(); err != io.EOF; received, err = stream.Recv() { |
| 79 | + if err != nil { |
| 80 | + return nil, unwrapNotFoundErr(err) |
| 81 | + } |
| 82 | + for i := range received.Spans { |
| 83 | + spans = append(spans, received.Spans[i]) |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + return spans, nil |
| 88 | +} |
0 commit comments