Skip to content

Commit

Permalink
DRY code, rename public methods to be more consistent
Browse files Browse the repository at this point in the history
Signed-off-by: Yuri Shkuro <[email protected]>
  • Loading branch information
yurishkuro committed Jul 20, 2024
1 parent 2034502 commit 9d2f9b2
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ type mockStorageExt struct {
metricsFactory *factoryMocks.MetricsFactory
}

var _ jaegerstorage.Extension = (*mockStorageExt)(nil)

func (*mockStorageExt) Start(context.Context, component.Host) error {
panic("not implemented")
}
Expand All @@ -52,14 +54,14 @@ func (*mockStorageExt) Shutdown(context.Context) error {
panic("not implemented")
}

func (m *mockStorageExt) Factory(name string) (storage.Factory, bool) {
func (m *mockStorageExt) TraceStorageFactory(name string) (storage.Factory, bool) {
if m.name == name {
return m.factory, true
}
return nil, false
}

func (m *mockStorageExt) MetricsFactory(name string) (storage.MetricsFactory, bool) {
func (m *mockStorageExt) MetricStorageFactory(name string) (storage.MetricsFactory, bool) {
if m.name == name {
return m.metricsFactory, true
}
Expand Down
4 changes: 2 additions & 2 deletions cmd/jaeger/internal/extension/jaegerquery/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,14 +87,14 @@ type fakeStorageExt struct{}

var _ jaegerstorage.Extension = (*fakeStorageExt)(nil)

func (fakeStorageExt) Factory(name string) (storage.Factory, bool) {
func (fakeStorageExt) TraceStorageFactory(name string) (storage.Factory, bool) {
if name == "need-factory-error" {
return nil, false
}
return fakeFactory{name: name}, true
}

func (fakeStorageExt) MetricsFactory(name string) (storage.MetricsFactory, bool) {
func (fakeStorageExt) MetricStorageFactory(name string) (storage.MetricsFactory, bool) {
if name == "need-factory-error" {
return nil, false
}
Expand Down
64 changes: 34 additions & 30 deletions cmd/jaeger/internal/extension/jaegerstorage/extension.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ var _ Extension = (*storageExt)(nil)

type Extension interface {
extension.Extension
Factory(name string) (storage.Factory, bool)
MetricsFactory(name string) (storage.MetricsFactory, bool)
TraceStorageFactory(name string) (storage.Factory, bool)
MetricStorageFactory(name string) (storage.MetricsFactory, bool)
}

type storageExt struct {
Expand All @@ -41,20 +41,11 @@ type storageExt struct {

// GetStorageFactory locates the extension in Host and retrieves a storage factory from it with the given name.
func GetStorageFactory(name string, host component.Host) (storage.Factory, error) {
var comp component.Component
for id, ext := range host.GetExtensions() {
if id.Type() == componentType {
comp = ext
break
}
}
if comp == nil {
return nil, fmt.Errorf(
"cannot find extension '%s' (make sure it's defined earlier in the config)",
componentType,
)
ext, err := findExtension(host)
if err != nil {
return nil, err
}
f, ok := comp.(Extension).Factory(name)
f, ok := ext.TraceStorageFactory(name)
if !ok {
return nil, fmt.Errorf(
"cannot find definition of storage '%s' in the configuration for extension '%s'",
Expand All @@ -66,20 +57,11 @@ func GetStorageFactory(name string, host component.Host) (storage.Factory, error

// GetMetricsFactory locates the extension in Host and retrieves a metrics factory from it with the given name.
func GetMetricsFactory(name string, host component.Host) (storage.MetricsFactory, error) {
var comp component.Component
for id, ext := range host.GetExtensions() {
if id.Type() == componentType {
comp = ext
break
}
}
if comp == nil {
return nil, fmt.Errorf(
"cannot find extension '%s' (make sure it's defined earlier in the config)",
componentType,
)
ext, err := findExtension(host)
if err != nil {
return nil, err

Check warning on line 62 in cmd/jaeger/internal/extension/jaegerstorage/extension.go

View check run for this annotation

Codecov / codecov/patch

cmd/jaeger/internal/extension/jaegerstorage/extension.go#L59-L62

Added lines #L59 - L62 were not covered by tests
}
mf, ok := comp.(Extension).MetricsFactory(name)
mf, ok := ext.MetricStorageFactory(name)

Check warning on line 64 in cmd/jaeger/internal/extension/jaegerstorage/extension.go

View check run for this annotation

Codecov / codecov/patch

cmd/jaeger/internal/extension/jaegerstorage/extension.go#L64

Added line #L64 was not covered by tests
if !ok {
return nil, fmt.Errorf(
"cannot find metric storage '%s' declared by '%s' extension",

Check warning on line 67 in cmd/jaeger/internal/extension/jaegerstorage/extension.go

View check run for this annotation

Codecov / codecov/patch

cmd/jaeger/internal/extension/jaegerstorage/extension.go#L67

Added line #L67 was not covered by tests
Expand All @@ -98,6 +80,28 @@ func GetStorageFactoryV2(name string, host component.Host) (spanstore.Factory, e
return factoryadapter.NewFactory(f), nil
}

func findExtension(host component.Host) (Extension, error) {
var id component.ID
var comp component.Component
for i, ext := range host.GetExtensions() {
if i.Type() == componentType {
id, comp = i, ext
break
}
}
if comp == nil {
return nil, fmt.Errorf(
"cannot find extension '%s' (make sure it's defined earlier in the config)",
componentType,
)
}
ext, ok := comp.(Extension)
if !ok {
return nil, fmt.Errorf("extension '%s' is not of expected type '%s'", id, componentType)

Check warning on line 100 in cmd/jaeger/internal/extension/jaegerstorage/extension.go

View check run for this annotation

Codecov / codecov/patch

cmd/jaeger/internal/extension/jaegerstorage/extension.go#L100

Added line #L100 was not covered by tests
}
return ext, nil
}

func newStorageExt(config *Config, telset component.TelemetrySettings) *storageExt {
return &storageExt{
config: config,
Expand Down Expand Up @@ -163,12 +167,12 @@ func (s *storageExt) Shutdown(context.Context) error {
return errors.Join(errs...)
}

func (s *storageExt) Factory(name string) (storage.Factory, bool) {
func (s *storageExt) TraceStorageFactory(name string) (storage.Factory, bool) {
f, ok := s.factories[name]
return f, ok
}

func (s *storageExt) MetricsFactory(name string) (storage.MetricsFactory, bool) {
func (s *storageExt) MetricStorageFactory(name string) (storage.MetricsFactory, bool) {
mf, ok := s.metricsFactories[name]
return mf, ok

Check warning on line 177 in cmd/jaeger/internal/extension/jaegerstorage/extension.go

View check run for this annotation

Codecov / codecov/patch

cmd/jaeger/internal/extension/jaegerstorage/extension.go#L175-L177

Added lines #L175 - L177 were not covered by tests
}
Original file line number Diff line number Diff line change
Expand Up @@ -50,14 +50,14 @@ func (*mockStorageExt) Shutdown(context.Context) error {
panic("not implemented")
}

func (m *mockStorageExt) Factory(name string) (storage.Factory, bool) {
func (m *mockStorageExt) TraceStorageFactory(name string) (storage.Factory, bool) {
if m.name == name {
return m.factory, true
}
return nil, false
}

func (m *mockStorageExt) MetricsFactory(name string) (storage.MetricsFactory, bool) {
func (m *mockStorageExt) MetricStorageFactory(name string) (storage.MetricsFactory, bool) {
if m.name == name {
return m.metricsFactory, true
}
Expand Down

0 comments on commit 9d2f9b2

Please sign in to comment.