Skip to content

Commit d516dbd

Browse files
committed
Add tracing
1 parent 49c1404 commit d516dbd

File tree

2 files changed

+27
-0
lines changed

2 files changed

+27
-0
lines changed

http.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package main
22

33
import (
44
"context"
5+
"fmt"
56
"io"
67
"io/ioutil"
78
"log"
@@ -10,6 +11,7 @@ import (
1011
"os"
1112
"path/filepath"
1213
"regexp"
14+
"runtime/trace"
1315
"strconv"
1416
"strings"
1517
"sync"
@@ -61,12 +63,14 @@ func posterThread(ctx context.Context, ch <-chan hRequest, wg *sync.WaitGroup) {
6163
return
6264
}
6365
//log.Printf("%#v", i)
66+
tr := trace.StartRegion(ctx, fmt.Sprintf("#%d %s %s", i.LoopNum, i.Method, i.URL))
6467
var f io.ReadCloser
6568
var err error
6669
if i.Filename != "" {
6770
f, err = os.Open(i.Filename)
6871
if err != nil {
6972
log.Printf("Unable to open %s", i.Filename)
73+
tr.End()
7074
continue
7175
}
7276
}
@@ -75,6 +79,7 @@ func posterThread(ctx context.Context, ch <-chan hRequest, wg *sync.WaitGroup) {
7579
if f != nil {
7680
f.Close()
7781
}
82+
tr.End()
7883
log.Fatal(err)
7984
}
8085
if i.Filename != "" {
@@ -107,6 +112,7 @@ func posterThread(ctx context.Context, ch <-chan hRequest, wg *sync.WaitGroup) {
107112
if err != nil {
108113
kubismus.Metric("Error", 1, 0)
109114
log.Print("HTTP error ", i.URL, ": ", err)
115+
tr.End()
110116
continue
111117
}
112118
kubismus.Metric("Sent", 1, float64(i.Size))
@@ -150,6 +156,7 @@ func posterThread(ctx context.Context, ch <-chan hRequest, wg *sync.WaitGroup) {
150156
if outfile != nil {
151157
outfile.Close()
152158
}
159+
tr.End()
153160
case <-done:
154161
return
155162
}

main.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
"os/signal"
1313
"runtime"
1414
"runtime/pprof"
15+
"runtime/trace"
1516
"strconv"
1617
"strings"
1718
"time"
@@ -45,6 +46,7 @@ var (
4546
filesPat string
4647
cpuProfile string
4748
memProfile string
49+
traceFile string
4850
cpus int
4951
workingDir string
5052
discard bool
@@ -84,6 +86,7 @@ func init() {
8486
// profiling
8587
flag.StringVar(&cpuProfile, "cpuprofile", "", "Write CPU profile to given file.")
8688
flag.StringVar(&memProfile, "memprofile", "", "Write memory profile to given file.")
89+
flag.StringVar(&traceFile, "trace", "", "Write a trace file.")
8790

8891
// runtime
8992
flag.IntVar(&cpus, "cpu", 0, "Number of CPUs to use.")
@@ -197,6 +200,23 @@ func main() {
197200
}()
198201
}
199202

203+
if traceFile != "" {
204+
f, err := os.Create(traceFile)
205+
if err != nil {
206+
log.Fatalf("failed to create trace output file: %v", err)
207+
}
208+
defer func() {
209+
if err := f.Close(); err != nil {
210+
log.Fatalf("failed to close trace file: %v", err)
211+
}
212+
}()
213+
214+
if err := trace.Start(f); err != nil {
215+
log.Fatalf("failed to start trace: %v", err)
216+
}
217+
defer trace.Stop()
218+
}
219+
200220
// create HTTP transport and client
201221
transport = &http.Transport{Proxy: http.ProxyFromEnvironment, DisableKeepAlives: noKeepAlive, MaxIdleConnsPerHost: conns, DisableCompression: noCompress, ResponseHeaderTimeout: timeout}
202222
client = &http.Client{Transport: transport, Timeout: timeout}

0 commit comments

Comments
 (0)