|
| 1 | +package harmonytask |
| 2 | + |
| 3 | +import ( |
| 4 | + promclient "github.com/prometheus/client_golang/prometheus" |
| 5 | + "go.opencensus.io/stats" |
| 6 | + "go.opencensus.io/stats/view" |
| 7 | + "go.opencensus.io/tag" |
| 8 | +) |
| 9 | + |
| 10 | +var ( |
| 11 | + taskNameTag, _ = tag.NewKey("task_name") |
| 12 | + sourceTag, _ = tag.NewKey("source") |
| 13 | + pre = "harmonytask_" |
| 14 | + |
| 15 | + // tasks can be short, but can extend to hours |
| 16 | + durationBuckets = []float64{0.5, 1, 3, 6, 10, 20, 30, 60, 120, 300, 600, 1800, 3600, 7200, 18000, 36000} |
| 17 | +) |
| 18 | + |
| 19 | +// TaskMeasures groups all harmonytask metrics. |
| 20 | +var TaskMeasures = struct { |
| 21 | + TasksStarted *stats.Int64Measure |
| 22 | + TasksCompleted *stats.Int64Measure |
| 23 | + TasksFailed *stats.Int64Measure |
| 24 | + TaskDuration promclient.Histogram |
| 25 | + ActiveTasks *stats.Int64Measure |
| 26 | + CpuUsage *stats.Float64Measure |
| 27 | + GpuUsage *stats.Float64Measure |
| 28 | + RamUsage *stats.Float64Measure |
| 29 | + PollerIterations *stats.Int64Measure |
| 30 | + AddedTasks *stats.Int64Measure |
| 31 | +}{ |
| 32 | + TasksStarted: stats.Int64(pre+"tasks_started", "Total number of tasks started.", stats.UnitDimensionless), |
| 33 | + TasksCompleted: stats.Int64(pre+"tasks_completed", "Total number of tasks completed successfully.", stats.UnitDimensionless), |
| 34 | + TasksFailed: stats.Int64(pre+"tasks_failed", "Total number of tasks that failed.", stats.UnitDimensionless), |
| 35 | + TaskDuration: promclient.NewHistogram(promclient.HistogramOpts{ |
| 36 | + Name: pre + "task_duration_seconds", |
| 37 | + Buckets: durationBuckets, |
| 38 | + Help: "The histogram of task durations in seconds.", |
| 39 | + }), |
| 40 | + ActiveTasks: stats.Int64(pre+"active_tasks", "Current number of active tasks.", stats.UnitDimensionless), |
| 41 | + CpuUsage: stats.Float64(pre+"cpu_usage", "Percentage of CPU in use.", stats.UnitDimensionless), |
| 42 | + GpuUsage: stats.Float64(pre+"gpu_usage", "Percentage of GPU in use.", stats.UnitDimensionless), |
| 43 | + RamUsage: stats.Float64(pre+"ram_usage", "Percentage of RAM in use.", stats.UnitDimensionless), |
| 44 | + PollerIterations: stats.Int64(pre+"poller_iterations", "Total number of poller iterations.", stats.UnitDimensionless), |
| 45 | + AddedTasks: stats.Int64(pre+"added_tasks", "Total number of tasks added.", stats.UnitDimensionless), |
| 46 | +} |
| 47 | + |
| 48 | +// TaskViews groups all harmonytask-related default views. |
| 49 | +func init() { |
| 50 | + err := view.Register( |
| 51 | + &view.View{ |
| 52 | + Measure: TaskMeasures.TasksStarted, |
| 53 | + Aggregation: view.Sum(), |
| 54 | + TagKeys: []tag.Key{taskNameTag, sourceTag}, |
| 55 | + }, |
| 56 | + &view.View{ |
| 57 | + Measure: TaskMeasures.TasksCompleted, |
| 58 | + Aggregation: view.Sum(), |
| 59 | + TagKeys: []tag.Key{taskNameTag}, |
| 60 | + }, |
| 61 | + &view.View{ |
| 62 | + Measure: TaskMeasures.TasksFailed, |
| 63 | + Aggregation: view.Sum(), |
| 64 | + TagKeys: []tag.Key{taskNameTag}, |
| 65 | + }, |
| 66 | + &view.View{ |
| 67 | + Measure: TaskMeasures.ActiveTasks, |
| 68 | + Aggregation: view.LastValue(), |
| 69 | + TagKeys: []tag.Key{taskNameTag}, |
| 70 | + }, |
| 71 | + &view.View{ |
| 72 | + Measure: TaskMeasures.CpuUsage, |
| 73 | + Aggregation: view.LastValue(), |
| 74 | + TagKeys: []tag.Key{}, |
| 75 | + }, |
| 76 | + &view.View{ |
| 77 | + Measure: TaskMeasures.GpuUsage, |
| 78 | + Aggregation: view.LastValue(), |
| 79 | + TagKeys: []tag.Key{}, |
| 80 | + }, |
| 81 | + &view.View{ |
| 82 | + Measure: TaskMeasures.RamUsage, |
| 83 | + Aggregation: view.LastValue(), |
| 84 | + TagKeys: []tag.Key{}, |
| 85 | + }, |
| 86 | + &view.View{ |
| 87 | + Measure: TaskMeasures.PollerIterations, |
| 88 | + Aggregation: view.Sum(), |
| 89 | + TagKeys: []tag.Key{}, |
| 90 | + }, |
| 91 | + &view.View{ |
| 92 | + Measure: TaskMeasures.AddedTasks, |
| 93 | + Aggregation: view.Sum(), |
| 94 | + TagKeys: []tag.Key{taskNameTag}, |
| 95 | + }, |
| 96 | + ) |
| 97 | + if err != nil { |
| 98 | + panic(err) |
| 99 | + } |
| 100 | + |
| 101 | + err = promclient.Register(TaskMeasures.TaskDuration) |
| 102 | + if err != nil { |
| 103 | + panic(err) |
| 104 | + } |
| 105 | +} |
0 commit comments