UNPKG

t-comm

Version:

专业、稳定、纯粹的工具库

201 lines (200 loc) 5.89 kB
/** 命名空间,伽利略协议枚举 */ export type GalileoNamespace = 'Production' | 'Development'; /** * 排序类型,对应 proto LogSortType * @ignore */ export declare enum GalileoLogSortType { DEFAULT = 0, ASC = 1, DESC = 2 } /** * message 搜索的关键词组合逻辑,对应 proto MessageSearch.FilterType * @ignore */ export declare enum GalileoMessageFilterType { EMPTY = 0, OR = 1, AND = 2 } /** * message 搜索模式,对应 proto MessageSearch.SearchType * @ignore */ export declare enum GalileoMessageSearchType { DEFAULT = 0, CASEINS = 1, TOKEN = 2, SUBSTR = 3, REGULAR = 4 } /** * 单个 tag 过滤字段,对应 proto LogTagsFields * @ignore */ export interface GalileoLogTagsFields { /** tag 名称,如 "tags.qimei36" */ name: string; /** tag 值列表;长度 1 时为 name=value,长度 >1 时为 name in [value] */ values: string[]; } /** tag 条件搜索,对应 proto TagSearch */ export interface GalileoTagSearch { trace_id?: string; level?: string[]; other_tags?: GalileoLogTagsFields[]; } /** message 关键字搜索,对应 proto MessageSearch */ export interface GalileoMessageSearch { keyword: string[]; filter?: GalileoMessageFilterType; search?: GalileoMessageSearchType; } /** * 蓝鲸 APIGW 鉴权配置 * @ignore */ export interface GalileoAuth { /** 蓝鲸 APP code */ bk_app_code: string; /** 蓝鲸 APP secret */ bk_app_secret: string; /** 可选,用户态 access_token */ access_token?: string; } /** * 查询日志请求参数,对应 proto QueryLogReq * @ignore */ export interface QueryGalileoLogReq { /** 观测对象,必填,如 "PCG-123.galileo.apiserver" */ target: string; /** 命名空间,必填 */ namespace: GalileoNamespace; /** 查询开始时间(毫秒) */ start: number; /** 查询结束时间(毫秒) */ end: number; /** 单次最大查询 100 条,取值 (0, 100] */ limit: number; /** tag 条件搜索 */ tag_where?: GalileoTagSearch; /** * message 关键字搜索,多关键字之间关系为"且"。 * @deprecated 建议使用 include / exclude */ message_keyword?: string[]; /** 游标翻页查询,下一页传入上一次响应的 cursor */ cursor?: string; /** message 包含搜索 */ include?: GalileoMessageSearch; /** message 排除搜索 */ exclude?: GalileoMessageSearch; /** * 伽利略查询语句。有查询语句时优先使用,没有再使用 tag_where、message_keyword、include、exclude。 * 例如 "message:target and level:error" */ query?: string; /** 排序 */ sort_type?: GalileoLogSortType; } /** * 单条日志记录,对应 proto LogRecord * @ignore */ export interface GalileoLogRecord { /** 时间戳(纳秒,字符串形式) */ timestamp: string; trace_id: string; span_id: string; message: string; level: string; tags: Record<string, string>; } /** 查询日志响应,对应 proto QueryLogRsp */ export interface QueryGalileoLogRsp { /** 0 为成功,其他为错误 */ code: number; msg: string; /** 当前实际查询所得条数 */ total: number; logs: GalileoLogRecord[]; /** 游标翻页查询 */ cursor: string; /** 是否有下一页 */ has_next_page: boolean; } /** 查询日志接口的额外配置 */ export interface QueryGalileoLogOptions { /** 蓝鲸 APIGW 鉴权 */ auth: GalileoAuth; /** 自定义请求地址,默认 https://galileo-api.apigw.o.woa.com/prod/log/query */ url?: string; /** 请求超时(毫秒),默认 15000 */ timeout?: number; } /** * 查询伽利略日志(调用蓝鲸网关 /prod/log/query) * * 参考文档:https://iwiki.woa.com/p/4007673553 * * 调用前需要在「伽利略 API 权限申请表」中申请 bk_app_code/bk_app_secret,以及对应 target 的查询权限。 * * @example * ```ts * import { queryGalileoLog, GalileoLogSortType } from 't-comm/lib/galileo-api'; * * const now = Date.now(); * const res = await queryGalileoLog( * { * target: 'PCG-123.galileo.apiserver', * namespace: 'Production', * start: now - 30 * 60 * 1000, * end: now, * limit: 100, * query: 'message:target and level:error', * sort_type: GalileoLogSortType.ASC, * }, * { * auth: { * bk_app_code: 'tip-tool', * bk_app_secret: 'xxx', * }, * }, * ); * console.log(res.logs, res.has_next_page, res.cursor); * ``` */ export declare function queryGalileoLog(req: QueryGalileoLogReq, options: QueryGalileoLogOptions): Promise<QueryGalileoLogRsp>; /** * 通过游标不断翻页,直到满足 limit 条数或 has_next_page=false。 * * 伽利略后端分片查询时,稀疏数据可能单次只返回少量条目,需要借助 cursor 继续翻页。 * 该方法会自动循环翻页直到累计条数 >= req.limit 或没有下一页。 * * @param req 查询参数,limit 为期望累计的总条数 * @param options 鉴权及请求配置 * @param extra 可选项:最大翻页次数、每页大小 * * @example * ```ts * const res = await queryGalileoLogAll( * { * target: 'PCG-123.galileo.apiserver', * namespace: 'Production', * start: Date.now() - 3600 * 1000, * end: Date.now(), * limit: 500, * query: 'level:error', * }, * { auth: { bk_app_code: 'xxx', bk_app_secret: 'xxx' } }, * ); * ``` */ export declare function queryGalileoLogAll(req: QueryGalileoLogReq, options: QueryGalileoLogOptions, extra?: { /** 最大翻页次数,默认 20,防止死循环 */ maxPage?: number; /** 每次翻页传给后端的 limit,默认 100(伽利略上限) */ pageSize?: number; }): Promise<QueryGalileoLogRsp>;