TUN-861: Disable cloudflared tracing by default; preserve the latest tracefile

This commit is contained in:
Chung-Ting Huang 2018-08-07 14:04:35 -05:00
parent 0c414ceb8d
commit 6fff7b436f
1 changed files with 27 additions and 20 deletions

View File

@ -2,6 +2,7 @@ package main
import ( import (
"fmt" "fmt"
"io/ioutil"
"os" "os"
"runtime/trace" "runtime/trace"
"sync" "sync"
@ -298,10 +299,8 @@ func main() {
}), }),
altsrc.NewStringFlag(&cli.StringFlag{ altsrc.NewStringFlag(&cli.StringFlag{
Name: "trace-output", Name: "trace-output",
Value: "cloudflared.trace.out", Usage: "Name of trace output file, generated when cloudflared stops.",
Usage: "Name of trace output file.",
EnvVars: []string{"TUNNEL_TRACE_OUTPUT"}, EnvVars: []string{"TUNNEL_TRACE_OUTPUT"},
Hidden: true,
}), }),
} }
app.Action = func(c *cli.Context) (err error) { app.Action = func(c *cli.Context) (err error) {
@ -406,23 +405,6 @@ func main() {
} }
func startServer(c *cli.Context, shutdownC, graceShutdownC chan struct{}) error { func startServer(c *cli.Context, shutdownC, graceShutdownC chan struct{}) error {
traceFile, err := os.Create(c.String("trace-output"))
if err != nil {
logger.WithError(err).Errorf("Failed to create trace output file %s", c.String("trace-output"))
return errors.Wrap(err, "Error creating trace output file")
}
defer func() {
if err := traceFile.Close(); err != nil {
logger.WithError(err).Error("Failed to close trace output file")
}
}()
if err := trace.Start(traceFile); err != nil {
logger.WithError(err).Error("Failed to start trace")
return errors.Wrap(err, "Error starting tracing")
}
defer trace.Stop()
var wg sync.WaitGroup var wg sync.WaitGroup
listeners := gracenet.Net{} listeners := gracenet.Net{}
errC := make(chan error) errC := make(chan error)
@ -442,6 +424,31 @@ func startServer(c *cli.Context, shutdownC, graceShutdownC chan struct{}) error
if err != nil { if err != nil {
return errors.Wrap(err, "Error configuring protocol logger") return errors.Wrap(err, "Error configuring protocol logger")
} }
if c.IsSet("trace-output") {
tmpTraceFile, err := ioutil.TempFile("", "trace")
if err != nil {
logger.WithError(err).Error("Failed to create new temporary file to save trace output")
}
defer func() {
if err := tmpTraceFile.Close(); err != nil {
logger.WithError(err).Error("Failed to close trace output file %s", tmpTraceFile.Name())
}
if err := os.Rename(tmpTraceFile.Name(), c.String("trace-output")); err != nil {
logger.WithError(err).Errorf("Failed to rename temporary trace output file %s to %s", tmpTraceFile.Name(), c.String("trace-output"))
} else {
os.Remove(tmpTraceFile.Name())
}
}()
if err := trace.Start(tmpTraceFile); err != nil {
logger.WithError(err).Error("Failed to start trace")
return errors.Wrap(err, "Error starting tracing")
}
defer trace.Stop()
}
if c.String("logfile") != "" { if c.String("logfile") != "" {
if err := initLogFile(c, logger, protoLogger); err != nil { if err := initLogFile(c, logger, protoLogger); err != nil {
logger.Error(err) logger.Error(err)