Skip to content

Commit

Permalink
Use HttpRouteHolder in camel instrumentation (open-telemetry#5242)
Browse files Browse the repository at this point in the history
  • Loading branch information
Mateusz Rzeszutek authored and RashmiRam committed May 23, 2022
1 parent 57e385c commit e1abfde
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -97,7 +97,7 @@ public void pre(
attributes.put(SemanticAttributes.HTTP_METHOD, getHttpMethod(exchange, endpoint));
}

private static boolean shouldSetPathAsName(CamelDirection camelDirection) {
static boolean shouldSetPathAsName(CamelDirection camelDirection) {
return CamelDirection.INBOUND.equals(camelDirection);
}

Expand All @@ -113,26 +113,21 @@ protected String getPath(Exchange exchange, Endpoint endpoint) {
}
}

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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit e1abfde

Please sign in to comment.