golang.org/x/build@v0.0.0-20240506185731-218518f32b70/cmd/coordinator/metrics.go (about) 1 // Copyright 2017 The Go Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 //go:build linux || darwin 6 7 package main 8 9 import ( 10 "context" 11 "fmt" 12 "time" 13 14 "github.com/gliderlabs/ssh" 15 "go.opencensus.io/stats" 16 "go.opencensus.io/stats/view" 17 "go.opencensus.io/tag" 18 "golang.org/x/build/internal/coordinator/pool" 19 ) 20 21 var ( 22 kBuilderType = tag.MustNewKey("go-build/coordinator/keys/builder_type") 23 kGomoteSSHSuccess = tag.MustNewKey("go-build/coordinator/keys/gomote_ssh_success") 24 kHostType = tag.MustNewKey("go-build/coordinator/host_type") 25 mGitHubAPIRemaining = stats.Int64("go-build/githubapi/remaining", "remaining GitHub API rate limit", stats.UnitDimensionless) 26 mGomoteCreateCount = stats.Int64("go-build/coordinator/gomote_create_count", "counter for gomote create invocations", stats.UnitDimensionless) 27 mGomoteRDPCount = stats.Int64("go-build/coordinator/gomote_rdp_count", "counter for gomote RDP invocations", stats.UnitDimensionless) 28 mGomoteSSHCount = stats.Int64("go-build/coordinator/gomote_ssh_count", "counter for gomote SSH invocations", stats.UnitDimensionless) 29 mReverseBuildlets = stats.Int64("go-build/coordinator/reverse_buildlets_count", "number of reverse buildlets", stats.UnitDimensionless) 30 ) 31 32 // views should contain all measurements. All *view.View added to this 33 // slice will be registered and exported to the metric service. 34 var views = []*view.View{ 35 { 36 Name: "go-build/coordinator/reverse_buildlets_count", 37 Description: "Number of reverse buildlets that are up", 38 Measure: mReverseBuildlets, 39 TagKeys: []tag.Key{kHostType}, 40 Aggregation: view.LastValue(), 41 }, 42 { 43 Name: "go-build/githubapi/remaining", 44 Description: "Remaining GitHub API rate limit", 45 Measure: mGitHubAPIRemaining, 46 Aggregation: view.LastValue(), 47 }, 48 { 49 Name: "go-build/coordinator/gomote_create_count", 50 Description: "Count of gomote create invocations", 51 Measure: mGomoteCreateCount, 52 TagKeys: []tag.Key{kBuilderType}, 53 Aggregation: view.Count(), 54 }, 55 { 56 Name: "go-build/coordinator/gomote_ssh_count", 57 Description: "Count of gomote SSH invocations", 58 Measure: mGomoteSSHCount, 59 TagKeys: []tag.Key{kGomoteSSHSuccess}, 60 Aggregation: view.Count(), 61 }, 62 { 63 Name: "go-build/coordinator/gomote_rdp_count", 64 Description: "Count of gomote RDP ivocations", 65 Measure: mGomoteRDPCount, 66 Aggregation: view.Count(), 67 }, 68 } 69 70 // reportReverseCountMetrics gathers and reports 71 // a count of running reverse buildlets per type. 72 func reportReverseCountMetrics() { 73 for { 74 // 1. Gather # buildlets up per reverse builder type. 75 totals := pool.ReversePool().HostTypeCount() 76 // 2. Write counts out to the metrics recorder, grouped by hostType. 77 for hostType, n := range totals { 78 stats.RecordWithTags(context.Background(), 79 []tag.Mutator{tag.Upsert(kHostType, hostType)}, 80 mReverseBuildlets.M(int64(n))) 81 } 82 83 time.Sleep(5 * time.Minute) 84 } 85 } 86 87 // recordBuildletCreate records information about gomote creates and sends them 88 // to the configured metrics backend. 89 func recordBuildletCreate(ctx context.Context, builderType string) { 90 stats.RecordWithTags(ctx, 91 []tag.Mutator{ 92 tag.Upsert(kBuilderType, builderType), 93 }, 94 mGomoteCreateCount.M(1)) 95 } 96 97 // recordSSHPublicKeyAuthHandler returns a handler which wraps and ssh public key handler and 98 // records information about gomote SSH usage and sends them to the configured metrics backend. 99 func recordSSHPublicKeyAuthHandler(fn ssh.PublicKeyHandler) ssh.PublicKeyHandler { 100 return func(ctx ssh.Context, key ssh.PublicKey) bool { 101 success := fn(ctx, key) 102 stats.RecordWithTags(ctx, 103 []tag.Mutator{ 104 tag.Upsert(kGomoteSSHSuccess, fmt.Sprintf("%t", success)), 105 }, 106 mGomoteSSHCount.M(1)) 107 return success 108 } 109 } 110 111 // recordGomoteRDPUsage records the use of the gomote RDP functionality and sends it 112 // to the configured metrics backend. 113 func recordGomoteRDPUsage(ctx context.Context) { 114 stats.Record(ctx, mGomoteRDPCount.M(1)) 115 }