proxy-go/web/static/js/metrics.js
wood chen e89ef02205 特性(代理):通过端口和身份验证更新来增强服务器配置和安全性。
- 更新 docker-compose.yml 文件,使用端口 3336
- 修改 Dockerfile 以暴露端口 3336
- 重构 main.go 以支持新的路由和管理端点
- 在 auth.go 中实现健壮的身份验证中间件
- 通过加强错误检查来改进指标处理。
- 添加用于安全类型转换的实用函数
- 引入请求ID生成功能,以便更好地进行追踪。
2025-02-15 08:07:28 +08:00

105 lines
3.0 KiB
JavaScript

async function loadMetrics() {
try {
const token = localStorage.getItem('token');
if (!token) {
window.location.href = '/metrics/ui';
return;
}
const response = await fetch('/metrics', {
headers: {
'Authorization': `Bearer ${token}`
}
});
if (!response.ok) {
if (response.status === 401) {
window.location.href = '/metrics/ui';
return;
}
throw new Error('加载监控数据失败');
}
const metrics = await response.json();
displayMetrics(metrics);
} catch (error) {
showMessage(error.message, true);
}
}
function displayMetrics(metrics) {
const container = document.getElementById('metrics');
container.innerHTML = '';
// 添加基本信息
addSection(container, '基本信息', {
'运行时间': metrics.uptime,
'总请求数': metrics.totalRequests,
'活跃请求数': metrics.activeRequests,
'错误请求数': metrics.totalErrors,
'总传输字节': formatBytes(metrics.totalBytes)
});
// 添加状态码统计
addSection(container, '状态码统计', metrics.statusStats);
// 添加路径统计
addSection(container, '路径统计', metrics.pathStats);
// 添加来源统计
addSection(container, '来源统计', metrics.refererStats);
// 添加延迟统计
addSection(container, '延迟统计', {
'平均延迟': `${metrics.avgLatency}ms`,
'延迟分布': metrics.latencyBuckets
});
}
function addSection(container, title, data) {
const section = document.createElement('div');
section.className = 'metrics-section';
const titleElem = document.createElement('h2');
titleElem.textContent = title;
section.appendChild(titleElem);
const content = document.createElement('div');
content.className = 'metrics-content';
for (const [key, value] of Object.entries(data)) {
const item = document.createElement('div');
item.className = 'metrics-item';
item.innerHTML = `<span class="key">${key}:</span> <span class="value">${value}</span>`;
content.appendChild(item);
}
section.appendChild(content);
container.appendChild(section);
}
function formatBytes(bytes) {
if (bytes === 0) return '0 B';
const k = 1024;
const sizes = ['B', 'KB', 'MB', 'GB', 'TB'];
const i = Math.floor(Math.log(bytes) / Math.log(k));
return parseFloat((bytes / Math.pow(k, i)).toFixed(2)) + ' ' + sizes[i];
}
function showMessage(msg, isError = false) {
const msgDiv = document.getElementById('message');
if (!msgDiv) return;
msgDiv.textContent = msg;
msgDiv.className = isError ? 'error' : 'success';
msgDiv.style.display = 'block';
setTimeout(() => {
msgDiv.style.display = 'none';
}, 5000);
}
// 初始加载监控数据
loadMetrics();
// 每30秒刷新一次数据
setInterval(loadMetrics, 30000);