Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
355 changes: 221 additions & 134 deletions spring-boot-admin-server-ui/package-lock.json

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions spring-boot-admin-server-ui/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@
"@vue/eslint-config-typescript": "^14.0.0",
"@vue/test-utils": "2.4.6",
"autoprefixer": "10.4.27",
"axios-mock-adapter": "^2.1.0",
"babel-loader": "10.1.1",
"eslint": "^10.0.0",
"eslint-config-prettier": "^10.0.0",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { AxiosError } from 'axios';
import { describe, expect, test, vi } from 'vitest';

import Instance from '@/services/instance';
Expand Down Expand Up @@ -30,6 +31,8 @@ describe('Instance', () => {

const instance = new Instance({
id: 'id',
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-expect-error
registration: {
metadata: {
['hide-url']: metadataHideUrl,
Expand All @@ -40,4 +43,142 @@ describe('Instance', () => {
expect(instance.showUrl()).toEqual(expectUrlToBeShownOnUI);
},
);

describe('fetchMetric', () => {
const instance = new Instance({
id: 'test-id',
registration: {
name: 'test',
healthUrl: '',
source: '',
},
availableMetrics: ['test.metric', 'cache.size', 'cache.gets'],
});
test('should pass suppressToast option to axios config', async () => {
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-expect-error
// Spy on axios.get
const axiosGetSpy = vi.spyOn(instance.axios, 'get');

// Mock the axios request
axiosGetSpy.mockResolvedValue({
data: {
measurements: [{ value: 42 }],
},
});

await instance.fetchMetric(
'test.metric',
{ tag: 'value' },
{
suppressToast: true,
},
);

// Verify suppressToast was passed in config
expect(axiosGetSpy).toHaveBeenCalledWith(
expect.stringContaining('actuator/metrics/test.metric'),
expect.objectContaining({
suppressToast: true,
}),
);
});

test('should work without options parameter for backward compatibility', async () => {
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-expect-error
const axiosGetSpy = vi.spyOn(instance.axios, 'get');

axiosGetSpy.mockResolvedValue({
data: {
measurements: [{ value: 42 }],
},
});

await instance.fetchMetric('test.metric', { tag: 'value' });

// Verify it was called without suppressToast
expect(axiosGetSpy).toHaveBeenCalledWith(
expect.stringContaining('actuator/metrics/test.metric'),
expect.objectContaining({
suppressToast: undefined,
}),
);
});

test('should pass suppressToast=false when explicitly set to false', async () => {
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-expect-error
const axiosGetSpy = vi.spyOn(instance.axios, 'get');

axiosGetSpy.mockResolvedValue({
data: {
measurements: [{ value: 42 }],
},
});

await instance.fetchMetric(
'test.metric',
{ tag: 'value' },
{
suppressToast: false,
},
);

expect(axiosGetSpy).toHaveBeenCalledWith(
expect.stringContaining('actuator/metrics/test.metric'),
expect.objectContaining({
suppressToast: false,
}),
);
});

test('should include tags in request parameters', async () => {
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-expect-error
const axiosGetSpy = vi.spyOn(instance.axios, 'get');

axiosGetSpy.mockResolvedValue({
data: {
measurements: [{ value: 42 }],
},
});

await instance.fetchMetric('cache.gets', {
name: 'my-cache',
result: 'hit',
});

const callArgs = axiosGetSpy.mock.calls[0];
const params = callArgs[1]?.params as URLSearchParams;

expect(params).toBeInstanceOf(URLSearchParams);
expect(params.getAll('tag')).toContain('name:my-cache');
expect(params.getAll('tag')).toContain('result:hit');
});

test('should pass suppressToast function to axios config', async () => {
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-expect-error
const axiosGetSpy = vi.spyOn(instance.axios, 'get');

axiosGetSpy.mockResolvedValue({
data: {
measurements: [{ value: 42 }],
},
});

const suppressFn = (err: AxiosError) => err.response?.status === 404;
await instance.fetchMetric(
'cache.size',
{},
{ suppressToast: suppressFn },
);

expect(axiosGetSpy).toHaveBeenCalledWith(
expect.any(String),
expect.objectContaining({ suppressToast: suppressFn }),
);
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { AxiosInstance } from 'axios';
import { AxiosError, AxiosInstance } from 'axios';
import saveAs from 'file-saver';
import { Observable, concat, from, ignoreElements } from 'rxjs';

Expand All @@ -29,6 +29,17 @@ import { useSbaConfig } from '@/sba-config';
import { actuatorMimeTypes } from '@/services/spring-mime-types';
import { transformToJSON } from '@/utils/transformToJSON';

// Extend AxiosRequestConfig to allow suppressToast
declare module 'axios' {
interface AxiosRequestConfig {
suppressToast?: boolean | ((error: AxiosError) => boolean);
}
}

export type FetchMetricOptions = {
suppressToast?: boolean | ((error: AxiosError) => boolean);
};

const isInstanceActuatorRequest = (url: string) =>
url.match(/^instances[/][^/]+[/]actuator([/].*)?$/);

Expand Down Expand Up @@ -168,7 +179,11 @@ class Instance {
return response;
}

async fetchMetric(metric: string, tags: Record<string, any>) {
async fetchMetric(
metric: string,
tags: Record<string, any>,
options?: FetchMetricOptions,
) {
if (this.availableMetrics.length === 0) {
try {
await this.fetchMetrics();
Expand Down Expand Up @@ -204,6 +219,7 @@ class Instance {
}
return this.axios.get(uri`actuator/metrics/${metric}`, {
params,
suppressToast: options?.suppressToast,
});
}

Expand Down
40 changes: 36 additions & 4 deletions spring-boot-admin-server-ui/src/main/frontend/tests/setup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,36 @@ import { afterAll, afterEach, beforeAll, vi } from 'vitest';
import { server } from '@/mocks/server';
import sbaConfig from '@/sba-config';

// Setup localStorage mock
const localStorageMock = (() => {
let store: Record<string, string> = {};

return {
get length(): number {
return Object.keys(store).length;
},
getItem: (key: string) => store[key] || null,
setItem: (key: string, value: string) => {
store[key] = value.toString();
},
removeItem: (key: string) => {
delete store[key];
},
clear: () => {
store = {};
},
};
})();

Object.defineProperty(window, 'localStorage', {
value: localStorageMock,
});

// Setup globalThis.errorSpy for toast notifications
if (!globalThis.errorSpy) {
globalThis.errorSpy = vi.fn();
}

global.IntersectionObserver = vi.fn().mockImplementation(function () {
return {
observe: vi.fn(),
Expand All @@ -24,10 +54,11 @@ global.matchMedia = vi.fn().mockReturnValue({
addEventListener: vi.fn(),
removeEventListener: vi.fn(),
});
global.EventSource = class {
constructor() {}
close() {}
};
global.EventSource = vi.fn().mockImplementation(function () {
return {
close: vi.fn(),
};
}) as unknown as typeof EventSource;

global.SBA = sbaConfig;

Expand All @@ -38,5 +69,6 @@ afterEach(() => server.resetHandlers());
// runs a cleanup after each test case (e.g. clearing jsdom)
afterEach(() => {
vi.clearAllMocks();
localStorage.clear();
cleanup();
});
Loading