Skip to content

Commit a226aaf

Browse files
daisyfaithaumakathaylOxyjun
authored andcommitted
[AIG] Logpush initial documentation (#16798)
* Logpush initial documentation * Update logpush.mdx * Update logpush.mdx * Added limits link * Updated details * Add script to run file * Update logpush.mdx * Update src/content/docs/ai-gateway/observability/logpush.mdx Replace Co-authored-by: Jun Lee <[email protected]> * Update src/content/docs/ai-gateway/observability/logpush.mdx Duplicate Co-authored-by: Jun Lee <[email protected]> * Update src/content/docs/ai-gateway/observability/logpush.mdx Co-authored-by: Jun Lee <[email protected]> * Update src/content/docs/ai-gateway/observability/logpush.mdx Co-authored-by: Jun Lee <[email protected]> --------- Co-authored-by: Kathy <[email protected]> Co-authored-by: Jun Lee <[email protected]>
1 parent 84ca323 commit a226aaf

File tree

1 file changed

+126
-0
lines changed

1 file changed

+126
-0
lines changed
Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
---
2+
pcx_content_type: reference
3+
title: Logpush
4+
sidebar:
5+
badge:
6+
text: Beta
7+
---
8+
9+
import { Render } from "~/components";
10+
11+
AI Gateway allows you to securely export logs to an external storage location, where you can decrypt and process them.
12+
You can toggle Logpush on and off in the [Cloudflare dashboard](https://dash.cloudflare.com) settings.
13+
14+
This guide explains how to set up Logpush for AI Gateway, generate an RSA key pair for encryption, and decrypt the logs once they are received.
15+
16+
You can store up to 10 million logs per gateway. If your limit is reached, new logs will stop being saved and will not be exported through Logpush. To continue saving and exporting logs, you must delete older logs to free up space for new logs. Logpush has a limit of 4 jobs.
17+
18+
:::note[Note]
19+
20+
To export logs using Logpush, you must have logs turned on for the gateway.
21+
22+
:::
23+
24+
## Setting up Logpush
25+
26+
To configure Logpush for AI Gateway, follow these steps:
27+
28+
## 1. Generate an RSA key pair locally
29+
30+
You need to generate a key pair to encrypt and decrypt the logs. This script will output your RSA privateKey and publicKey. Keep the private key secure, as it will be used to decrypt the logs. Below is a sample script to generate the keys using Node.js.
31+
32+
```js title="JavaScript"
33+
const crypto = require("crypto");
34+
35+
const { privateKey, publicKey } = crypto.generateKeyPairSync("rsa", {
36+
modulusLength: 4096,
37+
publicKeyEncoding: {
38+
type: "spki",
39+
format: "pem",
40+
},
41+
privateKeyEncoding: {
42+
type: "pkcs8",
43+
format: "pem",
44+
},
45+
});
46+
47+
console.log(publicKey);
48+
console.log(privateKey);
49+
```
50+
51+
Run the script by executing the below code on your terminal. Replace `file name` with the name of your JavaScript file.
52+
53+
```bash
54+
node {file name}
55+
```
56+
57+
58+
## 2. Upload public key to gateway settings
59+
60+
Once you have generated the key pair, upload the public key to your AI Gateway settings. This key will be used to encrypt your logs. In order to enable Logpush, you will need logs enabled for that gateway.
61+
62+
## 3. Set up Logpush
63+
64+
To set up Logpush, refer to [Logpush Get Started](/logs/get-started/).
65+
66+
## 4. Receive encrypted logs
67+
68+
After configuring Logpush, logs will be sent encrypted using the public key you uploaded. To access the data, you will need to decrypt it using your private key. The logs will be sent to the object storage provider that you have selected.
69+
70+
## 5. Decrypt logs
71+
72+
To decrypt the encrypted log bodies and metadata from AI Gateway, you can use the following Node.js script:
73+
74+
```js title="JavaScript"
75+
const privateKey = `-----BEGIN RSA PRIVATE KEY-----
76+
....
77+
-----END RSA PRIVATE KEY-----`;
78+
79+
const crypto = require("crypto");
80+
const key = crypto.createPrivateKey(privateKey);
81+
82+
const fs = require("fs");
83+
const zlib = require("zlib");
84+
const readline = require("readline");
85+
86+
function decryptBase64(key, string) {
87+
const decryptedData = crypto.privateDecrypt(
88+
{
89+
key: key,
90+
oaepHash: "SHA256",
91+
},
92+
Buffer.from(string, "base64"),
93+
);
94+
95+
return decryptedData.toString();
96+
}
97+
98+
let lineReader = readline.createInterface({
99+
input: fs.createReadStream("my_log.log.gz").pipe(zlib.createGunzip()),
100+
});
101+
102+
lineReader.on("line", (line) => {
103+
line = JSON.parse(line);
104+
105+
const { Metadata, RequestBody, ResponseBody, ...remaining } = line;
106+
107+
console.log({
108+
...remaining,
109+
Metadata: decryptBase64(key, Metadata.data),
110+
RequestBody: decryptBase64(key, RequestBody.data),
111+
ResponseBody: decryptBase64(key, ResponseBody.data),
112+
});
113+
console.log("--");
114+
});
115+
```
116+
117+
Run the script by executing the below code on your terminal. Replace `file name` with the name of your JavaScript file.
118+
119+
```bash
120+
node {file name}
121+
```
122+
123+
## Script Explanation
124+
125+
The script reads the encrypted log file `(my_log.log.gz)`, decrypts the metadata, request body, and response body, and prints the decrypted data.
126+
Ensure you replace the `privateKey` variable with your actual private RSA key that you generated in step 1.

0 commit comments

Comments
 (0)