Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support gRPC for query service #1307

Merged
merged 33 commits into from
Apr 22, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
33 commits
Select commit Hold shift + click to select a range
ab4d728
Initial commit, grpc for query-service
Jan 29, 2019
650fc55
Re-evaluate fields in GRPCHandler
Jan 29, 2019
30033df
Use cmux, use stream in GetTraceResponse, regenerate *pb.go and swagg…
Feb 26, 2019
6467d44
Edit GetTrace HTTP verb
Feb 28, 2019
5a65f9f
Re-add query definitions in a new file
Mar 27, 2019
9dcfe08
Address comments, correct API endpoint for query grpc
Mar 29, 2019
114ddba
Add definitions for other query functions
Mar 31, 2019
c62ce29
Address comments, make API as close to plugin proto
Apr 2, 2019
39dc5d4
Fix api definitions
Apr 3, 2019
20f028e
Add dependency query utility
Apr 3, 2019
14645f9
Make fmt, add findtraces implementation
Apr 3, 2019
3bb4cb1
Correct rebase
Apr 3, 2019
2854624
Merge branch 'master' into query-service-grpc
Apr 4, 2019
b4f58cd
Use end_time instead of duration
Apr 4, 2019
472de89
dep ensure, fix build
Apr 4, 2019
e698953
Merge branch 'master' into query-service-grpc
Apr 4, 2019
4a85f6f
Addressed comments, fix proto tool versions
Apr 6, 2019
b03b497
Fix build
Apr 7, 2019
6ea3b82
Fix build
Apr 7, 2019
64b57da
Remove grpc-gateway code generation; use streaming
Apr 8, 2019
8a6f286
Implement streaming
Apr 8, 2019
63d5638
make fmt lint
Apr 8, 2019
8d2535d
Merge branch 'master' into query-service-grpc
yurishkuro Apr 8, 2019
289385b
dep update
Apr 8, 2019
86a771f
Start adding tests
Apr 9, 2019
86d770b
Add tests for GRPC handler
Apr 12, 2019
e7f0e0d
Fix grpc handler tests
Apr 13, 2019
612dc13
WIP fixing tests
Apr 14, 2019
9c3f1ee
Fix tests
Apr 15, 2019
c1d9abe
Fix tests
Apr 16, 2019
5a32ba0
Add test
Apr 16, 2019
b61fc18
Merge branch 'master' into query-service-grpc
Apr 16, 2019
8dfc90a
Merge branch 'master' into query-service-grpc
yurishkuro Apr 19, 2019
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 14 additions & 13 deletions Gopkg.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 5 additions & 2 deletions Gopkg.toml
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,10 @@ required = [
name = "github.com/grpc-ecosystem/go-grpc-middleware"
version = "1.0.0"

[[constaint]]
[[constraint]]
name = "github.com/soheilhy/cmux"
version = "0.1.4"

[[constraint]]
name = "github.com/rs/cors"
version = "1.3.0"

5 changes: 3 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -385,9 +385,10 @@ proto:
$(PROTOC) \
$(PROTO_INCLUDES) \
--gogo_out=plugins=grpc,$(PROTO_GOGO_MAPPINGS):$(PWD)/proto-gen/ \
--grpc-gateway_out=$(PROTO_GOGO_MAPPINGS):$(PWD)/proto-gen/ \
--swagger_out=allow_merge=true:$(PWD)/proto-gen/openapi/ \
model/proto/api_v2/*.proto
### grpc-gateway generates 'query.pb.gw.go' that does not respect (gogoproto.customname) = "TraceID"
### --grpc-gateway_out=$(PROTO_GOGO_MAPPINGS):$(PWD)/proto-gen/ \
### --swagger_out=allow_merge=true:$(PWD)/proto-gen/openapi/ \

$(PROTOC) \
$(PROTO_INCLUDES) \
Expand Down
153 changes: 153 additions & 0 deletions cmd/query/app/grpc_handler.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,153 @@
// Copyright (c) 2019 The Jaeger Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package app

import (
"context"

"github.com/opentracing/opentracing-go"
"go.uber.org/zap"

"github.com/jaegertracing/jaeger/cmd/query/app/querysvc"
"github.com/jaegertracing/jaeger/model"
"github.com/jaegertracing/jaeger/proto-gen/api_v2"
"github.com/jaegertracing/jaeger/storage/spanstore"
)

const maxSpanCountInChunk = 10

// GRPCHandler implements the GRPC endpoint of the query service.
type GRPCHandler struct {
queryService querysvc.QueryService
logger *zap.Logger
tracer opentracing.Tracer
}

// NewGRPCHandler returns a GRPCHandler
func NewGRPCHandler(queryService querysvc.QueryService, logger *zap.Logger, tracer opentracing.Tracer) *GRPCHandler {
gH := &GRPCHandler{
queryService: queryService,
logger: logger,
tracer: tracer,
}

return gH
}

// GetTrace is the GRPC handler to fetch traces based on trace-id.
func (g *GRPCHandler) GetTrace(r *api_v2.GetTraceRequest, stream api_v2.QueryService_GetTraceServer) error {
trace, err := g.queryService.GetTrace(stream.Context(), r.TraceID)
if err == spanstore.ErrTraceNotFound {
g.logger.Error("trace not found", zap.Error(err))
return err
}
if err != nil {
g.logger.Error("Could not fetch spans from backend", zap.Error(err))
return err
}
return g.sendSpanChunks(trace.Spans, stream.Send)
}

// ArchiveTrace is the GRPC handler to archive traces.
func (g *GRPCHandler) ArchiveTrace(ctx context.Context, r *api_v2.ArchiveTraceRequest) (*api_v2.ArchiveTraceResponse, error) {
err := g.queryService.ArchiveTrace(ctx, r.TraceID)
if err == spanstore.ErrTraceNotFound {
g.logger.Error("trace not found", zap.Error(err))
return nil, err
}
if err != nil {
g.logger.Error("Could not fetch spans from backend", zap.Error(err))
return nil, err
}

return &api_v2.ArchiveTraceResponse{}, nil
}

// FindTraces is the GRPC handler to fetch traces based on TraceQueryParameters.
func (g *GRPCHandler) FindTraces(r *api_v2.FindTracesRequest, stream api_v2.QueryService_FindTracesServer) error {
query := r.GetQuery()
queryParams := spanstore.TraceQueryParameters{
ServiceName: query.ServiceName,
OperationName: query.OperationName,
Tags: query.Tags,
StartTimeMin: query.StartTimeMin,
StartTimeMax: query.StartTimeMax,
DurationMin: query.DurationMin,
DurationMax: query.DurationMax,
NumTraces: int(query.SearchDepth),
}
traces, err := g.queryService.FindTraces(stream.Context(), &queryParams)
if err != nil {
g.logger.Error("Error fetching traces", zap.Error(err))
return err
}
for _, trace := range traces {
if err := g.sendSpanChunks(trace.Spans, stream.Send); err != nil {
return err
}
}
return nil
}

func (g *GRPCHandler) sendSpanChunks(spans []*model.Span, sendFn func(*api_v2.SpansResponseChunk) error) error {
chunk := make([]model.Span, 0, len(spans))
for i := 0; i < len(spans); i += maxSpanCountInChunk {
chunk = chunk[:0]
for j := i; j < len(spans) && j < i+maxSpanCountInChunk; j++ {
chunk = append(chunk, *spans[j])
}
if err := sendFn(&api_v2.SpansResponseChunk{Spans: chunk}); err != nil {
g.logger.Error("failed to send response to client", zap.Error(err))
return err
}
}
return nil
}

// GetServices is the GRPC handler to fetch services.
func (g *GRPCHandler) GetServices(ctx context.Context, r *api_v2.GetServicesRequest) (*api_v2.GetServicesResponse, error) {
services, err := g.queryService.GetServices(ctx)
if err != nil {
g.logger.Error("Error fetching services", zap.Error(err))
return nil, err
}

return &api_v2.GetServicesResponse{Services: services}, nil
}

// GetOperations is the GRPC handler to fetch operations.
func (g *GRPCHandler) GetOperations(ctx context.Context, r *api_v2.GetOperationsRequest) (*api_v2.GetOperationsResponse, error) {
service := r.Service
operations, err := g.queryService.GetOperations(ctx, service)
if err != nil {
g.logger.Error("Error fetching operations", zap.Error(err))
return nil, err
}

return &api_v2.GetOperationsResponse{Operations: operations}, nil
}

// GetDependencies is the GRPC handler to fetch dependencies.
func (g *GRPCHandler) GetDependencies(ctx context.Context, r *api_v2.GetDependenciesRequest) (*api_v2.GetDependenciesResponse, error) {
startTime := r.StartTime
endTime := r.EndTime
dependencies, err := g.queryService.GetDependencies(startTime, endTime.Sub(startTime))
if err != nil {
g.logger.Error("Error fetching dependencies", zap.Error(err))
return nil, err
}

return &api_v2.GetDependenciesResponse{Dependencies: dependencies}, nil
}
Loading