代码拉取完成,页面将自动刷新
/*
Copyright 2022 The KubeVela Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package watcher
import (
"encoding/json"
"fmt"
"strings"
"sync"
"time"
"k8s.io/client-go/tools/cache"
"k8s.io/klog/v2"
ctrlcache "sigs.k8s.io/controller-runtime/pkg/cache"
"gitee.com/kubeship/workflow/api/v1alpha1"
"gitee.com/kubeship/workflow/pkg/monitor/metrics"
)
type workflowRunMetricsWatcher struct {
mu sync.Mutex
phaseCounter map[string]int
stepPhaseCounter map[string]int
phaseDirty map[string]struct{}
stepPhaseDirty map[string]struct{}
informer ctrlcache.Informer
stopCh chan struct{}
}
func (watcher *workflowRunMetricsWatcher) getRun(obj interface{}) *v1alpha1.WorkflowRun {
wr := &v1alpha1.WorkflowRun{}
bs, _ := json.Marshal(obj)
_ = json.Unmarshal(bs, wr)
return wr
}
func (watcher *workflowRunMetricsWatcher) getPhase(phase string) string {
if phase == "" {
return "-"
}
return phase
}
func (watcher *workflowRunMetricsWatcher) inc(wr *v1alpha1.WorkflowRun, delta int) {
watcher.mu.Lock()
defer watcher.mu.Unlock()
phase := watcher.getPhase(string(wr.Status.Phase))
watcher.phaseCounter[phase] += delta
watcher.phaseDirty[phase] = struct{}{}
for _, step := range wr.Status.Steps {
stepPhase := watcher.getPhase(string(step.Phase))
key := fmt.Sprintf("%s/%s#%s", step.Type, stepPhase, step.Reason)
watcher.stepPhaseCounter[key] += delta
watcher.stepPhaseDirty[key] = struct{}{}
}
}
func (watcher *workflowRunMetricsWatcher) report() {
watcher.mu.Lock()
defer watcher.mu.Unlock()
for phase := range watcher.phaseDirty {
metrics.WorkflowRunPhaseCounter.WithLabelValues(phase).Set(float64(watcher.phaseCounter[phase]))
}
for stepPhase := range watcher.stepPhaseDirty {
metrics.WorkflowRunStepPhaseGauge.WithLabelValues(strings.Split(stepPhase, "/")[:2]...).Set(float64(watcher.stepPhaseCounter[stepPhase]))
}
watcher.phaseDirty = map[string]struct{}{}
watcher.stepPhaseDirty = map[string]struct{}{}
}
func (watcher *workflowRunMetricsWatcher) run() {
go func() {
for {
select {
case <-watcher.stopCh:
return
default:
time.Sleep(time.Second)
watcher.report()
}
}
}()
}
// StartWorkflowRunMetricsWatcher start metrics watcher for reporting workflow run stats
func StartWorkflowRunMetricsWatcher(informer ctrlcache.Informer) {
watcher := &workflowRunMetricsWatcher{
phaseCounter: map[string]int{},
stepPhaseCounter: map[string]int{},
phaseDirty: map[string]struct{}{},
stepPhaseDirty: map[string]struct{}{},
informer: informer,
stopCh: make(chan struct{}, 1),
}
_, err := watcher.informer.AddEventHandler(cache.ResourceEventHandlerFuncs{
AddFunc: func(obj interface{}) {
wr := watcher.getRun(obj)
watcher.inc(wr, 1)
},
UpdateFunc: func(oldObj, obj interface{}) {
oldWr := watcher.getRun(oldObj)
wr := watcher.getRun(obj)
watcher.inc(oldWr, -1)
watcher.inc(wr, 1)
},
DeleteFunc: func(obj interface{}) {
wr := watcher.getRun(obj)
watcher.inc(wr, -1)
},
})
if err != nil {
klog.ErrorS(err, "failed to add event handler to workflow metrics watcher")
}
watcher.run()
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。