You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

无需调用HostResource等类,如何获取OpenTelemetry Agent所有已用属性?

获取OpenTelemetry Agent中所有已使用的资源属性

不用直接调用HostResource、OsResource等类的话,可以通过以下方式获取所有已注册的资源属性:

  • 首先从GlobalOpenTelemetry获取合并后的全局Resource实例,Agent自动发现的所有资源(包括主机、操作系统、进程等相关属性)都会被合并到这个实例中:

    Resource globalResource = GlobalOpenTelemetry.get().getResource();
    
  • 然后通过Resource.getAttributes()拿到所有属性集合,再遍历或者转换为Map来操作:

    Attributes allAttributes = globalResource.getAttributes();
    
    // 遍历所有属性
    allAttributes.forEach((attributeKey, attributeValue) -> {
        // 这里可以处理每个属性,比如打印或者收集
        System.out.printf("属性名: %s, 属性值: %s%n", attributeKey.getKey(), attributeValue);
    });
    
    // 转换为Map方便后续处理
    Map<String, Object> attributeMap = new HashMap<>();
    allAttributes.forEach((key, value) -> attributeMap.put(key.getKey(), value));
    

需要注意的是,要确保这段代码在OpenTelemetry Agent完成初始化之后执行,否则可能获取不到完整的属性集合。Agent启动时会自动加载并合并各类资源提供者的属性,GlobalOpenTelemetry.get().getResource()会直接返回这个合并后的结果,无需手动实例化各个具体的Resource类。

内容的提问来源于stack exchange,提问作者David Andalcio

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.09 08:46:02