英文:
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"
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论