Skip to content

Code flagged noexcept should not raise exceptions #4361

Description

@marcalff

Currently, many APIs are flagged as noexcept.

For example:

class OPENTELEMETRY_EXPORT TracerProvider
{
public:
  virtual nostd::shared_ptr<Tracer> GetTracer(
      nostd::string_view name,
      nostd::string_view version,
      nostd::string_view schema_url,
      const common::KeyValueIterable *attributes) noexcept = 0;

When implemented in the SDK:

nostd::shared_ptr<trace_api::Tracer> TracerProvider::GetTracer(
    nostd::string_view name,
    nostd::string_view version,
    nostd::string_view schema_url,
    const opentelemetry::common::KeyValueIterable *attributes) noexcept
{
...
  auto tracer = std::shared_ptr<Tracer>(new Tracer(context_, std::move(scope)));
  tracers_.push_back(tracer);
  return nostd::shared_ptr<trace_api::Tracer>{tracer};
}

The call to new Tracer() can fail with bad_alloc, so an exception is still raised, breaking the noexcept contract.

Proposal:

nostd::shared_ptr<trace_api::Tracer> TracerProvider::GetTracerImpl(
    nostd::string_view name,
    nostd::string_view version,
    nostd::string_view schema_url,
    const opentelemetry::common::KeyValueIterable *attributes) {
}
nostd::shared_ptr<trace_api::Tracer> TracerProvider::GetTracer(
    nostd::string_view name,
    nostd::string_view version,
    nostd::string_view schema_url,
    const opentelemetry::common::KeyValueIterable *attributes) noexcept
{
  try {
    // invoke GetTracerImpl
  }
  catch {
    return a pre allocated Noop tracer instead.
  }
}

Forcing down the noexcept to the entire code base is not realistic, entry points to the SDK surface needs to handle failures explicitly instead, to give room to the SDK implementation to fail internally with exceptions if needed.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workingtriage/acceptedIndicates an issue or PR is ready to be actively worked on.

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions