Skip to content

Commit

Permalink
feat: bcs-task mysqlstore add status index & metrics (#3594)
Browse files Browse the repository at this point in the history
* feat: bcs-task mysqlstore status index

* feat: add GetTaskIndexType

* feat: add builder add validate

* feat: add metrics

* feat: add metrics

* feat: add created index
  • Loading branch information
ifooth authored Nov 1, 2024
1 parent fcab98b commit f5d8bc4
Show file tree
Hide file tree
Showing 8 changed files with 130 additions and 8 deletions.
4 changes: 4 additions & 0 deletions bcs-common/common/task/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,5 +39,9 @@ func NewByTaskBuilder(builder types.TaskBuilder, opts ...types.TaskOption) (*typ
return nil, err
}

if err := task.Validate(); err != nil {
return nil, err
}

return task, nil
}
2 changes: 1 addition & 1 deletion bcs-common/common/task/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ require (
github.com/RichardKnop/machinery/v2 v2.0.13
github.com/Tencent/bk-bcs/bcs-common v0.0.0-20240803071244-414878b26fa9
github.com/google/uuid v1.6.0
github.com/prometheus/client_golang v1.19.1
github.com/stretchr/testify v1.9.0
github.com/urfave/cli v1.22.5
go.etcd.io/etcd/api/v3 v3.5.2
Expand Down Expand Up @@ -70,7 +71,6 @@ require (
github.com/pjbgf/sha1cd v0.3.0 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/prometheus/client_golang v1.19.0 // indirect
github.com/prometheus/client_model v0.5.0 // indirect
github.com/prometheus/common v0.48.0 // indirect
github.com/prometheus/procfs v0.12.0 // indirect
Expand Down
4 changes: 2 additions & 2 deletions bcs-common/common/task/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -371,8 +371,8 @@ github.com/prometheus/client_golang v0.9.1/go.mod h1:7SWBe2y4D6OKWSNQJUaRYU/AaXP
github.com/prometheus/client_golang v1.0.0/go.mod h1:db9x61etRT2tGnBNRi70OPL5FsnadC4Ky3P0J6CfImo=
github.com/prometheus/client_golang v1.7.1/go.mod h1:PY5Wy2awLA44sXw4AOSfFBetzPP4j5+D6mVACh+pe2M=
github.com/prometheus/client_golang v1.11.0/go.mod h1:Z6t4BnS23TR94PD6BsDNk8yVqroYurpAkEiz0P2BEV0=
github.com/prometheus/client_golang v1.19.0 h1:ygXvpU1AoN1MhdzckN+PyD9QJOSD4x7kmXYlnfbA6JU=
github.com/prometheus/client_golang v1.19.0/go.mod h1:ZRM9uEAypZakd+q/x7+gmsvXdURP+DABIEIjnmDdp+k=
github.com/prometheus/client_golang v1.19.1 h1:wZWJDwK+NameRJuPGDhlnFgx8e8HN3XHQeLaYJFJBOE=
github.com/prometheus/client_golang v1.19.1/go.mod h1:mP78NwGzrVks5S2H6ab8+ZZGJLZUq1hoULYBAYBw1Ho=
github.com/prometheus/client_model v0.0.0-20180712105110-5c3871d89910/go.mod h1:MbSGuTsp3dbXC40dX6PRTWyKYBIrTGTE9sqQNg2J8bo=
github.com/prometheus/client_model v0.0.0-20190129233127-fd36f4220a90/go.mod h1:xMI15A0UPsDsEKsMN9yxemIoYk6Tm2C1GtYGdfGttqA=
github.com/prometheus/client_model v0.0.0-20190812154241-14fe0d1b01d4/go.mod h1:xMI15A0UPsDsEKsMN9yxemIoYk6Tm2C1GtYGdfGttqA=
Expand Down
5 changes: 5 additions & 0 deletions bcs-common/common/task/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -348,6 +348,11 @@ func (m *TaskManager) doWork(taskID string, stepName string) error { // nolint
}

start := time.Now()

// metrics
collectMetricStart(state)
defer collectMetricEnd(state)

// step timeout
stepCtx, stepCancel := GetTimeOutCtx(context.Background(), step.MaxExecutionSeconds)
defer stepCancel()
Expand Down
97 changes: 97 additions & 0 deletions bcs-common/common/task/metrics.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
/*
* Tencent is pleased to support the open source community by making Blueking Container Service available.
* Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved.
* Licensed under the MIT License (the "License"); you may not use this file except
* in compliance with the License. You may obtain a copy of the License at
* http://opensource.org/licenses/MIT
* 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 task

import (
"github.com/prometheus/client_golang/prometheus"

"github.com/Tencent/bk-bcs/bcs-common/common/task/types"
)

var (
// 当前step执行数量
stepRunningCount = prometheus.NewGaugeVec(prometheus.GaugeOpts{
Name: "step_running_count",
Help: "The number of running step.",
}, []string{"task_type", "executor"})

// step执行总数
stepExecuteTotal = prometheus.NewCounterVec(prometheus.CounterOpts{
Name: "step_execute_total",
Help: "Counter of step execute count.",
}, []string{"task_type", "executor", "status"})

// step执行耗时
stepExecuteDuration = prometheus.NewHistogramVec(prometheus.HistogramOpts{
Name: "step_execute_duration_seconds",
Help: "Histogram of duration for step execute.",
Buckets: []float64{1, 10, 30, 60, 60 * 5, 60 * 10, 60 * 30, 3600, 3600 * 2, 3600 * 4, 3600 * 8},
}, []string{"task_type", "executor", "status"})

// task执行总数
taskExecuteTotal = prometheus.NewCounterVec(prometheus.CounterOpts{
Name: "task_execute_total",
Help: "Counter of task execute.",
}, []string{"task_type", "status"})

// task执行耗时
taskExecuteDuration = prometheus.NewHistogramVec(prometheus.HistogramOpts{
Name: "task_execute_duration_seconds",
Help: "Histogram of duration for task execute.",
Buckets: []float64{1, 10, 30, 60, 60 * 5, 60 * 10, 60 * 30, 3600, 3600 * 2, 3600 * 4, 3600 * 8},
}, []string{"task_type", "status"})
)

func init() {
prometheus.MustRegister(stepRunningCount)
prometheus.MustRegister(stepExecuteTotal)
prometheus.MustRegister(stepExecuteDuration)
prometheus.MustRegister(taskExecuteTotal)
prometheus.MustRegister(taskExecuteDuration)
}

// collectMetricStart metrics for task start
func collectMetricStart(state *State) {
stepRunningCount.WithLabelValues(
state.task.GetTaskType(),
state.step.Executor).Inc()
}

// collectMetricEnd metrics for task end
func collectMetricEnd(state *State) {
// 任务状态完成时, 记录执行结果
if state.task.GetStatus() != types.TaskStatusInit && state.task.GetStatus() != types.TaskStatusRunning {
taskExecuteTotal.WithLabelValues(
state.task.GetTaskType(),
state.task.GetStatus()).Inc()

taskExecuteDuration.WithLabelValues(
state.task.GetTaskType(),
state.task.GetStatus()).Observe(state.task.GetExecutionTime().Seconds())
}

// 任务步骤完成时, 记录执行结果
stepRunningCount.WithLabelValues(
state.task.GetTaskType(),
state.step.Executor).Dec()

stepExecuteTotal.WithLabelValues(
state.task.GetTaskType(),
state.step.Executor,
state.step.GetStatus()).Inc()

stepExecuteDuration.WithLabelValues(
state.task.GetTaskType(),
state.step.Executor,
state.step.GetStatus()).Observe(state.step.GetExecutionTime().Seconds())
}
5 changes: 5 additions & 0 deletions bcs-common/common/task/steps/iface/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,11 @@ func (c *Context) GetTaskIndex() string {
return c.task.GetTaskIndex()
}

// GetTaskIndexType get task index type
func (c *Context) GetTaskIndexType() string {
return c.task.GetTaskIndexType()
}

// GetTaskStatus get task status
func (c *Context) GetTaskStatus() string {
return c.task.GetStatus()
Expand Down
16 changes: 11 additions & 5 deletions bcs-common/common/task/stores/mysql/table.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,20 +34,26 @@ var (
UnixZeroTime = time.Unix(0, 0)
)

// BaseModel 添加 CreatedAt 索引
type BaseModel struct {
gorm.Model
CreatedAt time.Time `gorm:"index"`
}

// TaskRecord 任务记录
type TaskRecord struct {
gorm.Model
BaseModel
TaskID string `json:"taskID" gorm:"type:varchar(191);uniqueIndex:idx_task_id"` // 唯一索引
TaskType string `json:"taskType" gorm:"type:varchar(255)"`
TaskIndex string `json:"TaskIndex" gorm:"type:varchar(255)"`
TaskIndexType string `json:"TaskIndexType" gorm:"type:varchar(255)"`
TaskType string `json:"taskType" gorm:"type:varchar(191);index:idx_task_type"`
TaskIndex string `json:"TaskIndex" gorm:"type:varchar(191);index:idx_task_index"`
TaskIndexType string `json:"TaskIndexType" gorm:"type:varchar(191);index:idx_task_index"`
TaskName string `json:"taskName" gorm:"type:varchar(255)"`
CurrentStep string `json:"currentStep" gorm:"type:varchar(255)"`
StepSequence []string `json:"stepSequence" gorm:"type:text;serializer:json"`
CallbackName string `json:"callbackName" gorm:"type:varchar(255)"`
CommonParams map[string]string `json:"commonParams" gorm:"type:text;serializer:json"`
CommonPayload string `json:"commonPayload" gorm:"type:text"`
Status string `json:"status" gorm:"type:varchar(255)"`
Status string `json:"status" gorm:"type:varchar(191);index:idx_status"`
Message string `json:"message" gorm:"type:text"`
ExecutionTime uint32 `json:"executionTime"`
MaxExecutionSeconds uint32 `json:"maxExecutionSeconds"`
Expand Down
5 changes: 5 additions & 0 deletions bcs-common/common/task/types/task.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,11 @@ func (t *Task) GetTaskIndex() string {
return t.TaskIndex
}

// GetTaskIndexType get task index type
func (t *Task) GetTaskIndexType() string {
return t.TaskIndexType
}

// GetStep get step by name
func (t *Task) GetStep(stepName string) (*Step, bool) {
for _, step := range t.Steps {
Expand Down

0 comments on commit f5d8bc4

Please sign in to comment.