From 2e15fa54b50dfd6eedc552a20d477e1d551527e7 Mon Sep 17 00:00:00 2001
From: Vincent Biret <vibiret@microsoft.com>
Date: Wed, 18 Dec 2024 10:41:01 -0500
Subject: [PATCH 1/3] fix: aligns retry handler attributes with latest open
 telemetry specification

Signed-off-by: Vincent Biret <vibiret@microsoft.com>
---
 .../kiota/http/TelemetrySemanticConventions.java     |  6 ++++++
 .../kiota/http/middleware/RetryHandler.java          | 12 ++++++++----
 2 files changed, 14 insertions(+), 4 deletions(-)

diff --git a/components/http/okHttp/src/main/java/com/microsoft/kiota/http/TelemetrySemanticConventions.java b/components/http/okHttp/src/main/java/com/microsoft/kiota/http/TelemetrySemanticConventions.java
index 6fdb412fc..f55d8e3bc 100644
--- a/components/http/okHttp/src/main/java/com/microsoft/kiota/http/TelemetrySemanticConventions.java
+++ b/components/http/okHttp/src/main/java/com/microsoft/kiota/http/TelemetrySemanticConventions.java
@@ -25,6 +25,12 @@ private TelemetrySemanticConventions() {}
     public static final AttributeKey<Long> HTTP_REQUEST_RESEND_COUNT =
             longKey("http.request.resend_count"); // stable
 
+    /**
+     * HTTP Request resend delay
+     */
+    public static final AttributeKey<Long> HTTP_REQUEST_RESEND_DELAY =
+            longKey("http.request.resend_delay"); // stable
+
     /**
      * HTTP Request method
      */
diff --git a/components/http/okHttp/src/main/java/com/microsoft/kiota/http/middleware/RetryHandler.java b/components/http/okHttp/src/main/java/com/microsoft/kiota/http/middleware/RetryHandler.java
index 68fc2658f..c6eee8d81 100644
--- a/components/http/okHttp/src/main/java/com/microsoft/kiota/http/middleware/RetryHandler.java
+++ b/components/http/okHttp/src/main/java/com/microsoft/kiota/http/middleware/RetryHandler.java
@@ -15,6 +15,8 @@
 import okhttp3.Response;
 import okhttp3.ResponseBody;
 
+import static com.microsoft.kiota.http.TelemetrySemanticConventions.*;
+
 import java.io.IOException;
 import java.time.Instant;
 import java.time.format.DateTimeFormatter;
