Skip to content

Commit 135435a

Browse files
committed
add peer.service in serverless scenarios
1 parent 887ea39 commit 135435a

File tree

2 files changed

+21
-6
lines changed

2 files changed

+21
-6
lines changed

dd-java-agent/instrumentation/aws-java-sdk-1.11.0/src/main/java/datadog/trace/instrumentation/aws/v0/AwsSdkClientDecorator.java

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -85,12 +85,13 @@ public AgentSpan onRequest(final AgentSpan span, final Request request) {
8585
final AmazonWebServiceRequest originalRequest = request.getOriginalRequest();
8686
final Class<?> awsOperation = originalRequest.getClass();
8787
final GetterAccess access = GetterAccess.of(originalRequest);
88+
final String endpoint = request.getEndpoint().toString();
8889

8990
span.setTag(InstrumentationTags.AWS_AGENT, COMPONENT_NAME);
9091
span.setTag(InstrumentationTags.AWS_SERVICE, awsServiceName);
9192
span.setTag(InstrumentationTags.TOP_LEVEL_AWS_SERVICE, awsSimplifiedServiceName);
9293
span.setTag(InstrumentationTags.AWS_OPERATION, awsOperation.getSimpleName());
93-
span.setTag(InstrumentationTags.AWS_ENDPOINT, request.getEndpoint().toString());
94+
span.setTag(InstrumentationTags.AWS_ENDPOINT, endpoint);
9495

9596
CharSequence awsRequestName = AwsNameCache.getQualifiedName(request);
9697
span.setResourceName(awsRequestName, RPC_COMMAND_NAME);
@@ -184,11 +185,16 @@ public AgentSpan onRequest(final AgentSpan span, final Request request) {
184185
bestPeerService = tableName;
185186
}
186187

187-
// for aws we can calculate this eagerly without needing to have to looking up tags in the peer
188-
// service interceptor
189-
if (bestPrecursor != null && SpanNaming.instance().namingSchema().peerService().supports()) {
190-
span.setTag(Tags.PEER_SERVICE, bestPeerService);
191-
span.setTag(DDTags.PEER_SERVICE_SOURCE, bestPrecursor);
188+
String serverlessFunction = System.getenv("AWS_LAMBDA_FUNCTION_NAME");
189+
// Set peer.service based on environment
190+
if (serverlessFunction != null && !serverlessFunction.isEmpty()) {
191+
String hostname = endpoint.replaceFirst("^https?://", "").split("/")[0];
192+
span.setTag(Tags.PEER_SERVICE, hostname);
193+
} else {
194+
if (bestPrecursor != null && SpanNaming.instance().namingSchema().peerService().supports()) {
195+
span.setTag(Tags.PEER_SERVICE, bestPeerService);
196+
span.setTag(DDTags.PEER_SERVICE_SOURCE, bestPrecursor);
197+
}
192198
}
193199

194200
// DSM

dd-java-agent/instrumentation/aws-java-sdk-2.2/src/main/java/datadog/trace/instrumentation/aws/v2/AwsSdkClientDecorator.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,15 @@ public AgentSpan onSdkRequest(
214214
}
215215
}
216216

217+
// Set peer.service based on environment for serverless functions
218+
String serverlessFunction = System.getenv("AWS_LAMBDA_FUNCTION_NAME");
219+
if (serverlessFunction != null && !serverlessFunction.isEmpty()) {
220+
String endpoint = httpRequest.getUri().toString();
221+
// Extract just the hostname from the endpoint URL
222+
String hostname = endpoint.replaceFirst("^https?://", "").split("/")[0];
223+
span.setTag(Tags.PEER_SERVICE, hostname);
224+
}
225+
217226
return span;
218227
}
219228

0 commit comments

Comments
 (0)