Skip to content

Commit

Permalink
ddtrace/tracer: add interchangeable writer to logTraceWriter (#939)
Browse files Browse the repository at this point in the history
This change adds an interchangeable writer to logTraceWriter, to facilitate tests and reduce noise.
  • Loading branch information
knusbaum authored Jul 5, 2021
1 parent 1389fb2 commit 5f6503d
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
10 changes: 10 additions & 0 deletions ddtrace/tracer/tracer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,14 @@ loop:
}
}

// setLogWriter sets the io.Writer that any new logTraceWriter will write to and returns a function
// which will return the io.Writer to its original value.
func setLogWriter(w io.Writer) func() {
tmp := logWriter
logWriter = w
return func() { logWriter = tmp }
}

// TestTracerCleanStop does frenetic testing in a scenario where the tracer is started
// and stopped in parallel with spans being created.
func TestTracerCleanStop(t *testing.T) {
Expand Down Expand Up @@ -89,10 +97,12 @@ func TestTracerCleanStop(t *testing.T) {
}()
}

defer setLogWriter(ioutil.Discard)()
wg.Add(1)
go func() {
defer wg.Done()
for i := 0; i < n; i++ {
// Lambda mode is used to avoid the startup cost associated with agent discovery.
Start(withTransport(transport), WithLambdaMode(true))
time.Sleep(time.Millisecond)
Start(withTransport(transport), WithLambdaMode(true), WithSampler(NewRateSampler(0.99)))
Expand Down
5 changes: 4 additions & 1 deletion ddtrace/tracer/writer.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,9 @@ func (h *agentTraceWriter) flush() {
h.payload = newPayload()
}

// logWriter specifies the output target of the logTraceWriter; replaced in tests.
var logWriter io.Writer = os.Stdout

// logTraceWriter encodes traces into a format understood by the Datadog Forwarder
// (https://github.com/DataDog/datadog-serverless-functions/tree/master/aws/logs_monitoring)
// and writes them to os.Stdout. This is used to send traces from an AWS Lambda environment.
Expand All @@ -117,7 +120,7 @@ type logTraceWriter struct {
func newLogTraceWriter(c *config) *logTraceWriter {
w := &logTraceWriter{
config: c,
w: os.Stdout,
w: logWriter,
}
w.resetBuffer()
return w
Expand Down

0 comments on commit 5f6503d

Please sign in to comment.