|
| 1 | +import type { ExecutionContext, ResolvedCredential } from "../src/core/types.ts"; |
| 2 | + |
| 3 | +import { executors } from "../src/providers/aliyun_sls/executors.ts"; |
| 4 | +import { parseAliyunSlsCredential, resolveAliyunSlsLogstoreTarget } from "../src/providers/aliyun_sls/resources.ts"; |
| 5 | + |
| 6 | +const requiredEnvironment = [ |
| 7 | + "ALIYUN_SLS_ACCESS_KEY_ID", |
| 8 | + "ALIYUN_SLS_ACCESS_KEY_SECRET", |
| 9 | + "ALIYUN_SLS_ENDPOINT", |
| 10 | +] as const; |
| 11 | + |
| 12 | +async function main(): Promise<void> { |
| 13 | + const missing = requiredEnvironment.filter((name) => !process.env[name]?.trim()); |
| 14 | + if (missing.length > 0) { |
| 15 | + console.log(`Skip Alibaba Cloud SLS example: missing ${missing.join(", ")}.`); |
| 16 | + return; |
| 17 | + } |
| 18 | + |
| 19 | + const values: Record<string, string> = { |
| 20 | + accessKeyId: process.env.ALIYUN_SLS_ACCESS_KEY_ID!, |
| 21 | + accessKeySecret: process.env.ALIYUN_SLS_ACCESS_KEY_SECRET!, |
| 22 | + endpoint: process.env.ALIYUN_SLS_ENDPOINT!, |
| 23 | + }; |
| 24 | + if (process.env.ALIYUN_SLS_SECURITY_TOKEN?.trim()) { |
| 25 | + values.securityToken = process.env.ALIYUN_SLS_SECURITY_TOKEN; |
| 26 | + } |
| 27 | + if (process.env.ALIYUN_SLS_RESOURCE_SCOPE?.trim()) { |
| 28 | + values.resourceScope = process.env.ALIYUN_SLS_RESOURCE_SCOPE; |
| 29 | + } |
| 30 | + |
| 31 | + const credential = parseAliyunSlsCredential(values); |
| 32 | + let target; |
| 33 | + try { |
| 34 | + target = resolveAliyunSlsLogstoreTarget( |
| 35 | + credential, |
| 36 | + undefined, |
| 37 | + process.env.ALIYUN_SLS_PROJECT, |
| 38 | + process.env.ALIYUN_SLS_LOGSTORE, |
| 39 | + ); |
| 40 | + } catch { |
| 41 | + console.log( |
| 42 | + "Skip Alibaba Cloud SLS example: set ALIYUN_SLS_PROJECT and ALIYUN_SLS_LOGSTORE, or configure resourceScope with exactly one candidate Project and Logstore.", |
| 43 | + ); |
| 44 | + return; |
| 45 | + } |
| 46 | + |
| 47 | + const resolvedCredential: ResolvedCredential = { |
| 48 | + authType: "custom_credential", |
| 49 | + values, |
| 50 | + profile: { |
| 51 | + accountId: credential.accessKeyId, |
| 52 | + displayName: `${credential.accessKeyId}@${credential.endpoint}`, |
| 53 | + grantedScopes: [], |
| 54 | + }, |
| 55 | + metadata: {}, |
| 56 | + }; |
| 57 | + const context: ExecutionContext = { |
| 58 | + async getCredential(service) { |
| 59 | + return service === "aliyun_sls" ? resolvedCredential : undefined; |
| 60 | + }, |
| 61 | + }; |
| 62 | + |
| 63 | + const to = Math.floor(Date.now() / 1000); |
| 64 | + const result = await executors["aliyun_sls.query_logs"]!( |
| 65 | + { |
| 66 | + endpoint: target.endpoint, |
| 67 | + project: target.project, |
| 68 | + logstore: target.logstore, |
| 69 | + from: to - 900, |
| 70 | + to, |
| 71 | + query: process.env.ALIYUN_SLS_QUERY ?? "*", |
| 72 | + line: 100, |
| 73 | + reverse: true, |
| 74 | + }, |
| 75 | + context, |
| 76 | + ); |
| 77 | + console.log(JSON.stringify(result, null, 2)); |
| 78 | + if (!result.ok) process.exitCode = 1; |
| 79 | +} |
| 80 | + |
| 81 | +await main(); |
0 commit comments