如何自动添加默认属性到 opentelemetry 指标?

huangapple go评论108阅读模式
英文:

How can I automatically add default attributes to opentelemetry metrics?

问题

我目前正在使用Java Spring Boot中的拦截器和Python/Flask中的@before_request钩子来动态添加属性,例如HTTP端点和HTTP方法到我的所有指标中。

我知道可以将共享属性添加到资源中,但这里的属性值是固定的。我知道可以使用Micrometer来实现部分这种行为。但我想知道是否有一种“纯粹”的OpenTelemetry方法来实现这一点,而不是手动编写钩子和拦截器。是否有自动的方法来实现这一点?

英文:

I am currently using interceptors in Java Spring Boot and @before_request hooks with baggages in Python/Flask to dynamically add attributes like the http endpoint and http method to all of my metrics.

I know, that I can add shared attributes to resources, but the attribute values here are fixed. I know that I can get some of this behavior by using Micrometer. But I want to know a "pure" OpenTelemetry way of achieving it. Is there an automatic way to do this instead of manually writing hooks and interceptors?

答案1

得分: 3

对于Java(使用javaagent),您可以使用ResourceProvider创建一个扩展,这允许您动态定义资源。这是一个示例:

@AutoService(ResourceProvider.class)
public class DemoResourceProvider implements ResourceProvider {
  @Override
  public Resource createResource(ConfigProperties config) {
    Attributes attributes = Attributes.builder().put("custom.resource", "demo").build();
    return Resource.create(attributes);
  }
}

您可以参考这个文档了解更多关于扩展的信息。

英文:

For Java (with the javaagent), you can create an extension with a ResourceProvider. This allows you to dynamically define a resource.
Here's an example.

@AutoService(ResourceProvider.class)
public class DemoResourceProvider implements ResourceProvider {
  @Override
  public Resource createResource(ConfigProperties config) {
    Attributes attributes = Attributes.builder().put("custom.resource", "demo").build();
    return Resource.create(attributes);
  }
}

You can reference this doc for more info about extensions.

答案2

得分: 2

是的,Opentelemetry有一些环境变量,您可以在其中定义固定属性。

请查看使用的Opentelemetry库文档。应该是OTEL_RESOURCE_ATTRIBUTES变量,例如:

OTEL_RESOURCE_ATTRIBUTES="attribute1=value1,attribute2=value2"
英文:

Yes, Opentelemetry has some env variables, where you can define fixed attributes.

Check used Opentelemetry lib doc. It should be OTEL_RESOURCE_ATTRIBUTES variable, e.g.:

OTEL_RESOURCE_ATTRIBUTES="attribute1=value1,attribute2=value2"

huangapple
  • 本文由 发表于 2023年8月9日 00:36:12
  • 转载请务必保留本文链接:https://go.coder-hub.com/76861568.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定