Skip to content

Commit

Permalink
Send start and completion time to external logs proxy
Browse files Browse the repository at this point in the history
Include the TaskRuns start time and completion time as properties sent to the external logs proxy to allow for more fine-tuned queries to be run against a long-term log store.

Signed-off-by: Marcus Noble <github@marcusnoble.co.uk>
  • Loading branch information
AverageMarcus committed May 9, 2023
1 parent eb63f1b commit 4e0cb63
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 4 deletions.
14 changes: 12 additions & 2 deletions src/api/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -185,9 +185,19 @@ export function getExternalLogURL({
container,
externalLogsURL,
namespace,
podName
podName,
startTime,
completionTime
}) {
return `${externalLogsURL}/${namespace}/${podName}/${container}`;
let queryStringParams = []
if (startTime) queryStringParams.push(`startTime=${startTime}`)
if (completionTime) queryStringParams.push(`completionTime=${completionTime}`)

let queryString = ""
if (queryStringParams.length) {
queryString = `?${queryStringParams.join('&')}`
}
return `${externalLogsURL}/${namespace}/${podName}/${container}${queryString}`;
}

export function getPodLogURL({ container, name, namespace, follow }) {
Expand Down
23 changes: 23 additions & 0 deletions src/api/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,29 @@ it('useEvents', async () => {
});

it('getExternalLogURL', () => {
const container = 'fake_container';
const externalLogsURL = 'fake_externalLogsURL';
const namespace = 'fake_namespace';
const podName = 'fake_podName';
const startTime = '2000-01-02T03:04:05Z'
const completionTime = '2006-07-08T09:10:11Z'
expect(
API.getExternalLogURL({ container, externalLogsURL, namespace, podName, startTime, completionTime })
).toEqual(`${externalLogsURL}/${namespace}/${podName}/${container}?startTime=${startTime}&completionTime=${completionTime}`);
});

it('getExternalLogURL with empty completionTime', () => {
const container = 'fake_container';
const externalLogsURL = 'fake_externalLogsURL';
const namespace = 'fake_namespace';
const podName = 'fake_podName';
const startTime = '2000-01-02T03:04:05Z'
expect(
API.getExternalLogURL({ container, externalLogsURL, namespace, podName, startTime })
).toEqual(`${externalLogsURL}/${namespace}/${podName}/${container}?startTime=${startTime}`);
});

it('getExternalLogURL with empty startTime and completionTime', () => {
const container = 'fake_container';
const externalLogsURL = 'fake_externalLogsURL';
const namespace = 'fake_namespace';
Expand Down
11 changes: 9 additions & 2 deletions src/utils/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,10 +98,17 @@ export function fetchLogsFallback(externalLogsURL) {

return (stepName, stepStatus, taskRun) => {
const { namespace } = taskRun.metadata;
const { podName } = taskRun.status || {};
const { podName, startTime, completionTime } = taskRun.status || {};
const { container } = stepStatus;
return get(
getExternalLogURL({ container, externalLogsURL, namespace, podName }),
getExternalLogURL({
container,
externalLogsURL,
namespace,
podName,
startTime,
completionTime
}),
{
Accept: 'text/plain'
}
Expand Down
20 changes: 20 additions & 0 deletions src/utils/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,26 @@ describe('fetchLogsFallback', () => {
});

it('should return a function to retrieve logs from the external provider', () => {
const container = 'fake_container';
const externalLogsURL = 'fake_url';
const namespace = 'fake_namespace';
const podName = 'fake_podName';
const stepName = 'fake_stepName';
const startTime = '2000-01-02T03:04:05Z'
const completionTime = '2006-07-08T09:10:11Z'
const stepStatus = { container };
const taskRun = { metadata: { namespace }, status: { podName, startTime, completionTime } };
jest.spyOn(comms, 'get').mockImplementation(() => {});

const fallback = fetchLogsFallback(externalLogsURL);
fallback(stepName, stepStatus, taskRun);
expect(comms.get).toHaveBeenCalledWith(
`${externalLogsURL}/${namespace}/${podName}/${container}?startTime=${startTime}&completionTime=${completionTime}`,
{ Accept: 'text/plain' }
);
});

it('should handle a missing startTime and completionTime', () => {
const container = 'fake_container';
const externalLogsURL = 'fake_url';
const namespace = 'fake_namespace';
Expand Down

0 comments on commit 4e0cb63

Please sign in to comment.