Skip to content

Commit

Permalink
Merge branch 'master' into grpc-withresolver
Browse files Browse the repository at this point in the history
  • Loading branch information
yurishkuro authored Jun 11, 2021
2 parents 1dfa582 + 3f96a96 commit 08b590e
Show file tree
Hide file tree
Showing 5 changed files with 384 additions and 31 deletions.
71 changes: 71 additions & 0 deletions cmd/query/app/querysvc/metrics_query_service.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
// Copyright (c) 2021 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 querysvc

import (
"context"
"errors"
"time"

"github.com/jaegertracing/jaeger/proto-gen/api_v2/metrics"
"github.com/jaegertracing/jaeger/storage/metricsstore"
)

// MetricsQueryService contains the underlying reader required for querying the metrics store.
type MetricsQueryService struct {
metricsReader metricsstore.Reader
}

var errNilReader = errors.New("no reader defined for MetricsQueryService")

// 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,
}
}

// 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
}
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
}
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
}
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
}
return mqs.metricsReader.GetMinStepDuration(ctx, params)
}
130 changes: 130 additions & 0 deletions cmd/query/app/querysvc/metrics_query_service_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
// Copyright (c) 2021 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 querysvc

import (
"context"
"testing"
"time"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"

protometrics "github.com/jaegertracing/jaeger/proto-gen/api_v2/metrics"
"github.com/jaegertracing/jaeger/storage/metricsstore"
metricsmocks "github.com/jaegertracing/jaeger/storage/metricsstore/mocks"
)

type testMetricsQueryService struct {
queryService *MetricsQueryService
metricsReader *metricsmocks.Reader
}

func initializeTestMetricsQueryService() *testMetricsQueryService {
metricsReader := &metricsmocks.Reader{}

tqs := testMetricsQueryService{
metricsReader: metricsReader,
}

tqs.queryService = NewMetricsQueryService(metricsReader)
return &tqs
}

// Test QueryService.GetLatencies()
func TestGetLatencies(t *testing.T) {
tqs := initializeTestMetricsQueryService()
expectedLatencies := &protometrics.MetricFamily{
Name: "latencies",
Metrics: []*protometrics.Metric{},
}
qParams := &metricsstore.LatenciesQueryParameters{}
tqs.metricsReader.On("GetLatencies", mock.Anything, qParams).Return(expectedLatencies, nil).Times(1)

actualLatencies, err := tqs.queryService.GetLatencies(context.Background(), qParams)
assert.NoError(t, err)
assert.Equal(t, expectedLatencies, actualLatencies)
}

func TestGetLatenciesNilReader(t *testing.T) {
qs := NewMetricsQueryService(nil)
qParams := &metricsstore.LatenciesQueryParameters{}
r, err := qs.GetLatencies(context.Background(), qParams)
assert.Zero(t, r)
assert.EqualError(t, err, errNilReader.Error())
}

// Test QueryService.GetCallRates()
func TestGetCallRates(t *testing.T) {
tqs := initializeTestMetricsQueryService()
expectedCallRates := &protometrics.MetricFamily{
Name: "call rates",
Metrics: []*protometrics.Metric{},
}
qParams := &metricsstore.CallRateQueryParameters{}
tqs.metricsReader.On("GetCallRates", mock.Anything, qParams).Return(expectedCallRates, nil).Times(1)

actualCallRates, err := tqs.queryService.GetCallRates(context.Background(), qParams)
assert.NoError(t, err)
assert.Equal(t, expectedCallRates, actualCallRates)
}

func TestGetCallRatesNilReader(t *testing.T) {
qs := NewMetricsQueryService(nil)
qParams := &metricsstore.CallRateQueryParameters{}
r, err := qs.GetCallRates(context.Background(), qParams)
assert.Zero(t, r)
assert.EqualError(t, err, errNilReader.Error())
}

// Test QueryService.GetErrorRates()
func TestGetErrorRates(t *testing.T) {
tqs := initializeTestMetricsQueryService()
expectedErrorRates := &protometrics.MetricFamily{}
qParams := &metricsstore.ErrorRateQueryParameters{}
tqs.metricsReader.On("GetErrorRates", mock.Anything, qParams).Return(expectedErrorRates, nil).Times(1)

actualErrorRates, err := tqs.queryService.GetErrorRates(context.Background(), qParams)
assert.NoError(t, err)
assert.Equal(t, expectedErrorRates, actualErrorRates)
}

