-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathexpvar.go
65 lines (59 loc) · 1.47 KB
/
expvar.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
// Copyright 2018 GRAIL, Inc. All rights reserved.
// Use of this source code is governed by the Apache 2.0
// license that can be found in the LICENSE file.
package bigmachine
import (
"context"
"encoding/json"
"sync"
"time"
"github.com/grailbio/base/log"
"golang.org/x/sync/errgroup"
)
type machineVars struct{ *B }
// String returns a JSON-formatted string representing the exported
// variables of all underlying machines.
//
// TODO(marius): aggregate values too?
func (v machineVars) String() string {
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
defer cancel()
g, ctx := errgroup.WithContext(ctx)
var (
mu sync.Mutex
vars = make(map[string]Expvars)
)
for _, m := range v.B.Machines() {
// Only propagate stats for machines we own, otherwise we can
// create stats loops.
if !m.Owned() {
continue
}
m := m
g.Go(func() error {
var mvars Expvars
if err := m.Call(ctx, "Supervisor.Expvars", struct{}{}, &mvars); err != nil {
log.Error.Printf("failed to retrieve variables for %s: %v", m.Addr, err)
return nil
}
mu.Lock()
vars[m.Addr] = mvars
mu.Unlock()
return nil
})
}
if err := g.Wait(); err != nil {
b, err2 := json.Marshal(err.Error())
if err2 != nil {
log.Error.Printf("machineVars marshal: %v", err2)
return `"error"`
}
return string(b)
}
b, err := json.Marshal(vars)
if err != nil {
log.Error.Printf("machineVars marshal: %v", err)
return `"error"`
}
return string(b)
}