From e023041476a4914fae375df41107d691c527e1bc Mon Sep 17 00:00:00 2001 From: "gary.wgy" Date: Wed, 7 Aug 2024 16:10:42 +0800 Subject: [PATCH] Remove Attributes combanation from onEnd, move Attributes extract from onStart to onEnd --- .../semconv/rpc/RpcClientMetrics.java | 2 +- .../rpc/RpcCommonAttributesExtractor.java | 7 ++++--- .../semconv/rpc/RpcServerMetrics.java | 2 +- .../api/instrumenter/Instrumenter.java | 1 + .../http/HttpClientAttributesExtractor.java | 18 ++++++++---------- .../api/semconv/http/HttpClientMetrics.java | 4 +--- .../http/HttpCommonAttributesExtractor.java | 19 ++++++++++--------- .../http/HttpServerAttributesExtractor.java | 13 ++++++------- .../api/semconv/http/HttpServerMetrics.java | 4 +--- .../network/ClientAttributesExtractor.java | 5 +++-- .../network/ServerAttributesExtractor.java | 5 +++-- .../InternalClientAttributesExtractor.java | 2 +- .../InternalServerAttributesExtractor.java | 2 +- .../semconv/url/UrlAttributesExtractor.java | 5 +++-- .../InternalUrlAttributesExtractor.java | 2 +- 15 files changed, 45 insertions(+), 46 deletions(-) diff --git a/instrumentation-api-incubator/src/main/java/io/opentelemetry/instrumentation/api/incubator/semconv/rpc/RpcClientMetrics.java b/instrumentation-api-incubator/src/main/java/io/opentelemetry/instrumentation/api/incubator/semconv/rpc/RpcClientMetrics.java index db001a3afc87..ad93e99eea61 100644 --- a/instrumentation-api-incubator/src/main/java/io/opentelemetry/instrumentation/api/incubator/semconv/rpc/RpcClientMetrics.java +++ b/instrumentation-api-incubator/src/main/java/io/opentelemetry/instrumentation/api/incubator/semconv/rpc/RpcClientMetrics.java @@ -74,7 +74,7 @@ public void onEnd(Context context, Attributes endAttributes, long endNanos) { } clientDurationHistogram.record( (endNanos - state.startTimeNanos()) / NANOS_PER_MS, - state.startAttributes().toBuilder().putAll(endAttributes).build(), + endAttributes, context); } diff --git a/instrumentation-api-incubator/src/main/java/io/opentelemetry/instrumentation/api/incubator/semconv/rpc/RpcCommonAttributesExtractor.java b/instrumentation-api-incubator/src/main/java/io/opentelemetry/instrumentation/api/incubator/semconv/rpc/RpcCommonAttributesExtractor.java index f731e703586c..504fe03faca5 100644 --- a/instrumentation-api-incubator/src/main/java/io/opentelemetry/instrumentation/api/incubator/semconv/rpc/RpcCommonAttributesExtractor.java +++ b/instrumentation-api-incubator/src/main/java/io/opentelemetry/instrumentation/api/incubator/semconv/rpc/RpcCommonAttributesExtractor.java @@ -29,9 +29,7 @@ abstract class RpcCommonAttributesExtractor @Override public final void onStart(AttributesBuilder attributes, Context parentContext, REQUEST request) { - internalSet(attributes, RPC_SYSTEM, getter.getSystem(request)); - internalSet(attributes, RPC_SERVICE, getter.getService(request)); - internalSet(attributes, RPC_METHOD, getter.getMethod(request)); + } @Override @@ -42,5 +40,8 @@ public final void onEnd( @Nullable RESPONSE response, @Nullable Throwable error) { // No response attributes + internalSet(attributes, RPC_SYSTEM, getter.getSystem(request)); + internalSet(attributes, RPC_SERVICE, getter.getService(request)); + internalSet(attributes, RPC_METHOD, getter.getMethod(request)); } } diff --git a/instrumentation-api-incubator/src/main/java/io/opentelemetry/instrumentation/api/incubator/semconv/rpc/RpcServerMetrics.java b/instrumentation-api-incubator/src/main/java/io/opentelemetry/instrumentation/api/incubator/semconv/rpc/RpcServerMetrics.java index 08f50d8b3483..d2a396aff521 100644 --- a/instrumentation-api-incubator/src/main/java/io/opentelemetry/instrumentation/api/incubator/semconv/rpc/RpcServerMetrics.java +++ b/instrumentation-api-incubator/src/main/java/io/opentelemetry/instrumentation/api/incubator/semconv/rpc/RpcServerMetrics.java @@ -74,7 +74,7 @@ public void onEnd(Context context, Attributes endAttributes, long endNanos) { } serverDurationHistogram.record( (endNanos - state.startTimeNanos()) / NANOS_PER_MS, - state.startAttributes().toBuilder().putAll(endAttributes).build(), + endAttributes, context); } diff --git a/instrumentation-api/src/main/java/io/opentelemetry/instrumentation/api/instrumenter/Instrumenter.java b/instrumentation-api/src/main/java/io/opentelemetry/instrumentation/api/instrumenter/Instrumenter.java index a51990317067..4e09c154ec49 100644 --- a/instrumentation-api/src/main/java/io/opentelemetry/instrumentation/api/instrumenter/Instrumenter.java +++ b/instrumentation-api/src/main/java/io/opentelemetry/instrumentation/api/instrumenter/Instrumenter.java @@ -250,6 +250,7 @@ private void doEnd( if (operationListeners.length != 0) { long endNanos = getNanos(endTime); for (int i = operationListeners.length - 1; i >= 0; i--) { + operationListeners[i].onEnd(context, attributes, endNanos); } } diff --git a/instrumentation-api/src/main/java/io/opentelemetry/instrumentation/api/semconv/http/HttpClientAttributesExtractor.java b/instrumentation-api/src/main/java/io/opentelemetry/instrumentation/api/semconv/http/HttpClientAttributesExtractor.java index 5769d1169c3f..68dbd3a348fc 100644 --- a/instrumentation-api/src/main/java/io/opentelemetry/instrumentation/api/semconv/http/HttpClientAttributesExtractor.java +++ b/instrumentation-api/src/main/java/io/opentelemetry/instrumentation/api/semconv/http/HttpClientAttributesExtractor.java @@ -69,17 +69,7 @@ public static HttpClientAttributesExtractorBuilder 0) { - attributes.put(HttpAttributes.HTTP_REQUEST_RESEND_COUNT, resendCount); - } } @Override @@ -90,7 +80,15 @@ public void onEnd( @Nullable RESPONSE response, @Nullable Throwable error) { super.onEnd(attributes, context, request, response, error); + internalServerExtractor.onEnd(attributes, request); + String fullUrl = stripSensitiveData(getter.getUrlFull(request)); + internalSet(attributes, UrlAttributes.URL_FULL, fullUrl); + + int resendCount = resendCountIncrementer.applyAsInt(context); + if (resendCount > 0) { + attributes.put(HttpAttributes.HTTP_REQUEST_RESEND_COUNT, resendCount); + } internalNetworkExtractor.onEnd(attributes, request, response); } diff --git a/instrumentation-api/src/main/java/io/opentelemetry/instrumentation/api/semconv/http/HttpClientMetrics.java b/instrumentation-api/src/main/java/io/opentelemetry/instrumentation/api/semconv/http/HttpClientMetrics.java index 6f9974206e3d..860c35fc1926 100644 --- a/instrumentation-api/src/main/java/io/opentelemetry/instrumentation/api/semconv/http/HttpClientMetrics.java +++ b/instrumentation-api/src/main/java/io/opentelemetry/instrumentation/api/semconv/http/HttpClientMetrics.java @@ -78,9 +78,7 @@ public void onEnd(Context context, Attributes endAttributes, long endNanos) { return; } - Attributes attributes = state.startAttributes().toBuilder().putAll(endAttributes).build(); - - duration.record((endNanos - state.startTimeNanos()) / NANOS_PER_S, attributes, context); + duration.record((endNanos - state.startTimeNanos()) / NANOS_PER_S, endAttributes, context); } @AutoValue diff --git a/instrumentation-api/src/main/java/io/opentelemetry/instrumentation/api/semconv/http/HttpCommonAttributesExtractor.java b/instrumentation-api/src/main/java/io/opentelemetry/instrumentation/api/semconv/http/HttpCommonAttributesExtractor.java index a3810af5ce13..36c135f57765 100644 --- a/instrumentation-api/src/main/java/io/opentelemetry/instrumentation/api/semconv/http/HttpCommonAttributesExtractor.java +++ b/instrumentation-api/src/main/java/io/opentelemetry/instrumentation/api/semconv/http/HttpCommonAttributesExtractor.java @@ -58,6 +58,16 @@ abstract class HttpCommonAttributesExtractor< @Override public void onStart(AttributesBuilder attributes, Context parentContext, REQUEST request) { + + } + + @Override + public void onEnd( + AttributesBuilder attributes, + Context context, + REQUEST request, + @Nullable RESPONSE response, + @Nullable Throwable error) { String method = getter.getHttpRequestMethod(request); if (method == null || knownMethods.contains(method)) { internalSet(attributes, HttpAttributes.HTTP_REQUEST_METHOD, method); @@ -72,15 +82,6 @@ public void onStart(AttributesBuilder attributes, Context parentContext, REQUEST internalSet(attributes, requestAttributeKey(name), values); } } - } - - @Override - public void onEnd( - AttributesBuilder attributes, - Context context, - REQUEST request, - @Nullable RESPONSE response, - @Nullable Throwable error) { Integer statusCode = null; if (response != null) { diff --git a/instrumentation-api/src/main/java/io/opentelemetry/instrumentation/api/semconv/http/HttpServerAttributesExtractor.java b/instrumentation-api/src/main/java/io/opentelemetry/instrumentation/api/semconv/http/HttpServerAttributesExtractor.java index ed6077eab787..5c779411cb5c 100644 --- a/instrumentation-api/src/main/java/io/opentelemetry/instrumentation/api/semconv/http/HttpServerAttributesExtractor.java +++ b/instrumentation-api/src/main/java/io/opentelemetry/instrumentation/api/semconv/http/HttpServerAttributesExtractor.java @@ -75,14 +75,7 @@ public static HttpServerAttributesExtractorBuilder ClientAttributesExtractor c @Override public void onStart(AttributesBuilder attributes, Context parentContext, REQUEST request) { - internalExtractor.onStart(attributes, request); } @Override @@ -54,5 +53,7 @@ public void onEnd( Context context, REQUEST request, @Nullable RESPONSE response, - @Nullable Throwable error) {} + @Nullable Throwable error) { + internalExtractor.onEnd(attributes, request); + } } diff --git a/instrumentation-api/src/main/java/io/opentelemetry/instrumentation/api/semconv/network/ServerAttributesExtractor.java b/instrumentation-api/src/main/java/io/opentelemetry/instrumentation/api/semconv/network/ServerAttributesExtractor.java index 0f0c2ec4dc04..8e1782e61f3c 100644 --- a/instrumentation-api/src/main/java/io/opentelemetry/instrumentation/api/semconv/network/ServerAttributesExtractor.java +++ b/instrumentation-api/src/main/java/io/opentelemetry/instrumentation/api/semconv/network/ServerAttributesExtractor.java @@ -44,7 +44,6 @@ public static ServerAttributesExtractor c @Override public void onStart(AttributesBuilder attributes, Context parentContext, REQUEST request) { - internalExtractor.onStart(attributes, request); } @Override @@ -53,5 +52,7 @@ public void onEnd( Context context, REQUEST request, @Nullable RESPONSE response, - @Nullable Throwable error) {} + @Nullable Throwable error) { + internalExtractor.onEnd(attributes, request); + } } diff --git a/instrumentation-api/src/main/java/io/opentelemetry/instrumentation/api/semconv/network/internal/InternalClientAttributesExtractor.java b/instrumentation-api/src/main/java/io/opentelemetry/instrumentation/api/semconv/network/internal/InternalClientAttributesExtractor.java index cdb095e38344..91ac1fbaae0c 100644 --- a/instrumentation-api/src/main/java/io/opentelemetry/instrumentation/api/semconv/network/internal/InternalClientAttributesExtractor.java +++ b/instrumentation-api/src/main/java/io/opentelemetry/instrumentation/api/semconv/network/internal/InternalClientAttributesExtractor.java @@ -25,7 +25,7 @@ public InternalClientAttributesExtractor( this.capturePort = capturePort; } - public void onStart(AttributesBuilder attributes, REQUEST request) { + public void onEnd(AttributesBuilder attributes, REQUEST request) { AddressAndPort clientAddressAndPort = addressAndPortExtractor.extract(request); if (clientAddressAndPort.address != null) { diff --git a/instrumentation-api/src/main/java/io/opentelemetry/instrumentation/api/semconv/network/internal/InternalServerAttributesExtractor.java b/instrumentation-api/src/main/java/io/opentelemetry/instrumentation/api/semconv/network/internal/InternalServerAttributesExtractor.java index 1c23d093143e..ba08f7425bce 100644 --- a/instrumentation-api/src/main/java/io/opentelemetry/instrumentation/api/semconv/network/internal/InternalServerAttributesExtractor.java +++ b/instrumentation-api/src/main/java/io/opentelemetry/instrumentation/api/semconv/network/internal/InternalServerAttributesExtractor.java @@ -23,7 +23,7 @@ public InternalServerAttributesExtractor( this.addressAndPortExtractor = addressAndPortExtractor; } - public void onStart(AttributesBuilder attributes, REQUEST request) { + public void onEnd(AttributesBuilder attributes, REQUEST request) { AddressAndPort serverAddressAndPort = addressAndPortExtractor.extract(request); if (serverAddressAndPort.address != null) { diff --git a/instrumentation-api/src/main/java/io/opentelemetry/instrumentation/api/semconv/url/UrlAttributesExtractor.java b/instrumentation-api/src/main/java/io/opentelemetry/instrumentation/api/semconv/url/UrlAttributesExtractor.java index 58b409c29906..65e84cf20b1c 100644 --- a/instrumentation-api/src/main/java/io/opentelemetry/instrumentation/api/semconv/url/UrlAttributesExtractor.java +++ b/instrumentation-api/src/main/java/io/opentelemetry/instrumentation/api/semconv/url/UrlAttributesExtractor.java @@ -43,7 +43,6 @@ public static UrlAttributesExtractor crea @Override public void onStart(AttributesBuilder attributes, Context parentContext, REQUEST request) { - internalExtractor.onStart(attributes, request); } @Override @@ -52,5 +51,7 @@ public void onEnd( Context context, REQUEST request, @Nullable RESPONSE response, - @Nullable Throwable error) {} + @Nullable Throwable error) { + internalExtractor.onEnd(attributes, request); + } } diff --git a/instrumentation-api/src/main/java/io/opentelemetry/instrumentation/api/semconv/url/internal/InternalUrlAttributesExtractor.java b/instrumentation-api/src/main/java/io/opentelemetry/instrumentation/api/semconv/url/internal/InternalUrlAttributesExtractor.java index 37103b0c6983..be31359b7405 100644 --- a/instrumentation-api/src/main/java/io/opentelemetry/instrumentation/api/semconv/url/internal/InternalUrlAttributesExtractor.java +++ b/instrumentation-api/src/main/java/io/opentelemetry/instrumentation/api/semconv/url/internal/InternalUrlAttributesExtractor.java @@ -27,7 +27,7 @@ public InternalUrlAttributesExtractor( this.alternateSchemeProvider = alternateSchemeProvider; } - public void onStart(AttributesBuilder attributes, REQUEST request) { + public void onEnd(AttributesBuilder attributes, REQUEST request) { String urlScheme = getUrlScheme(request); String urlPath = getter.getUrlPath(request); String urlQuery = getter.getUrlQuery(request);