Skip to content

[BREAKINGCHANGE] remove fetch from usage metrics#124

Open
shahrokni wants to merge 1 commit intomainfrom
refactor/remove_fetch_from_usage_metrics
Open

[BREAKINGCHANGE] remove fetch from usage metrics#124
shahrokni wants to merge 1 commit intomainfrom
refactor/remove_fetch_from_usage_metrics

Conversation

@shahrokni
Copy link
Copy Markdown
Contributor

@shahrokni shahrokni commented Apr 29, 2026

This PR removes the fetch (from core) dependency in plugin-system\src\runtime\UsageMetricsProvider.tsx
This is an IOC (inversion of control) kind of change which gives the consumer the responsibility of Submitting Metrics as this should not be the concern of the plugin-system. From the Perses/Perses perspective, this responsibility will reside in UI, probably somewhere around ui\app\src\views\projects\dashboards\HelperDashboardView.tsx

The new added prop (submitMetric) is optional, so this is not a breaking change. From Perses/Perses we need to provide the submit function

This change also includes a bug fix. Apparently, there is a direct mutation of the context which should be fixed by this PR.

Finally there is a change in test files. Please read the comment about the dependency

/* All GlobalDatasourceResource, DatasourceResource, etc ... 
   have been used for the test purpose only, creating a wrong dependency to the core!
   We either move this test (somehow) to the Perses/UI or we need to duplicate the types
*/

Checklist

  • Pull request has a descriptive title and context useful to a reviewer.
  • Pull request title follows the [<catalog_entry>] <commit message> naming convention using one of the
    following catalog_entry values: FEATURE, ENHANCEMENT, BUGFIX, BREAKINGCHANGE, DOC,IGNORE.
  • All commits have DCO signoffs.

UI Changes

  • Changes that impact the UI include screenshots and/or screencasts of the relevant changes.
  • Code follows the UI guidelines.
  • E2E tests are stable and unlikely to be flaky.
    See e2e docs for more details. Common issues include:
    • Is the data inconsistent? You need to mock API requests.
    • Does the time change? You need to use consistent time values or mock time utilities.
    • Does it have loading states? You need to wait for loading to complete.

