Hacktoberfest 2026:维护者为十月标记出来的 issue,仍然开放、适合新手。 浏览 Hacktoberfest issue

[BUG] Resource::Create() throws bad_variant_access if process.executable.name isn't a string

未关闭 适合新手
#4,535 1 条评论 2 个 reaction 已指派 0 人 在 GitHub 查看

维护者通常 1 天内回复

@ryux1 已经在做这个了。

开始于 2026年9月8日。

  • #4540 来自 @ryux1 —— 未关闭
  • #4609 来自 @om7057 —— 已关闭,未合并

评估

难度
2/5
预计耗时
1-3 小时
新手友好度
84/100
Issue 类型
缺陷
描述清晰度
描述清楚
活跃度
活跃
技术栈
cpp

调研方向

从 sdk/src/resource/resource.cc 中的 Resource::Create 开始,重点关注默认服务名称逻辑和 process.executable.name 属性。使用非字符串属性验证行为,并确认创建会回退到 "unknown_service",且不会抛出异常或终止初始化。

由索引模型根据 Issue 内容生成。

描述

bug help wanted triage/accepted

Describe your environment

Reproduced by source read + build on main at 11fa0db0 (also present in v1.28.0, the latest release -- not a regression). sdk/src/resource/resource.cc, Resource::Create.

Steps to reproduce

Resource Resource::Create(const ResourceAttributes &attributes, const std::string &schema_url)
{
  ...
  if (resource.attributes_.find(semconv::service::kServiceName) == resource.attributes_.end())
  {
    std::string default_service_name = "unknown_service";
    auto it_process_executable_name =
        resource.attributes_.find(semconv::process::kProcessExecutableName);
    if (it_process_executable_name != resource.attributes_.end())
    {
      default_service_name += ":" + nostd::get<std::string>(it_process_executable_name->second);
    }
    resource.attributes_[semconv::service::kServiceName] = default_service_name;
  }
  return resource;
}

it_process_executable_name->second is an AttributeValue (nostd::variant). nostd::get<std::string> throws if the variant doesn't currently hold a std::string. process.executable.name can end up holding a different alternative than std::string in more than one way: a custom ResourceDetector setting it directly, an environment/config-driven attribute source, or simply a caller passing it in attributes as const char*/string_view/an integer by mistake -- none of those are prevented by the ResourceAttributes type itself.

To trigger: call Resource::Create(attributes, schema_url) with attributes containing {"process.executable.name", <anything that isn't a std::string>} and no service.name already set.

What is the expected behavior?

If process.executable.name is present but not a string, Resource::Create should fall back to just "unknown_service" (or otherwise degrade gracefully) rather than crash.

What is the actual behavior?

nostd::get<std::string> throws nostd::bad_variant_access. Since Resource::Create (and its callers up through TracerProvider/LoggerProvider/MeterProvider construction) isn't inside any exception handling, this propagates out and terminates initialization -- often during process/provider startup or a reload, i.e. exactly when an application least wants an unhandled crash.

Additional context

Suggested fix -- use nostd::get_if instead of the throwing nostd::get, and simply skip the suffix if the attribute isn't a string:

     if (it_process_executable_name != resource.attributes_.end())
     {
-      default_service_name += ":" + nostd::get<std::string>(it_process_executable_name->second);
+      if (const auto *executable_name =
+              nostd::get_if<std::string>(&it_process_executable_name->second))
+      {
+        default_service_name += ":" + *executable_name;
+      }
     }

Compile-checked against a clean build of this file -- no warnings or errors. Happy to open a PR with this if useful.

主要语言
C++
星标
1.4k
派生
647
平均合并
1 天 11 小时
30 天内合并 PR
73

环境准备

在 Codespaces 中打开

在浏览器里用你自己的 GitHub 账号启动这个项目的开发容器。

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

open-telemetry/opentelemetry-cpp 的其他 Issue

查看 open-telemetry/opentelemetry-cpp 的全部 Issue

相似的 Issue

更多 C++ Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。