Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[feature] Support monitoring of OpenAI accounts #1947

Merged
merged 5 commits into from
May 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@
import java.net.ConnectException;
import java.net.UnknownHostException;
import java.nio.charset.StandardCharsets;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;
Expand Down Expand Up @@ -91,7 +93,12 @@
*/
@Slf4j
public class HttpCollectImpl extends AbstractCollect {


public static final String OpenAIHost = "api.openai.com";
public static final String OpenAIUsageAPI = "/dashboard/billing/usage";
public static final String startDate = "start_date";
public static final String endDate = "end_date";

private final Set<Integer> defaultSuccessStatusCodes = Stream.of(HttpStatus.SC_OK, HttpStatus.SC_CREATED,
HttpStatus.SC_ACCEPTED, HttpStatus.SC_MULTIPLE_CHOICES, HttpStatus.SC_MOVED_PERMANENTLY,
HttpStatus.SC_MOVED_TEMPORARILY).collect(Collectors.toSet());
Expand Down Expand Up @@ -500,6 +507,14 @@ public HttpUriRequest createHttpRequest(HttpProtocol httpProtocol) {
}
}
}
// OpenAI /dashboard/billing/usage
if (OpenAIHost.equalsIgnoreCase(httpProtocol.getHost()) && OpenAIUsageAPI.equalsIgnoreCase(httpProtocol.getUrl())) {
LocalDate today = LocalDate.now();
LocalDate tomorrow = LocalDate.now().plusDays(1);
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
requestBuilder.addParameter(startDate, today.format(formatter));
requestBuilder.addParameter(endDate, tomorrow.format(formatter));
}
// The default request header can be overridden if customized
// keep-alive
requestBuilder.addHeader(HttpHeaders.CONNECTION, "keep-alive");
Expand Down
82 changes: 82 additions & 0 deletions home/docs/help/openai.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
---
id: openai
title: Monitoring OpenAI Account Status
sidebar_label: OpenAI Account Status
keywords: [open source monitoring system, open source network monitoring, OpenAI account monitoring]
---

### Preparation

#### Obtain Session Key
> 1. Open Chrome browser's network request interface
> `Mac: cmd + option + i`
> `Windows: ctrl + shift + i`
> 2. Visit https://platform.openai.com/usage
> 3. Find the request to https://api.openai.com/dashboard/billing/usage
> 4. Find the Authorization field in the request headers, and copy the content after `Bearer`. For example: `sess-123456`

### Notes

> 1. Please ensure that HertzBeat has external network access capability. If unsure, you can try detecting by creating HTTP API monitoring for openai.com in HertzBeat.
> 2. The monitoring period should be at least greater than 120 seconds to avoid sending requests too frequently, resulting in return status code 429 (Too Many Requests).

### Configuration Parameters

| Parameter Name | Parameter Description |
|:------------------|------------------------------------------------------------------------------------------------|
| Monitoring Host | Fill in api.openai.com here. |
| Task Name | Identify the name of this monitoring, ensuring uniqueness. |
| Session Key | The session key obtained in the preparation step. |
| Collector | Configure which collector to use for scheduling collection for this monitoring. |
| Monitoring Period | Interval time for periodic data collection, in seconds, with a minimum interval of 30 seconds. |
| Bound Tags | Tags for managing classification of monitoring resources. |
| Description | Additional identification and description for this monitoring, users can leave remarks here. |

### Collection Metrics

#### Metric Set: Credit Grants

| Metric Name | Metric Unit | Metric Description |
|----------------------|-------------|--------------------------------------|
| Total Granted | USD ($) | Total granted credit limit |
| Total Used | USD ($) | Total used credit limit |
| Total Available | USD ($) | Total available credit limit |
| Total Paid Available | USD ($) | Total payable available credit limit |

#### Metric Set: Model Cost

| Metric Name | Metric Unit | Metric Description |
|-------------|-------------|------------------------|
| Model Name | None | Name of the model |
| Cost | USD ($) | Expenses for the model |

#### Metric Set: Billing Subscription

| Metric Name | Metric Unit | Metric Description |
|--------------------------|-------------|-----------------------------------------|
| Has Payment Method | None | Whether payment method is available |
| Canceled | None | Whether subscription is cancelled |
| Canceled At | None | Time of subscription cancellation |
| Delinquent | None | Whether subscription is overdue |
| Soft Limit | Times | Maximum usage limit in a certain period |
| Hard Limit | Times | Maximum usage limit |
| System Hard Limit | Times | System hard limit usage |
| Soft Limit USD | USD ($) | Soft limit charge |
| Hard Limit USD | USD ($) | Hard limit charge |
| System Hard Limit USD | USD ($) | System hard limit charge |
| Plan | None | Subscription plan |
| Primary | None | Whether it's a primary subscription |
| Billing Mechanism | None | Settlement mechanism |
| Is Arrears Eligible | None | Whether eligible for overdue |
| Max Balance | USD ($) | Maximum balance |
| Auto Recharge Eligible | None | Whether eligible for auto recharge |
| Auto Recharge Enabled | None | Whether auto recharge is enabled |
| Auto Recharge Threshold | USD ($) | Auto recharge threshold |
| Auto Recharge To Balance | USD ($) | Auto recharge amount |
| Trust Tier | None | Credit level |
| Account Name | None | Account name |
| Po Number | None | Purchase order |
| Billing Email | None | Billing email |
| Tax IDs | None | Tax IDs |
| Billing Address | None | Billing address |
| Business Address | None | Business address |
2 changes: 1 addition & 1 deletion home/docs/help/udp_port.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
---
id: udp_port
title: MonitoringUDP port availability
title: Monitoring UDP port availability
sidebar_label: UDP Port availability
keywords: [open source monitoring tool, open source port monitoring tool, monitoring UDP port metrics]
---
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
---
id: openai
title: 监控:OpenAI 账户情况
sidebar_label: OpenAI 账户情况
keywords: [开源监控系统, 开源网络监控, OpenAI账户监控]
---