@shahrokni shahrokni requested a review from a team as a code owner April 29, 2026 11:30
@shahrokni shahrokni force-pushed the refactor/remove_fetch_from_usage_metrics branch 2 times, most recently from 81371d9 to e9d2fcf Compare April 29, 2026 13:12
const [renderDurationMs, setRenderDurationMs] = useState(0);
const [pendingQueries, setPendingQueries] = useState(new Map());
const [renderErrorCount, setRenderErrorCount] = useState(0);
const ctx: UsageMetrics = {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should probably separate the types of the context and the type that is sent with the submitMetrics.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually the parameter of the submit function is a subset of the main interface.
So, it should be fine no?
So, as you can see the extra stuff is dropped from the param.

type SubmitMetrics = (
  param: Omit<UsageMetrics, 'setRenderDurationMs' | 'setRenderErrorCount' | 'setPendingQueries'>
) => Promise<void>;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The name does not describe what the interface holds, one is the UsageMetricsContext, the other one is the actual UsageMetrics that is sent to the backend.

project,
dashboard,
children,
submitMetrics,
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess is coming after this is merged. But we need to adjust Perses UI to submit the metrics.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is the comment suggesting I need to change something here, or it is only an announcment. Because =>
Yes, we must to provide the submitMetrics from the Perses/UI side.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should have at least an issue to track this should be added, otherwise usage metrics won't be stored leading to data loss

Copy link
Copy Markdown
Contributor Author

@shahrokni shahrokni Apr 29, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should have at least an issue to track this should be added, otherwise usage metrics won't be stored leading to data loss

Let's lift the optional ? and then make it BREAKINGCHANGE. Inherently it is BREAKINGCHANGE.
WDYT?

@shahrokni shahrokni force-pushed the refactor/remove_fetch_from_usage_metrics branch from e9d2fcf to e435f82 Compare April 29, 2026 13:16
method: HttpMethod;
contentType: ContentType;
headers?: Record<string, string>;
fetch: (...args: Parameters<typeof globalThis.fetch>) => Promise<Response>;
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is breaking the plugins that use this interface. I don't think the interface should be changed like this. A better approach would be to create a fetchProvider which plugins or components can use

Copy link
Copy Markdown
Contributor Author

@shahrokni shahrokni Apr 29, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So, as you know we are trying to drop the fetch-core dependency. Where do you want to place the fetchProvider? Such a provider will be placed in the shared, meaning, again there will be the same dependency but somewhere else. Unless, we move the entire logic of the fetch into that provider under the plugin-system,

Again, maybe it could have its own small package...
This is becoming a dilemma
It should either move to plugin-system or have its own package.

/* FROM CORE =to=> plugin-system or a dedicated package */
export async function fetch(...args: Parameters<typeof globalThis.fetch>): Promise<Response> {
  const response = await globalThis.fetch(...args);
  if (response.ok === false) {
    const contentType = response.headers.get('content-type');
    if (contentType?.includes('application/json')) {
      const json = await response.clone().json();
      if (json.error) {
        throw new UserFriendlyError(json.error, response.status);
      }
      if (json.message) {
        throw new UserFriendlyError(json.message, response.status);
      }
    }

    const text = await response.clone().text();
    if (text) {
      throw new UserFriendlyError(text, response.status);
    }
    throw new FetchError(response);
  }
  return response;
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it probably has to be in the new api package. But it will provide a default implementation like above, then you can use a hook from the plugins to use it. And the provider can accept a custom globalThis.fetch

Copy link
Copy Markdown
Contributor Author

@shahrokni shahrokni Apr 29, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Nexucis
It seems inevitably there will be a small package!
This will contain the fetch and the following types.

@jgbernalp We also need to find a relevant name for the new package. @Nexucis believes api might be vague.

import { HTTPProxy } from './http-proxy';
export type RequestHeaders = Record<string, string>;
export interface HTTPDatasourceSpec {
    directUrl?: string;
    proxy?: HTTPProxy;
}

@shahrokni shahrokni changed the title [IGNORE] remove fetch from usage metrics [BREAKINGCHANGE] remove fetch from usage metrics Apr 29, 2026
@shahrokni shahrokni force-pushed the refactor/remove_fetch_from_usage_metrics branch 3 times, most recently from 6a6c92f to e3a0326 Compare April 29, 2026 14:21
Signed-off-by: Seyed Mahmoud SHAHROKNI <seyedmahmoud.shahrokni@amadeus.com>

Signed-off-by: Seyed Mahmoud SHAHROKNI <seyedmahmoud.shahrokni@amadeus.com>

Signed-off-by: Seyed Mahmoud SHAHROKNI <seyedmahmoud.shahrokni@amadeus.com>

Signed-off-by: Seyed Mahmoud SHAHROKNI <seyedmahmoud.shahrokni@amadeus.com>

Signed-off-by: Seyed Mahmoud SHAHROKNI <seyedmahmoud.shahrokni@amadeus.com>

Signed-off-by: Seyed Mahmoud SHAHROKNI <seyedmahmoud.shahrokni@amadeus.com>

Signed-off-by: Seyed Mahmoud SHAHROKNI <seyedmahmoud.shahrokni@amadeus.com>

Signed-off-by: Seyed Mahmoud SHAHROKNI <seyedmahmoud.shahrokni@amadeus.com>

Signed-off-by: Seyed Mahmoud SHAHROKNI <seyedmahmoud.shahrokni@amadeus.com>

Signed-off-by: Seyed Mahmoud SHAHROKNI <seyedmahmoud.shahrokni@amadeus.com>
@shahrokni shahrokni force-pushed the refactor/remove_fetch_from_usage_metrics branch from e3a0326 to d3aedc8 Compare April 29, 2026 14:24
@shahrokni shahrokni requested a review from jgbernalp April 29, 2026 14:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants