如何将可索引信息注入 APM 事务
How to inject indexable info to APM transaction
我正在使用程序化API设置来自附加并且我想将可索引信息添加到事务中。 (https://www.elastic.co/guide/en/apm/agent/java/current/public-api.html#api-tracer-api)
阅读文档后,我找到了一种方法。为此,我们可以使用 https://www.elastic.co/guide/en/apm/agent/java/1.x/public-api.html#api-transaction-add-tag transaction.setLabel("authorities", authorities);
。这是一个例子。
public class ApmContextDecorator extends OncePerRequestFilter {
private final SecuredAdminUtils securedAdminUtils;
@Override
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain chain) throws ServletException, IOException {
final Transaction transaction = ElasticApm.currentTransaction();
setUserData(transaction);
setCustomContext(transaction);
chain.doFilter(request, response);
}
private void setUserData(Transaction transaction) {
final String userId = Try.of(() -> securedAdminUtils.getClaim("userId")).getOrNull();
final String username = Try.of(() -> securedAdminUtils.getClaim("sub")).getOrNull();
transaction.setUser(userId, "", username);
}
private void setCustomContext(Transaction transaction) {
final List<String> roles = Try.of(securedAdminUtils::getRoles).getOrElse(List.of());
final String authorities = String.join(",", roles);
transaction.setLabel("organization", Try.of(() -> securedAdminUtils.getClaim("organization")).getOrNull());
transaction.setLabel("authorities", authorities);
}
}
我正在使用程序化API设置来自附加并且我想将可索引信息添加到事务中。 (https://www.elastic.co/guide/en/apm/agent/java/current/public-api.html#api-tracer-api)
阅读文档后,我找到了一种方法。为此,我们可以使用 https://www.elastic.co/guide/en/apm/agent/java/1.x/public-api.html#api-transaction-add-tag transaction.setLabel("authorities", authorities);
。这是一个例子。
public class ApmContextDecorator extends OncePerRequestFilter {
private final SecuredAdminUtils securedAdminUtils;
@Override
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain chain) throws ServletException, IOException {
final Transaction transaction = ElasticApm.currentTransaction();
setUserData(transaction);
setCustomContext(transaction);
chain.doFilter(request, response);
}
private void setUserData(Transaction transaction) {
final String userId = Try.of(() -> securedAdminUtils.getClaim("userId")).getOrNull();
final String username = Try.of(() -> securedAdminUtils.getClaim("sub")).getOrNull();
transaction.setUser(userId, "", username);
}
private void setCustomContext(Transaction transaction) {
final List<String> roles = Try.of(securedAdminUtils::getRoles).getOrElse(List.of());
final String authorities = String.join(",", roles);
transaction.setLabel("organization", Try.of(() -> securedAdminUtils.getClaim("organization")).getOrNull());
transaction.setLabel("authorities", authorities);
}
}