use of co.elastic.apm.api.Transaction in project ARLAS-server by gisaia.
the class AuthorizationFilter method filter.
@Override
public void filter(ContainerRequestContext ctx) {
Transaction transaction = ElasticApm.currentTransaction();
boolean isPublic = ctx.getUriInfo().getPath().concat(":").concat(ctx.getMethod()).matches(authConf.getPublicRegex());
String header = ctx.getHeaderString(HttpHeaders.AUTHORIZATION);
if (header == null || (header != null && !header.toLowerCase().startsWith("bearer "))) {
if (isPublic || ctx.getMethod() == "OPTIONS") {
return;
} else {
ctx.abortWith(Response.status(Response.Status.UNAUTHORIZED).build());
}
}
try {
// header presence and format already checked before in AuthenticationFilter
DecodedJWT jwt = jwtVerifier.verify(header.substring(7));
// remove it in case it's been set manually
ctx.getHeaders().remove(authConf.headerUser);
String userId = jwt.getSubject();
if (!StringUtil.isNullOrEmpty(userId)) {
ctx.getHeaders().putSingle(authConf.headerUser, userId);
transaction.setUser(userId, "", "");
}
// remove it in case it's been set manually
ctx.getHeaders().remove(authConf.headerGroup);
Claim jwtClaimRoles = jwt.getClaim(authConf.claimRoles);
if (!jwtClaimRoles.isNull()) {
List<String> groups = jwtClaimRoles.asList(String.class).stream().filter(r -> r.toLowerCase().startsWith("group")).collect(Collectors.toList());
ctx.setProperty("groups", groups);
ctx.getHeaders().put(authConf.headerGroup, groups);
}
Claim jwtClaimPermissions = jwt.getClaim(authConf.claimPermissions);
if (!jwtClaimPermissions.isNull()) {
ArlasClaims arlasClaims = new ArlasClaims(jwtClaimPermissions.asList(String.class));
ctx.setProperty("claims", arlasClaims);
if (arlasClaims.isAllowed(ctx.getMethod(), ctx.getUriInfo().getPath())) {
arlasClaims.injectHeaders(ctx.getHeaders(), transaction);
return;
}
}
if (isPublic) {
return;
} else {
ctx.abortWith(Response.status(Response.Status.FORBIDDEN).build());
}
} catch (JWTVerificationException e) {
LOGGER.warn("JWT verification failed.", e);
if (!isPublic) {
ctx.abortWith(Response.status(Response.Status.UNAUTHORIZED).build());
}
return;
}
ctx.abortWith(Response.status(Response.Status.FORBIDDEN).build());
}
use of co.elastic.apm.api.Transaction in project apm-agent-java by elastic.
the class SpanInstrumentationTest method testSampled.
@Test
void testSampled() {
assertThat(ElasticApm.currentSpan().isSampled()).isFalse();
assertThat(ElasticApm.currentTransaction().isSampled()).isFalse();
final Transaction transaction = ElasticApm.startTransaction();
assertThat(transaction.isSampled()).isTrue();
Span span = transaction.startSpan();
assertThat(span.isSampled()).isTrue();
span.end();
transaction.end();
}
use of co.elastic.apm.api.Transaction in project apm-agent-java by elastic.
the class TestApiServlet method doGet.
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse resp) throws ServletException, IOException {
Transaction transaction = ElasticApm.currentTransaction();
// set transaction name
transaction.setName("custom_transaction_name");
// set transaction type, here default value is 'request'
transaction.setType("custom_transaction_type");
// set custom transaction labels
transaction.addLabel("custom-label1", "label_value1");
transaction.addLabel("custom-label2", "label_value2");
// store custom context field
transaction.addCustomContext("custom-context", "custom-context-value");
// creating a custom span with annotation
captureSpanAnnotation();
doWork();
createCustomSpan();
}
use of co.elastic.apm.api.Transaction in project apm-agent-java by elastic.
the class JakartaTestApiServlet method doGet.
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse resp) throws ServletException, IOException {
Transaction transaction = ElasticApm.currentTransaction();
// set transaction name
transaction.setName("custom_transaction_name");
// set transaction type, here default value is 'request'
transaction.setType("custom_transaction_type");
// set custom transaction labels
transaction.addLabel("custom-label1", "label_value1");
transaction.addLabel("custom-label2", "label_value2");
// store custom context field
transaction.addCustomContext("custom-context", "custom-context-value");
// creating a custom span with annotation
captureSpanAnnotation();
doWork();
createCustomSpan();
}
use of co.elastic.apm.api.Transaction in project apm-agent-java by elastic.
the class SpanInstrumentationTest method testReferenceCounting.
@Test
void testReferenceCounting() {
final Transaction transaction = ElasticApm.startTransaction();
Span span = transaction.startSpan();
try (Scope scope = span.activate()) {
span.startSpan().end();
}
span.end();
transaction.end();
BookkeeperObjectPool<co.elastic.apm.agent.impl.transaction.Span> spanPool = objectPoolFactory.getSpanPool();
assertThat(spanPool.getRecyclablesToReturn().stream().filter(span1 -> span1.getReferenceCount() > 1).collect(Collectors.toList())).hasSize(spanPool.getRequestedObjectCount());
BookkeeperObjectPool<co.elastic.apm.agent.impl.transaction.Transaction> transactionPool = objectPoolFactory.getTransactionPool();
assertThat(transactionPool.getRecyclablesToReturn().stream().filter(transaction1 -> transaction1.getReferenceCount() > 1).collect(Collectors.toList())).hasSize(transactionPool.getRequestedObjectCount());
}
Aggregations