Skip to content

Commit

Permalink
Add more telemetry statistic data
Browse files Browse the repository at this point in the history
  • Loading branch information
wuhaixian1984 committed Jun 21, 2024
1 parent cf71f4e commit 5097184
Show file tree
Hide file tree
Showing 6 changed files with 98 additions and 10 deletions.
7 changes: 4 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
{
"name": "crust-smanager",
"version": "1.0.3",
"version": "2.0.0",
"description": "A storage manager integrated with Crust, IPFS and sWorker(storage inspector) of Crust protocol.",
"main": "build/src/main.js",
"repository": "https://github.com/crustio/crust-smanager.git",
"author": "Zikun Fan <kun@crust.network>",
"author": "Brian Wu <brianwu@crust.network>",
"engines": {
"node": ">= 14.16 <15"
},
Expand All @@ -30,7 +30,8 @@
"sqlite3": "^5.0.2",
"tslib": "~2.3.0",
"umzug": "^3.0.0-beta.16",
"winston": "^3.3.3"
"winston": "^3.3.3",
"node-os-utils": "^1.3.7"
},
"devDependencies": {
"@types/bignumber.js": "^5.0.0",
Expand Down
8 changes: 8 additions & 0 deletions src/sworker/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import axios, { AxiosInstance } from 'axios';
import qs from 'querystring';
import {
EnclaveIdInfo,
QuerySealInfoResult,
SealInfoMap,
SealInfoResp,
Expand Down Expand Up @@ -87,6 +88,13 @@ export default class SworkerApi {
return parseObj(res.data);
}

async getEnclaveIdInfo(): Promise<EnclaveIdInfo> {
const res = await this.sworker.get('/enclave/id_info');
if (!res || res.status !== 200) {
throw new Error(`invalid sworker response: ${res}`);
}
return parseObj(res.data);
}
/// READ methods
/**
* Query local free storage size
Expand Down
1 change: 0 additions & 1 deletion src/tasks/node-info-updater-task.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import { AppContext } from '../types/context';
import { SimpleTask } from '../types/tasks';
import { formatError } from '../utils';
import { SLOT_LENGTH } from '../utils/consts';
import { Dayjs } from '../utils/datetime';
import { makeIntervalTask } from './task-utils';

// the storage key for 'swork->workReport'
Expand Down
58 changes: 52 additions & 6 deletions src/tasks/telemetry-task.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,22 @@ import SworkerApi from '../sworker';
import { AppContext } from '../types/context';
import { PinStatus } from '../types/database';
import { NormalizedConfig } from '../types/smanager-config';
import { WorkloadInfo } from '../types/sworker';
import { EnclaveIdInfo, WorkloadInfo } from '../types/sworker';
import { SimpleTask } from '../types/tasks';
import {
PinStats,
QueueInfo,
SManagerInfo,
TelemetryData,
SWorkerStats,
OSInfo,
} from '../types/telemetry';
import { formatError, getTimestamp, toQuotedList } from '../utils';
import { Dayjs } from '../utils/datetime';
import { PendingStatus } from './pull-utils';
import { makeIntervalTask } from './task-utils';
import os from 'os';
import osu from 'node-os-utils';

const ReportSlotDuration = Dayjs.duration({
hours: 24,
Expand Down Expand Up @@ -63,9 +66,11 @@ async function collectStats(
[timeStart],
);
const workload = await getSworkerWorkload(sworkerApi, logger);
const enclaveIdInfo = await getSworkerEnclaveIdInfo(sworkerApi, logger);
let reportWL: SWorkerStats;
if (workload) {
reportWL = {
id_info: enclaveIdInfo,
srd: {
srd_complete: workload.srd.srd_complete,
srd_remaining_task: workload.srd.srd_remaining_task,
Expand All @@ -81,6 +86,8 @@ async function collectStats(
reportWL = null;
}

const osInfo = await collectOSInfo(logger);

return {
chainAccount: account,
smangerInfo,
Expand All @@ -96,6 +103,7 @@ async function collectStats(
deletedCount,
},
hasSealCoordinator: !!context.sealCoordinator,
osInfo
};
}

Expand All @@ -111,6 +119,18 @@ async function getSworkerWorkload(
}
}

async function getSworkerEnclaveIdInfo(
sworkerApi: SworkerApi,
logger: Logger,
): Promise<EnclaveIdInfo | null> {
try {
return await sworkerApi.getEnclaveIdInfo();
} catch (e) {
logger.error('failed to get sworker enclave id_info: %s', formatError(e));
return null;
}
}

async function collectQueueInfo(database): Promise<QueueInfo> {
const { pendingCount } = await database.get(
`select count(*) as pendingCount from file_record
Expand Down Expand Up @@ -164,16 +184,42 @@ async function getPinStats(database, timeStart: number): Promise<PinStats> {
};
}


async function collectOSInfo(logger: Logger): Promise<OSInfo> {

try {
const kernel = os.release();
const uptime = os.uptime();
const cpuModel = osu.cpu.model();
const cpuCount = osu.cpu.count();
const memInfo = await osu.mem.info();

return {
kernel,
uptime,
cpuInfo: {
cpuModel,
cpuCount,
},
memInfo
}
}catch(err){
logger.error(`failed to collect os info: ${err}`);
}

return null;
}

export async function createTelemetryReportTask(
context: AppContext,
loggerParent: Logger,
): Promise<SimpleTask> {
const reportInterval = Dayjs.duration({
hours: 1,
}).asMilliseconds();
// const reportInterval = Dayjs.duration({
// hours: 1,
// }).asMilliseconds();
return makeIntervalTask(
300 * 1000,
reportInterval,
1 * 1000,
2 * 1000,
'telemetry-report',
context,
loggerParent,
Expand Down
9 changes: 9 additions & 0 deletions src/types/sworker.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,15 @@ export interface WorkloadInfo {
};
}

export interface EnclaveIdInfo {
account: string;
attestation_mode: string | null;
mrenclave: string;
pub_key: string;
sworker_version: string;
version: string;
}

export type SealedType = 'valid' | 'lost' | 'pending';

export interface SealInfoData {
Expand Down
25 changes: 25 additions & 0 deletions src/types/telemetry.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,18 @@ export interface TelemetryData {
cleanupStats: CleanupStats;
groupInfo: GroupInfo;
hasSealCoordinator: boolean;
osInfo: OSInfo | null;
}

export interface SWorkerStats {
id_info: {
account: string;
attestation_mode: string | null;
mrenclave: string;
pub_key: string;
sworker_version: string;
version: string;
};
files: {
lost: {
num: number;
Expand Down Expand Up @@ -59,3 +68,19 @@ export interface PinStats {
export interface CleanupStats {
deletedCount: number;
}

export interface OSInfo {
kernel: string;
uptime: number;
cpuInfo: {
cpuModel: string;
cpuCount: number;
};
memInfo: {
totalMemMb: number;
usedMemMb: number;
freeMemMb: number;
usedMemPercentage: number;
freeMemPercentage: number;
};
}

0 comments on commit 5097184

Please sign in to comment.