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 usage of unkeyed struct literals for all Settings and types with exported members #12360

Open
bogdandrutu opened this issue Feb 11, 2025 · 2 comments

Comments

@bogdandrutu
Copy link
Member

bogdandrutu commented Feb 11, 2025

We need to be able to expand our Settings structs, see:

type TelemetrySettings struct {
	Logger *zap.Logger
	TracerProvider trace.TracerProvider
	MeterProvider metric.MeterProvider
	Resource pcommon.Resource
}

// Compiles fine, which means adding a new member will cause this to fail.
ts := component.TelemetrySettings{zap.NewNop(), nooptrace.NewTracerProvider(), noopmetric.NewMeterProvider(), pcommon.NewResource()}

Proposal use a blank field member (_ struct{}) to disallow unkeyed literals initialization.

type TelemetrySettings struct {
	Logger *zap.Logger
	TracerProvider trace.TracerProvider
	MeterProvider metric.MeterProvider
	Resource pcommon.Resource
	_ struct{} // Prevents unkeyed literals
}

// Compilation error without the last struct.
ts := component.TelemetrySettings{zap.NewNop(), nooptrace.NewTracerProvider(), noopmetric.NewMeterProvider(), pcommon.NewResource()}

// Compilation error because cannot initialize blank member.
ts := component.TelemetrySettings{zap.NewNop(), nooptrace.NewTracerProvider(), noopmetric.NewMeterProvider(), pcommon.NewResource(), struct{}{}}

In the main versioning guarantees we reserve the right to add new fields, see https://github.com/open-telemetry/opentelemetry-collector/blob/main/VERSIONING.md#general-go-api-considerations so even a module is stable we can still do this change.

@mx-psi
Copy link
Member

mx-psi commented Feb 12, 2025

I think it is useful to add this to all structs that don't have a lot of fields

@atoulme
Copy link
Contributor

atoulme commented Feb 13, 2025

Here is a list:

  • connector/forwardconnector struct "Config" does not prevent unkeyed literal initialization
  • exporter/debugexporter struct "Config" does not prevent unkeyed literal initialization
  • exporter/otlpexporter struct "Config" does not prevent unkeyed literal initialization
  • exporter/otlphttpexporter struct "EncodingType" does not prevent unkeyed literal initialization
  • extension/memorylimiterextension struct "Config" does not prevent unkeyed literal initialization
  • extension/zpagesextension struct "Config" does not prevent unkeyed literal initialization
  • processor/batchprocessor struct "Config" does not prevent unkeyed literal initialization
  • processor/memorylimiterprocessor struct "Config" does not prevent unkeyed literal initialization
  • receiver/otlpreceiver struct "Protocols" does not prevent unkeyed literal initialization
  • scraper struct "Settings" does not prevent unkeyed literal initialization
  • scraper/scraperhelper struct "ControllerOption" does not prevent unkeyed literal initialization
  • service struct "Settings" does not prevent unkeyed literal initialization

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Status: Todo
Development

No branches or pull requests

3 participants