From 5053c01463d10a57bf87ae275ba4af6bd2e98743 Mon Sep 17 00:00:00 2001 From: Ronak Date: Thu, 21 Oct 2021 13:33:02 +0530 Subject: [PATCH 01/10] feat: add fallback support for request and response size --- .../http/HttpSemanticConventionUtils.java | 20 ++++++- .../http/HttpSemanticConventionUtilsTest.java | 18 +++++- .../fieldgenerators/HttpFieldsGenerator.java | 26 +++++++-- .../HttpFieldsGeneratorTest.java | 58 +++++++++++++++++++ 4 files changed, 114 insertions(+), 8 deletions(-) diff --git a/semantic-convention-utils/src/main/java/org/hypertrace/semantic/convention/utils/http/HttpSemanticConventionUtils.java b/semantic-convention-utils/src/main/java/org/hypertrace/semantic/convention/utils/http/HttpSemanticConventionUtils.java index 22128c115..c2530b4b2 100644 --- a/semantic-convention-utils/src/main/java/org/hypertrace/semantic/convention/utils/http/HttpSemanticConventionUtils.java +++ b/semantic-convention-utils/src/main/java/org/hypertrace/semantic/convention/utils/http/HttpSemanticConventionUtils.java @@ -9,6 +9,8 @@ import static org.hypertrace.core.semantic.convention.constants.http.OTelHttpSemanticConventions.HTTP_URL; import static org.hypertrace.core.span.constants.v1.Envoy.ENVOY_REQUEST_SIZE; import static org.hypertrace.core.span.constants.v1.Envoy.ENVOY_RESPONSE_SIZE; +import static org.hypertrace.core.span.constants.v1.Http.HTTP_HTTP_REQUEST_BODY; +import static org.hypertrace.core.span.constants.v1.Http.HTTP_HTTP_RESPONSE_BODY; import static org.hypertrace.core.span.constants.v1.Http.HTTP_PATH; import static org.hypertrace.core.span.constants.v1.Http.HTTP_REQUEST_CONTENT_TYPE; import static org.hypertrace.core.span.constants.v1.Http.HTTP_REQUEST_HEADER_PATH; @@ -71,6 +73,10 @@ public class HttpSemanticConventionUtils { private static final String OTEL_HTTP_TARGET = OTelHttpSemanticConventions.HTTP_TARGET.getValue(); private static final String RELATIVE_URL_CONTEXT = "http://hypertrace.org"; + private static final String HTTP_REQUEST_BODY = RawSpanConstants.getValue(HTTP_HTTP_REQUEST_BODY); + private static final String HTTP_RESPONSE_BODY = RawSpanConstants.getValue(HTTP_HTTP_RESPONSE_BODY); + + private static final String SLASH = "/"; private static final List USER_AGENT_ATTRIBUTES = @@ -474,13 +480,23 @@ public static Optional getHttpQueryString(Event event) { public static Optional getHttpRequestSize(Event event) { String httpRequestSize = SpanAttributeUtils.getFirstAvailableStringAttribute(event, REQUEST_SIZE_ATTRIBUTES); - return Optional.ofNullable(httpRequestSize).map(Integer::parseInt); + + Optional requestSize = Optional.ofNullable(httpRequestSize); + if(!requestSize.isEmpty()) return requestSize.map(Integer::parseInt); + + String requestBody = SpanAttributeUtils.getStringAttribute(event, HTTP_REQUEST_BODY); + return Optional.ofNullable(requestBody).map(String::length); } public static Optional getHttpResponseSize(Event event) { String httpResponseSize = SpanAttributeUtils.getFirstAvailableStringAttribute(event, RESPONSE_SIZE_ATTRIBUTES); - return Optional.ofNullable(httpResponseSize).map(Integer::parseInt); + + Optional responseSize = Optional.ofNullable(httpResponseSize); + if (!responseSize.isEmpty()) return responseSize.map(Integer::parseInt); + + String responseBody = SpanAttributeUtils.getStringAttribute(event, HTTP_RESPONSE_BODY); + return Optional.ofNullable(responseBody).map(String::length); } public static int getHttpResponseStatusCode(Event event) { diff --git a/semantic-convention-utils/src/test/java/org/hypertrace/semantic/convention/utils/http/HttpSemanticConventionUtilsTest.java b/semantic-convention-utils/src/test/java/org/hypertrace/semantic/convention/utils/http/HttpSemanticConventionUtilsTest.java index fbf7211e7..b4850b5d1 100644 --- a/semantic-convention-utils/src/test/java/org/hypertrace/semantic/convention/utils/http/HttpSemanticConventionUtilsTest.java +++ b/semantic-convention-utils/src/test/java/org/hypertrace/semantic/convention/utils/http/HttpSemanticConventionUtilsTest.java @@ -13,6 +13,8 @@ import static org.hypertrace.core.span.constants.v1.CensusResponse.CENSUS_RESPONSE_CENSUS_STATUS_CODE; import static org.hypertrace.core.span.constants.v1.Envoy.ENVOY_REQUEST_SIZE; import static org.hypertrace.core.span.constants.v1.Envoy.ENVOY_RESPONSE_SIZE; +import static org.hypertrace.core.span.constants.v1.Http.HTTP_HTTP_REQUEST_BODY; +import static org.hypertrace.core.span.constants.v1.Http.HTTP_HTTP_RESPONSE_BODY; import static org.hypertrace.core.span.constants.v1.Http.HTTP_PATH; import static org.hypertrace.core.span.constants.v1.Http.HTTP_REQUEST_CONTENT_TYPE; import static org.hypertrace.core.span.constants.v1.Http.HTTP_REQUEST_HEADER_PATH; @@ -385,9 +387,15 @@ public void testGetHttpRequestSize() { OTelHttpSemanticConventions.HTTP_REQUEST_SIZE.getValue(), AttributeValue.newBuilder().setValue("150").build(), RawSpanConstants.getValue(HTTP_REQUEST_SIZE), - AttributeValue.newBuilder().setValue("200").build())) + AttributeValue.newBuilder().setValue("200").build(), + RawSpanConstants.getValue(HTTP_HTTP_REQUEST_BODY), + AttributeValue.newBuilder().setValue("Hello, there!").build())) .build()); assertEquals(Optional.of(100), HttpSemanticConventionUtils.getHttpRequestSize(event)); + + event = createMockEventWithAttribute(RawSpanConstants.getValue(HTTP_HTTP_REQUEST_BODY), "Hello, there!"); + assertEquals(Optional.of(13), HttpSemanticConventionUtils.getHttpRequestSize(event)); + } @Test @@ -407,9 +415,15 @@ public void testGetHttpResponseSize() { RawSpanConstants.getValue(HTTP_RESPONSE_SIZE), AttributeValue.newBuilder().setValue("150").build(), OTelHttpSemanticConventions.HTTP_RESPONSE_SIZE.getValue(), - AttributeValue.newBuilder().setValue("200").build())) + AttributeValue.newBuilder().setValue("200").build(), + RawSpanConstants.getValue(HTTP_HTTP_RESPONSE_BODY), + AttributeValue.newBuilder().setValue("Hello World!").build())) .build()); assertEquals(Optional.of(100), HttpSemanticConventionUtils.getHttpResponseSize(event)); + + event = + createMockEventWithAttribute(RawSpanConstants.getValue(HTTP_HTTP_RESPONSE_BODY), "Hello World!"); + assertEquals(Optional.of(12), HttpSemanticConventionUtils.getHttpResponseSize(event)); } @Test diff --git a/span-normalizer/span-normalizer/src/main/java/org/hypertrace/core/spannormalizer/fieldgenerators/HttpFieldsGenerator.java b/span-normalizer/span-normalizer/src/main/java/org/hypertrace/core/spannormalizer/fieldgenerators/HttpFieldsGenerator.java index 60e3e1077..25766f479 100644 --- a/span-normalizer/span-normalizer/src/main/java/org/hypertrace/core/spannormalizer/fieldgenerators/HttpFieldsGenerator.java +++ b/span-normalizer/span-normalizer/src/main/java/org/hypertrace/core/spannormalizer/fieldgenerators/HttpFieldsGenerator.java @@ -362,14 +362,18 @@ private static Map> initializeFieldGenerato // Request Body fieldGeneratorMap.put( RawSpanConstants.getValue(HTTP_HTTP_REQUEST_BODY), - (key, keyValue, builder, tagsMap) -> - builder.getRequestBuilder().setBody(keyValue.getVStr())); + (key, keyValue, builder, tagsMap) -> { + builder.getRequestBuilder().setBody(keyValue.getVStr()); + setRequestSize(builder, tagsMap); + }); // Response Body fieldGeneratorMap.put( RawSpanConstants.getValue(HTTP_HTTP_RESPONSE_BODY), - (key, keyValue, builder, tagsMap) -> - builder.getResponseBuilder().setBody(keyValue.getVStr())); + (key, keyValue, builder, tagsMap) -> { + builder.getResponseBuilder().setBody(keyValue.getVStr()); + setResponseSize(builder, tagsMap); + }); // Request Size fieldGeneratorMap.put( @@ -498,6 +502,13 @@ private static void setRequestSize( FirstMatchingKeyFinder.getIntegerValueByFirstMatchingKey(tagsMap, REQUEST_SIZE_ATTRIBUTES) .ifPresent(size -> httpBuilder.getRequestBuilder().setSize(size)); + + if (!httpBuilder.getRequestBuilder().hasSize() + && tagsMap.containsKey(RawSpanConstants.getValue(HTTP_HTTP_REQUEST_BODY))) { + String requestBody = + ValueConverter.getString(tagsMap.get(RawSpanConstants.getValue(HTTP_HTTP_REQUEST_BODY))); + httpBuilder.getRequestBuilder().setSize(requestBody.length()); + } } private static void setResponseSize( @@ -508,6 +519,13 @@ private static void setResponseSize( FirstMatchingKeyFinder.getIntegerValueByFirstMatchingKey(tagsMap, RESPONSE_SIZE_ATTRIBUTES) .ifPresent(size -> httpBuilder.getResponseBuilder().setSize(size)); + + if (!httpBuilder.getResponseBuilder().hasSize() + && tagsMap.containsKey(RawSpanConstants.getValue(HTTP_HTTP_RESPONSE_BODY))) { + String responseBody = + ValueConverter.getString(tagsMap.get(RawSpanConstants.getValue(HTTP_HTTP_RESPONSE_BODY))); + httpBuilder.getResponseBuilder().setSize(responseBody.length()); + } } private static void setResponseStatusCode( diff --git a/span-normalizer/span-normalizer/src/test/java/org/hypertrace/core/spannormalizer/fieldgenerators/HttpFieldsGeneratorTest.java b/span-normalizer/span-normalizer/src/test/java/org/hypertrace/core/spannormalizer/fieldgenerators/HttpFieldsGeneratorTest.java index 1fbd36c31..03b4b6e0b 100644 --- a/span-normalizer/span-normalizer/src/test/java/org/hypertrace/core/spannormalizer/fieldgenerators/HttpFieldsGeneratorTest.java +++ b/span-normalizer/span-normalizer/src/test/java/org/hypertrace/core/spannormalizer/fieldgenerators/HttpFieldsGeneratorTest.java @@ -493,6 +493,9 @@ public void testRequestSizeTagKeysPriority() { Map tagsMap1 = new HashMap<>(); tagsMap1.put(RawSpanConstants.getValue(ENVOY_REQUEST_SIZE), createKeyValue(50)); tagsMap1.put(RawSpanConstants.getValue(HTTP_REQUEST_SIZE), createKeyValue(40)); + tagsMap1.put(OTelHttpSemanticConventions.HTTP_REQUEST_SIZE.getValue(), createKeyValue(30)); + tagsMap1.put( + RawSpanConstants.getValue(HTTP_HTTP_REQUEST_BODY), createKeyValue("Hello, there!")); Event.Builder eventBuilder1 = Event.newBuilder(); Http.Builder httpBuilder1 = httpFieldsGenerator.getProtocolBuilder(eventBuilder1); @@ -505,6 +508,7 @@ public void testRequestSizeTagKeysPriority() { Map tagsMap2 = new HashMap<>(); tagsMap2.put(RawSpanConstants.getValue(HTTP_REQUEST_SIZE), createKeyValue(35)); + tagsMap2.put(OTelHttpSemanticConventions.HTTP_REQUEST_SIZE.getValue(), createKeyValue(30)); Event.Builder eventBuilder2 = Event.newBuilder(); Http.Builder httpBuilder2 = httpFieldsGenerator.getProtocolBuilder(eventBuilder2); @@ -514,6 +518,31 @@ public void testRequestSizeTagKeysPriority() { httpFieldsGenerator.addValueToBuilder(key, keyValue, eventBuilder2, tagsMap2)); assertEquals(35, httpBuilder2.getRequestBuilder().getSize()); + + Map tagsMap3 = new HashMap<>(); + tagsMap3.put(OTelHttpSemanticConventions.HTTP_REQUEST_SIZE.getValue(), createKeyValue(30)); + + Event.Builder eventBuilder3 = Event.newBuilder(); + Http.Builder httpBuilder3 = httpFieldsGenerator.getProtocolBuilder(eventBuilder3); + + tagsMap3.forEach( + (key, keyValue) -> + httpFieldsGenerator.addValueToBuilder(key, keyValue, eventBuilder3, tagsMap3)); + + assertEquals(30, httpBuilder3.getRequestBuilder().getSize()); + + Map tagsMap4 = new HashMap<>(); + tagsMap4.put( + RawSpanConstants.getValue(HTTP_HTTP_REQUEST_BODY), createKeyValue("Hello, there!")); + + Event.Builder eventBuilder4 = Event.newBuilder(); + Http.Builder httpBuilder4 = httpFieldsGenerator.getProtocolBuilder(eventBuilder4); + + tagsMap4.forEach( + (key, keyValue) -> + httpFieldsGenerator.addValueToBuilder(key, keyValue, eventBuilder4, tagsMap4)); + + assertEquals(13, httpBuilder4.getRequestBuilder().getSize()); } @Test @@ -523,6 +552,9 @@ public void testResponseSizeTagKeysPriority() { Map tagsMap1 = new HashMap<>(); tagsMap1.put(RawSpanConstants.getValue(ENVOY_RESPONSE_SIZE), createKeyValue(100)); tagsMap1.put(RawSpanConstants.getValue(HTTP_RESPONSE_SIZE), createKeyValue(90)); + tagsMap1.put(OTelHttpSemanticConventions.HTTP_RESPONSE_SIZE.getValue(), createKeyValue(80)); + tagsMap1.put( + RawSpanConstants.getValue(HTTP_HTTP_RESPONSE_BODY), createKeyValue("Hello World!")); Event.Builder eventBuilder1 = Event.newBuilder(); Http.Builder httpBuilder1 = httpFieldsGenerator.getProtocolBuilder(eventBuilder1); @@ -535,6 +567,7 @@ public void testResponseSizeTagKeysPriority() { Map tagsMap2 = new HashMap<>(); tagsMap2.put(RawSpanConstants.getValue(HTTP_RESPONSE_SIZE), createKeyValue(85)); + tagsMap2.put(OTelHttpSemanticConventions.HTTP_RESPONSE_SIZE.getValue(), createKeyValue(80)); Event.Builder eventBuilder2 = Event.newBuilder(); Http.Builder httpBuilder2 = httpFieldsGenerator.getProtocolBuilder(eventBuilder2); @@ -544,6 +577,31 @@ public void testResponseSizeTagKeysPriority() { httpFieldsGenerator.addValueToBuilder(key, keyValue, eventBuilder2, tagsMap2)); assertEquals(85, httpBuilder2.getResponseBuilder().getSize()); + + Map tagsMap3 = new HashMap<>(); + tagsMap3.put(OTelHttpSemanticConventions.HTTP_RESPONSE_SIZE.getValue(), createKeyValue(80)); + + Event.Builder eventBuilder3 = Event.newBuilder(); + Http.Builder httpBuilder3 = httpFieldsGenerator.getProtocolBuilder(eventBuilder3); + + tagsMap3.forEach( + (key, keyValue) -> + httpFieldsGenerator.addValueToBuilder(key, keyValue, eventBuilder3, tagsMap3)); + + assertEquals(80, httpBuilder3.getResponseBuilder().getSize()); + + Map tagsMap4 = new HashMap<>(); + tagsMap4.put( + RawSpanConstants.getValue(HTTP_HTTP_RESPONSE_BODY), createKeyValue("Hello World!")); + + Event.Builder eventBuilder4 = Event.newBuilder(); + Http.Builder httpBuilder4 = httpFieldsGenerator.getProtocolBuilder(eventBuilder4); + + tagsMap4.forEach( + (key, keyValue) -> + httpFieldsGenerator.addValueToBuilder(key, keyValue, eventBuilder4, tagsMap4)); + + assertEquals(12, httpBuilder4.getResponseBuilder().getSize()); } @Test From 1ced6b17c2ff3edc88fcf55ad0b420c881742fa0 Mon Sep 17 00:00:00 2001 From: Ronak Date: Thu, 21 Oct 2021 17:38:34 +0530 Subject: [PATCH 02/10] added support for content-type tag --- .../http/HttpSemanticConventionUtils.java | 14 +++--- .../http/HttpSemanticConventionUtilsTest.java | 23 ++++++++-- .../span/constants/v1/span_attribute.proto | 2 + .../fieldgenerators/HttpFieldsGenerator.java | 14 +++++- .../HttpFieldsGeneratorTest.java | 44 ++++++++++++++++--- 5 files changed, 81 insertions(+), 16 deletions(-) diff --git a/semantic-convention-utils/src/main/java/org/hypertrace/semantic/convention/utils/http/HttpSemanticConventionUtils.java b/semantic-convention-utils/src/main/java/org/hypertrace/semantic/convention/utils/http/HttpSemanticConventionUtils.java index c2530b4b2..752a7f9dc 100644 --- a/semantic-convention-utils/src/main/java/org/hypertrace/semantic/convention/utils/http/HttpSemanticConventionUtils.java +++ b/semantic-convention-utils/src/main/java/org/hypertrace/semantic/convention/utils/http/HttpSemanticConventionUtils.java @@ -12,6 +12,7 @@ import static org.hypertrace.core.span.constants.v1.Http.HTTP_HTTP_REQUEST_BODY; import static org.hypertrace.core.span.constants.v1.Http.HTTP_HTTP_RESPONSE_BODY; import static org.hypertrace.core.span.constants.v1.Http.HTTP_PATH; +import static org.hypertrace.core.span.constants.v1.Http.HTTP_REQUEST_CONTENT_LENGTH; import static org.hypertrace.core.span.constants.v1.Http.HTTP_REQUEST_CONTENT_TYPE; import static org.hypertrace.core.span.constants.v1.Http.HTTP_REQUEST_HEADER_PATH; import static org.hypertrace.core.span.constants.v1.Http.HTTP_REQUEST_METHOD; @@ -20,6 +21,7 @@ import static org.hypertrace.core.span.constants.v1.Http.HTTP_REQUEST_SIZE; import static org.hypertrace.core.span.constants.v1.Http.HTTP_REQUEST_URL; import static org.hypertrace.core.span.constants.v1.Http.HTTP_REQUEST_X_FORWARDED_FOR_HEADER; +import static org.hypertrace.core.span.constants.v1.Http.HTTP_RESPONSE_CONTENT_LENGTH; import static org.hypertrace.core.span.constants.v1.Http.HTTP_RESPONSE_SIZE; import static org.hypertrace.core.span.constants.v1.Http.HTTP_RESPONSE_STATUS_CODE; import static org.hypertrace.core.span.constants.v1.Http.HTTP_USER_AGENT; @@ -74,8 +76,8 @@ public class HttpSemanticConventionUtils { private static final String RELATIVE_URL_CONTEXT = "http://hypertrace.org"; private static final String HTTP_REQUEST_BODY = RawSpanConstants.getValue(HTTP_HTTP_REQUEST_BODY); - private static final String HTTP_RESPONSE_BODY = RawSpanConstants.getValue(HTTP_HTTP_RESPONSE_BODY); - + private static final String HTTP_RESPONSE_BODY = + RawSpanConstants.getValue(HTTP_HTTP_RESPONSE_BODY); private static final String SLASH = "/"; @@ -122,13 +124,15 @@ public class HttpSemanticConventionUtils { List.of( RawSpanConstants.getValue(ENVOY_REQUEST_SIZE), RawSpanConstants.getValue(HTTP_REQUEST_SIZE), - OTelHttpSemanticConventions.HTTP_REQUEST_SIZE.getValue()); + OTelHttpSemanticConventions.HTTP_REQUEST_SIZE.getValue(), + RawSpanConstants.getValue(HTTP_REQUEST_CONTENT_LENGTH)); private static final List RESPONSE_SIZE_ATTRIBUTES = List.of( RawSpanConstants.getValue(ENVOY_RESPONSE_SIZE), RawSpanConstants.getValue(HTTP_RESPONSE_SIZE), - OTelHttpSemanticConventions.HTTP_RESPONSE_SIZE.getValue()); + OTelHttpSemanticConventions.HTTP_RESPONSE_SIZE.getValue(), + RawSpanConstants.getValue(HTTP_RESPONSE_CONTENT_LENGTH)); private static final List STATUS_CODE_ATTRIBUTES = List.of( @@ -482,7 +486,7 @@ public static Optional getHttpRequestSize(Event event) { SpanAttributeUtils.getFirstAvailableStringAttribute(event, REQUEST_SIZE_ATTRIBUTES); Optional requestSize = Optional.ofNullable(httpRequestSize); - if(!requestSize.isEmpty()) return requestSize.map(Integer::parseInt); + if (!requestSize.isEmpty()) return requestSize.map(Integer::parseInt); String requestBody = SpanAttributeUtils.getStringAttribute(event, HTTP_REQUEST_BODY); return Optional.ofNullable(requestBody).map(String::length); diff --git a/semantic-convention-utils/src/test/java/org/hypertrace/semantic/convention/utils/http/HttpSemanticConventionUtilsTest.java b/semantic-convention-utils/src/test/java/org/hypertrace/semantic/convention/utils/http/HttpSemanticConventionUtilsTest.java index b4850b5d1..6c2b3cbab 100644 --- a/semantic-convention-utils/src/test/java/org/hypertrace/semantic/convention/utils/http/HttpSemanticConventionUtilsTest.java +++ b/semantic-convention-utils/src/test/java/org/hypertrace/semantic/convention/utils/http/HttpSemanticConventionUtilsTest.java @@ -16,6 +16,7 @@ import static org.hypertrace.core.span.constants.v1.Http.HTTP_HTTP_REQUEST_BODY; import static org.hypertrace.core.span.constants.v1.Http.HTTP_HTTP_RESPONSE_BODY; import static org.hypertrace.core.span.constants.v1.Http.HTTP_PATH; +import static org.hypertrace.core.span.constants.v1.Http.HTTP_REQUEST_CONTENT_LENGTH; import static org.hypertrace.core.span.constants.v1.Http.HTTP_REQUEST_CONTENT_TYPE; import static org.hypertrace.core.span.constants.v1.Http.HTTP_REQUEST_HEADER_PATH; import static org.hypertrace.core.span.constants.v1.Http.HTTP_REQUEST_METHOD; @@ -24,6 +25,7 @@ import static org.hypertrace.core.span.constants.v1.Http.HTTP_REQUEST_SIZE; import static org.hypertrace.core.span.constants.v1.Http.HTTP_REQUEST_URL; import static org.hypertrace.core.span.constants.v1.Http.HTTP_REQUEST_X_FORWARDED_FOR_HEADER; +import static org.hypertrace.core.span.constants.v1.Http.HTTP_RESPONSE_CONTENT_LENGTH; import static org.hypertrace.core.span.constants.v1.Http.HTTP_RESPONSE_SIZE; import static org.hypertrace.core.span.constants.v1.Http.HTTP_USER_AGENT_REQUEST_HEADER; import static org.hypertrace.core.span.constants.v1.Http.HTTP_USER_AGENT_WITH_DASH; @@ -388,14 +390,21 @@ public void testGetHttpRequestSize() { AttributeValue.newBuilder().setValue("150").build(), RawSpanConstants.getValue(HTTP_REQUEST_SIZE), AttributeValue.newBuilder().setValue("200").build(), + RawSpanConstants.getValue(HTTP_REQUEST_CONTENT_LENGTH), + AttributeValue.newBuilder().setValue("300").build(), RawSpanConstants.getValue(HTTP_HTTP_REQUEST_BODY), AttributeValue.newBuilder().setValue("Hello, there!").build())) .build()); assertEquals(Optional.of(100), HttpSemanticConventionUtils.getHttpRequestSize(event)); - event = createMockEventWithAttribute(RawSpanConstants.getValue(HTTP_HTTP_REQUEST_BODY), "Hello, there!"); - assertEquals(Optional.of(13), HttpSemanticConventionUtils.getHttpRequestSize(event)); + event = + createMockEventWithAttribute(RawSpanConstants.getValue(HTTP_REQUEST_CONTENT_LENGTH), "300"); + assertEquals(Optional.of(300), HttpSemanticConventionUtils.getHttpRequestSize(event)); + event = + createMockEventWithAttribute( + RawSpanConstants.getValue(HTTP_HTTP_REQUEST_BODY), "Hello, there!"); + assertEquals(Optional.of(13), HttpSemanticConventionUtils.getHttpRequestSize(event)); } @Test @@ -416,13 +425,21 @@ public void testGetHttpResponseSize() { AttributeValue.newBuilder().setValue("150").build(), OTelHttpSemanticConventions.HTTP_RESPONSE_SIZE.getValue(), AttributeValue.newBuilder().setValue("200").build(), + RawSpanConstants.getValue(HTTP_RESPONSE_CONTENT_LENGTH), + AttributeValue.newBuilder().setValue("300").build(), RawSpanConstants.getValue(HTTP_HTTP_RESPONSE_BODY), AttributeValue.newBuilder().setValue("Hello World!").build())) .build()); assertEquals(Optional.of(100), HttpSemanticConventionUtils.getHttpResponseSize(event)); event = - createMockEventWithAttribute(RawSpanConstants.getValue(HTTP_HTTP_RESPONSE_BODY), "Hello World!"); + createMockEventWithAttribute( + RawSpanConstants.getValue(HTTP_RESPONSE_CONTENT_LENGTH), "300"); + assertEquals(Optional.of(300), HttpSemanticConventionUtils.getHttpResponseSize(event)); + + event = + createMockEventWithAttribute( + RawSpanConstants.getValue(HTTP_HTTP_RESPONSE_BODY), "Hello World!"); assertEquals(Optional.of(12), HttpSemanticConventionUtils.getHttpResponseSize(event)); } diff --git a/span-normalizer/raw-span-constants/src/main/proto/org/hypertrace/core/span/constants/v1/span_attribute.proto b/span-normalizer/raw-span-constants/src/main/proto/org/hypertrace/core/span/constants/v1/span_attribute.proto index acb876f5b..7a523f3d8 100644 --- a/span-normalizer/raw-span-constants/src/main/proto/org/hypertrace/core/span/constants/v1/span_attribute.proto +++ b/span-normalizer/raw-span-constants/src/main/proto/org/hypertrace/core/span/constants/v1/span_attribute.proto @@ -67,6 +67,8 @@ enum Http { HTTP_REQUEST_HEADER_PATH = 36 [(string_value) = "http.request.header.:path"]; HTTP_REQUEST_BODY = 37 [(string_value) = "request.body"]; HTTP_RESPONSE_BODY = 38 [(string_value) = "response.body"]; + HTTP_REQUEST_CONTENT_LENGTH = 39 [(string_value) = "http.request.header.content-length"]; + HTTP_RESPONSE_CONTENT_LENGTH = 40 [(string_value) = "http.response.header.content-length"]; } // Model View Controller framework related attributes diff --git a/span-normalizer/span-normalizer/src/main/java/org/hypertrace/core/spannormalizer/fieldgenerators/HttpFieldsGenerator.java b/span-normalizer/span-normalizer/src/main/java/org/hypertrace/core/spannormalizer/fieldgenerators/HttpFieldsGenerator.java index 25766f479..6a533f048 100644 --- a/span-normalizer/span-normalizer/src/main/java/org/hypertrace/core/spannormalizer/fieldgenerators/HttpFieldsGenerator.java +++ b/span-normalizer/span-normalizer/src/main/java/org/hypertrace/core/spannormalizer/fieldgenerators/HttpFieldsGenerator.java @@ -7,6 +7,7 @@ import static org.hypertrace.core.span.constants.v1.Http.HTTP_HTTP_RESPONSE_BODY; import static org.hypertrace.core.span.constants.v1.Http.HTTP_PATH; import static org.hypertrace.core.span.constants.v1.Http.HTTP_REQUEST_AUTHORITY_HEADER; +import static org.hypertrace.core.span.constants.v1.Http.HTTP_REQUEST_CONTENT_LENGTH; import static org.hypertrace.core.span.constants.v1.Http.HTTP_REQUEST_CONTENT_TYPE; import static org.hypertrace.core.span.constants.v1.Http.HTTP_REQUEST_COOKIE; import static org.hypertrace.core.span.constants.v1.Http.HTTP_REQUEST_HEADER; @@ -20,6 +21,7 @@ import static org.hypertrace.core.span.constants.v1.Http.HTTP_REQUEST_SIZE; import static org.hypertrace.core.span.constants.v1.Http.HTTP_REQUEST_URL; import static org.hypertrace.core.span.constants.v1.Http.HTTP_REQUEST_X_FORWARDED_FOR_HEADER; +import static org.hypertrace.core.span.constants.v1.Http.HTTP_RESPONSE_CONTENT_LENGTH; import static org.hypertrace.core.span.constants.v1.Http.HTTP_RESPONSE_CONTENT_TYPE; import static org.hypertrace.core.span.constants.v1.Http.HTTP_RESPONSE_COOKIE; import static org.hypertrace.core.span.constants.v1.Http.HTTP_RESPONSE_HEADER; @@ -102,13 +104,15 @@ public class HttpFieldsGenerator extends ProtocolFieldsGenerator { List.of( RawSpanConstants.getValue(ENVOY_REQUEST_SIZE), RawSpanConstants.getValue(HTTP_REQUEST_SIZE), - OTelHttpSemanticConventions.HTTP_REQUEST_SIZE.getValue()); + OTelHttpSemanticConventions.HTTP_REQUEST_SIZE.getValue(), + RawSpanConstants.getValue(HTTP_REQUEST_CONTENT_LENGTH)); private static final List RESPONSE_SIZE_ATTRIBUTES = List.of( RawSpanConstants.getValue(ENVOY_RESPONSE_SIZE), RawSpanConstants.getValue(HTTP_RESPONSE_SIZE), - OTelHttpSemanticConventions.HTTP_RESPONSE_SIZE.getValue()); + OTelHttpSemanticConventions.HTTP_RESPONSE_SIZE.getValue(), + RawSpanConstants.getValue(HTTP_RESPONSE_CONTENT_LENGTH)); private static final List STATUS_CODE_ATTRIBUTES = List.of( @@ -385,6 +389,9 @@ private static Map> initializeFieldGenerato fieldGeneratorMap.put( OTelHttpSemanticConventions.HTTP_REQUEST_SIZE.getValue(), (key, keyValue, builder, tagsMap) -> setRequestSize(builder, tagsMap)); + fieldGeneratorMap.put( + RawSpanConstants.getValue(HTTP_REQUEST_CONTENT_LENGTH), + (key, keyValue, builder, tagsMap) -> setRequestSize(builder, tagsMap)); // Response Size fieldGeneratorMap.put( @@ -396,6 +403,9 @@ private static Map> initializeFieldGenerato fieldGeneratorMap.put( OTelHttpSemanticConventions.HTTP_RESPONSE_SIZE.getValue(), (key, keyValue, builder, tagsMap) -> setResponseSize(builder, tagsMap)); + fieldGeneratorMap.put( + RawSpanConstants.getValue(HTTP_RESPONSE_CONTENT_LENGTH), + (key, keyValue, builder, tagsMap) -> setResponseSize(builder, tagsMap)); // Response status and status code fieldGeneratorMap.put( diff --git a/span-normalizer/span-normalizer/src/test/java/org/hypertrace/core/spannormalizer/fieldgenerators/HttpFieldsGeneratorTest.java b/span-normalizer/span-normalizer/src/test/java/org/hypertrace/core/spannormalizer/fieldgenerators/HttpFieldsGeneratorTest.java index 03b4b6e0b..e18644c2e 100644 --- a/span-normalizer/span-normalizer/src/test/java/org/hypertrace/core/spannormalizer/fieldgenerators/HttpFieldsGeneratorTest.java +++ b/span-normalizer/span-normalizer/src/test/java/org/hypertrace/core/spannormalizer/fieldgenerators/HttpFieldsGeneratorTest.java @@ -7,6 +7,7 @@ import static org.hypertrace.core.span.constants.v1.Http.HTTP_HTTP_RESPONSE_BODY; import static org.hypertrace.core.span.constants.v1.Http.HTTP_PATH; import static org.hypertrace.core.span.constants.v1.Http.HTTP_REQUEST_AUTHORITY_HEADER; +import static org.hypertrace.core.span.constants.v1.Http.HTTP_REQUEST_CONTENT_LENGTH; import static org.hypertrace.core.span.constants.v1.Http.HTTP_REQUEST_CONTENT_TYPE; import static org.hypertrace.core.span.constants.v1.Http.HTTP_REQUEST_COOKIE; import static org.hypertrace.core.span.constants.v1.Http.HTTP_REQUEST_HEADER; @@ -20,6 +21,7 @@ import static org.hypertrace.core.span.constants.v1.Http.HTTP_REQUEST_SIZE; import static org.hypertrace.core.span.constants.v1.Http.HTTP_REQUEST_URL; import static org.hypertrace.core.span.constants.v1.Http.HTTP_REQUEST_X_FORWARDED_FOR_HEADER; +import static org.hypertrace.core.span.constants.v1.Http.HTTP_RESPONSE_CONTENT_LENGTH; import static org.hypertrace.core.span.constants.v1.Http.HTTP_RESPONSE_CONTENT_TYPE; import static org.hypertrace.core.span.constants.v1.Http.HTTP_RESPONSE_COOKIE; import static org.hypertrace.core.span.constants.v1.Http.HTTP_RESPONSE_HEADER; @@ -494,6 +496,7 @@ public void testRequestSizeTagKeysPriority() { tagsMap1.put(RawSpanConstants.getValue(ENVOY_REQUEST_SIZE), createKeyValue(50)); tagsMap1.put(RawSpanConstants.getValue(HTTP_REQUEST_SIZE), createKeyValue(40)); tagsMap1.put(OTelHttpSemanticConventions.HTTP_REQUEST_SIZE.getValue(), createKeyValue(30)); + tagsMap1.put(RawSpanConstants.getValue(HTTP_REQUEST_CONTENT_LENGTH), createKeyValue(20)); tagsMap1.put( RawSpanConstants.getValue(HTTP_HTTP_REQUEST_BODY), createKeyValue("Hello, there!")); @@ -509,6 +512,7 @@ public void testRequestSizeTagKeysPriority() { Map tagsMap2 = new HashMap<>(); tagsMap2.put(RawSpanConstants.getValue(HTTP_REQUEST_SIZE), createKeyValue(35)); tagsMap2.put(OTelHttpSemanticConventions.HTTP_REQUEST_SIZE.getValue(), createKeyValue(30)); + tagsMap2.put(RawSpanConstants.getValue(HTTP_REQUEST_CONTENT_LENGTH), createKeyValue(20)); Event.Builder eventBuilder2 = Event.newBuilder(); Http.Builder httpBuilder2 = httpFieldsGenerator.getProtocolBuilder(eventBuilder2); @@ -521,6 +525,7 @@ public void testRequestSizeTagKeysPriority() { Map tagsMap3 = new HashMap<>(); tagsMap3.put(OTelHttpSemanticConventions.HTTP_REQUEST_SIZE.getValue(), createKeyValue(30)); + tagsMap3.put(RawSpanConstants.getValue(HTTP_REQUEST_CONTENT_LENGTH), createKeyValue(20)); Event.Builder eventBuilder3 = Event.newBuilder(); Http.Builder httpBuilder3 = httpFieldsGenerator.getProtocolBuilder(eventBuilder3); @@ -532,8 +537,7 @@ public void testRequestSizeTagKeysPriority() { assertEquals(30, httpBuilder3.getRequestBuilder().getSize()); Map tagsMap4 = new HashMap<>(); - tagsMap4.put( - RawSpanConstants.getValue(HTTP_HTTP_REQUEST_BODY), createKeyValue("Hello, there!")); + tagsMap4.put(RawSpanConstants.getValue(HTTP_REQUEST_CONTENT_LENGTH), createKeyValue(20)); Event.Builder eventBuilder4 = Event.newBuilder(); Http.Builder httpBuilder4 = httpFieldsGenerator.getProtocolBuilder(eventBuilder4); @@ -542,7 +546,20 @@ public void testRequestSizeTagKeysPriority() { (key, keyValue) -> httpFieldsGenerator.addValueToBuilder(key, keyValue, eventBuilder4, tagsMap4)); - assertEquals(13, httpBuilder4.getRequestBuilder().getSize()); + assertEquals(20, httpBuilder4.getRequestBuilder().getSize()); + + Map tagsMap5 = new HashMap<>(); + tagsMap5.put( + RawSpanConstants.getValue(HTTP_HTTP_REQUEST_BODY), createKeyValue("Hello, there!")); + + Event.Builder eventBuilder5 = Event.newBuilder(); + Http.Builder httpBuilder5 = httpFieldsGenerator.getProtocolBuilder(eventBuilder5); + + tagsMap5.forEach( + (key, keyValue) -> + httpFieldsGenerator.addValueToBuilder(key, keyValue, eventBuilder5, tagsMap5)); + + assertEquals(13, httpBuilder5.getRequestBuilder().getSize()); } @Test @@ -553,6 +570,7 @@ public void testResponseSizeTagKeysPriority() { tagsMap1.put(RawSpanConstants.getValue(ENVOY_RESPONSE_SIZE), createKeyValue(100)); tagsMap1.put(RawSpanConstants.getValue(HTTP_RESPONSE_SIZE), createKeyValue(90)); tagsMap1.put(OTelHttpSemanticConventions.HTTP_RESPONSE_SIZE.getValue(), createKeyValue(80)); + tagsMap1.put(RawSpanConstants.getValue(HTTP_RESPONSE_CONTENT_LENGTH), createKeyValue(60)); tagsMap1.put( RawSpanConstants.getValue(HTTP_HTTP_RESPONSE_BODY), createKeyValue("Hello World!")); @@ -568,6 +586,7 @@ public void testResponseSizeTagKeysPriority() { Map tagsMap2 = new HashMap<>(); tagsMap2.put(RawSpanConstants.getValue(HTTP_RESPONSE_SIZE), createKeyValue(85)); tagsMap2.put(OTelHttpSemanticConventions.HTTP_RESPONSE_SIZE.getValue(), createKeyValue(80)); + tagsMap2.put(RawSpanConstants.getValue(HTTP_RESPONSE_CONTENT_LENGTH), createKeyValue(60)); Event.Builder eventBuilder2 = Event.newBuilder(); Http.Builder httpBuilder2 = httpFieldsGenerator.getProtocolBuilder(eventBuilder2); @@ -580,6 +599,7 @@ public void testResponseSizeTagKeysPriority() { Map tagsMap3 = new HashMap<>(); tagsMap3.put(OTelHttpSemanticConventions.HTTP_RESPONSE_SIZE.getValue(), createKeyValue(80)); + tagsMap3.put(RawSpanConstants.getValue(HTTP_RESPONSE_CONTENT_LENGTH), createKeyValue(60)); Event.Builder eventBuilder3 = Event.newBuilder(); Http.Builder httpBuilder3 = httpFieldsGenerator.getProtocolBuilder(eventBuilder3); @@ -591,8 +611,7 @@ public void testResponseSizeTagKeysPriority() { assertEquals(80, httpBuilder3.getResponseBuilder().getSize()); Map tagsMap4 = new HashMap<>(); - tagsMap4.put( - RawSpanConstants.getValue(HTTP_HTTP_RESPONSE_BODY), createKeyValue("Hello World!")); + tagsMap4.put(RawSpanConstants.getValue(HTTP_RESPONSE_CONTENT_LENGTH), createKeyValue(60)); Event.Builder eventBuilder4 = Event.newBuilder(); Http.Builder httpBuilder4 = httpFieldsGenerator.getProtocolBuilder(eventBuilder4); @@ -601,7 +620,20 @@ public void testResponseSizeTagKeysPriority() { (key, keyValue) -> httpFieldsGenerator.addValueToBuilder(key, keyValue, eventBuilder4, tagsMap4)); - assertEquals(12, httpBuilder4.getResponseBuilder().getSize()); + assertEquals(60, httpBuilder4.getResponseBuilder().getSize()); + + Map tagsMap5 = new HashMap<>(); + tagsMap5.put( + RawSpanConstants.getValue(HTTP_HTTP_RESPONSE_BODY), createKeyValue("Hello World!")); + + Event.Builder eventBuilder5 = Event.newBuilder(); + Http.Builder httpBuilder5 = httpFieldsGenerator.getProtocolBuilder(eventBuilder5); + + tagsMap5.forEach( + (key, keyValue) -> + httpFieldsGenerator.addValueToBuilder(key, keyValue, eventBuilder5, tagsMap5)); + + assertEquals(12, httpBuilder5.getResponseBuilder().getSize()); } @Test From 63def41d92d2a291d26ca14eadea04a909137c20 Mon Sep 17 00:00:00 2001 From: Ronak Date: Fri, 22 Oct 2021 14:14:07 +0530 Subject: [PATCH 03/10] feat: adds support for content-length for grpc --- .../utils/rpc/RpcSemanticConventionUtils.java | 70 +++--- .../rpc/RpcSemanticConventionUtilsTest.java | 78 ++++++ .../span/normalizer/constants/RpcSpanTag.java | 2 + .../fieldgenerators/GrpcFieldsGenerator.java | 30 +++ .../GrpcFieldsGeneratorTest.java | 234 ++++++++++++++++++ 5 files changed, 382 insertions(+), 32 deletions(-) diff --git a/semantic-convention-utils/src/main/java/org/hypertrace/semantic/convention/utils/rpc/RpcSemanticConventionUtils.java b/semantic-convention-utils/src/main/java/org/hypertrace/semantic/convention/utils/rpc/RpcSemanticConventionUtils.java index f1d3073ce..08eaaf299 100644 --- a/semantic-convention-utils/src/main/java/org/hypertrace/semantic/convention/utils/rpc/RpcSemanticConventionUtils.java +++ b/semantic-convention-utils/src/main/java/org/hypertrace/semantic/convention/utils/rpc/RpcSemanticConventionUtils.java @@ -12,10 +12,12 @@ import static org.hypertrace.core.span.normalizer.constants.RpcSpanTag.RPC_ERROR_MESSAGE; import static org.hypertrace.core.span.normalizer.constants.RpcSpanTag.RPC_REQUEST_BODY; import static org.hypertrace.core.span.normalizer.constants.RpcSpanTag.RPC_REQUEST_METADATA_AUTHORITY; +import static org.hypertrace.core.span.normalizer.constants.RpcSpanTag.RPC_REQUEST_METADATA_CONTENT_LENGTH; import static org.hypertrace.core.span.normalizer.constants.RpcSpanTag.RPC_REQUEST_METADATA_PATH; import static org.hypertrace.core.span.normalizer.constants.RpcSpanTag.RPC_REQUEST_METADATA_USER_AGENT; import static org.hypertrace.core.span.normalizer.constants.RpcSpanTag.RPC_REQUEST_METADATA_X_FORWARDED_FOR; import static org.hypertrace.core.span.normalizer.constants.RpcSpanTag.RPC_RESPONSE_BODY; +import static org.hypertrace.core.span.normalizer.constants.RpcSpanTag.RPC_RESPONSE_METADATA_CONTENT_LENGTH; import com.google.common.base.Joiner; import com.google.common.base.Splitter; @@ -275,23 +277,25 @@ public static Optional getGrpcRequestSize(Event event) { } Map attributeValueMap = event.getAttributes().getAttributeMap(); - if ((attributeValueMap.get(RawSpanConstants.getValue(GRPC_REQUEST_BODY)) != null) - || (isRpcSystemGrpc(attributeValueMap) - && attributeValueMap.get(RPC_REQUEST_BODY.getValue()) != null)) { - - if (attributeValueMap.get(RawSpanConstants.getValue(ENVOY_REQUEST_SIZE)) != null) { - return Optional.of( - Integer.parseInt( - attributeValueMap.get(RawSpanConstants.getValue(ENVOY_REQUEST_SIZE)).getValue())); - } else if (attributeValueMap.get(RawSpanConstants.getValue(GRPC_REQUEST_BODY)) != null) { - String requestBody = - attributeValueMap.get(RawSpanConstants.getValue(GRPC_REQUEST_BODY)).getValue(); - return Optional.of(requestBody.length()); - } else if (attributeValueMap.get(RPC_REQUEST_BODY.getValue()) != null) { - String requestBody = attributeValueMap.get(RPC_REQUEST_BODY.getValue()).getValue(); - return Optional.of(requestBody.length()); - } + if (attributeValueMap.get(RawSpanConstants.getValue(ENVOY_REQUEST_SIZE)) != null) { + return Optional.of( + Integer.parseInt( + attributeValueMap.get(RawSpanConstants.getValue(ENVOY_REQUEST_SIZE)).getValue())); + } else if (isRpcSystemGrpc(attributeValueMap) + && attributeValueMap.get(RPC_REQUEST_METADATA_CONTENT_LENGTH.getValue()) != null) { + return Optional.of( + Integer.parseInt( + attributeValueMap.get(RPC_REQUEST_METADATA_CONTENT_LENGTH.getValue()).getValue())); + } else if (attributeValueMap.get(RawSpanConstants.getValue(GRPC_REQUEST_BODY)) != null) { + String requestBody = + attributeValueMap.get(RawSpanConstants.getValue(GRPC_REQUEST_BODY)).getValue(); + return Optional.of(requestBody.length()); + } else if (isRpcSystemGrpc(attributeValueMap) + && attributeValueMap.get(RPC_REQUEST_BODY.getValue()) != null) { + String requestBody = attributeValueMap.get(RPC_REQUEST_BODY.getValue()).getValue(); + return Optional.of(requestBody.length()); } + return Optional.empty(); } @@ -301,23 +305,25 @@ public static Optional getGrpcResponseSize(Event event) { } Map attributeValueMap = event.getAttributes().getAttributeMap(); - if ((attributeValueMap.get(RawSpanConstants.getValue(GRPC_RESPONSE_BODY)) != null) - || (isRpcSystemGrpc(attributeValueMap) - && attributeValueMap.get(RPC_RESPONSE_BODY.getValue()) != null)) { - - if (attributeValueMap.get(RawSpanConstants.getValue(ENVOY_RESPONSE_SIZE)) != null) { - return Optional.of( - Integer.parseInt( - attributeValueMap.get(RawSpanConstants.getValue(ENVOY_RESPONSE_SIZE)).getValue())); - } else if (attributeValueMap.get(RawSpanConstants.getValue(GRPC_RESPONSE_BODY)) != null) { - String requestBody = - attributeValueMap.get(RawSpanConstants.getValue(GRPC_RESPONSE_BODY)).getValue(); - return Optional.of(requestBody.length()); - } else if (attributeValueMap.get(RPC_RESPONSE_BODY.getValue()) != null) { - String requestBody = attributeValueMap.get(RPC_RESPONSE_BODY.getValue()).getValue(); - return Optional.of(requestBody.length()); - } + if (attributeValueMap.get(RawSpanConstants.getValue(ENVOY_RESPONSE_SIZE)) != null) { + return Optional.of( + Integer.parseInt( + attributeValueMap.get(RawSpanConstants.getValue(ENVOY_RESPONSE_SIZE)).getValue())); + } else if (isRpcSystemGrpc(attributeValueMap) + && attributeValueMap.get(RPC_RESPONSE_METADATA_CONTENT_LENGTH.getValue()) != null) { + return Optional.of( + Integer.parseInt( + attributeValueMap.get(RPC_RESPONSE_METADATA_CONTENT_LENGTH.getValue()).getValue())); + } else if (attributeValueMap.get(RawSpanConstants.getValue(GRPC_RESPONSE_BODY)) != null) { + String requestBody = + attributeValueMap.get(RawSpanConstants.getValue(GRPC_RESPONSE_BODY)).getValue(); + return Optional.of(requestBody.length()); + } else if (isRpcSystemGrpc(attributeValueMap) + && attributeValueMap.get(RPC_RESPONSE_BODY.getValue()) != null) { + String requestBody = attributeValueMap.get(RPC_RESPONSE_BODY.getValue()).getValue(); + return Optional.of(requestBody.length()); } + return Optional.empty(); } diff --git a/semantic-convention-utils/src/test/java/org/hypertrace/semantic/convention/utils/rpc/RpcSemanticConventionUtilsTest.java b/semantic-convention-utils/src/test/java/org/hypertrace/semantic/convention/utils/rpc/RpcSemanticConventionUtilsTest.java index 17b238286..41f13b447 100644 --- a/semantic-convention-utils/src/test/java/org/hypertrace/semantic/convention/utils/rpc/RpcSemanticConventionUtilsTest.java +++ b/semantic-convention-utils/src/test/java/org/hypertrace/semantic/convention/utils/rpc/RpcSemanticConventionUtilsTest.java @@ -13,10 +13,12 @@ import static org.hypertrace.core.span.normalizer.constants.RpcSpanTag.RPC_ERROR_MESSAGE; import static org.hypertrace.core.span.normalizer.constants.RpcSpanTag.RPC_REQUEST_BODY; import static org.hypertrace.core.span.normalizer.constants.RpcSpanTag.RPC_REQUEST_METADATA_AUTHORITY; +import static org.hypertrace.core.span.normalizer.constants.RpcSpanTag.RPC_REQUEST_METADATA_CONTENT_LENGTH; import static org.hypertrace.core.span.normalizer.constants.RpcSpanTag.RPC_REQUEST_METADATA_PATH; import static org.hypertrace.core.span.normalizer.constants.RpcSpanTag.RPC_REQUEST_METADATA_USER_AGENT; import static org.hypertrace.core.span.normalizer.constants.RpcSpanTag.RPC_REQUEST_METADATA_X_FORWARDED_FOR; import static org.hypertrace.core.span.normalizer.constants.RpcSpanTag.RPC_RESPONSE_BODY; +import static org.hypertrace.core.span.normalizer.constants.RpcSpanTag.RPC_RESPONSE_METADATA_CONTENT_LENGTH; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertTrue; @@ -366,6 +368,9 @@ public void testGetGrpcRequestSizePriority() { put( RawSpanConstants.getValue(ENVOY_REQUEST_SIZE), AttributeValue.newBuilder().setValue("1").build()); + put( + RPC_REQUEST_METADATA_CONTENT_LENGTH.getValue(), + AttributeValue.newBuilder().setValue("2").build()); put( RawSpanConstants.getValue(GRPC_REQUEST_BODY), AttributeValue.newBuilder().setValue("some grpc request body").build()); @@ -381,6 +386,27 @@ public void testGetGrpcRequestSizePriority() { .thenReturn(Attributes.newBuilder().setAttributeMap(tagsMap).build()); assertEquals(Optional.of(1), RpcSemanticConventionUtils.getGrpcRequestSize(event)); + tagsMap = + new HashMap<>() { + { + put( + RPC_REQUEST_METADATA_CONTENT_LENGTH.getValue(), + AttributeValue.newBuilder().setValue("2").build()); + put( + RawSpanConstants.getValue(GRPC_REQUEST_BODY), + AttributeValue.newBuilder().setValue("some grpc request body").build()); + put( + RPC_REQUEST_BODY.getValue(), + AttributeValue.newBuilder().setValue("some rpc request body").build()); + put("rpc.system", AttributeValue.newBuilder().setValue("grpc").build()); + } + }; + + event = mock(Event.class); + when(event.getAttributes()) + .thenReturn(Attributes.newBuilder().setAttributeMap(tagsMap).build()); + assertEquals(Optional.of(2), RpcSemanticConventionUtils.getGrpcRequestSize(event)); + tagsMap = new HashMap<>() { { @@ -413,6 +439,20 @@ public void testGetGrpcRequestSizePriority() { when(event.getAttributes()) .thenReturn(Attributes.newBuilder().setAttributeMap(tagsMap).build()); assertEquals(Optional.of(21), RpcSemanticConventionUtils.getGrpcRequestSize(event)); + + tagsMap = + new HashMap<>() { + { + put( + RPC_REQUEST_BODY.getValue(), + AttributeValue.newBuilder().setValue("some rpc request body").build()); + } + }; + + event = mock(Event.class); + when(event.getAttributes()) + .thenReturn(Attributes.newBuilder().setAttributeMap(tagsMap).build()); + assertEquals(Optional.empty(), RpcSemanticConventionUtils.getGrpcRequestSize(event)); } @Test @@ -448,6 +488,9 @@ public void testGetGrpcResponseSizePriority() { put( RawSpanConstants.getValue(ENVOY_RESPONSE_SIZE), AttributeValue.newBuilder().setValue("1").build()); + put( + RPC_RESPONSE_METADATA_CONTENT_LENGTH.getValue(), + AttributeValue.newBuilder().setValue("2").build()); put( RawSpanConstants.getValue(GRPC_RESPONSE_BODY), AttributeValue.newBuilder().setValue("some grpc response body").build()); @@ -463,6 +506,27 @@ public void testGetGrpcResponseSizePriority() { .thenReturn(Attributes.newBuilder().setAttributeMap(tagsMap).build()); assertEquals(Optional.of(1), RpcSemanticConventionUtils.getGrpcResponseSize(event)); + tagsMap = + new HashMap<>() { + { + put( + RPC_RESPONSE_METADATA_CONTENT_LENGTH.getValue(), + AttributeValue.newBuilder().setValue("2").build()); + put( + RawSpanConstants.getValue(GRPC_RESPONSE_BODY), + AttributeValue.newBuilder().setValue("some grpc response body").build()); + put( + RPC_RESPONSE_BODY.getValue(), + AttributeValue.newBuilder().setValue("some rpc response body").build()); + put("rpc.system", AttributeValue.newBuilder().setValue("grpc").build()); + } + }; + + event = mock(Event.class); + when(event.getAttributes()) + .thenReturn(Attributes.newBuilder().setAttributeMap(tagsMap).build()); + assertEquals(Optional.of(2), RpcSemanticConventionUtils.getGrpcResponseSize(event)); + tagsMap = new HashMap<>() { { @@ -495,6 +559,20 @@ public void testGetGrpcResponseSizePriority() { when(event.getAttributes()) .thenReturn(Attributes.newBuilder().setAttributeMap(tagsMap).build()); assertEquals(Optional.of(22), RpcSemanticConventionUtils.getGrpcResponseSize(event)); + + tagsMap = + new HashMap<>() { + { + put( + RPC_RESPONSE_BODY.getValue(), + AttributeValue.newBuilder().setValue("some rpc response body").build()); + } + }; + + event = mock(Event.class); + when(event.getAttributes()) + .thenReturn(Attributes.newBuilder().setAttributeMap(tagsMap).build()); + assertEquals(Optional.empty(), RpcSemanticConventionUtils.getGrpcResponseSize(event)); } @Test diff --git a/span-normalizer/span-normalizer-constants/src/main/java/org/hypertrace/core/span/normalizer/constants/RpcSpanTag.java b/span-normalizer/span-normalizer-constants/src/main/java/org/hypertrace/core/span/normalizer/constants/RpcSpanTag.java index b010b4d5c..a98004ae4 100644 --- a/span-normalizer/span-normalizer-constants/src/main/java/org/hypertrace/core/span/normalizer/constants/RpcSpanTag.java +++ b/span-normalizer/span-normalizer-constants/src/main/java/org/hypertrace/core/span/normalizer/constants/RpcSpanTag.java @@ -11,9 +11,11 @@ public enum RpcSpanTag { RPC_REQUEST_METADATA_X_FORWARDED_FOR("rpc.request.metadata.x-forwarded-for"), RPC_REQUEST_METADATA_AUTHORITY("rpc.request.metadata.:authority"), RPC_REQUEST_METADATA_CONTENT_TYPE("rpc.request.metadata.content-type"), + RPC_REQUEST_METADATA_CONTENT_LENGTH("rpc.request.metadata.content-length"), RPC_REQUEST_METADATA_PATH("rpc.request.metadata.:path"), RPC_REQUEST_METADATA_USER_AGENT("rpc.request.metadata.user-agent"), RPC_RESPONSE_METADATA_CONTENT_TYPE("rpc.response.metadata.content-type"), + RPC_RESPONSE_METADATA_CONTENT_LENGTH("rpc.response.metadata.content-length"), RPC_BODY_DECODE_RAW("rpc.body.decode_raw"); private final String value; diff --git a/span-normalizer/span-normalizer/src/main/java/org/hypertrace/core/spannormalizer/fieldgenerators/GrpcFieldsGenerator.java b/span-normalizer/span-normalizer/src/main/java/org/hypertrace/core/spannormalizer/fieldgenerators/GrpcFieldsGenerator.java index 446c04d7f..335c603b0 100644 --- a/span-normalizer/span-normalizer/src/main/java/org/hypertrace/core/spannormalizer/fieldgenerators/GrpcFieldsGenerator.java +++ b/span-normalizer/span-normalizer/src/main/java/org/hypertrace/core/spannormalizer/fieldgenerators/GrpcFieldsGenerator.java @@ -16,11 +16,13 @@ import static org.hypertrace.core.span.normalizer.constants.RpcSpanTag.RPC_ERROR_NAME; import static org.hypertrace.core.span.normalizer.constants.RpcSpanTag.RPC_REQUEST_BODY; import static org.hypertrace.core.span.normalizer.constants.RpcSpanTag.RPC_REQUEST_METADATA_AUTHORITY; +import static org.hypertrace.core.span.normalizer.constants.RpcSpanTag.RPC_REQUEST_METADATA_CONTENT_LENGTH; import static org.hypertrace.core.span.normalizer.constants.RpcSpanTag.RPC_REQUEST_METADATA_CONTENT_TYPE; import static org.hypertrace.core.span.normalizer.constants.RpcSpanTag.RPC_REQUEST_METADATA_PATH; import static org.hypertrace.core.span.normalizer.constants.RpcSpanTag.RPC_REQUEST_METADATA_USER_AGENT; import static org.hypertrace.core.span.normalizer.constants.RpcSpanTag.RPC_REQUEST_METADATA_X_FORWARDED_FOR; import static org.hypertrace.core.span.normalizer.constants.RpcSpanTag.RPC_RESPONSE_BODY; +import static org.hypertrace.core.span.normalizer.constants.RpcSpanTag.RPC_RESPONSE_METADATA_CONTENT_LENGTH; import static org.hypertrace.core.span.normalizer.constants.RpcSpanTag.RPC_RESPONSE_METADATA_CONTENT_TYPE; import static org.hypertrace.core.span.normalizer.constants.RpcSpanTag.RPC_STATUS_CODE; @@ -126,6 +128,14 @@ private static Map> initializeFieldGenerato .getMetadata() .putAll(parseMetadataString(ValueConverter.getString(keyValue)))); + fieldGeneratorMap.put( + RawSpanConstants.getValue(ENVOY_REQUEST_SIZE), + (key, keyValue, builder, tagsMap) -> setRequestSize(builder, tagsMap)); + + fieldGeneratorMap.put( + RawSpanConstants.getValue(ENVOY_RESPONSE_SIZE), + (key, keyValue, builder, tagsMap) -> setResponseSize(builder, tagsMap)); + return fieldGeneratorMap; } @@ -206,6 +216,14 @@ private static Map> initializeRpcFieldGener .getResponseMetadataBuilder() .setContentType(ValueConverter.getString(keyValue))); + fieldGeneratorMap.put( + RPC_REQUEST_METADATA_CONTENT_LENGTH.getValue(), + (key, keyValue, builder, tagsMap) -> setRequestSize(builder, tagsMap)); + + fieldGeneratorMap.put( + RPC_RESPONSE_METADATA_CONTENT_LENGTH.getValue(), + (key, keyValue, builder, tagsMap) -> setResponseSize(builder, tagsMap)); + return fieldGeneratorMap; } @@ -239,6 +257,12 @@ private static void setRequestSize( .setSize( ValueConverter.getInteger( tagsMap.get(RawSpanConstants.getValue(ENVOY_REQUEST_SIZE)))); + } else if (tagsMap.containsKey(RPC_REQUEST_METADATA_CONTENT_LENGTH.getValue())) { + grpcBuilder + .getRequestBuilder() + .setSize( + ValueConverter.getInteger( + tagsMap.get(RPC_REQUEST_METADATA_CONTENT_LENGTH.getValue()))); } else if (tagsMap.containsKey(RawSpanConstants.getValue(GRPC_REQUEST_BODY))) { String requestBody = ValueConverter.getString(tagsMap.get(RawSpanConstants.getValue(GRPC_REQUEST_BODY))); @@ -257,6 +281,12 @@ private static void setResponseSize( .setSize( ValueConverter.getInteger( tagsMap.get(RawSpanConstants.getValue(ENVOY_RESPONSE_SIZE)))); + } else if (tagsMap.containsKey(RPC_RESPONSE_METADATA_CONTENT_LENGTH.getValue())) { + grpcBuilder + .getResponseBuilder() + .setSize( + ValueConverter.getInteger( + tagsMap.get(RPC_RESPONSE_METADATA_CONTENT_LENGTH.getValue()))); } else if (tagsMap.containsKey(RawSpanConstants.getValue(GRPC_RESPONSE_BODY))) { String responseBody = ValueConverter.getString(tagsMap.get(RawSpanConstants.getValue(GRPC_RESPONSE_BODY))); diff --git a/span-normalizer/span-normalizer/src/test/java/org/hypertrace/core/spannormalizer/fieldgenerators/GrpcFieldsGeneratorTest.java b/span-normalizer/span-normalizer/src/test/java/org/hypertrace/core/spannormalizer/fieldgenerators/GrpcFieldsGeneratorTest.java index 43ac9101f..faf3150e5 100644 --- a/span-normalizer/span-normalizer/src/test/java/org/hypertrace/core/spannormalizer/fieldgenerators/GrpcFieldsGeneratorTest.java +++ b/span-normalizer/span-normalizer/src/test/java/org/hypertrace/core/spannormalizer/fieldgenerators/GrpcFieldsGeneratorTest.java @@ -16,6 +16,12 @@ import static org.hypertrace.core.span.constants.v1.Grpc.GRPC_RESPONSE_BODY; import static org.hypertrace.core.span.constants.v1.Grpc.GRPC_RESPONSE_METADATA; import static org.hypertrace.core.span.constants.v1.Grpc.GRPC_STATUS_CODE; +import static org.hypertrace.core.span.normalizer.constants.OTelRpcSystem.OTEL_RPC_SYSTEM_GRPC; +import static org.hypertrace.core.span.normalizer.constants.OTelSpanTag.OTEL_SPAN_TAG_RPC_SYSTEM; +import static org.hypertrace.core.span.normalizer.constants.RpcSpanTag.RPC_REQUEST_BODY; +import static org.hypertrace.core.span.normalizer.constants.RpcSpanTag.RPC_REQUEST_METADATA_CONTENT_LENGTH; +import static org.hypertrace.core.span.normalizer.constants.RpcSpanTag.RPC_RESPONSE_BODY; +import static org.hypertrace.core.span.normalizer.constants.RpcSpanTag.RPC_RESPONSE_METADATA_CONTENT_LENGTH; import static org.hypertrace.core.spannormalizer.utils.TestUtils.createKeyValue; import static org.junit.jupiter.api.Assertions.assertEquals; @@ -134,6 +140,234 @@ public void testGrpcFieldsConverterEnvoyRequestAndResponseSizeHigherPriority() { Assertions.assertEquals(400, grpcBuilder.getResponseBuilder().getSize()); } + @Test + public void testGrpcRequestSizeTagKeysPriority() { + GrpcFieldsGenerator grpcFieldsGenerator = new GrpcFieldsGenerator(); + RpcFieldsGenerator rpcFieldsGenerator = new RpcFieldsGenerator(grpcFieldsGenerator); + + // test 1: respect ENVOY_REQUEST_SIZE + Map tagsMap1 = new HashMap<>(); + tagsMap1.put(RawSpanConstants.getValue(ENVOY_REQUEST_SIZE), createKeyValue(100)); + tagsMap1.put( + RawSpanConstants.getValue(GRPC_REQUEST_BODY), createKeyValue("Hey Grpc, you there!!")); + + Map rpcTagsMap1 = new HashMap<>(); + rpcTagsMap1.put( + OTEL_SPAN_TAG_RPC_SYSTEM.getValue(), createKeyValue(OTEL_RPC_SYSTEM_GRPC.getValue())); + rpcTagsMap1.put(RPC_REQUEST_METADATA_CONTENT_LENGTH.getValue(), createKeyValue(200)); + rpcTagsMap1.put(RPC_REQUEST_BODY.getValue(), createKeyValue("Hey rpc, you there!!")); + + Map combinedTags1 = new HashMap<>(); + combinedTags1.putAll(tagsMap1); + combinedTags1.putAll(rpcTagsMap1); + + Event.Builder eventBuilder1 = Event.newBuilder(); + Grpc.Builder grpcBuilder1 = grpcFieldsGenerator.getProtocolBuilder(eventBuilder1); + + tagsMap1.forEach( + (key, keyValue) -> + grpcFieldsGenerator.addValueToBuilder(key, keyValue, eventBuilder1, combinedTags1)); + + rpcTagsMap1.forEach( + (key, keyValue) -> + rpcFieldsGenerator.handleKeyIfNecessary(key, keyValue, eventBuilder1, combinedTags1)); + + assertEquals(100, grpcBuilder1.getRequestBuilder().getSize()); + + // test 2: respect RPC_REQUEST_METADATA_CONTENT_LENGTH + Map tagsMap2 = new HashMap<>(); + tagsMap2.put( + RawSpanConstants.getValue(GRPC_REQUEST_BODY), createKeyValue("Hey Grpc, you there!!")); + + Map rpcTagsMap2 = new HashMap<>(); + rpcTagsMap2.put( + OTEL_SPAN_TAG_RPC_SYSTEM.getValue(), createKeyValue(OTEL_RPC_SYSTEM_GRPC.getValue())); + rpcTagsMap2.put(RPC_REQUEST_METADATA_CONTENT_LENGTH.getValue(), createKeyValue(200)); + rpcTagsMap2.put(RPC_REQUEST_BODY.getValue(), createKeyValue("Hey rpc, you there!!")); + + Map combinedTags2 = new HashMap<>(); + combinedTags2.putAll(tagsMap2); + combinedTags2.putAll(rpcTagsMap2); + + Event.Builder eventBuilder2 = Event.newBuilder(); + Grpc.Builder grpcBuilder2 = grpcFieldsGenerator.getProtocolBuilder(eventBuilder2); + + tagsMap2.forEach( + (key, keyValue) -> + grpcFieldsGenerator.addValueToBuilder(key, keyValue, eventBuilder2, combinedTags2)); + + rpcTagsMap2.forEach( + (key, keyValue) -> + rpcFieldsGenerator.handleKeyIfNecessary(key, keyValue, eventBuilder2, combinedTags2)); + + assertEquals(200, grpcBuilder2.getRequestBuilder().getSize()); + + // test 3: respect GRPC_REQUEST_BODY + Map tagsMap3 = new HashMap<>(); + tagsMap3.put( + RawSpanConstants.getValue(GRPC_REQUEST_BODY), createKeyValue("Hey Grpc, you there!!")); + + Map rpcTagsMap3 = new HashMap<>(); + rpcTagsMap3.put( + OTEL_SPAN_TAG_RPC_SYSTEM.getValue(), createKeyValue(OTEL_RPC_SYSTEM_GRPC.getValue())); + rpcTagsMap3.put(RPC_REQUEST_BODY.getValue(), createKeyValue("Hey rpc, you there!!")); + + Map combinedTags3 = new HashMap<>(); + combinedTags3.putAll(tagsMap3); + combinedTags3.putAll(rpcTagsMap3); + + Event.Builder eventBuilder3 = Event.newBuilder(); + Grpc.Builder grpcBuilder3 = grpcFieldsGenerator.getProtocolBuilder(eventBuilder3); + + tagsMap3.forEach( + (key, keyValue) -> + grpcFieldsGenerator.addValueToBuilder(key, keyValue, eventBuilder3, combinedTags3)); + + rpcTagsMap3.forEach( + (key, keyValue) -> + rpcFieldsGenerator.handleKeyIfNecessary(key, keyValue, eventBuilder3, combinedTags3)); + + assertEquals(21, grpcBuilder3.getRequestBuilder().getSize()); + + // test 4: respect RPC_REQUEST_BODY + Map tagsMap4 = new HashMap<>(); + Map rpcTagsMap4 = new HashMap<>(); + rpcTagsMap4.put( + OTEL_SPAN_TAG_RPC_SYSTEM.getValue(), createKeyValue(OTEL_RPC_SYSTEM_GRPC.getValue())); + rpcTagsMap4.put(RPC_REQUEST_BODY.getValue(), createKeyValue("Hey rpc, you there!!")); + + Map combinedTags4 = new HashMap<>(); + combinedTags4.putAll(tagsMap4); + combinedTags4.putAll(rpcTagsMap4); + + Event.Builder eventBuilder4 = Event.newBuilder(); + Grpc.Builder grpcBuilder4 = grpcFieldsGenerator.getProtocolBuilder(eventBuilder4); + + tagsMap3.forEach( + (key, keyValue) -> + grpcFieldsGenerator.addValueToBuilder(key, keyValue, eventBuilder4, combinedTags4)); + + rpcTagsMap3.forEach( + (key, keyValue) -> + rpcFieldsGenerator.handleKeyIfNecessary(key, keyValue, eventBuilder4, combinedTags4)); + + assertEquals(20, grpcBuilder4.getRequestBuilder().getSize()); + } + + @Test + public void testGrpcResponseSizeTagKeysPriority() { + GrpcFieldsGenerator grpcFieldsGenerator = new GrpcFieldsGenerator(); + RpcFieldsGenerator rpcFieldsGenerator = new RpcFieldsGenerator(grpcFieldsGenerator); + + // test 1: respect ENVOY_RESPONSE_SIZE + Map tagsMap1 = new HashMap<>(); + tagsMap1.put(RawSpanConstants.getValue(ENVOY_RESPONSE_SIZE), createKeyValue(10)); + tagsMap1.put( + RawSpanConstants.getValue(GRPC_RESPONSE_BODY), createKeyValue("Hello from grpc!!")); + + Map rpcTagsMap1 = new HashMap<>(); + rpcTagsMap1.put( + OTEL_SPAN_TAG_RPC_SYSTEM.getValue(), createKeyValue(OTEL_RPC_SYSTEM_GRPC.getValue())); + rpcTagsMap1.put(RPC_RESPONSE_METADATA_CONTENT_LENGTH.getValue(), createKeyValue(20)); + rpcTagsMap1.put(RPC_RESPONSE_BODY.getValue(), createKeyValue("Hello from rpc!!")); + + Map combinedTags1 = new HashMap<>(); + combinedTags1.putAll(tagsMap1); + combinedTags1.putAll(rpcTagsMap1); + + Event.Builder eventBuilder1 = Event.newBuilder(); + Grpc.Builder grpcBuilder1 = grpcFieldsGenerator.getProtocolBuilder(eventBuilder1); + + tagsMap1.forEach( + (key, keyValue) -> + grpcFieldsGenerator.addValueToBuilder(key, keyValue, eventBuilder1, combinedTags1)); + + rpcTagsMap1.forEach( + (key, keyValue) -> + rpcFieldsGenerator.handleKeyIfNecessary(key, keyValue, eventBuilder1, combinedTags1)); + + assertEquals(10, grpcBuilder1.getResponseBuilder().getSize()); + + // test 2: respect RPC_RESPONSE_METADATA_CONTENT_LENGTH + Map tagsMap2 = new HashMap<>(); + tagsMap2.put( + RawSpanConstants.getValue(GRPC_RESPONSE_BODY), createKeyValue("Hello from grpc!!")); + + Map rpcTagsMap2 = new HashMap<>(); + rpcTagsMap2.put( + OTEL_SPAN_TAG_RPC_SYSTEM.getValue(), createKeyValue(OTEL_RPC_SYSTEM_GRPC.getValue())); + rpcTagsMap2.put(RPC_RESPONSE_METADATA_CONTENT_LENGTH.getValue(), createKeyValue(200)); + rpcTagsMap2.put(RPC_RESPONSE_BODY.getValue(), createKeyValue("Hello from rpc!!")); + + Map combinedTags2 = new HashMap<>(); + combinedTags2.putAll(tagsMap2); + combinedTags2.putAll(rpcTagsMap2); + + Event.Builder eventBuilder2 = Event.newBuilder(); + Grpc.Builder grpcBuilder2 = grpcFieldsGenerator.getProtocolBuilder(eventBuilder2); + + tagsMap2.forEach( + (key, keyValue) -> + grpcFieldsGenerator.addValueToBuilder(key, keyValue, eventBuilder2, combinedTags2)); + + rpcTagsMap2.forEach( + (key, keyValue) -> + rpcFieldsGenerator.handleKeyIfNecessary(key, keyValue, eventBuilder2, combinedTags2)); + + assertEquals(200, grpcBuilder2.getResponseBuilder().getSize()); + + // test 3: respect GRPC_RESPONSE_BODY + Map tagsMap3 = new HashMap<>(); + tagsMap3.put( + RawSpanConstants.getValue(GRPC_RESPONSE_BODY), createKeyValue("Hello from grpc!!")); + + Map rpcTagsMap3 = new HashMap<>(); + rpcTagsMap3.put( + OTEL_SPAN_TAG_RPC_SYSTEM.getValue(), createKeyValue(OTEL_RPC_SYSTEM_GRPC.getValue())); + rpcTagsMap3.put(RPC_RESPONSE_BODY.getValue(), createKeyValue("Hello from rpc!!")); + + Map combinedTags3 = new HashMap<>(); + combinedTags3.putAll(tagsMap3); + combinedTags3.putAll(rpcTagsMap3); + + Event.Builder eventBuilder3 = Event.newBuilder(); + Grpc.Builder grpcBuilder3 = grpcFieldsGenerator.getProtocolBuilder(eventBuilder3); + + tagsMap3.forEach( + (key, keyValue) -> + grpcFieldsGenerator.addValueToBuilder(key, keyValue, eventBuilder3, combinedTags3)); + + rpcTagsMap3.forEach( + (key, keyValue) -> + rpcFieldsGenerator.handleKeyIfNecessary(key, keyValue, eventBuilder3, combinedTags3)); + + assertEquals(17, grpcBuilder3.getResponseBuilder().getSize()); + + // test 4: respect RPC_REQUEST_BODY + Map tagsMap4 = new HashMap<>(); + Map rpcTagsMap4 = new HashMap<>(); + rpcTagsMap4.put( + OTEL_SPAN_TAG_RPC_SYSTEM.getValue(), createKeyValue(OTEL_RPC_SYSTEM_GRPC.getValue())); + rpcTagsMap4.put(RPC_RESPONSE_BODY.getValue(), createKeyValue("Hello from rpc!!")); + + Map combinedTags4 = new HashMap<>(); + combinedTags4.putAll(tagsMap4); + combinedTags4.putAll(rpcTagsMap4); + + Event.Builder eventBuilder4 = Event.newBuilder(); + Grpc.Builder grpcBuilder4 = grpcFieldsGenerator.getProtocolBuilder(eventBuilder4); + + tagsMap3.forEach( + (key, keyValue) -> + grpcFieldsGenerator.addValueToBuilder(key, keyValue, eventBuilder4, combinedTags4)); + + rpcTagsMap3.forEach( + (key, keyValue) -> + rpcFieldsGenerator.handleKeyIfNecessary(key, keyValue, eventBuilder4, combinedTags4)); + + assertEquals(16, grpcBuilder4.getResponseBuilder().getSize()); + } + @Test public void testGrpcFieldsConverterStatusCodePriority() { GrpcFieldsGenerator grpcFieldsGenerator = new GrpcFieldsGenerator(); From ebd6878c0cbcfe0e5fa444e72a03230b59ca9592 Mon Sep 17 00:00:00 2001 From: Ronak Date: Fri, 22 Oct 2021 14:21:51 +0530 Subject: [PATCH 04/10] fixed nit comment issues --- .../spannormalizer/fieldgenerators/GrpcFieldsGeneratorTest.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/span-normalizer/span-normalizer/src/test/java/org/hypertrace/core/spannormalizer/fieldgenerators/GrpcFieldsGeneratorTest.java b/span-normalizer/span-normalizer/src/test/java/org/hypertrace/core/spannormalizer/fieldgenerators/GrpcFieldsGeneratorTest.java index faf3150e5..3ffb88fc1 100644 --- a/span-normalizer/span-normalizer/src/test/java/org/hypertrace/core/spannormalizer/fieldgenerators/GrpcFieldsGeneratorTest.java +++ b/span-normalizer/span-normalizer/src/test/java/org/hypertrace/core/spannormalizer/fieldgenerators/GrpcFieldsGeneratorTest.java @@ -343,7 +343,7 @@ public void testGrpcResponseSizeTagKeysPriority() { assertEquals(17, grpcBuilder3.getResponseBuilder().getSize()); - // test 4: respect RPC_REQUEST_BODY + // test 4: respect RPC_RESPONSE_BODY Map tagsMap4 = new HashMap<>(); Map rpcTagsMap4 = new HashMap<>(); rpcTagsMap4.put( From 6af697c961868f27b22e1e846cdd21b441d65e13 Mon Sep 17 00:00:00 2001 From: Ronak Date: Sat, 23 Oct 2021 19:53:01 +0530 Subject: [PATCH 05/10] added suport for http request response truncated body attribute --- .../http/HttpSemanticConventionUtils.java | 12 +++++++ .../http/HttpSemanticConventionUtilsTest.java | 28 +++++++++++++++ .../span/constants/v1/span_attribute.proto | 2 ++ .../fieldgenerators/HttpFieldsGenerator.java | 18 ++++++++++ .../HttpFieldsGeneratorTest.java | 36 +++++++++++++++++++ 5 files changed, 96 insertions(+) diff --git a/semantic-convention-utils/src/main/java/org/hypertrace/semantic/convention/utils/http/HttpSemanticConventionUtils.java b/semantic-convention-utils/src/main/java/org/hypertrace/semantic/convention/utils/http/HttpSemanticConventionUtils.java index 752a7f9dc..5468879db 100644 --- a/semantic-convention-utils/src/main/java/org/hypertrace/semantic/convention/utils/http/HttpSemanticConventionUtils.java +++ b/semantic-convention-utils/src/main/java/org/hypertrace/semantic/convention/utils/http/HttpSemanticConventionUtils.java @@ -12,6 +12,7 @@ import static org.hypertrace.core.span.constants.v1.Http.HTTP_HTTP_REQUEST_BODY; import static org.hypertrace.core.span.constants.v1.Http.HTTP_HTTP_RESPONSE_BODY; import static org.hypertrace.core.span.constants.v1.Http.HTTP_PATH; +import static org.hypertrace.core.span.constants.v1.Http.HTTP_REQUEST_BODY_TRUNCATED; import static org.hypertrace.core.span.constants.v1.Http.HTTP_REQUEST_CONTENT_LENGTH; import static org.hypertrace.core.span.constants.v1.Http.HTTP_REQUEST_CONTENT_TYPE; import static org.hypertrace.core.span.constants.v1.Http.HTTP_REQUEST_HEADER_PATH; @@ -21,6 +22,7 @@ import static org.hypertrace.core.span.constants.v1.Http.HTTP_REQUEST_SIZE; import static org.hypertrace.core.span.constants.v1.Http.HTTP_REQUEST_URL; import static org.hypertrace.core.span.constants.v1.Http.HTTP_REQUEST_X_FORWARDED_FOR_HEADER; +import static org.hypertrace.core.span.constants.v1.Http.HTTP_RESPONSE_BODY_TRUNCATED; import static org.hypertrace.core.span.constants.v1.Http.HTTP_RESPONSE_CONTENT_LENGTH; import static org.hypertrace.core.span.constants.v1.Http.HTTP_RESPONSE_SIZE; import static org.hypertrace.core.span.constants.v1.Http.HTTP_RESPONSE_STATUS_CODE; @@ -488,6 +490,11 @@ public static Optional getHttpRequestSize(Event event) { Optional requestSize = Optional.ofNullable(httpRequestSize); if (!requestSize.isEmpty()) return requestSize.map(Integer::parseInt); + if (SpanAttributeUtils.getBooleanAttribute( + event, RawSpanConstants.getValue(HTTP_REQUEST_BODY_TRUNCATED))) { + return Optional.empty(); + } + String requestBody = SpanAttributeUtils.getStringAttribute(event, HTTP_REQUEST_BODY); return Optional.ofNullable(requestBody).map(String::length); } @@ -499,6 +506,11 @@ public static Optional getHttpResponseSize(Event event) { Optional responseSize = Optional.ofNullable(httpResponseSize); if (!responseSize.isEmpty()) return responseSize.map(Integer::parseInt); + if (SpanAttributeUtils.getBooleanAttribute( + event, RawSpanConstants.getValue(HTTP_RESPONSE_BODY_TRUNCATED))) { + return Optional.empty(); + } + String responseBody = SpanAttributeUtils.getStringAttribute(event, HTTP_RESPONSE_BODY); return Optional.ofNullable(responseBody).map(String::length); } diff --git a/semantic-convention-utils/src/test/java/org/hypertrace/semantic/convention/utils/http/HttpSemanticConventionUtilsTest.java b/semantic-convention-utils/src/test/java/org/hypertrace/semantic/convention/utils/http/HttpSemanticConventionUtilsTest.java index 6c2b3cbab..0e8961444 100644 --- a/semantic-convention-utils/src/test/java/org/hypertrace/semantic/convention/utils/http/HttpSemanticConventionUtilsTest.java +++ b/semantic-convention-utils/src/test/java/org/hypertrace/semantic/convention/utils/http/HttpSemanticConventionUtilsTest.java @@ -16,6 +16,7 @@ import static org.hypertrace.core.span.constants.v1.Http.HTTP_HTTP_REQUEST_BODY; import static org.hypertrace.core.span.constants.v1.Http.HTTP_HTTP_RESPONSE_BODY; import static org.hypertrace.core.span.constants.v1.Http.HTTP_PATH; +import static org.hypertrace.core.span.constants.v1.Http.HTTP_REQUEST_BODY_TRUNCATED; import static org.hypertrace.core.span.constants.v1.Http.HTTP_REQUEST_CONTENT_LENGTH; import static org.hypertrace.core.span.constants.v1.Http.HTTP_REQUEST_CONTENT_TYPE; import static org.hypertrace.core.span.constants.v1.Http.HTTP_REQUEST_HEADER_PATH; @@ -25,6 +26,7 @@ import static org.hypertrace.core.span.constants.v1.Http.HTTP_REQUEST_SIZE; import static org.hypertrace.core.span.constants.v1.Http.HTTP_REQUEST_URL; import static org.hypertrace.core.span.constants.v1.Http.HTTP_REQUEST_X_FORWARDED_FOR_HEADER; +import static org.hypertrace.core.span.constants.v1.Http.HTTP_RESPONSE_BODY_TRUNCATED; import static org.hypertrace.core.span.constants.v1.Http.HTTP_RESPONSE_CONTENT_LENGTH; import static org.hypertrace.core.span.constants.v1.Http.HTTP_RESPONSE_SIZE; import static org.hypertrace.core.span.constants.v1.Http.HTTP_USER_AGENT_REQUEST_HEADER; @@ -405,6 +407,19 @@ public void testGetHttpRequestSize() { createMockEventWithAttribute( RawSpanConstants.getValue(HTTP_HTTP_REQUEST_BODY), "Hello, there!"); assertEquals(Optional.of(13), HttpSemanticConventionUtils.getHttpRequestSize(event)); + + event = mock(Event.class); + when(event.getAttributes()) + .thenReturn( + Attributes.newBuilder() + .setAttributeMap( + Map.of( + RawSpanConstants.getValue(HTTP_HTTP_REQUEST_BODY), + AttributeValue.newBuilder().setValue("Hello, there!").build(), + RawSpanConstants.getValue(HTTP_REQUEST_BODY_TRUNCATED), + AttributeValue.newBuilder().setValue("true").build())) + .build()); + assertEquals(Optional.empty(), HttpSemanticConventionUtils.getHttpRequestSize(event)); } @Test @@ -441,6 +456,19 @@ public void testGetHttpResponseSize() { createMockEventWithAttribute( RawSpanConstants.getValue(HTTP_HTTP_RESPONSE_BODY), "Hello World!"); assertEquals(Optional.of(12), HttpSemanticConventionUtils.getHttpResponseSize(event)); + + event = mock(Event.class); + when(event.getAttributes()) + .thenReturn( + Attributes.newBuilder() + .setAttributeMap( + Map.of( + RawSpanConstants.getValue(HTTP_HTTP_RESPONSE_BODY), + AttributeValue.newBuilder().setValue("Hello World!").build(), + RawSpanConstants.getValue(HTTP_RESPONSE_BODY_TRUNCATED), + AttributeValue.newBuilder().setValue("true").build())) + .build()); + assertEquals(Optional.empty(), HttpSemanticConventionUtils.getHttpResponseSize(event)); } @Test diff --git a/span-normalizer/raw-span-constants/src/main/proto/org/hypertrace/core/span/constants/v1/span_attribute.proto b/span-normalizer/raw-span-constants/src/main/proto/org/hypertrace/core/span/constants/v1/span_attribute.proto index 7a523f3d8..dcec61d9c 100644 --- a/span-normalizer/raw-span-constants/src/main/proto/org/hypertrace/core/span/constants/v1/span_attribute.proto +++ b/span-normalizer/raw-span-constants/src/main/proto/org/hypertrace/core/span/constants/v1/span_attribute.proto @@ -69,6 +69,8 @@ enum Http { HTTP_RESPONSE_BODY = 38 [(string_value) = "response.body"]; HTTP_REQUEST_CONTENT_LENGTH = 39 [(string_value) = "http.request.header.content-length"]; HTTP_RESPONSE_CONTENT_LENGTH = 40 [(string_value) = "http.response.header.content-length"]; + HTTP_REQUEST_BODY_TRUNCATED = 41 [(string_value) = "http.request.body.truncated"]; + HTTP_RESPONSE_BODY_TRUNCATED = 42 [(string_value) = "http.response.body.truncated"]; } // Model View Controller framework related attributes diff --git a/span-normalizer/span-normalizer/src/main/java/org/hypertrace/core/spannormalizer/fieldgenerators/HttpFieldsGenerator.java b/span-normalizer/span-normalizer/src/main/java/org/hypertrace/core/spannormalizer/fieldgenerators/HttpFieldsGenerator.java index 6a533f048..a02fafcbe 100644 --- a/span-normalizer/span-normalizer/src/main/java/org/hypertrace/core/spannormalizer/fieldgenerators/HttpFieldsGenerator.java +++ b/span-normalizer/span-normalizer/src/main/java/org/hypertrace/core/spannormalizer/fieldgenerators/HttpFieldsGenerator.java @@ -7,6 +7,7 @@ import static org.hypertrace.core.span.constants.v1.Http.HTTP_HTTP_RESPONSE_BODY; import static org.hypertrace.core.span.constants.v1.Http.HTTP_PATH; import static org.hypertrace.core.span.constants.v1.Http.HTTP_REQUEST_AUTHORITY_HEADER; +import static org.hypertrace.core.span.constants.v1.Http.HTTP_REQUEST_BODY_TRUNCATED; import static org.hypertrace.core.span.constants.v1.Http.HTTP_REQUEST_CONTENT_LENGTH; import static org.hypertrace.core.span.constants.v1.Http.HTTP_REQUEST_CONTENT_TYPE; import static org.hypertrace.core.span.constants.v1.Http.HTTP_REQUEST_COOKIE; @@ -21,6 +22,7 @@ import static org.hypertrace.core.span.constants.v1.Http.HTTP_REQUEST_SIZE; import static org.hypertrace.core.span.constants.v1.Http.HTTP_REQUEST_URL; import static org.hypertrace.core.span.constants.v1.Http.HTTP_REQUEST_X_FORWARDED_FOR_HEADER; +import static org.hypertrace.core.span.constants.v1.Http.HTTP_RESPONSE_BODY_TRUNCATED; import static org.hypertrace.core.span.constants.v1.Http.HTTP_RESPONSE_CONTENT_LENGTH; import static org.hypertrace.core.span.constants.v1.Http.HTTP_RESPONSE_CONTENT_TYPE; import static org.hypertrace.core.span.constants.v1.Http.HTTP_RESPONSE_COOKIE; @@ -514,6 +516,7 @@ private static void setRequestSize( .ifPresent(size -> httpBuilder.getRequestBuilder().setSize(size)); if (!httpBuilder.getRequestBuilder().hasSize() + && !isRequestBodyTruncated(tagsMap) && tagsMap.containsKey(RawSpanConstants.getValue(HTTP_HTTP_REQUEST_BODY))) { String requestBody = ValueConverter.getString(tagsMap.get(RawSpanConstants.getValue(HTTP_HTTP_REQUEST_BODY))); @@ -521,6 +524,13 @@ private static void setRequestSize( } } + private static boolean isRequestBodyTruncated( + Map tagsMap) { + if (!tagsMap.containsKey(RawSpanConstants.getValue(HTTP_REQUEST_BODY_TRUNCATED))) return false; + return ValueConverter.getBoolean( + tagsMap.get(RawSpanConstants.getValue(HTTP_REQUEST_BODY_TRUNCATED))); + } + private static void setResponseSize( Http.Builder httpBuilder, Map tagsMap) { if (httpBuilder.getResponseBuilder().hasSize()) { @@ -531,6 +541,7 @@ private static void setResponseSize( .ifPresent(size -> httpBuilder.getResponseBuilder().setSize(size)); if (!httpBuilder.getResponseBuilder().hasSize() + && !isResponseBodyTruncated(tagsMap) && tagsMap.containsKey(RawSpanConstants.getValue(HTTP_HTTP_RESPONSE_BODY))) { String responseBody = ValueConverter.getString(tagsMap.get(RawSpanConstants.getValue(HTTP_HTTP_RESPONSE_BODY))); @@ -538,6 +549,13 @@ private static void setResponseSize( } } + private static boolean isResponseBodyTruncated( + Map tagsMap) { + if (!tagsMap.containsKey(RawSpanConstants.getValue(HTTP_RESPONSE_BODY_TRUNCATED))) return false; + return ValueConverter.getBoolean( + tagsMap.get(RawSpanConstants.getValue(HTTP_RESPONSE_BODY_TRUNCATED))); + } + private static void setResponseStatusCode( Http.Builder httpBuilder, Map tagsMap) { if (httpBuilder.getResponseBuilder().hasStatusCode()) { diff --git a/span-normalizer/span-normalizer/src/test/java/org/hypertrace/core/spannormalizer/fieldgenerators/HttpFieldsGeneratorTest.java b/span-normalizer/span-normalizer/src/test/java/org/hypertrace/core/spannormalizer/fieldgenerators/HttpFieldsGeneratorTest.java index e18644c2e..b7edaffa9 100644 --- a/span-normalizer/span-normalizer/src/test/java/org/hypertrace/core/spannormalizer/fieldgenerators/HttpFieldsGeneratorTest.java +++ b/span-normalizer/span-normalizer/src/test/java/org/hypertrace/core/spannormalizer/fieldgenerators/HttpFieldsGeneratorTest.java @@ -7,6 +7,7 @@ import static org.hypertrace.core.span.constants.v1.Http.HTTP_HTTP_RESPONSE_BODY; import static org.hypertrace.core.span.constants.v1.Http.HTTP_PATH; import static org.hypertrace.core.span.constants.v1.Http.HTTP_REQUEST_AUTHORITY_HEADER; +import static org.hypertrace.core.span.constants.v1.Http.HTTP_REQUEST_BODY_TRUNCATED; import static org.hypertrace.core.span.constants.v1.Http.HTTP_REQUEST_CONTENT_LENGTH; import static org.hypertrace.core.span.constants.v1.Http.HTTP_REQUEST_CONTENT_TYPE; import static org.hypertrace.core.span.constants.v1.Http.HTTP_REQUEST_COOKIE; @@ -21,6 +22,7 @@ import static org.hypertrace.core.span.constants.v1.Http.HTTP_REQUEST_SIZE; import static org.hypertrace.core.span.constants.v1.Http.HTTP_REQUEST_URL; import static org.hypertrace.core.span.constants.v1.Http.HTTP_REQUEST_X_FORWARDED_FOR_HEADER; +import static org.hypertrace.core.span.constants.v1.Http.HTTP_RESPONSE_BODY_TRUNCATED; import static org.hypertrace.core.span.constants.v1.Http.HTTP_RESPONSE_CONTENT_LENGTH; import static org.hypertrace.core.span.constants.v1.Http.HTTP_RESPONSE_CONTENT_TYPE; import static org.hypertrace.core.span.constants.v1.Http.HTTP_RESPONSE_COOKIE; @@ -560,6 +562,23 @@ public void testRequestSizeTagKeysPriority() { httpFieldsGenerator.addValueToBuilder(key, keyValue, eventBuilder5, tagsMap5)); assertEquals(13, httpBuilder5.getRequestBuilder().getSize()); + + // test for truncated body + Map tagsMap6 = new HashMap<>(); + tagsMap6.put( + RawSpanConstants.getValue(HTTP_HTTP_REQUEST_BODY), createKeyValue("Hello, there!")); + Map allTags = new HashMap<>(); + allTags.putAll(tagsMap6); + allTags.put(RawSpanConstants.getValue(HTTP_REQUEST_BODY_TRUNCATED), createKeyValue("true")); + + Event.Builder eventBuilder6 = Event.newBuilder(); + Http.Builder httpBuilder6 = httpFieldsGenerator.getProtocolBuilder(eventBuilder6); + + tagsMap6.forEach( + (key, keyValue) -> + httpFieldsGenerator.addValueToBuilder(key, keyValue, eventBuilder6, allTags)); + + assertEquals(0, httpBuilder6.getRequestBuilder().getSize()); } @Test @@ -634,6 +653,23 @@ public void testResponseSizeTagKeysPriority() { httpFieldsGenerator.addValueToBuilder(key, keyValue, eventBuilder5, tagsMap5)); assertEquals(12, httpBuilder5.getResponseBuilder().getSize()); + + // test for truncated reponse body + Map tagsMap6 = new HashMap<>(); + tagsMap6.put( + RawSpanConstants.getValue(HTTP_HTTP_RESPONSE_BODY), createKeyValue("Hello World!")); + Map allTags = new HashMap<>(); + allTags.putAll(tagsMap6); + allTags.put(RawSpanConstants.getValue(HTTP_RESPONSE_BODY_TRUNCATED), createKeyValue("true")); + + Event.Builder eventBuilder6 = Event.newBuilder(); + Http.Builder httpBuilder6 = httpFieldsGenerator.getProtocolBuilder(eventBuilder6); + + tagsMap5.forEach( + (key, keyValue) -> + httpFieldsGenerator.addValueToBuilder(key, keyValue, eventBuilder6, allTags)); + + assertEquals(0, httpBuilder6.getResponseBuilder().getSize()); } @Test From c7cf74ee1bbe257972afee3ef494899e996e8c17 Mon Sep 17 00:00:00 2001 From: Ronak Date: Sat, 23 Oct 2021 20:41:40 +0530 Subject: [PATCH 06/10] add support for truncated rpc/grpc attributes too --- .../utils/rpc/RpcSemanticConventionUtils.java | 59 ++++++++-- .../rpc/RpcSemanticConventionUtilsTest.java | 78 +++++++++++++ .../span/constants/v1/span_attribute.proto | 2 + .../span/normalizer/constants/RpcSpanTag.java | 4 +- .../fieldgenerators/GrpcFieldsGenerator.java | 50 +++++++- .../fieldgenerators/HttpFieldsGenerator.java | 16 ++- .../GrpcFieldsGeneratorTest.java | 107 +++++++++++++++++- 7 files changed, 293 insertions(+), 23 deletions(-) diff --git a/semantic-convention-utils/src/main/java/org/hypertrace/semantic/convention/utils/rpc/RpcSemanticConventionUtils.java b/semantic-convention-utils/src/main/java/org/hypertrace/semantic/convention/utils/rpc/RpcSemanticConventionUtils.java index 08eaaf299..11eacd575 100644 --- a/semantic-convention-utils/src/main/java/org/hypertrace/semantic/convention/utils/rpc/RpcSemanticConventionUtils.java +++ b/semantic-convention-utils/src/main/java/org/hypertrace/semantic/convention/utils/rpc/RpcSemanticConventionUtils.java @@ -6,17 +6,21 @@ import static org.hypertrace.core.span.constants.v1.Envoy.ENVOY_RESPONSE_SIZE; import static org.hypertrace.core.span.constants.v1.Grpc.GRPC_ERROR_MESSAGE; import static org.hypertrace.core.span.constants.v1.Grpc.GRPC_REQUEST_BODY; +import static org.hypertrace.core.span.constants.v1.Grpc.GRPC_REQUEST_BODY_TRUNCATED; import static org.hypertrace.core.span.constants.v1.Grpc.GRPC_RESPONSE_BODY; +import static org.hypertrace.core.span.constants.v1.Grpc.GRPC_RESPONSE_BODY_TRUNCATED; import static org.hypertrace.core.span.normalizer.constants.OTelSpanTag.OTEL_SPAN_TAG_RPC_METHOD; import static org.hypertrace.core.span.normalizer.constants.OTelSpanTag.OTEL_SPAN_TAG_RPC_SYSTEM; import static org.hypertrace.core.span.normalizer.constants.RpcSpanTag.RPC_ERROR_MESSAGE; import static org.hypertrace.core.span.normalizer.constants.RpcSpanTag.RPC_REQUEST_BODY; +import static org.hypertrace.core.span.normalizer.constants.RpcSpanTag.RPC_REQUEST_BODY_TRUNCATED; import static org.hypertrace.core.span.normalizer.constants.RpcSpanTag.RPC_REQUEST_METADATA_AUTHORITY; import static org.hypertrace.core.span.normalizer.constants.RpcSpanTag.RPC_REQUEST_METADATA_CONTENT_LENGTH; import static org.hypertrace.core.span.normalizer.constants.RpcSpanTag.RPC_REQUEST_METADATA_PATH; import static org.hypertrace.core.span.normalizer.constants.RpcSpanTag.RPC_REQUEST_METADATA_USER_AGENT; import static org.hypertrace.core.span.normalizer.constants.RpcSpanTag.RPC_REQUEST_METADATA_X_FORWARDED_FOR; import static org.hypertrace.core.span.normalizer.constants.RpcSpanTag.RPC_RESPONSE_BODY; +import static org.hypertrace.core.span.normalizer.constants.RpcSpanTag.RPC_RESPONSE_BODY_TRUNCATED; import static org.hypertrace.core.span.normalizer.constants.RpcSpanTag.RPC_RESPONSE_METADATA_CONTENT_LENGTH; import com.google.common.base.Joiner; @@ -271,6 +275,22 @@ public static Optional getGrpcAuthority(Event event) { return Optional.empty(); } + private static boolean isGrpcRequestBodyTruncated(Map attributeValueMap) { + if (attributeValueMap.get(RawSpanConstants.getValue(GRPC_REQUEST_BODY_TRUNCATED)) != null) { + return Boolean.parseBoolean( + attributeValueMap.get(RawSpanConstants.getValue(GRPC_REQUEST_BODY_TRUNCATED)).getValue()); + } + return false; + } + + private static boolean isRpcRequestBodyTruncated(Map attributeValueMap) { + if (attributeValueMap.get(RPC_REQUEST_BODY_TRUNCATED.getValue()) != null) { + return Boolean.parseBoolean( + attributeValueMap.get(RPC_REQUEST_BODY_TRUNCATED.getValue()).getValue()); + } + return false; + } + public static Optional getGrpcRequestSize(Event event) { if (event.getAttributes() == null || event.getAttributes().getAttributeMap() == null) { return Optional.empty(); @@ -286,12 +306,14 @@ public static Optional getGrpcRequestSize(Event event) { return Optional.of( Integer.parseInt( attributeValueMap.get(RPC_REQUEST_METADATA_CONTENT_LENGTH.getValue()).getValue())); - } else if (attributeValueMap.get(RawSpanConstants.getValue(GRPC_REQUEST_BODY)) != null) { + } else if (attributeValueMap.get(RawSpanConstants.getValue(GRPC_REQUEST_BODY)) != null + && !isGrpcRequestBodyTruncated(attributeValueMap)) { String requestBody = attributeValueMap.get(RawSpanConstants.getValue(GRPC_REQUEST_BODY)).getValue(); return Optional.of(requestBody.length()); } else if (isRpcSystemGrpc(attributeValueMap) - && attributeValueMap.get(RPC_REQUEST_BODY.getValue()) != null) { + && attributeValueMap.get(RPC_REQUEST_BODY.getValue()) != null + && !isRpcRequestBodyTruncated(attributeValueMap)) { String requestBody = attributeValueMap.get(RPC_REQUEST_BODY.getValue()).getValue(); return Optional.of(requestBody.length()); } @@ -299,6 +321,25 @@ public static Optional getGrpcRequestSize(Event event) { return Optional.empty(); } + private static boolean isGrpcResponseBodyTruncated( + Map attributeValueMap) { + if (attributeValueMap.get(RawSpanConstants.getValue(GRPC_RESPONSE_BODY_TRUNCATED)) != null) { + return Boolean.parseBoolean( + attributeValueMap + .get(RawSpanConstants.getValue(GRPC_RESPONSE_BODY_TRUNCATED)) + .getValue()); + } + return false; + } + + private static boolean isRpcResponseBodyTruncated(Map attributeValueMap) { + if (attributeValueMap.get(RPC_RESPONSE_BODY_TRUNCATED.getValue()) != null) { + return Boolean.parseBoolean( + attributeValueMap.get(RPC_RESPONSE_BODY_TRUNCATED.getValue()).getValue()); + } + return false; + } + public static Optional getGrpcResponseSize(Event event) { if (event.getAttributes() == null || event.getAttributes().getAttributeMap() == null) { return Optional.empty(); @@ -314,14 +355,16 @@ public static Optional getGrpcResponseSize(Event event) { return Optional.of( Integer.parseInt( attributeValueMap.get(RPC_RESPONSE_METADATA_CONTENT_LENGTH.getValue()).getValue())); - } else if (attributeValueMap.get(RawSpanConstants.getValue(GRPC_RESPONSE_BODY)) != null) { - String requestBody = + } else if (attributeValueMap.get(RawSpanConstants.getValue(GRPC_RESPONSE_BODY)) != null + && !isGrpcResponseBodyTruncated(attributeValueMap)) { + String responseBody = attributeValueMap.get(RawSpanConstants.getValue(GRPC_RESPONSE_BODY)).getValue(); - return Optional.of(requestBody.length()); + return Optional.of(responseBody.length()); } else if (isRpcSystemGrpc(attributeValueMap) - && attributeValueMap.get(RPC_RESPONSE_BODY.getValue()) != null) { - String requestBody = attributeValueMap.get(RPC_RESPONSE_BODY.getValue()).getValue(); - return Optional.of(requestBody.length()); + && attributeValueMap.get(RPC_RESPONSE_BODY.getValue()) != null + && !isRpcResponseBodyTruncated(attributeValueMap)) { + String responseBody = attributeValueMap.get(RPC_RESPONSE_BODY.getValue()).getValue(); + return Optional.of(responseBody.length()); } return Optional.empty(); diff --git a/semantic-convention-utils/src/test/java/org/hypertrace/semantic/convention/utils/rpc/RpcSemanticConventionUtilsTest.java b/semantic-convention-utils/src/test/java/org/hypertrace/semantic/convention/utils/rpc/RpcSemanticConventionUtilsTest.java index 41f13b447..014574d30 100644 --- a/semantic-convention-utils/src/test/java/org/hypertrace/semantic/convention/utils/rpc/RpcSemanticConventionUtilsTest.java +++ b/semantic-convention-utils/src/test/java/org/hypertrace/semantic/convention/utils/rpc/RpcSemanticConventionUtilsTest.java @@ -7,17 +7,21 @@ import static org.hypertrace.core.span.constants.v1.Envoy.ENVOY_RESPONSE_SIZE; import static org.hypertrace.core.span.constants.v1.Grpc.GRPC_ERROR_MESSAGE; import static org.hypertrace.core.span.constants.v1.Grpc.GRPC_REQUEST_BODY; +import static org.hypertrace.core.span.constants.v1.Grpc.GRPC_REQUEST_BODY_TRUNCATED; import static org.hypertrace.core.span.constants.v1.Grpc.GRPC_RESPONSE_BODY; +import static org.hypertrace.core.span.constants.v1.Grpc.GRPC_RESPONSE_BODY_TRUNCATED; import static org.hypertrace.core.span.normalizer.constants.OTelSpanTag.OTEL_SPAN_TAG_RPC_METHOD; import static org.hypertrace.core.span.normalizer.constants.OTelSpanTag.OTEL_SPAN_TAG_RPC_SYSTEM; import static org.hypertrace.core.span.normalizer.constants.RpcSpanTag.RPC_ERROR_MESSAGE; import static org.hypertrace.core.span.normalizer.constants.RpcSpanTag.RPC_REQUEST_BODY; +import static org.hypertrace.core.span.normalizer.constants.RpcSpanTag.RPC_REQUEST_BODY_TRUNCATED; import static org.hypertrace.core.span.normalizer.constants.RpcSpanTag.RPC_REQUEST_METADATA_AUTHORITY; import static org.hypertrace.core.span.normalizer.constants.RpcSpanTag.RPC_REQUEST_METADATA_CONTENT_LENGTH; import static org.hypertrace.core.span.normalizer.constants.RpcSpanTag.RPC_REQUEST_METADATA_PATH; import static org.hypertrace.core.span.normalizer.constants.RpcSpanTag.RPC_REQUEST_METADATA_USER_AGENT; import static org.hypertrace.core.span.normalizer.constants.RpcSpanTag.RPC_REQUEST_METADATA_X_FORWARDED_FOR; import static org.hypertrace.core.span.normalizer.constants.RpcSpanTag.RPC_RESPONSE_BODY; +import static org.hypertrace.core.span.normalizer.constants.RpcSpanTag.RPC_RESPONSE_BODY_TRUNCATED; import static org.hypertrace.core.span.normalizer.constants.RpcSpanTag.RPC_RESPONSE_METADATA_CONTENT_LENGTH; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertFalse; @@ -453,6 +457,43 @@ public void testGetGrpcRequestSizePriority() { when(event.getAttributes()) .thenReturn(Attributes.newBuilder().setAttributeMap(tagsMap).build()); assertEquals(Optional.empty(), RpcSemanticConventionUtils.getGrpcRequestSize(event)); + + // test truncated grpc request body + tagsMap = + new HashMap<>() { + { + put( + RawSpanConstants.getValue(GRPC_REQUEST_BODY), + AttributeValue.newBuilder().setValue("some grpc request body").build()); + put( + RawSpanConstants.getValue(GRPC_REQUEST_BODY_TRUNCATED), + AttributeValue.newBuilder().setValue("true").build()); + } + }; + + event = mock(Event.class); + when(event.getAttributes()) + .thenReturn(Attributes.newBuilder().setAttributeMap(tagsMap).build()); + assertEquals(Optional.empty(), RpcSemanticConventionUtils.getGrpcRequestSize(event)); + + // test truncated rpc request body + tagsMap = + new HashMap<>() { + { + put( + RPC_REQUEST_BODY.getValue(), + AttributeValue.newBuilder().setValue("some rpc request body").build()); + put("rpc.system", AttributeValue.newBuilder().setValue("grpc").build()); + put( + RPC_REQUEST_BODY_TRUNCATED.getValue(), + AttributeValue.newBuilder().setValue("true").build()); + } + }; + + event = mock(Event.class); + when(event.getAttributes()) + .thenReturn(Attributes.newBuilder().setAttributeMap(tagsMap).build()); + assertEquals(Optional.empty(), RpcSemanticConventionUtils.getGrpcRequestSize(event)); } @Test @@ -573,6 +614,43 @@ public void testGetGrpcResponseSizePriority() { when(event.getAttributes()) .thenReturn(Attributes.newBuilder().setAttributeMap(tagsMap).build()); assertEquals(Optional.empty(), RpcSemanticConventionUtils.getGrpcResponseSize(event)); + + // test truncated grpc response body + tagsMap = + new HashMap<>() { + { + put( + RawSpanConstants.getValue(GRPC_RESPONSE_BODY), + AttributeValue.newBuilder().setValue("some grpc response body").build()); + put( + RawSpanConstants.getValue(GRPC_RESPONSE_BODY_TRUNCATED), + AttributeValue.newBuilder().setValue("true").build()); + } + }; + + event = mock(Event.class); + when(event.getAttributes()) + .thenReturn(Attributes.newBuilder().setAttributeMap(tagsMap).build()); + assertEquals(Optional.empty(), RpcSemanticConventionUtils.getGrpcResponseSize(event)); + + // test truncated rpc response body + tagsMap = + new HashMap<>() { + { + put( + RPC_RESPONSE_BODY.getValue(), + AttributeValue.newBuilder().setValue("some rpc response body").build()); + put("rpc.system", AttributeValue.newBuilder().setValue("grpc").build()); + put( + RPC_RESPONSE_BODY_TRUNCATED.getValue(), + AttributeValue.newBuilder().setValue("true").build()); + } + }; + + event = mock(Event.class); + when(event.getAttributes()) + .thenReturn(Attributes.newBuilder().setAttributeMap(tagsMap).build()); + assertEquals(Optional.empty(), RpcSemanticConventionUtils.getGrpcResponseSize(event)); } @Test diff --git a/span-normalizer/raw-span-constants/src/main/proto/org/hypertrace/core/span/constants/v1/span_attribute.proto b/span-normalizer/raw-span-constants/src/main/proto/org/hypertrace/core/span/constants/v1/span_attribute.proto index dcec61d9c..c9a79dc6f 100644 --- a/span-normalizer/raw-span-constants/src/main/proto/org/hypertrace/core/span/constants/v1/span_attribute.proto +++ b/span-normalizer/raw-span-constants/src/main/proto/org/hypertrace/core/span/constants/v1/span_attribute.proto @@ -94,6 +94,8 @@ enum Grpc { GRPC_ERROR_MESSAGE = 8 [(string_value) = "grpc.error_message"]; GRPC_HOST_PORT = 9 [(string_value) = "grpc.host_port"]; GRPC_METHOD = 10 [(string_value) = "grpc.method"]; + GRPC_REQUEST_BODY_TRUNCATED = 11 [(string_value) = "grpc.request.body.truncated"]; + GRPC_RESPONSE_BODY_TRUNCATED = 12 [(string_value) = "grpc.response.body.truncated"]; } // Error related attributes diff --git a/span-normalizer/span-normalizer-constants/src/main/java/org/hypertrace/core/span/normalizer/constants/RpcSpanTag.java b/span-normalizer/span-normalizer-constants/src/main/java/org/hypertrace/core/span/normalizer/constants/RpcSpanTag.java index a98004ae4..dfcd27690 100644 --- a/span-normalizer/span-normalizer-constants/src/main/java/org/hypertrace/core/span/normalizer/constants/RpcSpanTag.java +++ b/span-normalizer/span-normalizer-constants/src/main/java/org/hypertrace/core/span/normalizer/constants/RpcSpanTag.java @@ -16,7 +16,9 @@ public enum RpcSpanTag { RPC_REQUEST_METADATA_USER_AGENT("rpc.request.metadata.user-agent"), RPC_RESPONSE_METADATA_CONTENT_TYPE("rpc.response.metadata.content-type"), RPC_RESPONSE_METADATA_CONTENT_LENGTH("rpc.response.metadata.content-length"), - RPC_BODY_DECODE_RAW("rpc.body.decode_raw"); + RPC_BODY_DECODE_RAW("rpc.body.decode_raw"), + RPC_REQUEST_BODY_TRUNCATED("rpc.request.body.truncated"), + RPC_RESPONSE_BODY_TRUNCATED("rpc.response.body.truncated"); private final String value; diff --git a/span-normalizer/span-normalizer/src/main/java/org/hypertrace/core/spannormalizer/fieldgenerators/GrpcFieldsGenerator.java b/span-normalizer/span-normalizer/src/main/java/org/hypertrace/core/spannormalizer/fieldgenerators/GrpcFieldsGenerator.java index 335c603b0..8d2a5a1eb 100644 --- a/span-normalizer/span-normalizer/src/main/java/org/hypertrace/core/spannormalizer/fieldgenerators/GrpcFieldsGenerator.java +++ b/span-normalizer/span-normalizer/src/main/java/org/hypertrace/core/spannormalizer/fieldgenerators/GrpcFieldsGenerator.java @@ -8,13 +8,16 @@ import static org.hypertrace.core.span.constants.v1.Grpc.GRPC_ERROR_NAME; import static org.hypertrace.core.span.constants.v1.Grpc.GRPC_HOST_PORT; import static org.hypertrace.core.span.constants.v1.Grpc.GRPC_REQUEST_BODY; +import static org.hypertrace.core.span.constants.v1.Grpc.GRPC_REQUEST_BODY_TRUNCATED; import static org.hypertrace.core.span.constants.v1.Grpc.GRPC_REQUEST_CALL_OPTIONS; import static org.hypertrace.core.span.constants.v1.Grpc.GRPC_REQUEST_METADATA; import static org.hypertrace.core.span.constants.v1.Grpc.GRPC_RESPONSE_BODY; +import static org.hypertrace.core.span.constants.v1.Grpc.GRPC_RESPONSE_BODY_TRUNCATED; import static org.hypertrace.core.span.constants.v1.Grpc.GRPC_RESPONSE_METADATA; import static org.hypertrace.core.span.normalizer.constants.RpcSpanTag.RPC_ERROR_MESSAGE; import static org.hypertrace.core.span.normalizer.constants.RpcSpanTag.RPC_ERROR_NAME; import static org.hypertrace.core.span.normalizer.constants.RpcSpanTag.RPC_REQUEST_BODY; +import static org.hypertrace.core.span.normalizer.constants.RpcSpanTag.RPC_REQUEST_BODY_TRUNCATED; import static org.hypertrace.core.span.normalizer.constants.RpcSpanTag.RPC_REQUEST_METADATA_AUTHORITY; import static org.hypertrace.core.span.normalizer.constants.RpcSpanTag.RPC_REQUEST_METADATA_CONTENT_LENGTH; import static org.hypertrace.core.span.normalizer.constants.RpcSpanTag.RPC_REQUEST_METADATA_CONTENT_TYPE; @@ -22,6 +25,7 @@ import static org.hypertrace.core.span.normalizer.constants.RpcSpanTag.RPC_REQUEST_METADATA_USER_AGENT; import static org.hypertrace.core.span.normalizer.constants.RpcSpanTag.RPC_REQUEST_METADATA_X_FORWARDED_FOR; import static org.hypertrace.core.span.normalizer.constants.RpcSpanTag.RPC_RESPONSE_BODY; +import static org.hypertrace.core.span.normalizer.constants.RpcSpanTag.RPC_RESPONSE_BODY_TRUNCATED; import static org.hypertrace.core.span.normalizer.constants.RpcSpanTag.RPC_RESPONSE_METADATA_CONTENT_LENGTH; import static org.hypertrace.core.span.normalizer.constants.RpcSpanTag.RPC_RESPONSE_METADATA_CONTENT_TYPE; import static org.hypertrace.core.span.normalizer.constants.RpcSpanTag.RPC_STATUS_CODE; @@ -263,16 +267,35 @@ private static void setRequestSize( .setSize( ValueConverter.getInteger( tagsMap.get(RPC_REQUEST_METADATA_CONTENT_LENGTH.getValue()))); - } else if (tagsMap.containsKey(RawSpanConstants.getValue(GRPC_REQUEST_BODY))) { + } else if (tagsMap.containsKey(RawSpanConstants.getValue(GRPC_REQUEST_BODY)) + && !isGrpcRequestBodyTruncated(tagsMap)) { String requestBody = ValueConverter.getString(tagsMap.get(RawSpanConstants.getValue(GRPC_REQUEST_BODY))); grpcBuilder.getRequestBuilder().setSize(requestBody.length()); - } else if (tagsMap.containsKey(RPC_REQUEST_BODY.getValue())) { + } else if (tagsMap.containsKey(RPC_REQUEST_BODY.getValue()) + && !isRpcRequestBodyTruncated(tagsMap)) { String requestBody = ValueConverter.getString(tagsMap.get(RPC_REQUEST_BODY.getValue())); grpcBuilder.getRequestBuilder().setSize(requestBody.length()); } } + private static boolean isGrpcRequestBodyTruncated( + Map tagsMap) { + if (tagsMap.containsKey(RawSpanConstants.getValue(GRPC_REQUEST_BODY_TRUNCATED))) { + return ValueConverter.getBoolean( + tagsMap.get(RawSpanConstants.getValue(GRPC_REQUEST_BODY_TRUNCATED))); + } + return false; + } + + private static boolean isRpcRequestBodyTruncated( + Map tagsMap) { + if (tagsMap.containsKey(RPC_REQUEST_BODY_TRUNCATED.getValue())) { + return ValueConverter.getBoolean(tagsMap.get(RPC_REQUEST_BODY_TRUNCATED.getValue())); + } + return false; + } + private static void setResponseSize( Grpc.Builder grpcBuilder, Map tagsMap) { if (tagsMap.containsKey(RawSpanConstants.getValue(ENVOY_RESPONSE_SIZE))) { @@ -287,16 +310,35 @@ private static void setResponseSize( .setSize( ValueConverter.getInteger( tagsMap.get(RPC_RESPONSE_METADATA_CONTENT_LENGTH.getValue()))); - } else if (tagsMap.containsKey(RawSpanConstants.getValue(GRPC_RESPONSE_BODY))) { + } else if (tagsMap.containsKey(RawSpanConstants.getValue(GRPC_RESPONSE_BODY)) + && !isGrpcResponseBodyTruncated(tagsMap)) { String responseBody = ValueConverter.getString(tagsMap.get(RawSpanConstants.getValue(GRPC_RESPONSE_BODY))); grpcBuilder.getResponseBuilder().setSize(responseBody.length()); - } else if (tagsMap.containsKey(RPC_RESPONSE_BODY.getValue())) { + } else if (tagsMap.containsKey(RPC_RESPONSE_BODY.getValue()) + && !isRpcResponseBodyTruncated(tagsMap)) { String responseBody = ValueConverter.getString(tagsMap.get(RPC_RESPONSE_BODY.getValue())); grpcBuilder.getResponseBuilder().setSize(responseBody.length()); } } + private static boolean isGrpcResponseBodyTruncated( + Map tagsMap) { + if (tagsMap.containsKey(RawSpanConstants.getValue(GRPC_RESPONSE_BODY_TRUNCATED))) { + return ValueConverter.getBoolean( + tagsMap.get(RawSpanConstants.getValue(GRPC_RESPONSE_BODY_TRUNCATED))); + } + return false; + } + + private static boolean isRpcResponseBodyTruncated( + Map tagsMap) { + if (tagsMap.containsKey(RPC_RESPONSE_BODY_TRUNCATED.getValue())) { + return ValueConverter.getBoolean(tagsMap.get(RPC_RESPONSE_BODY_TRUNCATED.getValue())); + } + return false; + } + private static Map parseMetadataString(String metadataString) { if (!metadataString.startsWith(METADATA_STR_VAL_PREFIX)) { return EMPTY_METADATA_MAP; diff --git a/span-normalizer/span-normalizer/src/main/java/org/hypertrace/core/spannormalizer/fieldgenerators/HttpFieldsGenerator.java b/span-normalizer/span-normalizer/src/main/java/org/hypertrace/core/spannormalizer/fieldgenerators/HttpFieldsGenerator.java index a02fafcbe..29a221dde 100644 --- a/span-normalizer/span-normalizer/src/main/java/org/hypertrace/core/spannormalizer/fieldgenerators/HttpFieldsGenerator.java +++ b/span-normalizer/span-normalizer/src/main/java/org/hypertrace/core/spannormalizer/fieldgenerators/HttpFieldsGenerator.java @@ -526,9 +526,11 @@ private static void setRequestSize( private static boolean isRequestBodyTruncated( Map tagsMap) { - if (!tagsMap.containsKey(RawSpanConstants.getValue(HTTP_REQUEST_BODY_TRUNCATED))) return false; - return ValueConverter.getBoolean( - tagsMap.get(RawSpanConstants.getValue(HTTP_REQUEST_BODY_TRUNCATED))); + if (tagsMap.containsKey(RawSpanConstants.getValue(HTTP_REQUEST_BODY_TRUNCATED))) { + return ValueConverter.getBoolean( + tagsMap.get(RawSpanConstants.getValue(HTTP_REQUEST_BODY_TRUNCATED))); + } + return false; } private static void setResponseSize( @@ -551,9 +553,11 @@ private static void setResponseSize( private static boolean isResponseBodyTruncated( Map tagsMap) { - if (!tagsMap.containsKey(RawSpanConstants.getValue(HTTP_RESPONSE_BODY_TRUNCATED))) return false; - return ValueConverter.getBoolean( - tagsMap.get(RawSpanConstants.getValue(HTTP_RESPONSE_BODY_TRUNCATED))); + if (tagsMap.containsKey(RawSpanConstants.getValue(HTTP_RESPONSE_BODY_TRUNCATED))) { + return ValueConverter.getBoolean( + tagsMap.get(RawSpanConstants.getValue(HTTP_RESPONSE_BODY_TRUNCATED))); + } + return false; } private static void setResponseStatusCode( diff --git a/span-normalizer/span-normalizer/src/test/java/org/hypertrace/core/spannormalizer/fieldgenerators/GrpcFieldsGeneratorTest.java b/span-normalizer/span-normalizer/src/test/java/org/hypertrace/core/spannormalizer/fieldgenerators/GrpcFieldsGeneratorTest.java index 3ffb88fc1..6d9865610 100644 --- a/span-normalizer/span-normalizer/src/test/java/org/hypertrace/core/spannormalizer/fieldgenerators/GrpcFieldsGeneratorTest.java +++ b/span-normalizer/span-normalizer/src/test/java/org/hypertrace/core/spannormalizer/fieldgenerators/GrpcFieldsGeneratorTest.java @@ -11,16 +11,20 @@ import static org.hypertrace.core.span.constants.v1.Grpc.GRPC_HOST_PORT; import static org.hypertrace.core.span.constants.v1.Grpc.GRPC_METHOD; import static org.hypertrace.core.span.constants.v1.Grpc.GRPC_REQUEST_BODY; +import static org.hypertrace.core.span.constants.v1.Grpc.GRPC_REQUEST_BODY_TRUNCATED; import static org.hypertrace.core.span.constants.v1.Grpc.GRPC_REQUEST_CALL_OPTIONS; import static org.hypertrace.core.span.constants.v1.Grpc.GRPC_REQUEST_METADATA; import static org.hypertrace.core.span.constants.v1.Grpc.GRPC_RESPONSE_BODY; +import static org.hypertrace.core.span.constants.v1.Grpc.GRPC_RESPONSE_BODY_TRUNCATED; import static org.hypertrace.core.span.constants.v1.Grpc.GRPC_RESPONSE_METADATA; import static org.hypertrace.core.span.constants.v1.Grpc.GRPC_STATUS_CODE; import static org.hypertrace.core.span.normalizer.constants.OTelRpcSystem.OTEL_RPC_SYSTEM_GRPC; import static org.hypertrace.core.span.normalizer.constants.OTelSpanTag.OTEL_SPAN_TAG_RPC_SYSTEM; import static org.hypertrace.core.span.normalizer.constants.RpcSpanTag.RPC_REQUEST_BODY; +import static org.hypertrace.core.span.normalizer.constants.RpcSpanTag.RPC_REQUEST_BODY_TRUNCATED; import static org.hypertrace.core.span.normalizer.constants.RpcSpanTag.RPC_REQUEST_METADATA_CONTENT_LENGTH; import static org.hypertrace.core.span.normalizer.constants.RpcSpanTag.RPC_RESPONSE_BODY; +import static org.hypertrace.core.span.normalizer.constants.RpcSpanTag.RPC_RESPONSE_BODY_TRUNCATED; import static org.hypertrace.core.span.normalizer.constants.RpcSpanTag.RPC_RESPONSE_METADATA_CONTENT_LENGTH; import static org.hypertrace.core.spannormalizer.utils.TestUtils.createKeyValue; import static org.junit.jupiter.api.Assertions.assertEquals; @@ -229,6 +233,25 @@ public void testGrpcRequestSizeTagKeysPriority() { assertEquals(21, grpcBuilder3.getRequestBuilder().getSize()); + // test 3.1: respect GRPC_REQUEST_BODY and tructed set to zero + Map tagsMap31 = new HashMap<>(); + tagsMap31.put( + RawSpanConstants.getValue(GRPC_REQUEST_BODY), createKeyValue("Hey Grpc, you there!!")); + + Map combinedTags31 = new HashMap<>(); + combinedTags31.putAll(tagsMap31); + combinedTags31.put( + RawSpanConstants.getValue(GRPC_REQUEST_BODY_TRUNCATED), createKeyValue("true")); + + Event.Builder eventBuilder31 = Event.newBuilder(); + Grpc.Builder grpcBuilder31 = grpcFieldsGenerator.getProtocolBuilder(eventBuilder31); + + tagsMap31.forEach( + (key, keyValue) -> + grpcFieldsGenerator.addValueToBuilder(key, keyValue, eventBuilder31, combinedTags31)); + + assertEquals(0, grpcBuilder31.getRequestBuilder().getSize()); + // test 4: respect RPC_REQUEST_BODY Map tagsMap4 = new HashMap<>(); Map rpcTagsMap4 = new HashMap<>(); @@ -243,15 +266,40 @@ public void testGrpcRequestSizeTagKeysPriority() { Event.Builder eventBuilder4 = Event.newBuilder(); Grpc.Builder grpcBuilder4 = grpcFieldsGenerator.getProtocolBuilder(eventBuilder4); - tagsMap3.forEach( + tagsMap4.forEach( (key, keyValue) -> grpcFieldsGenerator.addValueToBuilder(key, keyValue, eventBuilder4, combinedTags4)); - rpcTagsMap3.forEach( + rpcTagsMap4.forEach( (key, keyValue) -> rpcFieldsGenerator.handleKeyIfNecessary(key, keyValue, eventBuilder4, combinedTags4)); assertEquals(20, grpcBuilder4.getRequestBuilder().getSize()); + + // test 4.1: respect RPC_REQUEST_BODY and truncated body + Map tagsMap41 = new HashMap<>(); + Map rpcTagsMap41 = new HashMap<>(); + rpcTagsMap41.put( + OTEL_SPAN_TAG_RPC_SYSTEM.getValue(), createKeyValue(OTEL_RPC_SYSTEM_GRPC.getValue())); + rpcTagsMap4.put(RPC_REQUEST_BODY.getValue(), createKeyValue("Hey rpc, you there!!")); + + Map combinedTags41 = new HashMap<>(); + combinedTags41.putAll(tagsMap41); + combinedTags41.putAll(rpcTagsMap41); + combinedTags41.put(RPC_REQUEST_BODY_TRUNCATED.getValue(), createKeyValue("true")); + + Event.Builder eventBuilder41 = Event.newBuilder(); + Grpc.Builder grpcBuilder41 = grpcFieldsGenerator.getProtocolBuilder(eventBuilder41); + + tagsMap41.forEach( + (key, keyValue) -> + grpcFieldsGenerator.addValueToBuilder(key, keyValue, eventBuilder41, combinedTags41)); + + rpcTagsMap41.forEach( + (key, keyValue) -> + rpcFieldsGenerator.handleKeyIfNecessary(key, keyValue, eventBuilder41, combinedTags41)); + + assertEquals(0, grpcBuilder41.getRequestBuilder().getSize()); } @Test @@ -343,6 +391,32 @@ public void testGrpcResponseSizeTagKeysPriority() { assertEquals(17, grpcBuilder3.getResponseBuilder().getSize()); + // test 31: respect GRPC_RESPONSE_BODY and truncated body + Map tagsMap31 = new HashMap<>(); + tagsMap31.put( + RawSpanConstants.getValue(GRPC_RESPONSE_BODY), createKeyValue("Hello from grpc!!")); + + Map rpcTagsMap31 = new HashMap<>(); + + Map combinedTags31 = new HashMap<>(); + combinedTags31.putAll(tagsMap31); + combinedTags31.putAll(rpcTagsMap31); + combinedTags31.put( + RawSpanConstants.getValue(GRPC_RESPONSE_BODY_TRUNCATED), createKeyValue("true")); + + Event.Builder eventBuilder31 = Event.newBuilder(); + Grpc.Builder grpcBuilder31 = grpcFieldsGenerator.getProtocolBuilder(eventBuilder31); + + tagsMap31.forEach( + (key, keyValue) -> + grpcFieldsGenerator.addValueToBuilder(key, keyValue, eventBuilder31, combinedTags31)); + + rpcTagsMap31.forEach( + (key, keyValue) -> + rpcFieldsGenerator.handleKeyIfNecessary(key, keyValue, eventBuilder31, combinedTags31)); + + assertEquals(0, grpcBuilder31.getResponseBuilder().getSize()); + // test 4: respect RPC_RESPONSE_BODY Map tagsMap4 = new HashMap<>(); Map rpcTagsMap4 = new HashMap<>(); @@ -357,15 +431,40 @@ public void testGrpcResponseSizeTagKeysPriority() { Event.Builder eventBuilder4 = Event.newBuilder(); Grpc.Builder grpcBuilder4 = grpcFieldsGenerator.getProtocolBuilder(eventBuilder4); - tagsMap3.forEach( + tagsMap4.forEach( (key, keyValue) -> grpcFieldsGenerator.addValueToBuilder(key, keyValue, eventBuilder4, combinedTags4)); - rpcTagsMap3.forEach( + rpcTagsMap4.forEach( (key, keyValue) -> rpcFieldsGenerator.handleKeyIfNecessary(key, keyValue, eventBuilder4, combinedTags4)); assertEquals(16, grpcBuilder4.getResponseBuilder().getSize()); + + // test 41: respect RPC_RESPONSE_BODY and tructed rpc body + Map tagsMap41 = new HashMap<>(); + Map rpcTagsMap41 = new HashMap<>(); + rpcTagsMap41.put( + OTEL_SPAN_TAG_RPC_SYSTEM.getValue(), createKeyValue(OTEL_RPC_SYSTEM_GRPC.getValue())); + rpcTagsMap41.put(RPC_RESPONSE_BODY.getValue(), createKeyValue("Hello from rpc!!")); + + Map combinedTags41 = new HashMap<>(); + combinedTags41.putAll(tagsMap41); + combinedTags41.putAll(rpcTagsMap41); + combinedTags41.put(RPC_RESPONSE_BODY_TRUNCATED.getValue(), createKeyValue("true")); + + Event.Builder eventBuilder41 = Event.newBuilder(); + Grpc.Builder grpcBuilder41 = grpcFieldsGenerator.getProtocolBuilder(eventBuilder41); + + tagsMap41.forEach( + (key, keyValue) -> + grpcFieldsGenerator.addValueToBuilder(key, keyValue, eventBuilder41, combinedTags41)); + + rpcTagsMap41.forEach( + (key, keyValue) -> + rpcFieldsGenerator.handleKeyIfNecessary(key, keyValue, eventBuilder41, combinedTags41)); + + assertEquals(0, grpcBuilder41.getResponseBuilder().getSize()); } @Test From a6411ec149bc89038452316280c32f947ce3d97b Mon Sep 17 00:00:00 2001 From: Ronak Date: Mon, 25 Oct 2021 17:28:17 +0530 Subject: [PATCH 07/10] address comments related to repetative constants --- .../utils/rpc/RpcSemanticConventionUtils.java | 76 +++++++++++-------- 1 file changed, 46 insertions(+), 30 deletions(-) diff --git a/semantic-convention-utils/src/main/java/org/hypertrace/semantic/convention/utils/rpc/RpcSemanticConventionUtils.java b/semantic-convention-utils/src/main/java/org/hypertrace/semantic/convention/utils/rpc/RpcSemanticConventionUtils.java index 11eacd575..5187b5e9b 100644 --- a/semantic-convention-utils/src/main/java/org/hypertrace/semantic/convention/utils/rpc/RpcSemanticConventionUtils.java +++ b/semantic-convention-utils/src/main/java/org/hypertrace/semantic/convention/utils/rpc/RpcSemanticConventionUtils.java @@ -85,6 +85,28 @@ public class RpcSemanticConventionUtils { RawSpanConstants.getValue(CENSUS_RESPONSE_STATUS_MESSAGE), RawSpanConstants.getValue(ENVOY_GRPC_STATUS_MESSAGE)); + private static final String GRPC_REQUEST_BODY_TRUNCATED_ATTR = + RawSpanConstants.getValue(GRPC_REQUEST_BODY_TRUNCATED); + private static final String RPC_REQUEST_BODY_TRUNCATED_ATTR = + RPC_REQUEST_BODY_TRUNCATED.getValue(); + private static final String ENVOY_REQUEST_SIZE_ATTR = + RawSpanConstants.getValue(ENVOY_REQUEST_SIZE); + private static final String RPC_REQUEST_METADATA_CONTENT_LENGTH_ATTR = + RPC_REQUEST_METADATA_CONTENT_LENGTH.getValue(); + private static final String GRPC_REQUEST_BODY_ATTR = RawSpanConstants.getValue(GRPC_REQUEST_BODY); + private static final String RPC_REQUEST_BODY_ATTR = RPC_REQUEST_BODY.getValue(); + private static final String GRPC_RESPONSE_BODY_TRUNCATED_ATTR = + RawSpanConstants.getValue(GRPC_RESPONSE_BODY_TRUNCATED); + private static final String RPC_RESPONSE_BODY_TRUNCATED_ATTR = + RPC_RESPONSE_BODY_TRUNCATED.getValue(); + private static final String ENVOY_RESPONSE_SIZE_ATTR = + RawSpanConstants.getValue(ENVOY_RESPONSE_SIZE); + private static final String RPC_RESPONSE_METADATA_CONTENT_LENGTH_ATTR = + RPC_RESPONSE_METADATA_CONTENT_LENGTH.getValue(); + private static final String GRPC_RESPONSE_BODY_ATTR = + RawSpanConstants.getValue(GRPC_RESPONSE_BODY); + private static final String RPC_RESPONSE_BODY_ATTR = RPC_RESPONSE_BODY.getValue(); + /** * Differs from {@link * org.hypertrace.semantic.convention.utils.rpc.RpcSemanticConventionUtils#getGrpcURI(Event) in @@ -276,17 +298,17 @@ public static Optional getGrpcAuthority(Event event) { } private static boolean isGrpcRequestBodyTruncated(Map attributeValueMap) { - if (attributeValueMap.get(RawSpanConstants.getValue(GRPC_REQUEST_BODY_TRUNCATED)) != null) { + if (attributeValueMap.get(GRPC_REQUEST_BODY_TRUNCATED_ATTR) != null) { return Boolean.parseBoolean( - attributeValueMap.get(RawSpanConstants.getValue(GRPC_REQUEST_BODY_TRUNCATED)).getValue()); + attributeValueMap.get(GRPC_REQUEST_BODY_TRUNCATED_ATTR).getValue()); } return false; } private static boolean isRpcRequestBodyTruncated(Map attributeValueMap) { - if (attributeValueMap.get(RPC_REQUEST_BODY_TRUNCATED.getValue()) != null) { + if (attributeValueMap.get(RPC_REQUEST_BODY_TRUNCATED_ATTR) != null) { return Boolean.parseBoolean( - attributeValueMap.get(RPC_REQUEST_BODY_TRUNCATED.getValue()).getValue()); + attributeValueMap.get(RPC_REQUEST_BODY_TRUNCATED_ATTR).getValue()); } return false; } @@ -297,24 +319,22 @@ public static Optional getGrpcRequestSize(Event event) { } Map attributeValueMap = event.getAttributes().getAttributeMap(); - if (attributeValueMap.get(RawSpanConstants.getValue(ENVOY_REQUEST_SIZE)) != null) { + if (attributeValueMap.get(ENVOY_REQUEST_SIZE_ATTR) != null) { return Optional.of( - Integer.parseInt( - attributeValueMap.get(RawSpanConstants.getValue(ENVOY_REQUEST_SIZE)).getValue())); + Integer.parseInt(attributeValueMap.get(ENVOY_REQUEST_SIZE_ATTR).getValue())); } else if (isRpcSystemGrpc(attributeValueMap) - && attributeValueMap.get(RPC_REQUEST_METADATA_CONTENT_LENGTH.getValue()) != null) { + && attributeValueMap.get(RPC_REQUEST_METADATA_CONTENT_LENGTH_ATTR) != null) { return Optional.of( Integer.parseInt( - attributeValueMap.get(RPC_REQUEST_METADATA_CONTENT_LENGTH.getValue()).getValue())); - } else if (attributeValueMap.get(RawSpanConstants.getValue(GRPC_REQUEST_BODY)) != null + attributeValueMap.get(RPC_REQUEST_METADATA_CONTENT_LENGTH_ATTR).getValue())); + } else if (attributeValueMap.get(GRPC_REQUEST_BODY_ATTR) != null && !isGrpcRequestBodyTruncated(attributeValueMap)) { - String requestBody = - attributeValueMap.get(RawSpanConstants.getValue(GRPC_REQUEST_BODY)).getValue(); + String requestBody = attributeValueMap.get(GRPC_REQUEST_BODY_ATTR).getValue(); return Optional.of(requestBody.length()); } else if (isRpcSystemGrpc(attributeValueMap) - && attributeValueMap.get(RPC_REQUEST_BODY.getValue()) != null + && attributeValueMap.get(RPC_REQUEST_BODY_ATTR) != null && !isRpcRequestBodyTruncated(attributeValueMap)) { - String requestBody = attributeValueMap.get(RPC_REQUEST_BODY.getValue()).getValue(); + String requestBody = attributeValueMap.get(RPC_REQUEST_BODY_ATTR).getValue(); return Optional.of(requestBody.length()); } @@ -323,19 +343,17 @@ public static Optional getGrpcRequestSize(Event event) { private static boolean isGrpcResponseBodyTruncated( Map attributeValueMap) { - if (attributeValueMap.get(RawSpanConstants.getValue(GRPC_RESPONSE_BODY_TRUNCATED)) != null) { + if (attributeValueMap.get(GRPC_RESPONSE_BODY_TRUNCATED_ATTR) != null) { return Boolean.parseBoolean( - attributeValueMap - .get(RawSpanConstants.getValue(GRPC_RESPONSE_BODY_TRUNCATED)) - .getValue()); + attributeValueMap.get(GRPC_RESPONSE_BODY_TRUNCATED_ATTR).getValue()); } return false; } private static boolean isRpcResponseBodyTruncated(Map attributeValueMap) { - if (attributeValueMap.get(RPC_RESPONSE_BODY_TRUNCATED.getValue()) != null) { + if (attributeValueMap.get(RPC_RESPONSE_BODY_TRUNCATED_ATTR) != null) { return Boolean.parseBoolean( - attributeValueMap.get(RPC_RESPONSE_BODY_TRUNCATED.getValue()).getValue()); + attributeValueMap.get(RPC_RESPONSE_BODY_TRUNCATED_ATTR).getValue()); } return false; } @@ -346,24 +364,22 @@ public static Optional getGrpcResponseSize(Event event) { } Map attributeValueMap = event.getAttributes().getAttributeMap(); - if (attributeValueMap.get(RawSpanConstants.getValue(ENVOY_RESPONSE_SIZE)) != null) { + if (attributeValueMap.get(ENVOY_RESPONSE_SIZE_ATTR) != null) { return Optional.of( - Integer.parseInt( - attributeValueMap.get(RawSpanConstants.getValue(ENVOY_RESPONSE_SIZE)).getValue())); + Integer.parseInt(attributeValueMap.get(ENVOY_RESPONSE_SIZE_ATTR).getValue())); } else if (isRpcSystemGrpc(attributeValueMap) - && attributeValueMap.get(RPC_RESPONSE_METADATA_CONTENT_LENGTH.getValue()) != null) { + && attributeValueMap.get(RPC_RESPONSE_METADATA_CONTENT_LENGTH_ATTR) != null) { return Optional.of( Integer.parseInt( - attributeValueMap.get(RPC_RESPONSE_METADATA_CONTENT_LENGTH.getValue()).getValue())); - } else if (attributeValueMap.get(RawSpanConstants.getValue(GRPC_RESPONSE_BODY)) != null + attributeValueMap.get(RPC_RESPONSE_METADATA_CONTENT_LENGTH_ATTR).getValue())); + } else if (attributeValueMap.get(GRPC_RESPONSE_BODY_ATTR) != null && !isGrpcResponseBodyTruncated(attributeValueMap)) { - String responseBody = - attributeValueMap.get(RawSpanConstants.getValue(GRPC_RESPONSE_BODY)).getValue(); + String responseBody = attributeValueMap.get(GRPC_RESPONSE_BODY_ATTR).getValue(); return Optional.of(responseBody.length()); } else if (isRpcSystemGrpc(attributeValueMap) - && attributeValueMap.get(RPC_RESPONSE_BODY.getValue()) != null + && attributeValueMap.get(RPC_RESPONSE_BODY_ATTR) != null && !isRpcResponseBodyTruncated(attributeValueMap)) { - String responseBody = attributeValueMap.get(RPC_RESPONSE_BODY.getValue()).getValue(); + String responseBody = attributeValueMap.get(RPC_RESPONSE_BODY_ATTR).getValue(); return Optional.of(responseBody.length()); } From 1cee5d2c32eabe1b916eeccb03396395c463b84d Mon Sep 17 00:00:00 2001 From: Ronak Date: Tue, 26 Oct 2021 12:42:23 +0530 Subject: [PATCH 08/10] address few more comments --- .../http/HttpSemanticConventionUtils.java | 10 +- .../utils/rpc/RpcSemanticConventionUtils.java | 110 +++++++++--------- 2 files changed, 64 insertions(+), 56 deletions(-) diff --git a/semantic-convention-utils/src/main/java/org/hypertrace/semantic/convention/utils/http/HttpSemanticConventionUtils.java b/semantic-convention-utils/src/main/java/org/hypertrace/semantic/convention/utils/http/HttpSemanticConventionUtils.java index 5468879db..e594692a8 100644 --- a/semantic-convention-utils/src/main/java/org/hypertrace/semantic/convention/utils/http/HttpSemanticConventionUtils.java +++ b/semantic-convention-utils/src/main/java/org/hypertrace/semantic/convention/utils/http/HttpSemanticConventionUtils.java @@ -78,8 +78,12 @@ public class HttpSemanticConventionUtils { private static final String RELATIVE_URL_CONTEXT = "http://hypertrace.org"; private static final String HTTP_REQUEST_BODY = RawSpanConstants.getValue(HTTP_HTTP_REQUEST_BODY); + private static final String HTTP_REQUEST_BODY_TRUNCATED_ATTR = + RawSpanConstants.getValue(HTTP_REQUEST_BODY_TRUNCATED); private static final String HTTP_RESPONSE_BODY = RawSpanConstants.getValue(HTTP_HTTP_RESPONSE_BODY); + private static final String HTTP_RESPONSE_BODY_TRUNCATED_ATTR = + RawSpanConstants.getValue(HTTP_RESPONSE_BODY_TRUNCATED); private static final String SLASH = "/"; @@ -490,8 +494,7 @@ public static Optional getHttpRequestSize(Event event) { Optional requestSize = Optional.ofNullable(httpRequestSize); if (!requestSize.isEmpty()) return requestSize.map(Integer::parseInt); - if (SpanAttributeUtils.getBooleanAttribute( - event, RawSpanConstants.getValue(HTTP_REQUEST_BODY_TRUNCATED))) { + if (SpanAttributeUtils.getBooleanAttribute(event, HTTP_REQUEST_BODY_TRUNCATED_ATTR)) { return Optional.empty(); } @@ -506,8 +509,7 @@ public static Optional getHttpResponseSize(Event event) { Optional responseSize = Optional.ofNullable(httpResponseSize); if (!responseSize.isEmpty()) return responseSize.map(Integer::parseInt); - if (SpanAttributeUtils.getBooleanAttribute( - event, RawSpanConstants.getValue(HTTP_RESPONSE_BODY_TRUNCATED))) { + if (SpanAttributeUtils.getBooleanAttribute(event, HTTP_RESPONSE_BODY_TRUNCATED_ATTR)) { return Optional.empty(); } diff --git a/semantic-convention-utils/src/main/java/org/hypertrace/semantic/convention/utils/rpc/RpcSemanticConventionUtils.java b/semantic-convention-utils/src/main/java/org/hypertrace/semantic/convention/utils/rpc/RpcSemanticConventionUtils.java index 5187b5e9b..b101a65b3 100644 --- a/semantic-convention-utils/src/main/java/org/hypertrace/semantic/convention/utils/rpc/RpcSemanticConventionUtils.java +++ b/semantic-convention-utils/src/main/java/org/hypertrace/semantic/convention/utils/rpc/RpcSemanticConventionUtils.java @@ -298,19 +298,17 @@ public static Optional getGrpcAuthority(Event event) { } private static boolean isGrpcRequestBodyTruncated(Map attributeValueMap) { - if (attributeValueMap.get(GRPC_REQUEST_BODY_TRUNCATED_ATTR) != null) { - return Boolean.parseBoolean( - attributeValueMap.get(GRPC_REQUEST_BODY_TRUNCATED_ATTR).getValue()); - } - return false; + Optional attributeValue = + Optional.ofNullable(attributeValueMap.get(GRPC_REQUEST_BODY_TRUNCATED_ATTR)); + + return attributeValue.filter(av -> Boolean.parseBoolean(av.getValue())).isPresent(); } private static boolean isRpcRequestBodyTruncated(Map attributeValueMap) { - if (attributeValueMap.get(RPC_REQUEST_BODY_TRUNCATED_ATTR) != null) { - return Boolean.parseBoolean( - attributeValueMap.get(RPC_REQUEST_BODY_TRUNCATED_ATTR).getValue()); - } - return false; + Optional attributeValue = + Optional.ofNullable(attributeValueMap.get(RPC_REQUEST_BODY_TRUNCATED_ATTR)); + + return attributeValue.filter(av -> Boolean.parseBoolean(av.getValue())).isPresent(); } public static Optional getGrpcRequestSize(Event event) { @@ -319,23 +317,28 @@ public static Optional getGrpcRequestSize(Event event) { } Map attributeValueMap = event.getAttributes().getAttributeMap(); - if (attributeValueMap.get(ENVOY_REQUEST_SIZE_ATTR) != null) { - return Optional.of( - Integer.parseInt(attributeValueMap.get(ENVOY_REQUEST_SIZE_ATTR).getValue())); - } else if (isRpcSystemGrpc(attributeValueMap) - && attributeValueMap.get(RPC_REQUEST_METADATA_CONTENT_LENGTH_ATTR) != null) { - return Optional.of( - Integer.parseInt( - attributeValueMap.get(RPC_REQUEST_METADATA_CONTENT_LENGTH_ATTR).getValue())); - } else if (attributeValueMap.get(GRPC_REQUEST_BODY_ATTR) != null - && !isGrpcRequestBodyTruncated(attributeValueMap)) { - String requestBody = attributeValueMap.get(GRPC_REQUEST_BODY_ATTR).getValue(); - return Optional.of(requestBody.length()); - } else if (isRpcSystemGrpc(attributeValueMap) - && attributeValueMap.get(RPC_REQUEST_BODY_ATTR) != null + Optional attributeValue = + Optional.ofNullable(attributeValueMap.get(ENVOY_REQUEST_SIZE_ATTR)); + if (attributeValue.isPresent()) { + return Optional.of(Integer.parseInt(attributeValue.get().getValue())); + } + + attributeValue = + Optional.ofNullable(attributeValueMap.get(RPC_REQUEST_METADATA_CONTENT_LENGTH_ATTR)); + if (isRpcSystemGrpc(attributeValueMap) && attributeValue.isPresent()) { + return Optional.of(Integer.parseInt(attributeValue.get().getValue())); + } + + attributeValue = Optional.ofNullable(attributeValueMap.get(GRPC_REQUEST_BODY_ATTR)); + if (attributeValue.isPresent() && !isGrpcRequestBodyTruncated(attributeValueMap)) { + return Optional.of(attributeValue.get().getValue().length()); + } + + attributeValue = Optional.ofNullable(attributeValueMap.get(RPC_REQUEST_BODY_ATTR)); + if (isRpcSystemGrpc(attributeValueMap) + && attributeValue.isPresent() && !isRpcRequestBodyTruncated(attributeValueMap)) { - String requestBody = attributeValueMap.get(RPC_REQUEST_BODY_ATTR).getValue(); - return Optional.of(requestBody.length()); + return Optional.of(attributeValue.get().getValue().length()); } return Optional.empty(); @@ -343,19 +346,17 @@ public static Optional getGrpcRequestSize(Event event) { private static boolean isGrpcResponseBodyTruncated( Map attributeValueMap) { - if (attributeValueMap.get(GRPC_RESPONSE_BODY_TRUNCATED_ATTR) != null) { - return Boolean.parseBoolean( - attributeValueMap.get(GRPC_RESPONSE_BODY_TRUNCATED_ATTR).getValue()); - } - return false; + Optional attributeValue = + Optional.ofNullable(attributeValueMap.get(GRPC_RESPONSE_BODY_TRUNCATED_ATTR)); + + return attributeValue.filter(av -> Boolean.parseBoolean(av.getValue())).isPresent(); } private static boolean isRpcResponseBodyTruncated(Map attributeValueMap) { - if (attributeValueMap.get(RPC_RESPONSE_BODY_TRUNCATED_ATTR) != null) { - return Boolean.parseBoolean( - attributeValueMap.get(RPC_RESPONSE_BODY_TRUNCATED_ATTR).getValue()); - } - return false; + Optional attributeValue = + Optional.ofNullable(attributeValueMap.get(RPC_RESPONSE_BODY_TRUNCATED_ATTR)); + + return attributeValue.filter(av -> Boolean.parseBoolean(av.getValue())).isPresent(); } public static Optional getGrpcResponseSize(Event event) { @@ -364,23 +365,28 @@ public static Optional getGrpcResponseSize(Event event) { } Map attributeValueMap = event.getAttributes().getAttributeMap(); - if (attributeValueMap.get(ENVOY_RESPONSE_SIZE_ATTR) != null) { - return Optional.of( - Integer.parseInt(attributeValueMap.get(ENVOY_RESPONSE_SIZE_ATTR).getValue())); - } else if (isRpcSystemGrpc(attributeValueMap) - && attributeValueMap.get(RPC_RESPONSE_METADATA_CONTENT_LENGTH_ATTR) != null) { - return Optional.of( - Integer.parseInt( - attributeValueMap.get(RPC_RESPONSE_METADATA_CONTENT_LENGTH_ATTR).getValue())); - } else if (attributeValueMap.get(GRPC_RESPONSE_BODY_ATTR) != null - && !isGrpcResponseBodyTruncated(attributeValueMap)) { - String responseBody = attributeValueMap.get(GRPC_RESPONSE_BODY_ATTR).getValue(); - return Optional.of(responseBody.length()); - } else if (isRpcSystemGrpc(attributeValueMap) - && attributeValueMap.get(RPC_RESPONSE_BODY_ATTR) != null + Optional attributeValue = + Optional.ofNullable(attributeValueMap.get(ENVOY_RESPONSE_SIZE_ATTR)); + if (attributeValue.isPresent()) { + return Optional.of(Integer.parseInt(attributeValue.get().getValue())); + } + + attributeValue = + Optional.ofNullable(attributeValueMap.get(RPC_RESPONSE_METADATA_CONTENT_LENGTH_ATTR)); + if (isRpcSystemGrpc(attributeValueMap) && attributeValue.isPresent()) { + return Optional.of(Integer.parseInt(attributeValue.get().getValue())); + } + + attributeValue = Optional.ofNullable(attributeValueMap.get(GRPC_RESPONSE_BODY_ATTR)); + if (attributeValue.isPresent() && !isGrpcResponseBodyTruncated(attributeValueMap)) { + return Optional.of(attributeValue.get().getValue().length()); + } + + attributeValue = Optional.ofNullable(attributeValueMap.get(RPC_RESPONSE_BODY_ATTR)); + if (isRpcSystemGrpc(attributeValueMap) + && attributeValue.isPresent() && !isRpcResponseBodyTruncated(attributeValueMap)) { - String responseBody = attributeValueMap.get(RPC_RESPONSE_BODY_ATTR).getValue(); - return Optional.of(responseBody.length()); + return Optional.of(attributeValue.get().getValue().length()); } return Optional.empty(); From 85fa08777ee2e70983befeb917632bbf0a0cb31f Mon Sep 17 00:00:00 2001 From: Ronak Date: Tue, 26 Oct 2021 13:02:12 +0530 Subject: [PATCH 09/10] refactor a bit for redability --- .../utils/rpc/RpcSemanticConventionUtils.java | 29 ++++++++++--------- 1 file changed, 15 insertions(+), 14 deletions(-) diff --git a/semantic-convention-utils/src/main/java/org/hypertrace/semantic/convention/utils/rpc/RpcSemanticConventionUtils.java b/semantic-convention-utils/src/main/java/org/hypertrace/semantic/convention/utils/rpc/RpcSemanticConventionUtils.java index b101a65b3..7e4ce5e46 100644 --- a/semantic-convention-utils/src/main/java/org/hypertrace/semantic/convention/utils/rpc/RpcSemanticConventionUtils.java +++ b/semantic-convention-utils/src/main/java/org/hypertrace/semantic/convention/utils/rpc/RpcSemanticConventionUtils.java @@ -317,28 +317,29 @@ public static Optional getGrpcRequestSize(Event event) { } Map attributeValueMap = event.getAttributes().getAttributeMap(); + Optional attributeValue = Optional.ofNullable(attributeValueMap.get(ENVOY_REQUEST_SIZE_ATTR)); if (attributeValue.isPresent()) { - return Optional.of(Integer.parseInt(attributeValue.get().getValue())); + return attributeValue.map(av -> Integer.parseInt(av.getValue())); } attributeValue = Optional.ofNullable(attributeValueMap.get(RPC_REQUEST_METADATA_CONTENT_LENGTH_ATTR)); - if (isRpcSystemGrpc(attributeValueMap) && attributeValue.isPresent()) { - return Optional.of(Integer.parseInt(attributeValue.get().getValue())); + if (attributeValue.isPresent() && isRpcSystemGrpc(attributeValueMap)) { + return attributeValue.map(av -> Integer.parseInt(av.getValue())); } attributeValue = Optional.ofNullable(attributeValueMap.get(GRPC_REQUEST_BODY_ATTR)); if (attributeValue.isPresent() && !isGrpcRequestBodyTruncated(attributeValueMap)) { - return Optional.of(attributeValue.get().getValue().length()); + return attributeValue.map(av -> av.getValue().length()); } attributeValue = Optional.ofNullable(attributeValueMap.get(RPC_REQUEST_BODY_ATTR)); - if (isRpcSystemGrpc(attributeValueMap) - && attributeValue.isPresent() + if (attributeValue.isPresent() + && isRpcSystemGrpc(attributeValueMap) && !isRpcRequestBodyTruncated(attributeValueMap)) { - return Optional.of(attributeValue.get().getValue().length()); + return attributeValue.map(av -> av.getValue().length()); } return Optional.empty(); @@ -368,25 +369,25 @@ public static Optional getGrpcResponseSize(Event event) { Optional attributeValue = Optional.ofNullable(attributeValueMap.get(ENVOY_RESPONSE_SIZE_ATTR)); if (attributeValue.isPresent()) { - return Optional.of(Integer.parseInt(attributeValue.get().getValue())); + return attributeValue.map(av -> Integer.parseInt(av.getValue())); } attributeValue = Optional.ofNullable(attributeValueMap.get(RPC_RESPONSE_METADATA_CONTENT_LENGTH_ATTR)); - if (isRpcSystemGrpc(attributeValueMap) && attributeValue.isPresent()) { - return Optional.of(Integer.parseInt(attributeValue.get().getValue())); + if (attributeValue.isPresent() && isRpcSystemGrpc(attributeValueMap)) { + return attributeValue.map(av -> Integer.parseInt(av.getValue())); } attributeValue = Optional.ofNullable(attributeValueMap.get(GRPC_RESPONSE_BODY_ATTR)); if (attributeValue.isPresent() && !isGrpcResponseBodyTruncated(attributeValueMap)) { - return Optional.of(attributeValue.get().getValue().length()); + return attributeValue.map(av -> av.getValue().length()); } attributeValue = Optional.ofNullable(attributeValueMap.get(RPC_RESPONSE_BODY_ATTR)); - if (isRpcSystemGrpc(attributeValueMap) - && attributeValue.isPresent() + if (attributeValue.isPresent() + && isRpcSystemGrpc(attributeValueMap) && !isRpcResponseBodyTruncated(attributeValueMap)) { - return Optional.of(attributeValue.get().getValue().length()); + return attributeValue.map(av -> av.getValue().length()); } return Optional.empty(); From f8387fe2cb09061327984fa3b5c1fbc8c6834bd5 Mon Sep 17 00:00:00 2001 From: Ronak Date: Tue, 26 Oct 2021 13:17:32 +0530 Subject: [PATCH 10/10] bit refactor for redability --- .../utils/rpc/RpcSemanticConventionUtils.java | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/semantic-convention-utils/src/main/java/org/hypertrace/semantic/convention/utils/rpc/RpcSemanticConventionUtils.java b/semantic-convention-utils/src/main/java/org/hypertrace/semantic/convention/utils/rpc/RpcSemanticConventionUtils.java index 7e4ce5e46..40f53cb15 100644 --- a/semantic-convention-utils/src/main/java/org/hypertrace/semantic/convention/utils/rpc/RpcSemanticConventionUtils.java +++ b/semantic-convention-utils/src/main/java/org/hypertrace/semantic/convention/utils/rpc/RpcSemanticConventionUtils.java @@ -321,25 +321,25 @@ public static Optional getGrpcRequestSize(Event event) { Optional attributeValue = Optional.ofNullable(attributeValueMap.get(ENVOY_REQUEST_SIZE_ATTR)); if (attributeValue.isPresent()) { - return attributeValue.map(av -> Integer.parseInt(av.getValue())); + return attributeValue.map(av -> av.getValue()).map(Integer::parseInt); } attributeValue = Optional.ofNullable(attributeValueMap.get(RPC_REQUEST_METADATA_CONTENT_LENGTH_ATTR)); if (attributeValue.isPresent() && isRpcSystemGrpc(attributeValueMap)) { - return attributeValue.map(av -> Integer.parseInt(av.getValue())); + return attributeValue.map(av -> av.getValue()).map(Integer::parseInt); } attributeValue = Optional.ofNullable(attributeValueMap.get(GRPC_REQUEST_BODY_ATTR)); if (attributeValue.isPresent() && !isGrpcRequestBodyTruncated(attributeValueMap)) { - return attributeValue.map(av -> av.getValue().length()); + return attributeValue.map(av -> av.getValue()).map(String::length); } attributeValue = Optional.ofNullable(attributeValueMap.get(RPC_REQUEST_BODY_ATTR)); if (attributeValue.isPresent() && isRpcSystemGrpc(attributeValueMap) && !isRpcRequestBodyTruncated(attributeValueMap)) { - return attributeValue.map(av -> av.getValue().length()); + return attributeValue.map(av -> av.getValue()).map(String::length); } return Optional.empty(); @@ -369,25 +369,25 @@ public static Optional getGrpcResponseSize(Event event) { Optional attributeValue = Optional.ofNullable(attributeValueMap.get(ENVOY_RESPONSE_SIZE_ATTR)); if (attributeValue.isPresent()) { - return attributeValue.map(av -> Integer.parseInt(av.getValue())); + return attributeValue.map(av -> av.getValue()).map(Integer::parseInt); } attributeValue = Optional.ofNullable(attributeValueMap.get(RPC_RESPONSE_METADATA_CONTENT_LENGTH_ATTR)); if (attributeValue.isPresent() && isRpcSystemGrpc(attributeValueMap)) { - return attributeValue.map(av -> Integer.parseInt(av.getValue())); + return attributeValue.map(av -> av.getValue()).map(Integer::parseInt); } attributeValue = Optional.ofNullable(attributeValueMap.get(GRPC_RESPONSE_BODY_ATTR)); if (attributeValue.isPresent() && !isGrpcResponseBodyTruncated(attributeValueMap)) { - return attributeValue.map(av -> av.getValue().length()); + return attributeValue.map(av -> av.getValue()).map(String::length); } attributeValue = Optional.ofNullable(attributeValueMap.get(RPC_RESPONSE_BODY_ATTR)); if (attributeValue.isPresent() && isRpcSystemGrpc(attributeValueMap) && !isRpcResponseBodyTruncated(attributeValueMap)) { - return attributeValue.map(av -> av.getValue().length()); + return attributeValue.map(av -> av.getValue()).map(String::length); } return Optional.empty();