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

Separate query-service functionality from http handler #1312

Merged
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
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
42 changes: 6 additions & 36 deletions cmd/all-in-one/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,14 +52,14 @@ import (
"github.com/jaegertracing/jaeger/cmd/env"
"github.com/jaegertracing/jaeger/cmd/flags"
queryApp "github.com/jaegertracing/jaeger/cmd/query/app"
"github.com/jaegertracing/jaeger/cmd/query/app/querysvc"
"github.com/jaegertracing/jaeger/pkg/config"
"github.com/jaegertracing/jaeger/pkg/healthcheck"
pMetrics "github.com/jaegertracing/jaeger/pkg/metrics"
"github.com/jaegertracing/jaeger/pkg/recoveryhandler"
"github.com/jaegertracing/jaeger/pkg/version"
ss "github.com/jaegertracing/jaeger/plugin/sampling/strategystore"
"github.com/jaegertracing/jaeger/plugin/storage"
istorage "github.com/jaegertracing/jaeger/storage"
"github.com/jaegertracing/jaeger/storage/dependencystore"
"github.com/jaegertracing/jaeger/storage/spanstore"
storageMetrics "github.com/jaegertracing/jaeger/storage/spanstore/metrics"
Expand Down Expand Up @@ -142,7 +142,7 @@ func main() {

startAgent(aOpts, repOpts, tchannelRepOpts, grpcRepOpts, cOpts, logger, metricsFactory)
grpcServer := startCollector(cOpts, spanWriter, logger, metricsFactory, strategyStore, hc)
startQuery(qOpts, spanReader, dependencyReader, logger, rootMetricsFactory, metricsFactory, mBldr, hc, archiveOptions(storageFactory, logger))
startQuery(qOpts, spanReader, dependencyReader, logger, rootMetricsFactory, metricsFactory, mBldr, hc, queryApp.ArchiveOptions(storageFactory, logger))
hc.Ready()
<-signalsChannel
logger.Info("Shutting down")
Expand Down Expand Up @@ -341,7 +341,7 @@ func startQuery(
baseFactory metrics.Factory,
metricsBuilder *pMetrics.Builder,
hc *healthcheck.HealthCheck,
handlerOpts []queryApp.HandlerOption,
queryOpts querysvc.QueryServiceOptions,
) {
tracer, closer, err := jaegerClientConfig.Configuration{
Sampler: &jaegerClientConfig.SamplerConfig{
Expand All @@ -361,10 +361,10 @@ func startQuery(

spanReader = storageMetrics.NewReadMetricsDecorator(spanReader, baseFactory.Namespace(metrics.NSOptions{Name: "query", Tags: nil}))

handlerOpts = append(handlerOpts, queryApp.HandlerOptions.Logger(logger), queryApp.HandlerOptions.Tracer(tracer))
qs := querysvc.NewQueryService(spanReader, depReader, queryOpts)
handlerOpts := []queryApp.HandlerOption{queryApp.HandlerOptions.Logger(logger), queryApp.HandlerOptions.Tracer(tracer)}
apiHandler := queryApp.NewAPIHandler(
spanReader,
depReader,
qs,
handlerOpts...)

r := mux.NewRouter()
Expand Down Expand Up @@ -405,33 +405,3 @@ func initSamplingStrategyStore(
}
return strategyStore
}

func archiveOptions(storageFactory istorage.Factory, logger *zap.Logger) []queryApp.HandlerOption {
archiveFactory, ok := storageFactory.(istorage.ArchiveFactory)
if !ok {
logger.Info("Archive storage not supported by the factory")
return nil
}
reader, err := archiveFactory.CreateArchiveSpanReader()
if err == istorage.ErrArchiveStorageNotConfigured || err == istorage.ErrArchiveStorageNotSupported {
logger.Info("Archive storage not created", zap.String("reason", err.Error()))
return nil
}
if err != nil {
logger.Error("Cannot init archive storage reader", zap.Error(err))
return nil
}
writer, err := archiveFactory.CreateArchiveSpanWriter()
if err == istorage.ErrArchiveStorageNotConfigured || err == istorage.ErrArchiveStorageNotSupported {
logger.Info("Archive storage not created", zap.String("reason", err.Error()))
return nil
}
if err != nil {
logger.Error("Cannot init archive storage writer", zap.Error(err))
return nil
}
return []queryApp.HandlerOption{
queryApp.HandlerOptions.ArchiveSpanReader(reader),
queryApp.HandlerOptions.ArchiveSpanWriter(writer),
}
}
11 changes: 6 additions & 5 deletions cmd/query/app/handler_archive_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"

"github.com/jaegertracing/jaeger/cmd/query/app/querysvc"
"github.com/jaegertracing/jaeger/model"
"github.com/jaegertracing/jaeger/storage/spanstore"
spanstoremocks "github.com/jaegertracing/jaeger/storage/spanstore/mocks"
Expand All @@ -42,7 +43,7 @@ func TestGetArchivedTrace_NotFound(t *testing.T) {
assert.EqualError(t, err,
`404 error from server: {"data":null,"total":0,"limit":0,"offset":0,"errors":[{"code":404,"msg":"trace not found"}]}`+"\n",
)
}, HandlerOptions.ArchiveSpanReader(archiveReader)) // nil is ok
}, querysvc.QueryServiceOptions{ArchiveSpanReader: archiveReader}) // nil is ok
})
}
}
Expand All @@ -62,15 +63,15 @@ func TestGetArchivedTraceSuccess(t *testing.T) {
assert.Len(t, response.Errors, 0)
assert.Len(t, response.Traces, 1)
assert.Equal(t, traceID.String(), string(response.Traces[0].TraceID))
}, HandlerOptions.ArchiveSpanReader(mockReader))
}, querysvc.QueryServiceOptions{ArchiveSpanReader: mockReader})
}