### 准备工作

#### 获取会话密钥
> 1. 打开 Chrome 浏览器的网络请求界面
> `Mac: cmd + option + i`
> `Windows: ctrl + shift + i`
> 2. 访问 https://platform.openai.com/usage
> 3. 找到 https://api.openai.com/dashboard/billing/usage 请求
> 4. 找到请求头中 Authorization 字段,并复制 `Bearer ` 之后的内容。例如: `sess-123456`

### 注意事项

> 1. 请确保 HertzBeat 可以具备外网访问能力。若不确定可以通过在 HertzBeat 中创建对 openai.com 的 HTTP API 监控进行探测尝试。
> 2. 监控周期应至少大于 120 秒,避免发送的请求过于频繁,导致返回状态码 429 (太多请求)。

### 配置参数

| 参数名称 | 参数帮助描述 |
|:-------|---------------------------------|
| 监控Host | 此处填写 api.openai.com 。 |
| 任务名称 | 标识此监控的名称,名称需要保证唯一性。 | |
| 会话密钥 | 即准备工作中获取的会话密钥。 | |
| 采集器 | 配置此监控使用哪台采集器调度采集。 |
| 监控周期 | 监控周期性采集数据间隔时间,单位秒,可设置的最小间隔为30秒。 |
| 绑定标签 | 对监控资源的分类管理标签。 |
| 描述备注 | 更多标识和描述此监控的备注信息,用户可以在这里备注信息。 |

### 采集指标

#### 指标集合:信用额度授予

| 指标名称 | 指标单位 | 指标帮助描述 |
|---------|--------|---------|
| 总授予额度 | 美元 ($) | 总授予额度 |
| 总使用额度 | 美元 ($) | 总使用额度 |
| 总可用额度 | 美元 ($) | 总可用额度 |
| 总付费可用额度 | 美元 ($) | 总付费可用额度 |

#### 指标集合:模型花费

| 指标名称 | 指标单位 | 指标帮助描述 |
|---------|--------|---------|
| 模型名称 | 无 | 模型名称 |
| 花费 | 美元 ($) | 花费 |

#### 指标集合:订阅计费

| 指标名称 | 指标单位 | 指标帮助描述 |
|--------------|--------|--------------|
| 是否有支付方式 | 无 | 是否有支付方式 |
| 订阅是否已取消 | 无 | 订阅是否已取消 |
| 订阅取消时间 | 无 | 订阅取消时间 |
| 订阅是否拖欠 | 无 | 订阅是否拖欠 |
| 一定时间内最大使用量限制 | 次 | 一定时间内最大使用量限制 |
| 最大使用量限制 | 次 | 最大使用量限制 |
| 系统硬限制使用量 | 次 | 系统硬限制使用量 |
| 软限制费用 | 美元 ($) | 软限制费用 |
| 硬限制费用 | 美元 ($) | 硬限制费用 |
| 系统硬限制费用 | 美元 ($) | 系统硬限制费用 |
| 订阅计划 | 无 | 订阅计划 |
| 是否是主要订阅 | 无 | 是否是主要订阅 |
| 结算机制 | 无 | 结算机制 |
| 是否符合拖欠资格 | 无 | 是否符合拖欠资格 |
| 最大余额 | 美元 ($) | 最大余额 |
| 是否符合自动充值资格 | 无 | 是否符合自动充值资格 |
| 是否启用自动充值 | 无 | 是否启用自动充值 |
| 自动充值阈值 | 美元 ($) | 自动充值阈值 |
| 自动充值金额 | 美元 ($) | 自动充值金额 |
| 信用等级 | 无 | 信用等级 |
| 账户名称 | 无 | 账户名称 |
| 采购号 | 无 | 采购号 |
| 账单邮箱 | 无 | 账单邮箱 |
| 税务ID | 无 | 税务ID |
| 结算地址 | 无 | 结算地址 |
| 业务地址 | 无 | 业务地址 |
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
---
id: udp_port
title: 监控:UDP 端口可用性监控
sidebar_label: UDP 端口可用性监控
sidebar_label: UDP 端口可用性
keywords: [开源监控系统, 开源网络监控, UDP 端口可用性监控]
---

Expand Down
1 change: 1 addition & 0 deletions home/sidebars.json
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@
"items": [
"help/website",
"help/api",
"help/openai",
"help/ping",
"help/port",
"help/udp_port",
Expand Down
Loading
Loading