open-telemetry / open-telemetry/opentelemetry-cpp

Code flagged noexcept should not raise exceptions

Open
#4,361 3 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

bug triage/accepted
Dominant language
C++
Stars
1.4k
Forks
632
Avg merge
1d 13h
Merged PRs (30d)
75

Description

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.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with TracerProvider::GetTracer and compare its noexcept API declaration with the SDK implementation shown in the issue. Trace other SDK surface entry points with the same contract, then define how failures and the pre-allocated Noop tracer should be handled; done means the affected entry points preserve noexcept without allowing allocation exceptions to escape.

Written by the indexing model from the issue text.

Assessment

Tech stack
cpp
Domain
api, backend-api-design
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Active
Clarity
Mostly clear
Newbie friendliness
38/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.