|
| 1 | +package service |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "os" |
| 7 | + "runtime" |
| 8 | + "time" |
| 9 | + |
| 10 | + "github.com/ovh/cds/sdk" |
| 11 | + "go.opencensus.io/stats" |
| 12 | + "go.opencensus.io/stats/view" |
| 13 | + "go.opencensus.io/tag" |
| 14 | +) |
| 15 | + |
| 16 | +func CommonMetricsView(ctx context.Context) []*view.View { |
| 17 | + allocStats := stats.Int64( |
| 18 | + "cds/alloc", |
| 19 | + "Alloc is bytes of allocated heap objects", |
| 20 | + stats.UnitBytes, |
| 21 | + ) |
| 22 | + |
| 23 | + totalAllocStats := stats.Int64( |
| 24 | + fmt.Sprintf("cds/total_alloc"), |
| 25 | + "Total Alloc is cumulative bytes allocated for heap objects", |
| 26 | + stats.UnitBytes, |
| 27 | + ) |
| 28 | + |
| 29 | + sysStats := stats.Int64( |
| 30 | + fmt.Sprintf("cds/sys"), |
| 31 | + "Sys is the total bytes of memory obtained from the OS", |
| 32 | + stats.UnitBytes, |
| 33 | + ) |
| 34 | + |
| 35 | + gcStats := stats.Int64( |
| 36 | + fmt.Sprintf("cds/num_gc"), |
| 37 | + "NumGC is the number of completed GC cycles", |
| 38 | + stats.UnitDimensionless, |
| 39 | + ) |
| 40 | + |
| 41 | + tagHostname, _ := tag.NewKey("hostname") |
| 42 | + |
| 43 | + allocView := view.View{ |
| 44 | + Name: "cds/mem/alloc", |
| 45 | + Description: allocStats.Description(), |
| 46 | + Measure: allocStats, |
| 47 | + TagKeys: []tag.Key{tagHostname}, |
| 48 | + Aggregation: view.LastValue(), |
| 49 | + } |
| 50 | + |
| 51 | + totalAllocView := view.View{ |
| 52 | + Name: "cds/mem/total_alloc", |
| 53 | + Description: totalAllocStats.Description(), |
| 54 | + Measure: totalAllocStats, |
| 55 | + TagKeys: []tag.Key{tagHostname}, |
| 56 | + Aggregation: view.LastValue(), |
| 57 | + } |
| 58 | + |
| 59 | + sysView := view.View{ |
| 60 | + Name: "cds/mem/sys", |
| 61 | + Description: sysStats.Description(), |
| 62 | + Measure: sysStats, |
| 63 | + TagKeys: []tag.Key{tagHostname}, |
| 64 | + Aggregation: view.LastValue(), |
| 65 | + } |
| 66 | + |
| 67 | + gcView := view.View{ |
| 68 | + Name: "cds/mem/gc", |
| 69 | + Description: gcStats.Description(), |
| 70 | + Measure: gcStats, |
| 71 | + TagKeys: []tag.Key{tagHostname}, |
| 72 | + Aggregation: view.LastValue(), |
| 73 | + } |
| 74 | + |
| 75 | + sdk.GoRoutine(ctx, "service_metrics", func(ctx context.Context) { |
| 76 | + hostname, _ := os.Hostname() |
| 77 | + ctx, _ = tag.New(ctx, tag.Upsert(tagHostname, hostname)) |
| 78 | + |
| 79 | + var tick = time.NewTicker(10 * time.Second) |
| 80 | + defer tick.Stop() |
| 81 | + for { |
| 82 | + select { |
| 83 | + case <-ctx.Done(): |
| 84 | + return |
| 85 | + case <-tick.C: |
| 86 | + var m runtime.MemStats |
| 87 | + runtime.ReadMemStats(&m) |
| 88 | + stats.Record(ctx, allocStats.M(int64(m.Alloc))) |
| 89 | + stats.Record(ctx, totalAllocStats.M(int64(m.TotalAlloc))) |
| 90 | + stats.Record(ctx, sysStats.M(int64(m.Sys))) |
| 91 | + stats.Record(ctx, gcStats.M(int64(m.NumGC))) |
| 92 | + } |
| 93 | + } |
| 94 | + }) |
| 95 | + |
| 96 | + return []*view.View{&allocView, &totalAllocView, &sysView, &gcView} |
| 97 | +} |
0 commit comments