删除探针、服务监控时同步删除 victoriametrics 数据

This commit is contained in:
dushixiang committed 2026-02-25 14:28:11 +08:00
1 parent 1a64b27af6
commit 4df28e3536
4 files changed
+95

No files matched your search

+9
View File
@@ -229,6 +229,15 @@ func (h *AgentHandler) Delete(c echo.Context) error {
client.Conn.Close()
}
// 删除探针在 VictoriaMetrics 中的历史指标数据
if err := h.metricService.DeleteAgentMetrics(ctx, agentID); err != nil {
h.logger.Error("删除探针 VictoriaMetrics 数据失败",
zap.String("agentID", agentID),
zap.String("name", agent.Name),
zap.Error(err))
return orz.NewError(500, "删除探针指标数据失败")
}
// 删除探针及其所有相关数据
if err := h.agentService.DeleteAgent(ctx, agentID); err != nil {
h.logger.Error("删除探针失败",
+8
View File
@@ -124,6 +124,14 @@ func (h *MonitorHandler) Delete(c echo.Context) error {
id := c.Param("id")
ctx := c.Request().Context()
// 删除服务监控在 VictoriaMetrics 中的历史指标数据
if err := h.metricService.DeleteMonitorMetrics(ctx, id); err != nil {
h.logger.Error("删除服务监控 VictoriaMetrics 数据失败",
zap.String("monitorID", id),
zap.Error(err))
return orz.NewError(500, "删除服务监控指标数据失败")
}
if err := h.monitorService.DeleteMonitor(ctx, id); err != nil {
return err
}
+44
View File
@@ -344,6 +344,50 @@ func (s *MetricService) GetLatestMetrics(agentID string) (*metric.LatestMetrics,
return metrics, ok
}
// DeleteAgentMetrics 删除探针在 VictoriaMetrics 中的历史指标
func (s *MetricService) DeleteAgentMetrics(ctx context.Context, agentID string) error {
if agentID == "" {
return fmt.Errorf("agentID 不能为空")
}
// 删除内存中的最新指标缓存
s.latestCache.Delete(agentID)
// 删除 VictoriaMetrics 中该探针的所有 pika 指标
matcher := fmt.Sprintf(`{__name__=~"pika_.*",agent_id=%q}`, agentID)
if err := s.vmClient.DeleteSeries(ctx, []string{matcher}); err != nil {
s.logger.Error("删除探针 VictoriaMetrics 数据失败",
zap.String("agentID", agentID),
zap.Error(err))
return err
}
s.logger.Info("探针 VictoriaMetrics 数据删除成功", zap.String("agentID", agentID))
return nil
}
// DeleteMonitorMetrics 删除服务监控在 VictoriaMetrics 中的历史指标
func (s *MetricService) DeleteMonitorMetrics(ctx context.Context, monitorID string) error {
if monitorID == "" {
return fmt.Errorf("monitorID 不能为空")
}
// 删除内存中的监控缓存
s.monitorLatestCache.Delete(monitorID)
// 删除 VictoriaMetrics 中该监控任务的所有 pika monitor 指标
matcher := fmt.Sprintf(`{__name__=~"pika_monitor_.*",monitor_id=%q}`, monitorID)
if err := s.vmClient.DeleteSeries(ctx, []string{matcher}); err != nil {
s.logger.Error("删除服务监控 VictoriaMetrics 数据失败",
zap.String("monitorID", monitorID),
zap.Error(err))
return err
}
s.logger.Info("服务监控 VictoriaMetrics 数据删除成功", zap.String("monitorID", monitorID))
return nil
}
// GetAvailableNetworkInterfaces 获取探针的可用网卡列表(从 VictoriaMetrics 查询)
func (s *MetricService) GetAvailableNetworkInterfaces(ctx context.Context, agentID string) ([]string, error) {
// 查询 interface label 的所有值,排除空字符串(汇总数据)
+34
View File
@@ -220,6 +220,40 @@ func (c *VMClient) Query(ctx context.Context, query string) (*QueryResult, error
return &result, nil
}
// DeleteSeries 删除时间序列数据
func (c *VMClient) DeleteSeries(ctx context.Context, matchers []string) error {
if len(matchers) == 0 {
return nil
}
reqCtx, cancel := context.WithTimeout(ctx, c.queryTimeout)
defer cancel()
params := url.Values{}
for _, matcher := range matchers {
params.Add("match[]", matcher)
}
reqURL := fmt.Sprintf("%s/api/v1/admin/tsdb/delete_series?%s", c.baseURL, params.Encode())
req, err := http.NewRequestWithContext(reqCtx, http.MethodPost, reqURL, nil)
if err != nil {
return fmt.Errorf("create request failed: %w", err)
}
resp, err := c.httpClient.Do(req)
if err != nil {
return fmt.Errorf("delete series failed: %w", err)
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK && resp.StatusCode != http.StatusNoContent {
body, _ := io.ReadAll(resp.Body)
return fmt.Errorf("delete series failed with status %d: %s", resp.StatusCode, string(body))
}
return nil
}
// ConvertToDataPoints 将查询结果转换为数据点列表
func ConvertToDataPoints(result *QueryResult) []DataPoint {
if result == nil || len(result.Data.Result) == 0 {