Skip to content

Commit 51163c3

Browse files
authored
Fix the cloud logging deep link (#136)
1 parent 0fd5c0b commit 51163c3

File tree

1 file changed

+30
-16
lines changed

1 file changed

+30
-16
lines changed

src/QueryEditor.tsx

Lines changed: 30 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -98,43 +98,57 @@ export function LoggingQueryEditor({ datasource, query, range, onChange, onRunQu
9898
/**
9999
* Keep an up-to-date URI that links to the equivalent query in the GCP console
100100
*/
101-
const gcpConsoleURI = useMemo<string | undefined>(() => {
101+
const gcpConsoleURI = useMemo<string | undefined>(() => {
102102
if (!query.queryText) {
103103
return undefined;
104104
}
105105

106106
let storageScope = "";
107107
if (query.projectId) {
108-
storageScope = `;storageScope=storage,projects/${query.projectId}`;
108+
let scopePath = `storage,projects/${query.projectId}`;
109109

110110
if (query.bucketId) {
111-
storageScope += `/locations/${query.bucketId}`;
111+
// Check if bucketId already includes 'locations/' prefix
112+
if (query.bucketId.startsWith('locations/')) {
113+
scopePath += `/${query.bucketId}`;
114+
} else {
115+
scopePath += `/locations/${query.bucketId}`;
116+
}
112117
} else {
113-
storageScope += `/locations/global/buckets/_Default`;
118+
scopePath += `/locations/global/buckets/_Default`;
114119
}
115120
if (query.viewId) {
116-
storageScope += `/views/${query.viewId}`;
121+
scopePath += `/views/${query.viewId}`;
117122
} else {
118-
storageScope += `/views/_AllLogs`;
123+
scopePath += `/views/_AllLogs`;
119124
}
125+
126+
// URL encode the forward slashes in the storage scope
127+
storageScope = `;storageScope=${scopePath.replace(/\//g, '%2F')}`;
120128
}
121129

122-
const encodedText = encodeURIComponent(`${query.queryText}${storageScope}`).replace(/[!'()*]/g, function(c) {
130+
const encodedText = encodeURIComponent(`${query.queryText}`).replace(/[!'()*]/g, function(c) {
123131
if (c === '(' || c === ')') {
124132
return '%25' + c.charCodeAt(0).toString(16);
125133
}
126134
return '%' + c.charCodeAt(0).toString(16);
127135
});
128-
const queryText = `query=${encodedText}`;
129-
// If range is somehow undefined, don't add timeRange to the URI
130-
const timeRange = range !== undefined ?
131-
`timeRange=${range?.from?.toISOString()}%2F${range?.to?.toISOString()}`
132-
: '';
136+
137+
// Build query string parameters
138+
let queryParams = [`project=${query.projectId}`, `query=${encodedText}`];
139+
140+
// Add storageScope without the semicolon prefix
141+
if (storageScope) {
142+
queryParams.push(storageScope.substring(1)); // Remove the leading semicolon
143+
}
144+
145+
// Add time parameters
146+
if (range !== undefined) {
147+
queryParams.push(`startTime=${range?.from?.toISOString()}`);
148+
queryParams.push(`endTime=${range?.to?.toISOString()}`);
149+
}
133150

134-
return `https://console.cloud.google.com/logs/query;` +
135-
queryText +
136-
`;${timeRange}` +
137-
`?project=${query.projectId}`;
151+
return `https://console.cloud.google.com/logs/query?${queryParams.join('&')}`;
138152
}, [query, range]);
139153

140154
return (

0 commit comments

Comments
 (0)