-
Notifications
You must be signed in to change notification settings - Fork 0
/
writer.go
62 lines (53 loc) · 1.19 KB
/
writer.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
package autoprof
import (
"context"
"encoding/json"
"expvar"
"fmt"
"io"
"runtime/pprof"
)
func errSource(err error) *DataSource {
return &DataSource{WriteTo: func(ctx context.Context, w io.Writer) error {
return err
}}
}
func metaSource(meta *ArchiveMeta) *DataSource {
buf, err := json.Marshal(meta)
if err != nil {
return errSource(err)
}
return &DataSource{WriteTo: func(ctx context.Context, w io.Writer) error {
_, err = w.Write(buf)
if err != nil {
return err
}
return nil
}}
}
func expvarSource() *DataSource {
return expvarStyleSource(expvar.Do)
}
func expvarStyleSource(do func(func(kv expvar.KeyValue))) *DataSource {
return &DataSource{WriteTo: func(ctx context.Context, w io.Writer) error {
var err error
printf := func(format string, a ...interface{}) {
if err == nil {
_, err = fmt.Fprintf(w, format, a...)
}
}
prefix := ""
printf("{\n")
do(func(kv expvar.KeyValue) {
printf("%s%q: %s", prefix, kv.Key, kv.Value)
prefix = ",\n"
})
printf("\n}\n")
return nil
}}
}
func pprofSource(profile *pprof.Profile) *DataSource {
return &DataSource{WriteTo: func(ctx context.Context, w io.Writer) error {
return profile.WriteTo(w, 0)
}}
}