Skip to content

Commit

Permalink
feat: add support for fetching the canister logs
Browse files Browse the repository at this point in the history
Signed-off-by: David Dal Busco <david.dalbusco@dfinity.org>
  • Loading branch information
peterpeterparker committed Nov 20, 2024
1 parent aed8e3b commit c4fff99
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 2 deletions.
53 changes: 52 additions & 1 deletion packages/ic-management/src/ic-management.canister.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,10 @@ import {
type StoredChunksParams,
type UploadChunkParams,
} from "./types/ic-management.params";
import { CanisterStatusResponse } from "./types/ic-management.responses";
import {
CanisterStatusResponse,
type FetchCanisterLogsResponse,
} from "./types/ic-management.responses";

describe("ICManagementCanister", () => {
const mockAgent: HttpAgent = mock<HttpAgent>();
Expand Down Expand Up @@ -667,4 +670,52 @@ describe("ICManagementCanister", () => {
expect(call).rejects.toThrowError(Error);
});
});

describe("fetchCanisterLogs", () => {
it("returns canister logs when success", async () => {
const settings = {
freezing_threshold: BigInt(2),
controllers: [mockPrincipal],
memory_allocation: BigInt(4),
compute_allocation: BigInt(10),
reserved_cycles_limit: BigInt(11),
log_visibility: { controllers: null },
wasm_memory_limit: BigInt(500_00),
};
const response: FetchCanisterLogsResponse = {
canister_log_records: [
{
idx: 123n,
content: [1, 2, 3],
timestamp_nanos: 12345n,
},
{
idx: 456n,
content: [9, 8, 7],
timestamp_nanos: 12346n,
},
],
};
const service = mock<IcManagementService>();
service.fetch_canister_logs.mockResolvedValue(response);

const icManagement = await createICManagement(service);

const res = await icManagement.fetchCanisterLogs(mockCanisterId);

expect(res).toEqual(response);
});

it("throws Error", async () => {
const error = new Error("Test");
const service = mock<IcManagementService>();
service.fetch_canister_logs.mockRejectedValue(error);

const icManagement = await createICManagement(service);

const call = () => icManagement.fetchCanisterLogs(mockCanisterId);

expect(call).rejects.toThrowError(Error);
});
});
});
18 changes: 17 additions & 1 deletion packages/ic-management/src/ic-management.canister.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,10 @@ import {
type UpdateSettingsParams,
type UploadChunkParams,
} from "./types/ic-management.params";
import type { CanisterStatusResponse } from "./types/ic-management.responses";
import type {
CanisterStatusResponse,
FetchCanisterLogsResponse,
} from "./types/ic-management.responses";

export class ICManagementCanister {
private constructor(private readonly service: IcManagementService) {
Expand Down Expand Up @@ -308,4 +311,17 @@ export class ICManagementCanister {

return canister_id;
};

/**
* Given a canister ID as input, this method returns a vector of logs of that canister including its trap messages. The canister logs are not collected in canister methods running in non-replicated mode (NRQ, CQ, CRy, CRt, CC, and F modes, as defined in Overview of imports). The total size of all returned logs does not exceed 4KiB. If new logs are added resulting in exceeding the maximum total log size of 4KiB, the oldest logs will be removed. Logs persist across canister upgrades and they are deleted if the canister is reinstalled or uninstalled.
*
* @param {Principal} canisterId
* @returns {Promise<FetchCanisterLogsResponse>}
*/
fetchCanisterLogs = (
canisterId: Principal,
): Promise<FetchCanisterLogsResponse> =>
this.service.fetch_canister_logs({
canister_id: canisterId,
});
}
5 changes: 5 additions & 0 deletions packages/ic-management/src/types/ic-management.responses.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,8 @@ export type CanisterStatusResponse = ServiceResponse<
IcManagementService,
"canister_status"
>;

export type FetchCanisterLogsResponse = ServiceResponse<
IcManagementService,
"fetch_canister_logs"
>;

0 comments on commit c4fff99

Please sign in to comment.