@@ -82,7 +84,8 @@ boolean retryRequest(
             @Nonnull final Response response,
             int executionCount,
             @Nonnull final Request request,
-            @Nonnull final RetryHandlerOption retryOption) {
+            @Nonnull final RetryHandlerOption retryOption,
+            @Nonnull final Span span) {
 
         // Should retry option
         // Use should retry common for all requests
@@ -107,6 +110,7 @@ && isBuffered(request)
 
         if (shouldRetry) {
             long retryInterval = getRetryAfter(response, retryOption.delay(), executionCount);
+            span.setAttribute(HTTP_REQUEST_RESEND_DELAY, Math.round(retryInterval / 1000f));
             try {
                 Thread.sleep(retryInterval);
             } catch (InterruptedException e) {
@@ -234,7 +238,7 @@ boolean isBuffered(final Request request) {
             }
 
             int executionCount = 1;
-            while (retryRequest(response, executionCount, request, retryOption)) {
+            while (retryRequest(response, executionCount, request, retryOption, span)) {
                 final Request.Builder builder =
                         request.newBuilder()
                                 .addHeader(RETRY_ATTEMPT_HEADER, String.valueOf(executionCount));
@@ -254,8 +258,8 @@ boolean isBuffered(final Request request) {
                                 request,
                                 "RetryHandler_Intercept - attempt " + executionCount,
                                 span);
-                retrySpan.setAttribute("http.retry_count", executionCount);
-                retrySpan.setAttribute("http.status_code", response.code());
+                retrySpan.setAttribute(HTTP_REQUEST_RESEND_COUNT, executionCount);
+                retrySpan.setAttribute(HTTP_RESPONSE_STATUS_CODE, response.code());
                 retrySpan.end();
                 response = chain.proceed(request);
                 if (response == null)

From d38411a5d4351b2b0ed5f59180d278ee16813064 Mon Sep 17 00:00:00 2001
From: Vincent Biret <vibiret@microsoft.com>
Date: Wed, 18 Dec 2024 10:49:43 -0500
Subject: [PATCH 2/3] fix: aligns http attributes names with latest open
 telemetry specification

Signed-off-by: Vincent Biret <vibiret@microsoft.com>
---
 .../kiota/http/OkHttpRequestAdapter.java        |  8 ++++----
 .../http/TelemetrySemanticConventions.java      | 17 +++++++++++------
 2 files changed, 15 insertions(+), 10 deletions(-)

diff --git a/components/http/okHttp/src/main/java/com/microsoft/kiota/http/OkHttpRequestAdapter.java b/components/http/okHttp/src/main/java/com/microsoft/kiota/http/OkHttpRequestAdapter.java
index b634cf7da..1b830f71e 100644
--- a/components/http/okHttp/src/main/java/com/microsoft/kiota/http/OkHttpRequestAdapter.java
+++ b/components/http/okHttp/src/main/java/com/microsoft/kiota/http/OkHttpRequestAdapter.java
@@ -255,7 +255,7 @@ private Span startSpan(
                 GlobalOpenTelemetry.getTracer(obsOptions.getTracerInstrumentationName())
                         .spanBuilder(methodName + " - " + cleanedUriTemplate)
                         .startSpan();
-        span.setAttribute("http.uri_template", decodedUriTemplate);
+        span.setAttribute(URL_TEMPLATE, decodedUriTemplate);
         return span;
     }
 
@@ -725,11 +725,11 @@ private Response getHttpResponseMessage(
             final String contentTypeHeaderValue = getHeaderValue(response, CONTENT_TYPE_HEADER_KEY);
             if (contentTypeHeaderValue != null && !contentTypeHeaderValue.isEmpty()) {
                 spanForAttributes.setAttribute(
-                        CUSTOM_HTTP_RESPONSE_CONTENT_TYPE, contentTypeHeaderValue);
+                        HTTP_RESPONSE_HEADER_CONTENT_TYPE, contentTypeHeaderValue);
             }
             spanForAttributes.setAttribute(HTTP_RESPONSE_STATUS_CODE, response.code());
             spanForAttributes.setAttribute(
-                    NETWORK_PROTOCOL_VERSION,
+                    NETWORK_PROTOCOL_NAME,
                     response.protocol().toString().toUpperCase(Locale.ROOT));
             return this.retryCAEResponseIfRequired(
                     response, requestInfo, span, spanForAttributes, claims);
@@ -869,7 +869,7 @@ public MediaType contentType() {
                                         final String contentType =
                                                 contentTypes.toArray(new String[] {})[0];
                                         spanForAttributes.setAttribute(
-                                                CUSTOM_HTTP_REQUEST_CONTENT_TYPE, contentType);
+                                                HTTP_REQUEST_HEADER_CONTENT_TYPE, contentType);
                                         return MediaType.parse(contentType);
                                     }
                                 }
diff --git a/components/http/okHttp/src/main/java/com/microsoft/kiota/http/TelemetrySemanticConventions.java b/components/http/okHttp/src/main/java/com/microsoft/kiota/http/TelemetrySemanticConventions.java
index f55d8e3bc..7ac1ac94a 100644
--- a/components/http/okHttp/src/main/java/com/microsoft/kiota/http/TelemetrySemanticConventions.java
+++ b/components/http/okHttp/src/main/java/com/microsoft/kiota/http/TelemetrySemanticConventions.java
@@ -40,14 +40,19 @@ private TelemetrySemanticConventions() {}
     /**
      * Network connection protocol version
      */
-    public static final AttributeKey<String> NETWORK_PROTOCOL_VERSION =
-            stringKey("network.protocol.version"); // stable
+    public static final AttributeKey<String> NETWORK_PROTOCOL_NAME =
+            stringKey("network.protocol.name"); // stable
 
     /**
      * Full HTTP request URL
      */
     public static final AttributeKey<String> URL_FULL = stringKey("url.full"); // stable
 
+    /**
+     * Full HTTP request URL template
+     */
+    public static final AttributeKey<String> URL_TEMPLATE = stringKey("url.uri_template"); // custom
+
     /**
      * HTTP request URL scheme
      */
@@ -78,12 +83,12 @@ private TelemetrySemanticConventions() {}
     /**
      * HTTP response content type
      */
-    public static final AttributeKey<String> CUSTOM_HTTP_RESPONSE_CONTENT_TYPE =
-            stringKey("http.response_content_type"); // custom
+    public static final AttributeKey<String> HTTP_RESPONSE_HEADER_CONTENT_TYPE =
+            stringKey("http.response.header.content-type"); // stable
 
     /**
      * HTTP request content type
      */
-    public static final AttributeKey<String> CUSTOM_HTTP_REQUEST_CONTENT_TYPE =
-            stringKey("http.request_content_type"); // custom
+    public static final AttributeKey<String> HTTP_REQUEST_HEADER_CONTENT_TYPE =
+            stringKey("http.request.header.content-type"); // stable
 }

From 6fe9258a9e13e157a03c986edb564a16d94dc234 Mon Sep 17 00:00:00 2001
From: Vincent Biret <vibiret@microsoft.com>
Date: Wed, 18 Dec 2024 10:55:32 -0500
Subject: [PATCH 3/3] chore: fixes formatting

---
 .../java/com/microsoft/kiota/http/OkHttpRequestAdapter.java   | 3 +--
 .../com/microsoft/kiota/http/middleware/RetryHandler.java     | 4 ++--
 2 files changed, 3 insertions(+), 4 deletions(-)

diff --git a/components/http/okHttp/src/main/java/com/microsoft/kiota/http/OkHttpRequestAdapter.java b/components/http/okHttp/src/main/java/com/microsoft/kiota/http/OkHttpRequestAdapter.java
index 1b830f71e..e41ff3851 100644
--- a/components/http/okHttp/src/main/java/com/microsoft/kiota/http/OkHttpRequestAdapter.java
+++ b/components/http/okHttp/src/main/java/com/microsoft/kiota/http/OkHttpRequestAdapter.java
@@ -729,8 +729,7 @@ private Response getHttpResponseMessage(
             }
             spanForAttributes.setAttribute(HTTP_RESPONSE_STATUS_CODE, response.code());
             spanForAttributes.setAttribute(
-                    NETWORK_PROTOCOL_NAME,
-                    response.protocol().toString().toUpperCase(Locale.ROOT));
+                    NETWORK_PROTOCOL_NAME, response.protocol().toString().toUpperCase(Locale.ROOT));
             return this.retryCAEResponseIfRequired(
                     response, requestInfo, span, spanForAttributes, claims);
         } catch (IOException | URISyntaxException ex) {
diff --git a/components/http/okHttp/src/main/java/com/microsoft/kiota/http/middleware/RetryHandler.java b/components/http/okHttp/src/main/java/com/microsoft/kiota/http/middleware/RetryHandler.java
index c6eee8d81..86a551e6b 100644
--- a/components/http/okHttp/src/main/java/com/microsoft/kiota/http/middleware/RetryHandler.java
+++ b/components/http/okHttp/src/main/java/com/microsoft/kiota/http/middleware/RetryHandler.java
@@ -1,5 +1,7 @@
 package com.microsoft.kiota.http.middleware;
 
+import static com.microsoft.kiota.http.TelemetrySemanticConventions.*;
+
 import com.microsoft.kiota.http.middleware.options.IShouldRetry;
 import com.microsoft.kiota.http.middleware.options.RetryHandlerOption;
 
@@ -15,8 +17,6 @@
 import okhttp3.Response;
 import okhttp3.ResponseBody;
 
-import static com.microsoft.kiota.http.TelemetrySemanticConventions.*;
-
 import java.io.IOException;
 import java.time.Instant;
 import java.time.format.DateTimeFormatter;