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

Prevent infinite/recursive tracing of gRPC storage #5979

Closed
wants to merge 1 commit into from
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
37 changes: 21 additions & 16 deletions plugin/storage/grpc/factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,8 @@ type Factory struct {
configV1 Configuration
configV2 *ConfigV2

services *ClientPluginServices
remoteConn *grpc.ClientConn
services *ClientPluginServices
remoteConns []*grpc.ClientConn
}

// NewFactory creates a new Factory.
Expand Down Expand Up @@ -95,19 +95,18 @@ func (f *Factory) Initialize(metricsFactory metrics.Factory, logger *zap.Logger)
}

telset := component.TelemetrySettings{
Logger: logger,
TracerProvider: f.tracerProvider,
Logger: logger,
// TODO needs to be joined with the metricsFactory
LeveledMeterProvider: func(_ configtelemetry.Level) metric.MeterProvider {
return noopmetric.NewMeterProvider()
},
}
newClientFn := func(opts ...grpc.DialOption) (conn *grpc.ClientConn, err error) {
newClientFn := func(telSettings component.TelemetrySettings, opts ...grpc.DialOption) (conn *grpc.ClientConn, err error) {
Copy link
Member

Choose a reason for hiding this comment

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

Please elaborate on this part. In newRemoteStorage() you never modify telset when calling newClientFn twice, so why do you need to pass it vs. how it was passed already here via closure?

clientOpts := make([]configgrpc.ToClientConnOption, 0)
for _, opt := range opts {
clientOpts = append(clientOpts, configgrpc.WithGrpcDialOption(opt))
}
return f.configV2.ToClientConnWithOptions(context.Background(), componenttest.NewNopHost(), telset, clientOpts...)
return f.configV2.ToClientConnWithOptions(context.Background(), componenttest.NewNopHost(), telSettings, clientOpts...)
}

var err error
Expand All @@ -119,13 +118,11 @@ func (f *Factory) Initialize(metricsFactory metrics.Factory, logger *zap.Logger)
return nil
}

type newClientFn func(opts ...grpc.DialOption) (*grpc.ClientConn, error)
type newClientFn func(telSettings component.TelemetrySettings, opts ...grpc.DialOption) (*grpc.ClientConn, error)

func (f *Factory) newRemoteStorage(telset component.TelemetrySettings, newClient newClientFn) (*ClientPluginServices, error) {
c := f.configV2
opts := []grpc.DialOption{
grpc.WithStatsHandler(otelgrpc.NewClientHandler(otelgrpc.WithTracerProvider(telset.TracerProvider))),
}
opts := make([]grpc.DialOption, 0)
if c.Auth != nil {
return nil, fmt.Errorf("authenticator is not supported")
}
Expand All @@ -136,12 +133,20 @@ func (f *Factory) newRemoteStorage(telset component.TelemetrySettings, newClient
opts = append(opts, grpc.WithStreamInterceptor(tenancy.NewClientStreamInterceptor(tenancyMgr)))
}

remoteConn, err := newClient(opts...)
noTraceConn, err := newClient(telset, opts...)
if err != nil {
return nil, fmt.Errorf("error creating remote storage client: %w", err)
return nil, fmt.Errorf("error creating remote storage client without tracing: %w", err)
}
f.remoteConn = remoteConn
grpcClient := shared.NewGRPCClient(remoteConn)
f.remoteConns = append(f.remoteConns, noTraceConn)

opts = append(opts, grpc.WithStatsHandler(otelgrpc.NewClientHandler(otelgrpc.WithTracerProvider(f.tracerProvider))))
traceConn, err := newClient(telset, opts...)
if err != nil {
return nil, fmt.Errorf("error creating remote storage client with tracing: %w", err)
}
f.remoteConns = append(f.remoteConns, traceConn)

grpcClient := shared.NewGRPCClient(traceConn, noTraceConn)
return &ClientPluginServices{
PluginServices: shared.PluginServices{
Store: grpcClient,
Expand Down Expand Up @@ -205,8 +210,8 @@ func (f *Factory) CreateArchiveSpanWriter() (spanstore.Writer, error) {
// Close closes the resources held by the factory
func (f *Factory) Close() error {
var errs []error
if f.remoteConn != nil {
errs = append(errs, f.remoteConn.Close())
for i := range f.remoteConns {
errs = append(errs, f.remoteConns[i].Close())
}
errs = append(errs, f.configV1.RemoteTLS.Close())
return errors.Join(errs...)
Expand Down
2 changes: 1 addition & 1 deletion plugin/storage/grpc/factory_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ func TestNewFactoryError(t *testing.T) {
f, err := NewFactoryWithConfig(ConfigV2{}, metrics.NullFactory, zap.NewNop())
require.NoError(t, err)
t.Cleanup(func() { require.NoError(t, f.Close()) })
newClientFn := func(_ ...grpc.DialOption) (conn *grpc.ClientConn, err error) {
newClientFn := func(_ component.TelemetrySettings, _ ...grpc.DialOption) (conn *grpc.ClientConn, err error) {
return nil, errors.New("test error")
}
_, err = f.newRemoteStorage(component.TelemetrySettings{}, newClientFn)
Expand Down
16 changes: 8 additions & 8 deletions plugin/storage/grpc/shared/grpc_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,15 +46,15 @@ type GRPCClient struct {
streamWriterClient storage_v1.StreamingSpanWriterPluginClient
}

func NewGRPCClient(c *grpc.ClientConn) *GRPCClient {
func NewGRPCClient(withTraceConn *grpc.ClientConn, withoutTraceConn *grpc.ClientConn) *GRPCClient {
return &GRPCClient{
readerClient: storage_v1.NewSpanReaderPluginClient(c),
writerClient: storage_v1.NewSpanWriterPluginClient(c),
archiveReaderClient: storage_v1.NewArchiveSpanReaderPluginClient(c),
archiveWriterClient: storage_v1.NewArchiveSpanWriterPluginClient(c),
capabilitiesClient: storage_v1.NewPluginCapabilitiesClient(c),
depsReaderClient: storage_v1.NewDependenciesReaderPluginClient(c),
streamWriterClient: storage_v1.NewStreamingSpanWriterPluginClient(c),
readerClient: storage_v1.NewSpanReaderPluginClient(withTraceConn),
writerClient: storage_v1.NewSpanWriterPluginClient(withoutTraceConn),
archiveReaderClient: storage_v1.NewArchiveSpanReaderPluginClient(withTraceConn),
archiveWriterClient: storage_v1.NewArchiveSpanWriterPluginClient(withoutTraceConn),
capabilitiesClient: storage_v1.NewPluginCapabilitiesClient(withTraceConn),
depsReaderClient: storage_v1.NewDependenciesReaderPluginClient(withTraceConn),
streamWriterClient: storage_v1.NewStreamingSpanWriterPluginClient(withoutTraceConn),
}
}

Expand Down
2 changes: 1 addition & 1 deletion plugin/storage/grpc/shared/grpc_client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ func withGRPCClient(fn func(r *grpcClientTest)) {

func TestNewGRPCClient(t *testing.T) {
conn := &grpc.ClientConn{}
client := NewGRPCClient(conn)
client := NewGRPCClient(conn, conn)
assert.NotNil(t, client)

assert.Implements(t, (*storage_v1.SpanReaderPluginClient)(nil), client.readerClient)
Expand Down