chore: add pageId and applicationSlug to all traces (#37219)

## Description
Add pageId and applicationSlug as common attributes to all spans


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.Sanity"

### 🔍 Cypress test results
<!-- This is an auto-generated comment: Cypress test results  -->
> [!IMPORTANT]
> 🟣 🟣 🟣 Your tests are running.
> Tests running at:
<https://github.com/appsmithorg/appsmith/actions/runs/11679770316>
> Commit: de6069395a4e0c6a92ad1604742f8e525b47ad28
> Workflow: `PR Automation test suite`
> Tags: `@tag.Sanity`
> Spec: ``
> <hr>Tue, 05 Nov 2024 07:52:37 UTC
<!-- end of auto-generated comment: Cypress test results  -->


## Communication
Should the DevRel and Marketing teams inform users about this change?
- [ ] Yes
- [x] No
This commit is contained in:
Diljit 2024-11-06 14:57:57 +05:30 committed by GitHub
parent 756dc5421e
commit ff3f22e55f
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 39 additions and 21 deletions

View File

@ -14,10 +14,9 @@ import {
osName, osName,
osVersion, osVersion,
} from "react-device-detect"; } from "react-device-detect";
import { APP_MODE } from "entities/App";
import { matchBuilderPath, matchViewerPath } from "constants/routes";
import nanoid from "nanoid"; import nanoid from "nanoid";
import memoizeOne from "memoize-one"; import memoizeOne from "memoize-one";
import { getApplicationParamsFromUrl } from "ee/utils/serviceWorkerUtils";
const GENERATOR_TRACE = "generator-tracer"; const GENERATOR_TRACE = "generator-tracer";
@ -26,25 +25,36 @@ export type SpanAttributes = Attributes;
const OTLP_SESSION_ID = nanoid(); const OTLP_SESSION_ID = nanoid();
const getAppMode = memoizeOne((pathname: string) => { const getAppParams = memoizeOne(
const isEditorUrl = matchBuilderPath(pathname); (origin: string, pathname: string, search: string) => {
const isViewerUrl = matchViewerPath(pathname); const applicationParams = getApplicationParamsFromUrl({
origin,
pathname,
search,
});
const appMode = isEditorUrl const {
? APP_MODE.EDIT applicationSlug,
: isViewerUrl appMode = "",
? APP_MODE.PUBLISHED basePageId: pageId,
: ""; branchName,
} = applicationParams || {};
return appMode; return {
}); appMode,
pageId,
branchName,
applicationSlug,
};
},
);
export const getCommonTelemetryAttributes = () => { export const getCommonTelemetryAttributes = () => {
const pathname = window.location.pathname; const { origin, pathname, search } = window.location;
const appMode = getAppMode(pathname); const appParams = getAppParams(origin, pathname, search);
return { return {
appMode, ...appParams,
deviceType, deviceType,
browserName, browserName,
browserVersion, browserVersion,

View File

@ -212,6 +212,7 @@ describe("serviceWorkerUtils", () => {
const expectedParams: TApplicationParams = { const expectedParams: TApplicationParams = {
origin: "https://app.appsmith.com", origin: "https://app.appsmith.com",
basePageId, basePageId,
applicationSlug: "my-app",
baseApplicationId: undefined, baseApplicationId: undefined,
branchName: "main", branchName: "main",
appMode: APP_MODE.EDIT, appMode: APP_MODE.EDIT,
@ -227,6 +228,7 @@ describe("serviceWorkerUtils", () => {
const expectedParams: TApplicationParams = { const expectedParams: TApplicationParams = {
origin: "https://app.appsmith.com", origin: "https://app.appsmith.com",
basePageId, basePageId,
applicationSlug: "my-app",
baseApplicationId: undefined, baseApplicationId: undefined,
branchName: "main", branchName: "main",
appMode: APP_MODE.PUBLISHED, appMode: APP_MODE.PUBLISHED,
@ -307,6 +309,7 @@ describe("serviceWorkerUtils", () => {
); );
const expectedParams: TApplicationParams = { const expectedParams: TApplicationParams = {
origin: "https://app.appsmith.com", origin: "https://app.appsmith.com",
applicationSlug: "my-app",
basePageId, basePageId,
baseApplicationId: undefined, baseApplicationId: undefined,
branchName: "", branchName: "",

View File

@ -14,6 +14,7 @@ import {
interface TMatchResult { interface TMatchResult {
basePageId?: string; basePageId?: string;
baseApplicationId?: string; baseApplicationId?: string;
applicationSlug?: string;
} }
export interface TApplicationParams { export interface TApplicationParams {
@ -22,6 +23,7 @@ export interface TApplicationParams {
baseApplicationId?: string; baseApplicationId?: string;
branchName: string; branchName: string;
appMode: APP_MODE; appMode: APP_MODE;
applicationSlug?: string;
} }
type TApplicationParamsOrNull = TApplicationParams | null; type TApplicationParamsOrNull = TApplicationParams | null;
@ -57,33 +59,36 @@ export const getSearchQuery = (search = "", key: string) => {
}; };
export const getApplicationParamsFromUrl = ( export const getApplicationParamsFromUrl = (
url: URL, urlParams: Pick<URL, "origin" | "pathname" | "search">,
): TApplicationParamsOrNull => { ): TApplicationParamsOrNull => {
const { origin, pathname, search } = urlParams;
// Get the branch name from the query string // Get the branch name from the query string
const branchName = getSearchQuery(url.search, "branch"); const branchName = getSearchQuery(search, "branch");
const matchedBuilder: Match<TMatchResult> = matchBuilderPath(url.pathname, { const matchedBuilder: Match<TMatchResult> = matchBuilderPath(pathname, {
end: false, end: false,
}); });
const matchedViewer: Match<TMatchResult> = matchViewerPath(url.pathname); const matchedViewer: Match<TMatchResult> = matchViewerPath(pathname);
if (matchedBuilder) { if (matchedBuilder) {
return { return {
origin: url.origin, origin,
basePageId: matchedBuilder.params.basePageId, basePageId: matchedBuilder.params.basePageId,
baseApplicationId: matchedBuilder.params.baseApplicationId, baseApplicationId: matchedBuilder.params.baseApplicationId,
branchName, branchName,
appMode: APP_MODE.EDIT, appMode: APP_MODE.EDIT,
applicationSlug: matchedBuilder.params.applicationSlug,
}; };
} }
if (matchedViewer) { if (matchedViewer) {
return { return {
origin: url.origin, origin,
basePageId: matchedViewer.params.basePageId, basePageId: matchedViewer.params.basePageId,
baseApplicationId: matchedViewer.params.baseApplicationId, baseApplicationId: matchedViewer.params.baseApplicationId,
branchName, branchName,
appMode: APP_MODE.PUBLISHED, appMode: APP_MODE.PUBLISHED,
applicationSlug: matchedViewer.params.applicationSlug,
}; };
} }