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

[storage] Remove usages of jaegerstorage.GetStorageFactory #6624

Closed
Closed
Show file tree
Hide file tree
Changes from all 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
16 changes: 10 additions & 6 deletions cmd/jaeger/internal/exporters/storageexporter/exporter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ import (
"github.com/jaegertracing/jaeger/plugin/storage/memory"
"github.com/jaegertracing/jaeger/storage"
factoryMocks "github.com/jaegertracing/jaeger/storage/mocks"
"github.com/jaegertracing/jaeger/storage/spanstore"
"github.com/jaegertracing/jaeger/storage_v2/tracestore"
"github.com/jaegertracing/jaeger/storage_v2/v1adapter"
)

type mockStorageExt struct {
Expand Down Expand Up @@ -143,17 +144,20 @@ func TestExporter(t *testing.T) {
err = tracesExporter.ConsumeTraces(ctx, traces)
require.NoError(t, err)

storageFactory, err := jaegerstorage.GetStorageFactory(memstoreName, host)
storageFactory, err := jaegerstorage.GetTraceStoreFactory(memstoreName, host)
require.NoError(t, err)
spanReader, err := storageFactory.CreateSpanReader()
traceReader, err := storageFactory.CreateTraceReader()
require.NoError(t, err)
requiredTraceID := model.NewTraceID(0, 1) // 00000000000000000000000000000001
requiredTrace, err := spanReader.GetTrace(ctx, spanstore.GetTraceParameters{TraceID: requiredTraceID})
requiredTraceIter := traceReader.GetTraces(ctx, tracestore.GetTraceParams{
TraceID: v1adapter.FromV1TraceID(requiredTraceID),
})
requiredTrace, err := v1adapter.V1TracesFromSeq2(requiredTraceIter)
require.NoError(t, err)
assert.Equal(t, spanID.String(), requiredTrace.Spans[0].SpanID.String())
assert.Equal(t, spanID.String(), requiredTrace[0].Spans[0].SpanID.String())

// check that the service name attribute was added by the sanitizer
require.Equal(t, "missing-service-name", requiredTrace.Spans[0].Process.ServiceName)
require.Equal(t, "missing-service-name", requiredTrace[0].Spans[0].Process.ServiceName)
}

func makeStorageExtension(t *testing.T, memstoreName string) component.Host {
Expand Down
27 changes: 9 additions & 18 deletions cmd/jaeger/internal/extension/jaegerstorage/extension.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,23 +40,6 @@ type storageExt struct {
metricsFactories map[string]storage.MetricStoreFactory
}

// GetStorageFactory locates the extension in Host and retrieves
// a trace storage factory from it with the given name.
func GetStorageFactory(name string, host component.Host) (storage.Factory, error) {
ext, err := findExtension(host)
if err != nil {
return nil, err
}
f, ok := ext.TraceStorageFactory(name)
if !ok {
return nil, fmt.Errorf(
"cannot find definition of storage '%s' in the configuration for extension '%s'",
name, componentType,
)
}
return f, nil
}

// GetMetricStorageFactory locates the extension in Host and retrieves
// a metric storage factory from it with the given name.
func GetMetricStorageFactory(name string, host component.Host) (storage.MetricStoreFactory, error) {
Expand All @@ -75,11 +58,19 @@ func GetMetricStorageFactory(name string, host component.Host) (storage.MetricSt
}

func GetTraceStoreFactory(name string, host component.Host) (tracestore.Factory, error) {
f, err := GetStorageFactory(name, host)
ext, err := findExtension(host)
if err != nil {
return nil, err
}

f, ok := ext.TraceStorageFactory(name)
if !ok {
return nil, fmt.Errorf(
"cannot find definition of storage '%s' in the configuration for extension '%s'",
name, componentType,
)
}

return v1adapter.NewFactory(f), nil
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,13 +58,13 @@ func (e errorFactory) Close() error {
}

func TestStorageFactoryBadHostError(t *testing.T) {
_, err := GetStorageFactory("something", componenttest.NewNopHost())
_, err := GetTraceStoreFactory("something", componenttest.NewNopHost())
require.ErrorContains(t, err, "cannot find extension")
}

func TestStorageFactoryBadNameError(t *testing.T) {
host := storagetest.NewStorageHost().WithExtension(ID, startStorageExtension(t, "foo", ""))
_, err := GetStorageFactory("bar", host)
_, err := GetTraceStoreFactory("bar", host)
require.ErrorContains(t, err, "cannot find definition of storage 'bar'")
}

Expand Down Expand Up @@ -106,7 +106,7 @@ func TestGetFactory(t *testing.T) {
const name = "foo"
const metricname = "bar"
host := storagetest.NewStorageHost().WithExtension(ID, startStorageExtension(t, name, metricname))
f, err := GetStorageFactory(name, host)
f, err := GetTraceStoreFactory(name, host)
require.NoError(t, err)
require.NotNil(t, f)

Expand Down
9 changes: 8 additions & 1 deletion cmd/jaeger/internal/extension/remotesampling/extension.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
"github.com/jaegertracing/jaeger/pkg/metrics"
"github.com/jaegertracing/jaeger/storage"
"github.com/jaegertracing/jaeger/storage/samplingstore"
"github.com/jaegertracing/jaeger/storage_v2/v1adapter"
)

var _ extension.Extension = (*rsExtension)(nil)
Expand Down Expand Up @@ -184,7 +185,7 @@

func (ext *rsExtension) startAdaptiveStrategyProvider(host component.Host) error {
storageName := ext.cfg.Adaptive.SamplingStore
f, err := jaegerstorage.GetStorageFactory(storageName, host)
f, err := jaegerstorage.GetTraceStoreFactory(storageName, host)
if err != nil {
return fmt.Errorf("cannot find storage factory: %w", err)
}
Expand All @@ -195,13 +196,19 @@
}

store, err := storeFactory.CreateSamplingStore(ext.cfg.Adaptive.AggregationBuckets)
if errors.Is(err, v1adapter.ErrSamplingStoreNotImplemented) {
return fmt.Errorf("storage '%s' does not support sampling store", storageName)
}

Check warning on line 201 in cmd/jaeger/internal/extension/remotesampling/extension.go

View check run for this annotation

Codecov / codecov/patch

cmd/jaeger/internal/extension/remotesampling/extension.go#L200-L201

Added lines #L200 - L201 were not covered by tests
if err != nil {
return fmt.Errorf("failed to create the sampling store: %w", err)
}
ext.adaptiveStore = store

{
lock, err := storeFactory.CreateLock()
if errors.Is(err, v1adapter.ErrSamplingStoreNotImplemented) {
return fmt.Errorf("storage '%s' does not support sampling store", storageName)
}

Check warning on line 211 in cmd/jaeger/internal/extension/remotesampling/extension.go

View check run for this annotation

Codecov / codecov/patch

cmd/jaeger/internal/extension/remotesampling/extension.go#L210-L211

Added lines #L210 - L211 were not covered by tests
if err != nil {
return fmt.Errorf("failed to create the distributed lock: %w", err)
}
Expand Down
9 changes: 7 additions & 2 deletions cmd/jaeger/internal/integration/storagecleaner/extension.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (

"github.com/jaegertracing/jaeger/cmd/jaeger/internal/extension/jaegerstorage"
"github.com/jaegertracing/jaeger/storage"
"github.com/jaegertracing/jaeger/storage_v2/v1adapter"
)

var (
Expand All @@ -45,7 +46,7 @@ func newStorageCleaner(config *Config, telset component.TelemetrySettings) *stor
}

func (c *storageCleaner) Start(_ context.Context, host component.Host) error {
storageFactory, err := jaegerstorage.GetStorageFactory(c.config.TraceStorage, host)
storageFactory, err := jaegerstorage.GetTraceStoreFactory(c.config.TraceStorage, host)
if err != nil {
return fmt.Errorf("cannot find storage factory '%s': %w", c.config.TraceStorage, err)
}
Expand All @@ -55,7 +56,11 @@ func (c *storageCleaner) Start(_ context.Context, host component.Host) error {
if !ok {
return fmt.Errorf("storage %s does not implement Purger interface", c.config.TraceStorage)
}
if err := purger.Purge(httpContext); err != nil {
err = purger.Purge(httpContext)
if errors.Is(err, v1adapter.ErrPurgerNotImplemented) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it doesn't make sense to be checking an error from v1adapter here. We need storage_v2 APIs to be self-sufficient. I would recommend moving Sampling/Lock/Purger APIs to storage_v2 and replacing them with type aliases in v1 storage (it should be a separate PR before this one).

Copy link
Contributor Author

@ary82 ary82 Jan 30, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm a bit confused, wouldn't we still need to check for the casting error in the adapter to return an error like storage %s does not implement Purger interface with the correct storage_name that is used?

I would recommend moving Sampling/Lock/Purger APIs to storage_v2 and replacing them with type aliases in v1 storage (it should be a separate PR before this one)

Should each of these APIs be in a different module in storage_v2, in a similar way DependencyStore has been moved to storage_v2/depstore? Or should they be defined in the storage_v2/tracestore? What do you think?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

wouldn't we still need to check for the casting error

in the adapter you can check anything you want, but the extension code should not be dependent on adapter package.

Should each of these APIs be in a different module in storage_v2

Yes, follow the existing pattern.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Got it, thanks! Closing this PR now to work on moving the APIs to v2.

return fmt.Errorf("storage %s does not implement Purger interface", c.config.TraceStorage)
}
if err != nil {
return fmt.Errorf("error purging storage: %w", err)
}
return nil
Expand Down
38 changes: 38 additions & 0 deletions storage_v2/v1adapter/factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,21 @@

import (
"context"
"errors"
"io"

"github.com/jaegertracing/jaeger/pkg/distributedlock"
storage_v1 "github.com/jaegertracing/jaeger/storage"
"github.com/jaegertracing/jaeger/storage/samplingstore"
"github.com/jaegertracing/jaeger/storage_v2/depstore"
"github.com/jaegertracing/jaeger/storage_v2/tracestore"
)

var (
ErrPurgerNotImplemented = errors.New("storage backend does not support Purger")
ErrSamplingStoreNotImplemented = errors.New("storage backend does not support sampling store")
)

type Factory struct {
ss storage_v1.Factory
}
Expand Down Expand Up @@ -61,3 +69,33 @@
}
return NewDependencyReader(dr), nil
}

// CreateLock implements storage_v1.SamplingStoreFactory
func (f *Factory) CreateLock() (distributedlock.Lock, error) {
Copy link
Collaborator

@mahadzaryab1 mahadzaryab1 Jan 28, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@yurishkuro This approach works but there's a slight change in behaviour. Consider the following:

  • Before this PR, if a storage didn't implement the storage.SamplingStoreFactory - it would fail here
  • As a result of the changes PR, the check I linked above will always pass but it would fail in the following check here

I don't see a huge downside to doing it this way except maybe a slightly different/confusing error message. We could mitigate that by returning a specific error here that we check for upstream and return the same error message as before. What do you think? The other approach would be to only construct the factory adapter based on the interfaces the underlying factory implements.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As long as we fail during startup, as opposed to later when processing incoming data, it should be fine.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could mitigate that by returning a specific error here that we check for upstream and return the same error message as before

I've changed the error checking to something along the lines of this. Please have a look.

ss, ok := f.ss.(storage_v1.SamplingStoreFactory)
if !ok {
return nil, ErrSamplingStoreNotImplemented
}
lock, err := ss.CreateLock()
return lock, err

Check warning on line 80 in storage_v2/v1adapter/factory.go

View check run for this annotation

Codecov / codecov/patch

storage_v2/v1adapter/factory.go#L74-L80

Added lines #L74 - L80 were not covered by tests
}

// CreateSamplingStore implements storage_v1.SamplingStoreFactory
func (f *Factory) CreateSamplingStore(maxBuckets int) (samplingstore.Store, error) {
ss, ok := f.ss.(storage_v1.SamplingStoreFactory)
if !ok {
return nil, ErrSamplingStoreNotImplemented
}
store, err := ss.CreateSamplingStore(maxBuckets)
return store, err

Check warning on line 90 in storage_v2/v1adapter/factory.go

View check run for this annotation

Codecov / codecov/patch

storage_v2/v1adapter/factory.go#L84-L90

Added lines #L84 - L90 were not covered by tests
}

// Purge implements storage_v1.Purger
func (f *Factory) Purge(ctx context.Context) error {
p, ok := f.ss.(storage_v1.Purger)
if !ok {
return ErrPurgerNotImplemented
}
err := p.Purge(ctx)
return err

Check warning on line 100 in storage_v2/v1adapter/factory.go

View check run for this annotation

Codecov / codecov/patch

storage_v2/v1adapter/factory.go#L94-L100

Added lines #L94 - L100 were not covered by tests
}
Loading