Deprecate AttributesExtractor#set() method (#5749)

* Deprecate AttributesExtractor#set() method

* code review comments
This commit is contained in:
Mateusz Rzeszutek 2022-04-06 05:20:02 +02:00 committed by GitHub
parent 39e1fcb48e
commit 50f91e6dee
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
35 changed files with 238 additions and 162 deletions

View File

@ -19,7 +19,7 @@ event's MDC copy:
(same as `Span.current().getSpanContext().getTraceFlags().asHex()`).
Those three pieces of information can be included in log statements produced by the logging library
by specifying them in the pattern/format.
by specifying them in the pattern/format.
Tip: for Spring Boot configuration which uses logback, you can add MDC to log lines by overriding only the `logging.pattern.level`:
```properties

View File

@ -5,6 +5,8 @@
package io.opentelemetry.instrumentation.api.instrumenter.code;
import static io.opentelemetry.instrumentation.api.internal.AttributesExtractorUtil.internalSet;
import io.opentelemetry.api.common.AttributesBuilder;
import io.opentelemetry.context.Context;
import io.opentelemetry.instrumentation.api.instrumenter.AttributesExtractor;
@ -35,9 +37,9 @@ public final class CodeAttributesExtractor<REQUEST, RESPONSE>
public void onStart(AttributesBuilder attributes, Context parentContext, REQUEST request) {
Class<?> cls = getter.codeClass(request);
if (cls != null) {
set(attributes, SemanticAttributes.CODE_NAMESPACE, cls.getName());
internalSet(attributes, SemanticAttributes.CODE_NAMESPACE, cls.getName());
}
set(attributes, SemanticAttributes.CODE_FUNCTION, getter.methodName(request));
internalSet(attributes, SemanticAttributes.CODE_FUNCTION, getter.methodName(request));
}
@Override

View File

@ -5,6 +5,8 @@
package io.opentelemetry.instrumentation.api.instrumenter.db;
import static io.opentelemetry.instrumentation.api.internal.AttributesExtractorUtil.internalSet;
import io.opentelemetry.api.common.AttributesBuilder;
import io.opentelemetry.context.Context;
import io.opentelemetry.semconv.trace.attributes.SemanticAttributes;
@ -35,7 +37,7 @@ public final class DbClientAttributesExtractor<REQUEST, RESPONSE>
public void onStart(AttributesBuilder attributes, Context parentContext, REQUEST request) {
super.onStart(attributes, parentContext, request);
set(attributes, SemanticAttributes.DB_STATEMENT, getter.statement(request));
set(attributes, SemanticAttributes.DB_OPERATION, getter.operation(request));
internalSet(attributes, SemanticAttributes.DB_STATEMENT, getter.statement(request));
internalSet(attributes, SemanticAttributes.DB_OPERATION, getter.operation(request));
}
}

View File

@ -5,6 +5,8 @@
package io.opentelemetry.instrumentation.api.instrumenter.db;
import static io.opentelemetry.instrumentation.api.internal.AttributesExtractorUtil.internalSet;
import io.opentelemetry.api.common.AttributesBuilder;
import io.opentelemetry.context.Context;
import io.opentelemetry.instrumentation.api.annotations.UnstableApi;
@ -26,10 +28,11 @@ abstract class DbClientCommonAttributesExtractor<
@Override
public void onStart(AttributesBuilder attributes, Context parentContext, REQUEST request) {
set(attributes, SemanticAttributes.DB_SYSTEM, getter.system(request));
set(attributes, SemanticAttributes.DB_USER, getter.user(request));
set(attributes, SemanticAttributes.DB_NAME, getter.name(request));
set(attributes, SemanticAttributes.DB_CONNECTION_STRING, getter.connectionString(request));
internalSet(attributes, SemanticAttributes.DB_SYSTEM, getter.system(request));
internalSet(attributes, SemanticAttributes.DB_USER, getter.user(request));
internalSet(attributes, SemanticAttributes.DB_NAME, getter.name(request));
internalSet(
attributes, SemanticAttributes.DB_CONNECTION_STRING, getter.connectionString(request));
}
@Override

View File

@ -5,6 +5,8 @@
package io.opentelemetry.instrumentation.api.instrumenter.db;
import static io.opentelemetry.instrumentation.api.internal.AttributesExtractorUtil.internalSet;
import io.opentelemetry.api.common.AttributeKey;
import io.opentelemetry.api.common.AttributesBuilder;
import io.opentelemetry.context.Context;
@ -55,8 +57,8 @@ public final class SqlClientAttributesExtractor<REQUEST, RESPONSE>
SqlStatementInfo sanitizedStatement =
SqlStatementSanitizer.sanitize(getter.rawStatement(request));
set(attributes, SemanticAttributes.DB_STATEMENT, sanitizedStatement.getFullStatement());
set(attributes, SemanticAttributes.DB_OPERATION, sanitizedStatement.getOperation());
set(attributes, dbTableAttribute, sanitizedStatement.getTable());
internalSet(attributes, SemanticAttributes.DB_STATEMENT, sanitizedStatement.getFullStatement());
internalSet(attributes, SemanticAttributes.DB_OPERATION, sanitizedStatement.getOperation());
internalSet(attributes, dbTableAttribute, sanitizedStatement.getTable());
}
}

View File

@ -5,6 +5,8 @@
package io.opentelemetry.instrumentation.api.instrumenter.http;
import static io.opentelemetry.instrumentation.api.internal.AttributesExtractorUtil.internalSet;
import io.opentelemetry.api.common.AttributesBuilder;
import io.opentelemetry.context.Context;
import io.opentelemetry.instrumentation.api.annotations.UnstableApi;
@ -53,7 +55,7 @@ public final class HttpClientAttributesExtractor<REQUEST, RESPONSE>
@Override
public void onStart(AttributesBuilder attributes, Context parentContext, REQUEST request) {
super.onStart(attributes, parentContext, request);
set(attributes, SemanticAttributes.HTTP_URL, getter.url(request));
internalSet(attributes, SemanticAttributes.HTTP_URL, getter.url(request));
}
@Override
@ -64,7 +66,7 @@ public final class HttpClientAttributesExtractor<REQUEST, RESPONSE>
@Nullable RESPONSE response,
@Nullable Throwable error) {
super.onEnd(attributes, context, request, response, error);
set(attributes, SemanticAttributes.HTTP_FLAVOR, getter.flavor(request, response));
internalSet(attributes, SemanticAttributes.HTTP_FLAVOR, getter.flavor(request, response));
}
/**

View File

@ -8,6 +8,7 @@ package io.opentelemetry.instrumentation.api.instrumenter.http;
import static io.opentelemetry.instrumentation.api.instrumenter.http.CapturedHttpHeadersUtil.lowercase;
import static io.opentelemetry.instrumentation.api.instrumenter.http.CapturedHttpHeadersUtil.requestAttributeKey;
import static io.opentelemetry.instrumentation.api.instrumenter.http.CapturedHttpHeadersUtil.responseAttributeKey;
import static io.opentelemetry.instrumentation.api.internal.AttributesExtractorUtil.internalSet;
import io.opentelemetry.api.common.AttributesBuilder;
import io.opentelemetry.context.Context;
@ -38,13 +39,13 @@ abstract class HttpCommonAttributesExtractor<
@Override
public void onStart(AttributesBuilder attributes, Context parentContext, REQUEST request) {
set(attributes, SemanticAttributes.HTTP_METHOD, getter.method(request));
set(attributes, SemanticAttributes.HTTP_USER_AGENT, userAgent(request));
internalSet(attributes, SemanticAttributes.HTTP_METHOD, getter.method(request));
internalSet(attributes, SemanticAttributes.HTTP_USER_AGENT, userAgent(request));
for (String name : capturedRequestHeaders) {
List<String> values = getter.requestHeader(request, name);
if (!values.isEmpty()) {
set(attributes, requestAttributeKey(name), values);
internalSet(attributes, requestAttributeKey(name), values);
}
}
}
@ -57,11 +58,11 @@ abstract class HttpCommonAttributesExtractor<
@Nullable RESPONSE response,
@Nullable Throwable error) {
set(
internalSet(
attributes,
SemanticAttributes.HTTP_REQUEST_CONTENT_LENGTH,
getter.requestContentLength(request, response));
set(
internalSet(
attributes,
SemanticAttributes.HTTP_REQUEST_CONTENT_LENGTH_UNCOMPRESSED,
getter.requestContentLengthUncompressed(request, response));
@ -69,13 +70,13 @@ abstract class HttpCommonAttributesExtractor<
if (response != null) {
Integer statusCode = getter.statusCode(request, response);
if (statusCode != null && statusCode > 0) {
set(attributes, SemanticAttributes.HTTP_STATUS_CODE, (long) statusCode);
internalSet(attributes, SemanticAttributes.HTTP_STATUS_CODE, (long) statusCode);
}
set(
internalSet(
attributes,
SemanticAttributes.HTTP_RESPONSE_CONTENT_LENGTH,
getter.responseContentLength(request, response));
set(
internalSet(
attributes,
SemanticAttributes.HTTP_RESPONSE_CONTENT_LENGTH_UNCOMPRESSED,
getter.responseContentLengthUncompressed(request, response));
@ -83,7 +84,7 @@ abstract class HttpCommonAttributesExtractor<
for (String name : capturedResponseHeaders) {
List<String> values = getter.responseHeader(request, response, name);
if (!values.isEmpty()) {
set(attributes, responseAttributeKey(name), values);
internalSet(attributes, responseAttributeKey(name), values);
}
}
}

View File

@ -9,6 +9,7 @@ import static io.opentelemetry.instrumentation.api.instrumenter.http.ForwardedHe
import static io.opentelemetry.instrumentation.api.instrumenter.http.ForwardedHeaderParser.extractClientIpFromForwardedHeader;
import static io.opentelemetry.instrumentation.api.instrumenter.http.ForwardedHeaderParser.extractProtoFromForwardedHeader;
import static io.opentelemetry.instrumentation.api.instrumenter.http.ForwardedHeaderParser.extractProtoFromForwardedProtoHeader;
import static io.opentelemetry.instrumentation.api.internal.AttributesExtractorUtil.internalSet;
import io.opentelemetry.api.common.AttributesBuilder;
import io.opentelemetry.context.Context;
@ -68,17 +69,15 @@ public final class HttpServerAttributesExtractor<REQUEST, RESPONSE>
public void onStart(AttributesBuilder attributes, Context parentContext, REQUEST request) {
super.onStart(attributes, parentContext, request);
set(attributes, SemanticAttributes.HTTP_FLAVOR, getter.flavor(request));
internalSet(attributes, SemanticAttributes.HTTP_FLAVOR, getter.flavor(request));
String forwardedProto = forwardedProto(request);
set(
attributes,
SemanticAttributes.HTTP_SCHEME,
forwardedProto != null ? forwardedProto : getter.scheme(request));
set(attributes, SemanticAttributes.HTTP_HOST, host(request));
set(attributes, SemanticAttributes.HTTP_TARGET, getter.target(request));
set(attributes, SemanticAttributes.HTTP_ROUTE, getter.route(request));
set(attributes, SemanticAttributes.HTTP_SERVER_NAME, getter.serverName(request));
set(attributes, SemanticAttributes.HTTP_CLIENT_IP, clientIp(request));
String value = forwardedProto != null ? forwardedProto : getter.scheme(request);
internalSet(attributes, SemanticAttributes.HTTP_SCHEME, value);
internalSet(attributes, SemanticAttributes.HTTP_HOST, host(request));
internalSet(attributes, SemanticAttributes.HTTP_TARGET, getter.target(request));
internalSet(attributes, SemanticAttributes.HTTP_ROUTE, getter.route(request));
internalSet(attributes, SemanticAttributes.HTTP_SERVER_NAME, getter.serverName(request));
internalSet(attributes, SemanticAttributes.HTTP_CLIENT_IP, clientIp(request));
}
@Override
@ -90,7 +89,7 @@ public final class HttpServerAttributesExtractor<REQUEST, RESPONSE>
@Nullable Throwable error) {
super.onEnd(attributes, context, request, response, error);
set(attributes, SemanticAttributes.HTTP_ROUTE, httpRouteHolderGetter.apply(context));
internalSet(attributes, SemanticAttributes.HTTP_ROUTE, httpRouteHolderGetter.apply(context));
}
@Nullable

View File

@ -7,6 +7,7 @@ package io.opentelemetry.instrumentation.api.instrumenter.messaging;
import static io.opentelemetry.instrumentation.api.instrumenter.messaging.MessageOperation.PROCESS;
import static io.opentelemetry.instrumentation.api.instrumenter.messaging.MessageOperation.RECEIVE;
import static io.opentelemetry.instrumentation.api.internal.AttributesExtractorUtil.internalSet;
import io.opentelemetry.api.common.AttributesBuilder;
import io.opentelemetry.context.Context;
@ -50,29 +51,33 @@ public final class MessagingAttributesExtractor<REQUEST, RESPONSE>
@SuppressWarnings("deprecation") // operationName
@Override
public void onStart(AttributesBuilder attributes, Context parentContext, REQUEST request) {
set(attributes, SemanticAttributes.MESSAGING_SYSTEM, getter.system(request));
set(attributes, SemanticAttributes.MESSAGING_DESTINATION_KIND, getter.destinationKind(request));
internalSet(attributes, SemanticAttributes.MESSAGING_SYSTEM, getter.system(request));
internalSet(
attributes, SemanticAttributes.MESSAGING_DESTINATION_KIND, getter.destinationKind(request));
boolean isTemporaryDestination = getter.temporaryDestination(request);
if (isTemporaryDestination) {
set(attributes, SemanticAttributes.MESSAGING_TEMP_DESTINATION, true);
set(attributes, SemanticAttributes.MESSAGING_DESTINATION, TEMP_DESTINATION_NAME);
internalSet(attributes, SemanticAttributes.MESSAGING_TEMP_DESTINATION, true);
internalSet(attributes, SemanticAttributes.MESSAGING_DESTINATION, TEMP_DESTINATION_NAME);
} else {
set(attributes, SemanticAttributes.MESSAGING_DESTINATION, getter.destination(request));
internalSet(
attributes, SemanticAttributes.MESSAGING_DESTINATION, getter.destination(request));
}
set(attributes, SemanticAttributes.MESSAGING_PROTOCOL, getter.protocol(request));
set(attributes, SemanticAttributes.MESSAGING_PROTOCOL_VERSION, getter.protocolVersion(request));
set(attributes, SemanticAttributes.MESSAGING_URL, getter.url(request));
set(attributes, SemanticAttributes.MESSAGING_CONVERSATION_ID, getter.conversationId(request));
set(
internalSet(attributes, SemanticAttributes.MESSAGING_PROTOCOL, getter.protocol(request));
internalSet(
attributes, SemanticAttributes.MESSAGING_PROTOCOL_VERSION, getter.protocolVersion(request));
internalSet(attributes, SemanticAttributes.MESSAGING_URL, getter.url(request));
internalSet(
attributes, SemanticAttributes.MESSAGING_CONVERSATION_ID, getter.conversationId(request));
internalSet(
attributes,
SemanticAttributes.MESSAGING_MESSAGE_PAYLOAD_SIZE_BYTES,
getter.messagePayloadSize(request));
set(
internalSet(
attributes,
SemanticAttributes.MESSAGING_MESSAGE_PAYLOAD_COMPRESSED_SIZE_BYTES,
getter.messagePayloadCompressedSize(request));
if (operation == RECEIVE || operation == PROCESS) {
set(attributes, SemanticAttributes.MESSAGING_OPERATION, operation.operationName());
internalSet(attributes, SemanticAttributes.MESSAGING_OPERATION, operation.operationName());
}
}
@ -83,7 +88,8 @@ public final class MessagingAttributesExtractor<REQUEST, RESPONSE>
REQUEST request,
@Nullable RESPONSE response,
@Nullable Throwable error) {
set(attributes, SemanticAttributes.MESSAGING_MESSAGE_ID, getter.messageId(request, response));
internalSet(
attributes, SemanticAttributes.MESSAGING_MESSAGE_ID, getter.messageId(request, response));
}
/**

View File

@ -5,6 +5,8 @@
package io.opentelemetry.instrumentation.api.instrumenter.net;
import static io.opentelemetry.instrumentation.api.internal.AttributesExtractorUtil.internalSet;
import io.opentelemetry.api.common.AttributesBuilder;
import io.opentelemetry.context.Context;
import io.opentelemetry.instrumentation.api.instrumenter.AttributesExtractor;
@ -45,19 +47,19 @@ public final class NetClientAttributesExtractor<REQUEST, RESPONSE>
@Nullable RESPONSE response,
@Nullable Throwable error) {
set(attributes, SemanticAttributes.NET_TRANSPORT, getter.transport(request, response));
internalSet(attributes, SemanticAttributes.NET_TRANSPORT, getter.transport(request, response));
String peerIp = getter.peerIp(request, response);
String peerName = getter.peerName(request, response);
if (peerName != null && !peerName.equals(peerIp)) {
set(attributes, SemanticAttributes.NET_PEER_NAME, peerName);
internalSet(attributes, SemanticAttributes.NET_PEER_NAME, peerName);
}
set(attributes, SemanticAttributes.NET_PEER_IP, peerIp);
internalSet(attributes, SemanticAttributes.NET_PEER_IP, peerIp);
Integer peerPort = getter.peerPort(request, response);
if (peerPort != null && peerPort > 0) {
set(attributes, SemanticAttributes.NET_PEER_PORT, (long) peerPort);
internalSet(attributes, SemanticAttributes.NET_PEER_PORT, (long) peerPort);
}
}
}

View File

@ -5,6 +5,8 @@
package io.opentelemetry.instrumentation.api.instrumenter.net;
import static io.opentelemetry.instrumentation.api.internal.AttributesExtractorUtil.internalSet;
import io.opentelemetry.api.common.AttributesBuilder;
import io.opentelemetry.context.Context;
import io.opentelemetry.instrumentation.api.instrumenter.AttributesExtractor;
@ -33,15 +35,15 @@ public final class NetServerAttributesExtractor<REQUEST, RESPONSE>
@Override
public void onStart(AttributesBuilder attributes, Context parentContext, REQUEST request) {
set(attributes, SemanticAttributes.NET_TRANSPORT, getter.transport(request));
internalSet(attributes, SemanticAttributes.NET_TRANSPORT, getter.transport(request));
String peerIp = getter.peerIp(request);
set(attributes, SemanticAttributes.NET_PEER_IP, peerIp);
internalSet(attributes, SemanticAttributes.NET_PEER_IP, peerIp);
Integer peerPort = getter.peerPort(request);
if (peerPort != null && peerPort > 0) {
set(attributes, SemanticAttributes.NET_PEER_PORT, (long) peerPort);
internalSet(attributes, SemanticAttributes.NET_PEER_PORT, (long) peerPort);
}
}

View File

@ -5,6 +5,8 @@
package io.opentelemetry.instrumentation.api.instrumenter.rpc;
import static io.opentelemetry.instrumentation.api.internal.AttributesExtractorUtil.internalSet;
import io.opentelemetry.api.common.AttributesBuilder;
import io.opentelemetry.context.Context;
import io.opentelemetry.instrumentation.api.instrumenter.AttributesExtractor;
@ -22,9 +24,9 @@ abstract class RpcCommonAttributesExtractor<REQUEST, RESPONSE>
@Override
public final void onStart(AttributesBuilder attributes, Context parentContext, REQUEST request) {
set(attributes, SemanticAttributes.RPC_SYSTEM, getter.system(request));
set(attributes, SemanticAttributes.RPC_SERVICE, getter.service(request));
set(attributes, SemanticAttributes.RPC_METHOD, getter.method(request));
internalSet(attributes, SemanticAttributes.RPC_SYSTEM, getter.system(request));
internalSet(attributes, SemanticAttributes.RPC_SERVICE, getter.service(request));
internalSet(attributes, SemanticAttributes.RPC_METHOD, getter.method(request));
}
@Override

View File

@ -40,7 +40,11 @@ public interface AttributesExtractor<REQUEST, RESPONSE> {
/**
* Sets the {@code value} with the given {@code key} to the {@link AttributesBuilder} if {@code
* value} is not {@code null}.
*
* @deprecated This method will be removed.
*/
// TODO: remove after 1.13 release
@Deprecated
default <T> void set(AttributesBuilder attributes, AttributeKey<T> key, @Nullable T value) {
if (value != null) {
attributes.put(key, value);

View File

@ -0,0 +1,30 @@
/*
* Copyright The OpenTelemetry Authors
* SPDX-License-Identifier: Apache-2.0
*/
package io.opentelemetry.instrumentation.api.internal;
import io.opentelemetry.api.common.AttributeKey;
import io.opentelemetry.api.common.AttributesBuilder;
import javax.annotation.Nullable;
/**
* This class is internal and is hence not for public use. Its APIs are unstable and can change at
* any time.
*/
public final class AttributesExtractorUtil {
/**
* Sets the {@code value} with the given {@code key} to the {@link AttributesBuilder} if {@code
* value} is not {@code null}.
*/
public static <T> void internalSet(
AttributesBuilder attributes, AttributeKey<T> key, @Nullable T value) {
if (value != null) {
attributes.put(key, value);
}
}
private AttributesExtractorUtil() {}
}

View File

@ -25,8 +25,8 @@ class AttributesExtractorTest {
@Override
public void onStart(
AttributesBuilder attributes, Context parentContext, Map<String, String> request) {
set(attributes, AttributeKey.stringKey("animal"), request.get("animal"));
set(attributes, AttributeKey.stringKey("country"), request.get("country"));
attributes.put(AttributeKey.stringKey("animal"), request.get("animal"));
attributes.put(AttributeKey.stringKey("country"), request.get("country"));
}
@Override
@ -37,11 +37,11 @@ class AttributesExtractorTest {
@Nullable Map<String, String> response,
@Nullable Throwable error) {
if (response != null) {
set(attributes, AttributeKey.stringKey("food"), response.get("food"));
set(attributes, AttributeKey.stringKey("number"), request.get("number"));
attributes.put(AttributeKey.stringKey("food"), response.get("food"));
attributes.put(AttributeKey.stringKey("number"), request.get("number"));
}
if (error != null) {
set(attributes, AttributeKey.stringKey("full_error_class"), error.getClass().getName());
attributes.put(AttributeKey.stringKey("full_error_class"), error.getClass().getName());
}
}
}

View File

@ -27,12 +27,13 @@ public class AsyncHttpClientAdditionalAttributesExtractor
RequestContext requestContext,
@Nullable Response response,
@Nullable Throwable error) {
NettyRequest nettyRequest = requestContext.getNettyRequest();
if (nettyRequest != null) {
set(
attributes,
SemanticAttributes.HTTP_USER_AGENT,
nettyRequest.getHttpRequest().headers().get("User-Agent"));
String userAgent = nettyRequest.getHttpRequest().headers().get("User-Agent");
if (userAgent != null) {
attributes.put(SemanticAttributes.HTTP_USER_AGENT, userAgent);
}
}
}
}

View File

@ -49,9 +49,12 @@ public final class AwsLambdaFunctionAttributesExtractor
io.opentelemetry.context.Context parentContext,
AwsLambdaRequest request) {
Context awsContext = request.getAwsContext();
set(attributes, FAAS_EXECUTION, awsContext.getAwsRequestId());
set(attributes, FAAS_ID, getFunctionArn(awsContext));
set(attributes, CLOUD_ACCOUNT_ID, getAccountId(getFunctionArn(awsContext)));
attributes.put(FAAS_EXECUTION, awsContext.getAwsRequestId());
String arn = getFunctionArn(awsContext);
if (arn != null) {
attributes.put(FAAS_ID, arn);
attributes.put(CLOUD_ACCOUNT_ID, getAccountId(arn));
}
}
@Override
@ -74,11 +77,7 @@ public final class AwsLambdaFunctionAttributesExtractor
}
}
@Nullable
private String getAccountId(@Nullable String arn) {
if (arn == null) {
return null;
}
private String getAccountId(String arn) {
if (accountId == null) {
synchronized (this) {
if (accountId == null) {

View File

@ -32,17 +32,23 @@ final class ApiGatewayProxyAttributesExtractor
public void onStart(
AttributesBuilder attributes, Context parentContext, AwsLambdaRequest request) {
if (request.getInput() instanceof APIGatewayProxyRequestEvent) {
set(attributes, FAAS_TRIGGER, SemanticAttributes.FaasTriggerValues.HTTP);
attributes.put(FAAS_TRIGGER, SemanticAttributes.FaasTriggerValues.HTTP);
onRequest(attributes, (APIGatewayProxyRequestEvent) request.getInput());
}
}
void onRequest(AttributesBuilder attributes, APIGatewayProxyRequestEvent request) {
set(attributes, HTTP_METHOD, request.getHttpMethod());
attributes.put(HTTP_METHOD, request.getHttpMethod());
Map<String, String> headers = lowercaseMap(request.getHeaders());
set(attributes, HTTP_USER_AGENT, headers.get("user-agent"));
set(attributes, HTTP_URL, getHttpUrl(request, headers));
String userAgent = headers.get("user-agent");
if (userAgent != null) {
attributes.put(HTTP_USER_AGENT, userAgent);
}
String httpUrl = getHttpUrl(request, headers);
if (httpUrl != null) {
attributes.put(HTTP_URL, httpUrl);
}
}
private static String getHttpUrl(

View File

@ -17,9 +17,11 @@ import static io.opentelemetry.instrumentation.awssdk.v1_11.AwsExperimentalAttri
import com.amazonaws.AmazonWebServiceResponse;
import com.amazonaws.Request;
import com.amazonaws.Response;
import io.opentelemetry.api.common.AttributeKey;
import io.opentelemetry.api.common.AttributesBuilder;
import io.opentelemetry.context.Context;
import io.opentelemetry.instrumentation.api.instrumenter.AttributesExtractor;
import java.util.function.Function;
import javax.annotation.Nullable;
class AwsSdkExperimentalAttributesExtractor
@ -28,15 +30,26 @@ class AwsSdkExperimentalAttributesExtractor
@Override
public void onStart(AttributesBuilder attributes, Context parentContext, Request<?> request) {
set(attributes, AWS_AGENT, COMPONENT_NAME);
set(attributes, AWS_ENDPOINT, request.getEndpoint().toString());
attributes.put(AWS_AGENT, COMPONENT_NAME);
attributes.put(AWS_ENDPOINT, request.getEndpoint().toString());
Object originalRequest = request.getOriginalRequest();
set(attributes, AWS_BUCKET_NAME, RequestAccess.getBucketName(originalRequest));
set(attributes, AWS_QUEUE_URL, RequestAccess.getQueueUrl(originalRequest));
set(attributes, AWS_QUEUE_NAME, RequestAccess.getQueueName(originalRequest));
set(attributes, AWS_STREAM_NAME, RequestAccess.getStreamName(originalRequest));
set(attributes, AWS_TABLE_NAME, RequestAccess.getTableName(originalRequest));
setRequestAttribute(attributes, AWS_BUCKET_NAME, originalRequest, RequestAccess::getBucketName);
setRequestAttribute(attributes, AWS_QUEUE_URL, originalRequest, RequestAccess::getQueueUrl);
setRequestAttribute(attributes, AWS_QUEUE_NAME, originalRequest, RequestAccess::getQueueName);
setRequestAttribute(attributes, AWS_STREAM_NAME, originalRequest, RequestAccess::getStreamName);
setRequestAttribute(attributes, AWS_TABLE_NAME, originalRequest, RequestAccess::getTableName);
}
private static void setRequestAttribute(
AttributesBuilder attributes,
AttributeKey<String> key,
Object request,
Function<Object, String> getter) {
String value = getter.apply(request);
if (value != null) {
attributes.put(key, value);
}
}
@Override
@ -48,7 +61,10 @@ class AwsSdkExperimentalAttributesExtractor
@Nullable Throwable error) {
if (response != null && response.getAwsResponse() instanceof AmazonWebServiceResponse) {
AmazonWebServiceResponse<?> awsResp = (AmazonWebServiceResponse<?>) response.getAwsResponse();
set(attributes, AWS_REQUEST_ID, awsResp.getRequestId());
String requestId = awsResp.getRequestId();
if (requestId != null) {
attributes.put(AWS_REQUEST_ID, requestId);
}
}
}
}

View File

@ -37,52 +37,41 @@ final class CassandraAttributesExtractor
Node coordinator = executionInfo.getCoordinator();
if (coordinator != null) {
if (coordinator.getDatacenter() != null) {
set(
attributes,
SemanticAttributes.DB_CASSANDRA_COORDINATOR_DC,
coordinator.getDatacenter());
attributes.put(SemanticAttributes.DB_CASSANDRA_COORDINATOR_DC, coordinator.getDatacenter());
}
if (coordinator.getHostId() != null) {
set(
attributes,
SemanticAttributes.DB_CASSANDRA_COORDINATOR_ID,
coordinator.getHostId().toString());
attributes.put(
SemanticAttributes.DB_CASSANDRA_COORDINATOR_ID, coordinator.getHostId().toString());
}
}
set(
attributes,
attributes.put(
SemanticAttributes.DB_CASSANDRA_SPECULATIVE_EXECUTION_COUNT,
(long) executionInfo.getSpeculativeExecutionCount());
executionInfo.getSpeculativeExecutionCount());
Statement<?> statement = executionInfo.getStatement();
String consistencyLevel;
DriverExecutionProfile config =
request.getSession().getContext().getConfig().getDefaultProfile();
if (statement.getConsistencyLevel() != null) {
set(
attributes,
SemanticAttributes.DB_CASSANDRA_CONSISTENCY_LEVEL,
statement.getConsistencyLevel().name());
consistencyLevel = statement.getConsistencyLevel().name();
} else {
set(
attributes,
SemanticAttributes.DB_CASSANDRA_CONSISTENCY_LEVEL,
config.getString(DefaultDriverOption.REQUEST_CONSISTENCY));
consistencyLevel = config.getString(DefaultDriverOption.REQUEST_CONSISTENCY);
}
attributes.put(SemanticAttributes.DB_CASSANDRA_CONSISTENCY_LEVEL, consistencyLevel);
if (statement.getPageSize() > 0) {
set(attributes, SemanticAttributes.DB_CASSANDRA_PAGE_SIZE, (long) statement.getPageSize());
attributes.put(SemanticAttributes.DB_CASSANDRA_PAGE_SIZE, statement.getPageSize());
} else {
int pageSize = config.getInt(DefaultDriverOption.REQUEST_PAGE_SIZE);
if (pageSize > 0) {
set(attributes, SemanticAttributes.DB_CASSANDRA_PAGE_SIZE, (long) pageSize);
attributes.put(SemanticAttributes.DB_CASSANDRA_PAGE_SIZE, pageSize);
}
}
if (statement.isIdempotent() != null) {
set(attributes, SemanticAttributes.DB_CASSANDRA_IDEMPOTENCE, statement.isIdempotent());
} else {
set(
attributes,
SemanticAttributes.DB_CASSANDRA_IDEMPOTENCE,
config.getBoolean(DefaultDriverOption.REQUEST_DEFAULT_IDEMPOTENCE));
Boolean idempotent = statement.isIdempotent();
if (idempotent == null) {
idempotent = config.getBoolean(DefaultDriverOption.REQUEST_DEFAULT_IDEMPOTENCE);
}
attributes.put(SemanticAttributes.DB_CASSANDRA_IDEMPOTENCE, idempotent);
}
}

View File

@ -27,9 +27,9 @@ final class ExperimentalAttributesExtractor implements AttributesExtractor<Hystr
String groupName = hystrixRequest.command().getCommandGroup().name();
boolean circuitOpen = hystrixRequest.command().isCircuitBreakerOpen();
set(attributes, HYSTRIX_COMMAND, commandName);
set(attributes, HYSTRIX_GROUP, groupName);
set(attributes, HYSTRIX_CIRCUIT_OPEN, circuitOpen);
attributes.put(HYSTRIX_COMMAND, commandName);
attributes.put(HYSTRIX_GROUP, groupName);
attributes.put(HYSTRIX_CIRCUIT_OPEN, circuitOpen);
}
@Override

View File

@ -21,12 +21,9 @@ public final class KafkaConsumerAdditionalAttributesExtractor
@Override
public void onStart(
AttributesBuilder attributes, Context parentContext, ConsumerRecord<?, ?> consumerRecord) {
set(
attributes,
SemanticAttributes.MESSAGING_KAFKA_PARTITION,
(long) consumerRecord.partition());
attributes.put(SemanticAttributes.MESSAGING_KAFKA_PARTITION, (long) consumerRecord.partition());
if (consumerRecord.value() == null) {
set(attributes, SemanticAttributes.MESSAGING_KAFKA_TOMBSTONE, true);
attributes.put(SemanticAttributes.MESSAGING_KAFKA_TOMBSTONE, true);
}
}

View File

@ -37,17 +37,15 @@ public final class KafkaConsumerExperimentalAttributesExtractor
@Override
public void onStart(
AttributesBuilder attributes, Context parentContext, ConsumerRecord<?, ?> consumerRecord) {
set(attributes, KAFKA_OFFSET, consumerRecord.offset());
attributes.put(KAFKA_OFFSET, consumerRecord.offset());
// don't record a duration if the message was sent from an old Kafka client
if (consumerRecord.timestampType() != TimestampType.NO_TIMESTAMP_TYPE) {
long produceTime = consumerRecord.timestamp();
// this attribute shows how much time elapsed between the producer and the consumer of this
// message, which can be helpful for identifying queue bottlenecks
set(
attributes,
KAFKA_RECORD_QUEUE_TIME_MS,
Math.max(0L, System.currentTimeMillis() - produceTime));
attributes.put(
KAFKA_RECORD_QUEUE_TIME_MS, Math.max(0L, System.currentTimeMillis() - produceTime));
}
}

View File

@ -23,10 +23,10 @@ public final class KafkaProducerAdditionalAttributesExtractor
AttributesBuilder attributes, Context parentContext, ProducerRecord<?, ?> producerRecord) {
Integer partition = producerRecord.partition();
if (partition != null) {
set(attributes, SemanticAttributes.MESSAGING_KAFKA_PARTITION, partition.longValue());
attributes.put(SemanticAttributes.MESSAGING_KAFKA_PARTITION, partition.longValue());
}
if (producerRecord.value() == null) {
set(attributes, SemanticAttributes.MESSAGING_KAFKA_TOMBSTONE, true);
attributes.put(SemanticAttributes.MESSAGING_KAFKA_TOMBSTONE, true);
}
}

View File

@ -21,7 +21,10 @@ class MongoAttributesExtractor implements AttributesExtractor<CommandStartedEven
@Override
public void onStart(
AttributesBuilder attributes, Context parentContext, CommandStartedEvent event) {
set(attributes, DB_MONGODB_COLLECTION, collectionName(event));
String collectionName = collectionName(event);
if (collectionName != null) {
attributes.put(DB_MONGODB_COLLECTION, collectionName);
}
}
@Override

View File

@ -27,13 +27,11 @@ class RabbitDeliveryExperimentalAttributesExtractor
// this will be set if the sender sets the timestamp,
// or if a plugin is installed on the rabbitmq broker
long produceTimeMillis = timestamp.getTime();
set(
attributes,
RABBITMQ_QUEUE_TIME,
Math.max(0L, System.currentTimeMillis() - produceTimeMillis));
attributes.put(
RABBITMQ_QUEUE_TIME, Math.max(0L, System.currentTimeMillis() - produceTimeMillis));
}
set(attributes, RABBITMQ_COMMAND, "basic.deliver");
attributes.put(RABBITMQ_COMMAND, "basic.deliver");
}
@Override

View File

@ -20,7 +20,7 @@ class RabbitDeliveryExtraAttributesExtractor implements AttributesExtractor<Deli
Envelope envelope = request.getEnvelope();
String routingKey = envelope.getRoutingKey();
if (routingKey != null && !routingKey.isEmpty()) {
set(attributes, SemanticAttributes.MESSAGING_RABBITMQ_ROUTING_KEY, routingKey);
attributes.put(SemanticAttributes.MESSAGING_RABBITMQ_ROUTING_KEY, routingKey);
}
}

View File

@ -22,8 +22,8 @@ class RabbitReceiveExperimentalAttributesExtractor
@Override
public void onStart(
AttributesBuilder attributes, Context parentContext, ReceiveRequest receiveRequest) {
set(attributes, RABBITMQ_COMMAND, "basic.get");
set(attributes, RABBITMQ_QUEUE, receiveRequest.getQueue());
attributes.put(RABBITMQ_COMMAND, "basic.get");
attributes.put(RABBITMQ_QUEUE, receiveRequest.getQueue());
}
@Override

View File

@ -9,6 +9,7 @@ import io.opentelemetry.api.common.AttributeKey;
import io.opentelemetry.api.common.AttributesBuilder;
import io.opentelemetry.context.Context;
import io.opentelemetry.instrumentation.api.instrumenter.AttributesExtractor;
import java.net.SocketAddress;
import javax.annotation.Nullable;
import org.apache.rocketmq.common.message.MessageExt;
@ -27,19 +28,20 @@ enum RocketMqConsumerExperimentalAttributeExtractor
@Override
public void onStart(AttributesBuilder attributes, Context parentContext, MessageExt msg) {
set(attributes, MESSAGING_ROCKETMQ_TAGS, msg.getTags());
set(attributes, MESSAGING_ROCKETMQ_QUEUE_ID, (long) msg.getQueueId());
set(attributes, MESSAGING_ROCKETMQ_QUEUE_OFFSET, msg.getQueueOffset());
set(attributes, MESSAGING_ROCKETMQ_BROKER_ADDRESS, getBrokerHost(msg));
String tags = msg.getTags();
if (tags != null) {
attributes.put(MESSAGING_ROCKETMQ_TAGS, tags);
}
attributes.put(MESSAGING_ROCKETMQ_QUEUE_ID, msg.getQueueId());
attributes.put(MESSAGING_ROCKETMQ_QUEUE_OFFSET, msg.getQueueOffset());
SocketAddress storeHost = msg.getStoreHost();
if (storeHost != null) {
attributes.put(MESSAGING_ROCKETMQ_BROKER_ADDRESS, getBrokerHost(storeHost));
}
}
@Nullable
private static String getBrokerHost(MessageExt msg) {
if (msg.getStoreHost() != null) {
return msg.getStoreHost().toString().replace("/", "");
} else {
return null;
}
private static String getBrokerHost(SocketAddress storeHost) {
return storeHost.toString().replace("/", "");
}
@Override

View File

@ -27,9 +27,15 @@ enum RocketMqProducerExperimentalAttributeExtractor
public void onStart(
AttributesBuilder attributes, Context parentContext, SendMessageContext request) {
if (request.getMessage() != null) {
set(attributes, MESSAGING_ROCKETMQ_TAGS, request.getMessage().getTags());
String tags = request.getMessage().getTags();
if (tags != null) {
attributes.put(MESSAGING_ROCKETMQ_TAGS, tags);
}
}
String brokerAddr = request.getBrokerAddr();
if (brokerAddr != null) {
attributes.put(MESSAGING_ROCKETMQ_BROKER_ADDRESS, brokerAddr);
}
set(attributes, MESSAGING_ROCKETMQ_BROKER_ADDRESS, request.getBrokerAddr());
}
@Override
@ -40,10 +46,8 @@ enum RocketMqProducerExperimentalAttributeExtractor
@Nullable Void unused,
@Nullable Throwable error) {
if (request.getSendResult() != null) {
set(
attributes,
MESSAGING_ROCKETMQ_SEND_RESULT,
request.getSendResult().getSendStatus().name());
attributes.put(
MESSAGING_ROCKETMQ_SEND_RESULT, request.getSendResult().getSendStatus().name());
}
}
}

View File

@ -44,13 +44,16 @@ public class ServletAdditionalAttributesExtractor<REQUEST, RESPONSE>
@Nullable Throwable error) {
Principal principal = accessor.getRequestUserPrincipal(requestContext.request());
if (principal != null) {
set(attributes, SemanticAttributes.ENDUSER_ID, principal.getName());
String name = principal.getName();
if (name != null) {
attributes.put(SemanticAttributes.ENDUSER_ID, name);
}
}
if (!CAPTURE_EXPERIMENTAL_SPAN_ATTRIBUTES) {
return;
}
if (responseContext != null && responseContext.hasTimeout()) {
set(attributes, SERVLET_TIMEOUT, responseContext.getTimeout());
attributes.put(SERVLET_TIMEOUT, responseContext.getTimeout());
}
}
}

View File

@ -66,7 +66,7 @@ public class ServletRequestParametersExtractor<REQUEST, RESPONSE>
// request parameters are extracted at the end of the request to make sure that we don't access
// them before request encoding has been set
REQUEST request = requestContext.request();
setAttributes(request, (key, value) -> set(attributes, key, value));
setAttributes(request, attributes::put);
}
private static AttributeKey<List<String>> parameterAttributeKey(String headerName) {

View File

@ -42,8 +42,8 @@ final class SpringWebfluxExperimentalAttributesExtractor
// no response and no error means that the request has been cancelled
if (response == null && error == null) {
set(attributes, SPRING_WEBFLUX_EVENT, "cancelled");
set(attributes, SPRING_WEBFLUX_MESSAGE, "The subscription was cancelled");
attributes.put(SPRING_WEBFLUX_EVENT, "cancelled");
attributes.put(SPRING_WEBFLUX_MESSAGE, "The subscription was cancelled");
}
}
}

View File

@ -40,7 +40,7 @@ final class StatusCodeExtractor
statusCode = response.getStatus();
}
set(attributes, SemanticAttributes.HTTP_STATUS_CODE, statusCode);
attributes.put(SemanticAttributes.HTTP_STATUS_CODE, statusCode);
}
}
}

View File

@ -40,7 +40,10 @@ public class TomcatAdditionalAttributesExtractor<REQUEST, RESPONSE>
REQUEST servletRequest = servletEntityProvider.getServletRequest(request);
Principal principal = accessor.getRequestUserPrincipal(servletRequest);
if (principal != null) {
set(attributes, SemanticAttributes.ENDUSER_ID, principal.getName());
String name = principal.getName();
if (name != null) {
attributes.put(SemanticAttributes.ENDUSER_ID, name);
}
}
}
}