func TestArchiveTrace_NoStorage(t *testing.T) {
withTestServer(t, func(ts *testServer) {
var response structuredResponse
err := postJSON(ts.server.URL+"/api/archive/"+mockTraceID.String(), []string{}, &response)
assert.EqualError(t, err, `500 error from server: {"data":null,"total":0,"limit":0,"offset":0,"errors":[{"code":500,"msg":"archive span storage was not configured"}]}`+"\n")
})
}, querysvc.QueryServiceOptions{})
}

func TestArchiveTrace_Success(t *testing.T) {
Expand All @@ -83,7 +84,7 @@ func TestArchiveTrace_Success(t *testing.T) {
var response structuredResponse
err := postJSON(ts.server.URL+"/api/archive/"+mockTraceID.String(), []string{}, &response)
assert.NoError(t, err)
}, HandlerOptions.ArchiveSpanWriter(mockWriter))
}, querysvc.QueryServiceOptions{ArchiveSpanWriter: mockWriter})
}

func TestArchiveTrace_WriteErrors(t *testing.T) {
Expand All @@ -96,5 +97,5 @@ func TestArchiveTrace_WriteErrors(t *testing.T) {
var response structuredResponse
err := postJSON(ts.server.URL+"/api/archive/"+mockTraceID.String(), []string{}, &response)
assert.EqualError(t, err, `500 error from server: {"data":null,"total":0,"limit":0,"offset":0,"errors":[{"code":500,"msg":"[cannot save, cannot save]"}]}`+"\n")
}, HandlerOptions.ArchiveSpanWriter(mockWriter))
}, querysvc.QueryServiceOptions{ArchiveSpanWriter: mockWriter})
}
24 changes: 0 additions & 24 deletions cmd/query/app/handler_options.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,6 @@ import (

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

"github.com/jaegertracing/jaeger/model/adjuster"
"github.com/jaegertracing/jaeger/storage/spanstore"
)

// HandlerOption is a function that sets some option on the APIHandler
Expand All @@ -40,13 +37,6 @@ func (handlerOptions) Logger(logger *zap.Logger) HandlerOption {
}
}

// Adjusters creates a HandlerOption that initializes the sequence of Adjusters on the APIHandler,
func (handlerOptions) Adjusters(adjusters ...adjuster.Adjuster) HandlerOption {
return func(apiHandler *APIHandler) {
apiHandler.adjuster = adjuster.Sequence(adjusters...)
}
}

// BasePath creates a HandlerOption that initializes the base path for all HTTP routes
func (handlerOptions) BasePath(prefix string) HandlerOption {
return func(apiHandler *APIHandler) {
Expand All @@ -68,20 +58,6 @@ func (handlerOptions) QueryLookbackDuration(queryLookbackDuration time.Duration)
}
}

// ArchiveSpanReader creates a HandlerOption that initializes lookback duration
func (handlerOptions) ArchiveSpanReader(reader spanstore.Reader) HandlerOption {
return func(apiHandler *APIHandler) {
apiHandler.archiveSpanReader = reader
}
}

// ArchiveSpanWriter creates a HandlerOption that initializes lookback duration
func (handlerOptions) ArchiveSpanWriter(writer spanstore.Writer) HandlerOption {
return func(apiHandler *APIHandler) {
apiHandler.archiveSpanWriter = writer
}
}

// Tracer creates a HandlerOption that initializes OpenTracing tracer
func (handlerOptions) Tracer(tracer opentracing.Tracer) HandlerOption {
return func(apiHandler *APIHandler) {
Expand Down
Loading