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

Using component for no op host instead of component test #6058

Closed
Closed
4 changes: 2 additions & 2 deletions cmd/jaeger/internal/extension/jaegerstorage/extension.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ func newStorageExt(config *Config, telset component.TelemetrySettings) *storageE
}
}

func (s *storageExt) Start(_ context.Context, _ component.Host) error {
func (s *storageExt) Start(_ context.Context, host component.Host) error {
baseFactory := otelmetrics.NewFactory(s.telset.MeterProvider)
mf := baseFactory.Namespace(metrics.NSOptions{Name: "jaeger"})
for storageName, cfg := range s.config.Backends {
Expand All @@ -126,7 +126,7 @@ func (s *storageExt) Start(_ context.Context, _ component.Host) error {
factory, err = badger.NewFactoryWithConfig(*cfg.Badger, mf, s.telset.Logger)
case cfg.GRPC != nil:
//nolint: contextcheck
factory, err = grpc.NewFactoryWithConfig(*cfg.GRPC, mf, s.telset.Logger)
factory, err = grpc.NewFactoryWithConfig(*cfg.GRPC, mf, s.telset.Logger, host)
case cfg.Cassandra != nil:
factory, err = cassandra.NewFactoryWithConfig(*cfg.Cassandra, mf, s.telset.Logger)
case cfg.Elasticsearch != nil:
Expand Down
25 changes: 23 additions & 2 deletions plugin/storage/grpc/factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import (

"github.com/spf13/viper"
"go.opentelemetry.io/collector/component"
"go.opentelemetry.io/collector/component/componenttest"
"go.opentelemetry.io/collector/config/configgrpc"
"go.opentelemetry.io/collector/config/configtelemetry"
"go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc"
Expand Down Expand Up @@ -47,6 +46,25 @@ type Factory struct {
config Config
services *ClientPluginServices
remoteConn *grpc.ClientConn
host component.Host
}

type host struct {
Copy link
Member

Choose a reason for hiding this comment

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

We don't need this type.

extensions map[component.ID]component.Component
}
Copy link
Collaborator

@mahadzaryab1 mahadzaryab1 Oct 8, 2024

Choose a reason for hiding this comment

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

@chahatsagarmain so we actually don't need to define a host at all. if you take a look at this method, you'll see that the host is available to us already. What you need to do is pass the host down from the method I linked above into this function and you can then pass it into Initialize and then into ToClientConn. Does that make sense?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Passing as a params to Initialize gives declaration error for Factory but Creating a host in NewFactoryWithConfig wouldnt that also be a no op host?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@mahadzaryab1 can the otel collector initialize a component.Host in collectorParams which can be used here ?

Copy link
Collaborator

@mahadzaryab1 mahadzaryab1 Oct 11, 2024

Choose a reason for hiding this comment

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

I think you'll want to do something like this:

  1. Update Factory struct to contain the host
type Factory struct {
	metricsFactory metrics.Factory
	logger         *zap.Logger
	tracerProvider trace.TracerProvider
	config         Config
	services       *ClientPluginServices
	remoteConn     *grpc.ClientConn
	host           component.Host
}
  1. Update NewFactoryWithConfig so that it takes in the host and sets it in the factory
func NewFactoryWithConfig(
	cfg Config,
	metricsFactory metrics.Factory,
	logger *zap.Logger,
	host component.Host,
) (*Factory, error) {
	f := NewFactory()
	f.config = cfg
	f.host = host
	if err := f.Initialize(metricsFactory, logger); err != nil {
		return nil, err
	}
	return f, nil
}
  1. Use f.host in Initialize

Copy link
Member

Choose a reason for hiding this comment

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

+1 - don't change the interface, but ok to change constructor


func NewHost() component.Host {
return &host{
extensions: make(map[component.ID]component.Component),
}
}

func (h *host) GetExtensions() map[component.ID]component.Component {
return h.extensions
}

func (h *host) AddExtensions(id component.ID, ext component.Component) {
h.extensions[id] = ext
}

// NewFactory creates a new Factory.
Expand All @@ -59,9 +77,11 @@ func NewFactoryWithConfig(
cfg Config,
metricsFactory metrics.Factory,
logger *zap.Logger,
host component.Host,
) (*Factory, error) {
f := NewFactory()
f.config = cfg
f.host = host
if err := f.Initialize(metricsFactory, logger); err != nil {
return nil, err
}
Expand Down Expand Up @@ -98,7 +118,8 @@ func (f *Factory) Initialize(metricsFactory metrics.Factory, logger *zap.Logger)
for _, opt := range opts {
clientOpts = append(clientOpts, configgrpc.WithGrpcDialOption(opt))
}
return f.config.ToClientConn(context.Background(), componenttest.NewNopHost(), telset, clientOpts...)

return f.config.ToClientConn(context.Background(), f.host, telset, clientOpts...)
}

var err error
Expand Down
6 changes: 3 additions & 3 deletions plugin/storage/grpc/factory_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ func TestNewFactoryError(t *testing.T) {
},
}
t.Run("with_config", func(t *testing.T) {
_, err := NewFactoryWithConfig(*cfg, metrics.NullFactory, zap.NewNop())
_, err := NewFactoryWithConfig(*cfg, metrics.NullFactory, zap.NewNop(), NewHost())
require.Error(t, err)
assert.Contains(t, err.Error(), "authenticator")
})
Expand All @@ -121,7 +121,7 @@ func TestNewFactoryError(t *testing.T) {

t.Run("client", func(t *testing.T) {
// this is a silly test to verify handling of error from grpc.NewClient, which cannot be induced via params.
f, err := NewFactoryWithConfig(Config{}, metrics.NullFactory, zap.NewNop())
f, err := NewFactoryWithConfig(Config{}, metrics.NullFactory, zap.NewNop(), NewHost())
require.NoError(t, err)
t.Cleanup(func() { require.NoError(t, f.Close()) })
newClientFn := func(_ ...grpc.DialOption) (conn *grpc.ClientConn, err error) {
Expand Down Expand Up @@ -173,7 +173,7 @@ func TestGRPCStorageFactoryWithConfig(t *testing.T) {
Enabled: true,
},
}
f, err := NewFactoryWithConfig(cfg, metrics.NullFactory, zap.NewNop())
f, err := NewFactoryWithConfig(cfg, metrics.NullFactory, zap.NewNop(), NewHost())
require.NoError(t, err)
require.NoError(t, f.Close())
}
Expand Down