-
Notifications
You must be signed in to change notification settings - Fork 2.5k
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
Hook up MetricsQueryService to main funcs #3079
Changes from 1 commit
29cc2da
b68914e
3756c35
188c1a1
f562a1f
99ea523
53a486b
c81a23c
2eaa9c3
64fa935
a8a420f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -28,44 +28,47 @@ type MetricsQueryService struct { | |
metricsReader metricsstore.Reader | ||
} | ||
|
||
var errNilReader = errors.New("no reader defined for MetricsQueryService") | ||
var errMetricsQueryDisabled = errors.New("metrics querying is currently disabled") | ||
|
||
// NewMetricsQueryService returns a new MetricsQueryService. | ||
// A nil reader will result in a nil MetricsQueryService being returned. | ||
func NewMetricsQueryService(reader metricsstore.Reader) *MetricsQueryService { | ||
return &MetricsQueryService{ | ||
metricsReader: reader, | ||
} | ||
} | ||
|
||
func (mqs MetricsQueryService) Enabled() bool { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hopefully I understood your suggestion correctly, @yurishkuro. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. no, what I meant was adding this:
and using it in RPC handlers. Basically, solve the problem with polymorphism, not with a bunch of nil checks all over the place. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. that's pretty cool :) although, unless if I'm mistaken, I see two minor issues:
what do you think? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. point (1) is probably not particularly important, what's the downside of parsing inputs? point (2) is valid if you want to differentiate real failure from not-implemented (grpc has UNIMPLEMENTED status code iirc). But the handler should look like this:
There will be only one place in the code, errWithStatusCode(), that needs to understand There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. and yes, you'd want to use There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
not a large downside, and the emphasis wasn't so much on the input parsing but on when the handler knows if the feature is enabled or disabled. I feel the intent is clearer when reading the handler code instead of needing to follow it to the point where the error is handled:
In any case, it's a small matter I think, and happy to go ahead with your suggestion. |
||
return mqs.metricsReader != nil | ||
} | ||
|
||
// GetLatencies is the queryService implementation of metricsstore.Reader. | ||
func (mqs MetricsQueryService) GetLatencies(ctx context.Context, params *metricsstore.LatenciesQueryParameters) (*metrics.MetricFamily, error) { | ||
if mqs.metricsReader == nil { | ||
return nil, errNilReader | ||
if !mqs.Enabled() { | ||
return nil, errMetricsQueryDisabled | ||
} | ||
return mqs.metricsReader.GetLatencies(ctx, params) | ||
} | ||
|
||
// GetCallRates is the queryService implementation of metricsstore.Reader. | ||
func (mqs MetricsQueryService) GetCallRates(ctx context.Context, params *metricsstore.CallRateQueryParameters) (*metrics.MetricFamily, error) { | ||
if mqs.metricsReader == nil { | ||
return nil, errNilReader | ||
if !mqs.Enabled() { | ||
return nil, errMetricsQueryDisabled | ||
} | ||
return mqs.metricsReader.GetCallRates(ctx, params) | ||
} | ||
|
||
// GetErrorRates is the queryService implementation of metricsstore.Reader. | ||
func (mqs MetricsQueryService) GetErrorRates(ctx context.Context, params *metricsstore.ErrorRateQueryParameters) (*metrics.MetricFamily, error) { | ||
if mqs.metricsReader == nil { | ||
return nil, errNilReader | ||
if !mqs.Enabled() { | ||
return nil, errMetricsQueryDisabled | ||
} | ||
return mqs.metricsReader.GetErrorRates(ctx, params) | ||
} | ||
|
||
// GetMinStepDuration is the queryService implementation of metricsstore.Reader. | ||
func (mqs MetricsQueryService) GetMinStepDuration(ctx context.Context, params *metricsstore.MinStepDurationQueryParameters) (time.Duration, error) { | ||
if mqs.metricsReader == nil { | ||
return 0, errNilReader | ||
if !mqs.Enabled() { | ||
return 0, errMetricsQueryDisabled | ||
} | ||
return mqs.metricsReader.GetMinStepDuration(ctx, params) | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -52,7 +52,7 @@ type Server struct { | |
} | ||
|
||
// NewServer creates and initializes Server | ||
func NewServer(logger *zap.Logger, querySvc *querysvc.QueryService, options *QueryOptions, tracer opentracing.Tracer) (*Server, error) { | ||
func NewServer(logger *zap.Logger, querySvc *querysvc.QueryService, metricsQuerySvc *querysvc.MetricsQueryService, options *QueryOptions, tracer opentracing.Tracer) (*Server, error) { | ||
|
||
_, httpPort, err := net.SplitHostPort(options.HTTPHostPort) | ||
if err != nil { | ||
|
@@ -67,12 +67,12 @@ func NewServer(logger *zap.Logger, querySvc *querysvc.QueryService, options *Que | |
return nil, errors.New("server with TLS enabled can not use same host ports for gRPC and HTTP. Use dedicated HTTP and gRPC host ports instead") | ||
} | ||
|
||
grpcServer, err := createGRPCServer(querySvc, options, logger, tracer) | ||
grpcServer, err := createGRPCServer(querySvc, metricsQuerySvc, options, logger, tracer) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
httpServer, err := createHTTPServer(querySvc, options, tracer, logger) | ||
httpServer, err := createHTTPServer(querySvc, metricsQuerySvc, options, tracer, logger) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
@@ -94,7 +94,7 @@ func (s Server) HealthCheckStatus() chan healthcheck.Status { | |
return s.unavailableChannel | ||
} | ||
|
||
func createGRPCServer(querySvc *querysvc.QueryService, options *QueryOptions, logger *zap.Logger, tracer opentracing.Tracer) (*grpc.Server, error) { | ||
func createGRPCServer(querySvc *querysvc.QueryService, metricsQuerySvc *querysvc.MetricsQueryService, options *QueryOptions, logger *zap.Logger, tracer opentracing.Tracer) (*grpc.Server, error) { | ||
var grpcOpts []grpc.ServerOption | ||
|
||
if options.TLSGRPC.Enabled { | ||
|
@@ -111,11 +111,15 @@ func createGRPCServer(querySvc *querysvc.QueryService, options *QueryOptions, lo | |
server := grpc.NewServer(grpcOpts...) | ||
|
||
handler := NewGRPCHandler(querySvc, logger, tracer) | ||
|
||
// TODO: Register MetricsQueryService | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Will be addressed in a follow-up PR implementing MetricsQuery support for the gRPC handler. |
||
api_v2.RegisterQueryServiceServer(server, handler) | ||
|
||
return server, nil | ||
} | ||
|
||
func createHTTPServer(querySvc *querysvc.QueryService, queryOpts *QueryOptions, tracer opentracing.Tracer, logger *zap.Logger) (*http.Server, error) { | ||
func createHTTPServer(querySvc *querysvc.QueryService, metricsQuerySvc *querysvc.MetricsQueryService, queryOpts *QueryOptions, tracer opentracing.Tracer, logger *zap.Logger) (*http.Server, error) { | ||
// TODO: Add HandlerOptions.MetricsQueryService | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Will be addressed in a follow-up PR implementing MetricsQuery support for the HTTP handler. |
||
apiHandlerOptions := []HandlerOption{ | ||
HandlerOptions.Logger(logger), | ||
HandlerOptions.Tracer(tracer), | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
are these checks still necessary? wouldn't you always get something back (perhaps disabled service)?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, it's still necessary otherwise:
Cannot initialize metrics store factory: unknown metrics type "". Valid types are [prometheus]
Do you think it's worth doing something similar with the factories? i.e. have
disabledMetricsReaderFactory
that simply returns anil
Reader? That way, these checks won't be necessary.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think you can extend the same approach - consider default metrics store type to be "none" and return a "disabled reader" in that case. Basically push this all the way down (you won't need "disabled service" in this case).