func TestGetErrorRatesNilReader(t *testing.T) {
qs := NewMetricsQueryService(nil)
qParams := &metricsstore.ErrorRateQueryParameters{}
r, err := qs.GetErrorRates(context.Background(), qParams)
assert.Zero(t, r)
assert.EqualError(t, err, errNilReader.Error())
}

// Test QueryService.GetMinStepDurations()
func TestGetMinStepDurations(t *testing.T) {
tqs := initializeTestMetricsQueryService()
expectedMinStep := time.Second
qParams := &metricsstore.MinStepDurationQueryParameters{}
tqs.metricsReader.On("GetMinStepDuration", mock.Anything, qParams).Return(expectedMinStep, nil).Times(1)

actualMinStep, err := tqs.queryService.GetMinStepDuration(context.Background(), qParams)
assert.NoError(t, err)
assert.Equal(t, expectedMinStep, actualMinStep)
}

func TestGetMinStepDurationsNilReader(t *testing.T) {
qs := NewMetricsQueryService(nil)
qParams := &metricsstore.MinStepDurationQueryParameters{}
r, err := qs.GetMinStepDuration(context.Background(), qParams)
assert.Zero(t, r)
assert.EqualError(t, err, errNilReader.Error())
}
10 changes: 5 additions & 5 deletions cmd/query/app/querysvc/query_service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,23 +71,23 @@ type testQueryService struct {
type testOption func(*testQueryService, *QueryServiceOptions)

func withArchiveSpanReader() testOption {
return func(mocks *testQueryService, options *QueryServiceOptions) {
return func(tqs *testQueryService, options *QueryServiceOptions) {
r := &spanstoremocks.Reader{}
mocks.archiveSpanReader = r
tqs.archiveSpanReader = r
options.ArchiveSpanReader = r
}
}

func withArchiveSpanWriter() testOption {
return func(mocks *testQueryService, options *QueryServiceOptions) {
return func(tqs *testQueryService, options *QueryServiceOptions) {
r := &spanstoremocks.Writer{}
mocks.archiveSpanWriter = r
tqs.archiveSpanWriter = r
options.ArchiveSpanWriter = r
}
}

func withAdjuster() testOption {
return func(mocks *testQueryService, options *QueryServiceOptions) {
return func(tqs *testQueryService, options *QueryServiceOptions) {
options.Adjuster = adjuster.Func(func(trace *model.Trace) (*model.Trace, error) {
return trace, errAdjustment
})
Expand Down
10 changes: 4 additions & 6 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,10 @@ require (
github.com/gocql/gocql v0.0.0-20200228163523-cd4b606dd2fb
github.com/gogo/googleapis v1.4.1
github.com/gogo/protobuf v1.3.2
github.com/golang/mock v1.4.3 // indirect
github.com/golang/protobuf v1.5.2
github.com/gorilla/handlers v1.5.1
github.com/gorilla/mux v1.8.0
github.com/grpc-ecosystem/go-grpc-middleware v1.2.2
github.com/grpc-ecosystem/go-grpc-middleware v1.3.0
github.com/grpc-ecosystem/grpc-opentracing v0.0.0-20180507213350-8e809c8a8645
github.com/hashicorp/go-hclog v0.16.1
github.com/hashicorp/go-plugin v1.4.2
Expand All @@ -43,9 +42,8 @@ require (
github.com/opentracing-contrib/go-grpc v0.0.0-20191001143057-db30781987df
github.com/opentracing-contrib/go-stdlib v0.0.0-20190519235532-cf7a6c988dc9
github.com/opentracing/opentracing-go v1.2.0
github.com/prometheus/client_golang v1.5.1
github.com/prometheus/common v0.10.0
github.com/prometheus/procfs v0.1.3 // indirect
github.com/prometheus/client_golang v1.11.0
github.com/prometheus/common v0.29.0
github.com/rs/cors v1.7.0
github.com/securego/gosec v0.0.0-20200203094520-d13bb6d2420c
github.com/soheilhy/cmux v0.1.5
Expand All @@ -62,7 +60,7 @@ require (
github.com/wadey/gocovmerge v0.0.0-20160331181800-b5bfa59ec0ad
github.com/xdg-go/scram v1.0.2
go.mongodb.org/mongo-driver v1.5.2 // indirect
go.uber.org/atomic v1.7.0
go.uber.org/atomic v1.8.0
go.uber.org/automaxprocs v1.4.0
go.uber.org/zap v1.17.0
golang.org/x/lint v0.0.0-20210508222113-6edffad5e616
Expand Down
Loading

0 comments on commit 08b590e

Please sign in to comment.