Google 云监控 - 警报政策:状态代码:409 - 对项目配置的并发编辑过多

我正在尝试为Google Cloud Platform中的Kubernetes Clusters创建警报策略。下面是示例代码。


service, err := monitoring.NewService(context.Background())

if err != nil {

    log.Panicln(err)

    return

}


mqlCondition := &monitoring.MonitoringQueryLanguageCondition{

    Duration: "60s",

    Query: `fetch k8s_pod

            | metric 'kubernetes.io/pod/volume/utilization'

            | filter

                (resource.cluster_name == 'test'

                 && resource.pod_name =~ 'server.*')

                && (metric.volume_name =~ 'dat.*')

            | align mean_aligner()

            | window 10m

            | condition value.utilization > 0.001 '10^2.%'

            `,

    Trigger: &monitoring.Trigger{

        Count: 1,

    },

}

condition := monitoring.Condition{

    DisplayName:                      "MQL-based Condition",

    ConditionMonitoringQueryLanguage: mqlCondition,

}

alertpolicy := &monitoring.AlertPolicy{

    DisplayName:          "Prakash1",

    Combiner:             "OR",

    Conditions:           []*monitoring.Condition{&condition},

    NotificationChannels: []string{"projects/abc-app/notificationChannels/16000000099515524778"},

    

}

p, err := service.Projects.AlertPolicies.Create("projects/abc-app", alertpolicy).Context(context.Background()).Do()

if err != nil {

    log.Panicln(err)

    return

}

当我同时创建两个或多个警报策略时,我收到以下错误:


"googleapi: Error 409: Too many concurrent edits to the project configuration. Please try again., aborted"

你能告诉我如何解决这个错误吗?


小怪兽爱吃肉
浏览 129回答 1
1回答

拉丁的传说

如错误所述,您无法同时添加策略。我建议您并行生成创建请求并序列化实际的 API 调用。您可以通过缓冲通道和多个 go 例程来实现此目的。例如:var(&nbsp; backlogSize = 3 //change as per your needs&nbsp; requests = make(chan *monitoring.AlertPolicy, backlogSize))func createPolicies(){&nbsp; ...&nbsp; go func(){&nbsp; &nbsp; &nbsp;//init the service&nbsp; &nbsp; &nbsp;...&nbsp; &nbsp; &nbsp;for policy := range requests {&nbsp; &nbsp; &nbsp; &nbsp;p, err := service.Projects.AlertPolicies.Create("projects/abc-app", policy).Context(context.Background()).Do()&nbsp; &nbsp; &nbsp; &nbsp;if err != nil {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;log.Println(err)&nbsp; &nbsp; &nbsp; &nbsp;}&nbsp; &nbsp; &nbsp;}&nbsp; }()&nbsp; go func(){&nbsp; &nbsp; &nbsp;newPolicy := &monitoring.AlertPolicy&nbsp; &nbsp; &nbsp;//fill policy&nbsp; &nbsp; &nbsp;...&nbsp; &nbsp; &nbsp;requests <- newPolicy&nbsp;&nbsp; }()&nbsp; ...&nbsp; //wait for completion and close requests channel}另一种解决方案是使用指数退避+抖动重试每个失败的并发请求
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Go