Use HttpRouteHolder in camel instrumentation (#5242)

This commit is contained in:
Mateusz Rzeszutek 2022-01-27 12:38:42 +01:00 committed by GitHub
parent 6c855d9789
commit 9ff7b92841
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 17 additions and 24 deletions

View File

@ -24,9 +24,9 @@
package io.opentelemetry.javaagent.instrumentation.apachecamel.decorators;
import io.opentelemetry.api.common.AttributesBuilder;
import io.opentelemetry.api.trace.Span;
import io.opentelemetry.context.Context;
import io.opentelemetry.instrumentation.api.server.ServerSpan;
import io.opentelemetry.instrumentation.api.instrumenter.http.HttpRouteHolder;
import io.opentelemetry.instrumentation.api.instrumenter.http.HttpRouteSource;
import io.opentelemetry.javaagent.instrumentation.apachecamel.CamelDirection;
import io.opentelemetry.semconv.trace.attributes.SemanticAttributes;
import java.net.MalformedURLException;
@ -97,7 +97,7 @@ class HttpSpanDecorator extends BaseSpanDecorator {
attributes.put(SemanticAttributes.HTTP_METHOD, getHttpMethod(exchange, endpoint));
}
private static boolean shouldSetPathAsName(CamelDirection camelDirection) {
static boolean shouldSetPathAsName(CamelDirection camelDirection) {
return CamelDirection.INBOUND.equals(camelDirection);
}
@ -113,26 +113,21 @@ class HttpSpanDecorator extends BaseSpanDecorator {
}
}
private static boolean shouldUpdateServerSpanName(
Span serverSpan, CamelDirection camelDirection) {
return (serverSpan != null && shouldSetPathAsName(camelDirection));
}
private void updateServerSpanName(Span serverSpan, Exchange exchange, Endpoint endpoint) {
String path = getPath(exchange, endpoint);
if (path != null) {
// TODO should update SERVER span name/route using ServerSpanNaming
serverSpan.updateName(path);
}
}
@Override
public void updateServerSpanName(
Context context, Exchange exchange, Endpoint endpoint, CamelDirection camelDirection) {
Span serverSpan = ServerSpan.fromContextOrNull(context);
if (shouldUpdateServerSpanName(serverSpan, camelDirection)) {
updateServerSpanName(serverSpan, exchange, endpoint);
Context context,
Exchange camelExchange,
Endpoint camelEndpoint,
CamelDirection camelDirection) {
if (!shouldSetPathAsName(camelDirection)) {
return;
}
HttpRouteHolder.updateHttpRoute(
context,
HttpRouteSource.CONTROLLER,
(c, exchange, endpoint) -> getPath(exchange, endpoint),
camelExchange,
camelEndpoint);
}
protected String getHttpUrl(Exchange exchange, Endpoint endpoint) {

View File

@ -99,8 +99,7 @@ class RestCamelTest extends AgentInstrumentationSpecification implements RetryOn
"$SemanticAttributes.NET_PEER_PORT" Long
"$SemanticAttributes.HTTP_SERVER_NAME" String
"$SemanticAttributes.NET_TRANSPORT" IP_TCP
// TODO: camel instrumentation does not use ServerSpanNaming to update the route, so the matched route is provided by the servlet instrumentation
"$SemanticAttributes.HTTP_ROUTE" "/*"
"$SemanticAttributes.HTTP_ROUTE" "/api/{module}/unit/{unitId}"
}
}
it.span(3) {

View File

@ -135,8 +135,7 @@ class TwoServicesWithDirectClientCamelTest extends AgentInstrumentationSpecifica
"$SemanticAttributes.HTTP_SERVER_NAME" String
"$SemanticAttributes.NET_TRANSPORT" IP_TCP
"$SemanticAttributes.HTTP_REQUEST_CONTENT_LENGTH" Long
// TODO: camel instrumentation does not use ServerSpanNaming to update the route, so the matched route is provided by the servlet instrumentation
"$SemanticAttributes.HTTP_ROUTE" "/*"
"$SemanticAttributes.HTTP_ROUTE" "/serviceTwo"
}
}
it.span(5) {