@@ -298,58 +298,68 @@ export async function fetchMetrics(data: {
298298 subPath ?: string ;
299299} ) : Promise < object > {
300300 const params = new URLSearchParams ( ) ;
301- if ( data . from ) params . append ( 'start' , data . from . toString ( ) ) ;
302- if ( data . to ) params . append ( 'end' , data . to . toString ( ) ) ;
303- if ( data . step ) params . append ( 'step' , data . step . toString ( ) ) ;
304- if ( data . query ) params . append ( 'query' , data . query ) ;
301+ if ( data . from ) {
302+ params . append ( 'start' , data . from . toString ( ) ) ;
303+ }
304+ if ( data . to ) {
305+ params . append ( 'end' , data . to . toString ( ) ) ;
306+ }
307+ if ( data . step ) {
308+ params . append ( 'step' , data . step . toString ( ) ) ;
309+ }
310+ if ( data . query ) {
311+ params . append ( 'query' , data . query ) ;
312+ }
305313
306314 const isExternal = isHttpUrl ( data . prefix ) ;
307- let url : string ;
315+ var url : string ;
308316
309- // 1. Construct the URL based on connection type
310317 if ( isExternal ) {
311- // --- EXTERNAL URL LOGIC ---
312- const base = data . prefix . replace ( / \/ $ / , '' ) ;
318+ let base = data . prefix ;
319+ if ( base . endsWith ( '/' ) ) {
320+ base = base . slice ( 0 , - 1 ) ;
321+ }
322+
313323 let apiPath = 'api/v1/query_range' ;
314324 if ( data . subPath && data . subPath !== '' ) {
315- const sub = data . subPath . replace ( / ^ \/ | \/ $ / g, '' ) ;
316- apiPath = `${ sub } /api/v1/query_range` ;
325+ if ( data . subPath . startsWith ( '/' ) ) {
326+ data . subPath = data . subPath . slice ( 1 ) ;
327+ }
328+ if ( data . subPath . endsWith ( '/' ) ) {
329+ data . subPath = data . subPath . slice ( 0 , - 1 ) ;
330+ }
331+ apiPath = `${ data . subPath } /api/v1/query_range` ;
317332 }
333+
318334 url = `${ base } /${ apiPath } ?${ params . toString ( ) } ` ;
319335 } else {
320- // --- KUBERNETES PROXY LOGIC (FIXED) ---
321- const sub = data . subPath ? data . subPath . replace ( / ^ \/ | \/ $ / g, '' ) : '' ;
322- const apiPath = sub ? `${ sub } /api/v1/query_range` : 'api/v1/query_range' ;
323-
324- // data.prefix is expected as "namespace/services/service-name:port"
325- url = `/api/v1/namespaces/${ data . prefix } /proxy/${ apiPath } ?${ params . toString ( ) } ` ;
326- }
327-
328- try {
329- if ( isExternal ) {
330- // Direct fetch for external/local URL
331- const response = await fetch ( url , { method : 'GET' } ) ;
332-
333- if ( ! response . ok ) {
334- if ( response . status === 404 ) {
335- throw new Error ( `404 Not Found at: ${ url } . Check your Endpoint and Subpath.` ) ;
336- }
337- throw new Error ( `Prometheus Error (${ response . status } ): ${ response . statusText } ` ) ;
336+ url = `/api/v1/namespaces/${ data . prefix } /proxy/api/v1/query_range?${ params . toString ( ) } ` ;
337+ if ( data . subPath && data . subPath !== '' ) {
338+ if ( data . subPath . startsWith ( '/' ) ) {
339+ data . subPath = data . subPath . slice ( 1 ) ;
338340 }
339- return await response . json ( ) ;
340- } else {
341- // Headlamp Proxy for K8s services
342- const response = await request ( url , { method : 'GET' , isJSON : false } ) ;
343-
344- if ( response . status !== 200 ) {
345- throw new Error ( `K8s Proxy Error ${ response . status } : Path was ${ url } ` ) ;
341+ if ( data . subPath . endsWith ( '/' ) ) {
342+ data . subPath = data . subPath . slice ( 0 , - 1 ) ;
346343 }
347- return await response . json ( ) ;
348- }
349- } catch ( err : any ) {
350- if ( err . message . includes ( 'Failed to fetch' ) ) {
351- throw new Error ( `Connection refused to ${ url } . Did you enable --web.cors.origin=".*"?` ) ;
344+ url = `/api/v1/namespaces/${ data . prefix } /proxy/${
345+ data . subPath
346+ } /api/v1/query_range?${ params . toString ( ) } `;
352347 }
353- throw err ;
354348 }
355- }
349+
350+ if ( isExternal ) {
351+ const response = await fetch ( url , { method : 'GET' } ) ;
352+ return response . json ( ) ;
353+ }
354+
355+ const response = await request ( url , {
356+ method : 'GET' ,
357+ isJSON : false ,
358+ } ) ;
359+ if ( response . status === 200 ) {
360+ return response . json ( ) ;
361+ } else {
362+ const error = new Error ( response . statusText ) ;
363+ return Promise . reject ( error ) ;
364+ }
365+ }
0 commit comments