From 2a35c9331121662d754cee0a266323d8e4e79202 Mon Sep 17 00:00:00 2001 From: Apeksha Bhosale <7846888+ApekshaBhosale@users.noreply.github.com> Date: Mon, 3 Feb 2025 13:50:56 +0530 Subject: [PATCH] chore: added errorcode and exception to http_server_requests for better slo (#38892) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ## Description > [!TIP] > _Add a TL;DR when the description is longer than 500 words or extremely technical (helps the content, marketing, and DevRel team)._ > > _Please also include relevant motivation and context. List any dependencies that are required for this change. Add links to Notion, Figma or any other documents that might be relevant to the PR._ Fixes #`Issue Number` _or_ Fixes `Issue URL` > [!WARNING] > _If no issue exists, please create an issue first, and check with the maintainers if the issue is valid._ ## Automation /ok-to-test tags="@tag.All" ### :mag: Cypress test results > [!TIP] > 🟢 🟢 🟢 All cypress tests have passed! 🎉 🎉 🎉 > Workflow run: > Commit: cc763a76e87863bb806b9740d8c7d239d8f02af8 > Cypress dashboard. > Tags: `@tag.All` > Spec: >
Mon, 03 Feb 2025 07:58:57 UTC ## Communication Should the DevRel and Marketing teams inform users about this change? - [ ] Yes - [ ] No ## Summary by CodeRabbit - **Observability Improvements** - Enhanced error tracking and monitoring capabilities - Added detailed error context for server request observations - Improved error information logging with specific error codes and titles - Introduced a new constant for representing a "none" state in the application --- .../configurations/ObservationConfig.java | 36 +++++++++++++++++++ .../server/constants/ce/FieldNameCE.java | 1 + .../exceptions/GlobalExceptionHandler.java | 8 +++++ 3 files changed, 45 insertions(+) create mode 100644 app/server/appsmith-server/src/main/java/com/appsmith/server/configurations/ObservationConfig.java diff --git a/app/server/appsmith-server/src/main/java/com/appsmith/server/configurations/ObservationConfig.java b/app/server/appsmith-server/src/main/java/com/appsmith/server/configurations/ObservationConfig.java new file mode 100644 index 0000000000..6d54c2ab34 --- /dev/null +++ b/app/server/appsmith-server/src/main/java/com/appsmith/server/configurations/ObservationConfig.java @@ -0,0 +1,36 @@ +package com.appsmith.server.configurations; + +import com.appsmith.server.constants.FieldName; +import com.appsmith.server.exceptions.AppsmithException; +import io.micrometer.common.KeyValue; +import io.micrometer.common.KeyValues; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.http.server.reactive.observation.DefaultServerRequestObservationConvention; +import org.springframework.http.server.reactive.observation.ServerRequestObservationContext; +import org.springframework.http.server.reactive.observation.ServerRequestObservationConvention; + +@Configuration +public class ObservationConfig { + + @Bean + public ServerRequestObservationConvention customObservationConvention() { + return new DefaultServerRequestObservationConvention() { + @Override + public KeyValues getLowCardinalityKeyValues(ServerRequestObservationContext context) { + KeyValues keyValues = super.getLowCardinalityKeyValues(context); + + // Extract error code safely + String errorCode = context.getError() != null && context.getError() instanceof AppsmithException + ? (((AppsmithException) context.getError()).getAppErrorCode()) + : FieldName.NONE; + + String errorTitle = context.getError() != null && context.getError() instanceof AppsmithException + ? (((AppsmithException) context.getError()).getTitle()) + : FieldName.NONE; + + return keyValues.and(KeyValue.of("errorCode", errorCode)).and(KeyValue.of("exception", errorTitle)); + } + }; + } +} diff --git a/app/server/appsmith-server/src/main/java/com/appsmith/server/constants/ce/FieldNameCE.java b/app/server/appsmith-server/src/main/java/com/appsmith/server/constants/ce/FieldNameCE.java index 4a2aa044ce..ab487f3e12 100644 --- a/app/server/appsmith-server/src/main/java/com/appsmith/server/constants/ce/FieldNameCE.java +++ b/app/server/appsmith-server/src/main/java/com/appsmith/server/constants/ce/FieldNameCE.java @@ -203,4 +203,5 @@ public class FieldNameCE { public static final String ARTIFACT_CONTEXT = "artifactContext"; public static final String ARTIFACT_ID = "artifactId"; public static final String BODY = "body"; + public static final String NONE = "none"; } diff --git a/app/server/appsmith-server/src/main/java/com/appsmith/server/exceptions/GlobalExceptionHandler.java b/app/server/appsmith-server/src/main/java/com/appsmith/server/exceptions/GlobalExceptionHandler.java index f9ef6a4dfb..9c73bd5639 100644 --- a/app/server/appsmith-server/src/main/java/com/appsmith/server/exceptions/GlobalExceptionHandler.java +++ b/app/server/appsmith-server/src/main/java/com/appsmith/server/exceptions/GlobalExceptionHandler.java @@ -10,6 +10,7 @@ import com.appsmith.server.helpers.CommonGitFileUtils; import com.appsmith.server.helpers.RedisUtils; import com.appsmith.server.services.AnalyticsService; import com.appsmith.server.services.SessionUserService; +import io.micrometer.common.KeyValue; import io.micrometer.core.instrument.util.StringUtils; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; @@ -17,6 +18,7 @@ import org.eclipse.jgit.api.errors.JGitInternalException; import org.eclipse.jgit.errors.LockFailedException; import org.springframework.core.io.buffer.DataBufferLimitException; import org.springframework.http.HttpStatus; +import org.springframework.http.server.reactive.observation.ServerRequestObservationContext; import org.springframework.security.access.AccessDeniedException; import org.springframework.validation.FieldError; import org.springframework.web.bind.annotation.ControllerAdvice; @@ -84,6 +86,12 @@ public class GlobalExceptionHandler { e.getAppErrorCode(), e.getErrorType(), e.getMessage(), e.getTitle(), e.getReferenceDoc())); } + ServerRequestObservationContext.findCurrent(exchange.getAttributes()).ifPresent(context -> { + context.setError(e); + context.addLowCardinalityKeyValue(KeyValue.of("errorCode", e.getAppErrorCode())); + context.addLowCardinalityKeyValue(KeyValue.of("exception", e.getTitle())); + }); + return getResponseDTOMono(urlPath, response); }