Skip to content

Commit

Permalink
Merge pull request #26 from boyand/master
Browse files Browse the repository at this point in the history
Fix rsp body closing on error and empty buffer flush to agent
  • Loading branch information
pglombardo authored Apr 26, 2017
2 parents a8be84f + b714887 commit 6ba0655
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 22 deletions.
3 changes: 2 additions & 1 deletion agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,10 +117,11 @@ func (r *agentS) fullRequestResponse(url string, method string, data interface{}
client := &http.Client{Timeout: 5 * time.Second}
resp, err = client.Do(req)
if err == nil {
defer resp.Body.Close()

if resp.StatusCode < 200 || resp.StatusCode >= 300 {
err = errors.New(resp.Status)
} else {
defer resp.Body.Close()

log.debug("agent response:", url, resp.Status)

Expand Down
47 changes: 26 additions & 21 deletions recorder.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,18 +145,21 @@ func collectLogs(rawSpan basictracer.RawSpan) map[uint64]map[string]interface{}
func (r *SpanRecorder) init() {
r.reset()

if !r.testMode {
ticker := time.NewTicker(1 * time.Second)
go func() {
for range ticker.C {
// Only attempt to send spans if we're announced.
if sensor.agent.canSend() {
log.debug("Sending spans to agent", len(r.spans))
r.send()
}
}
}()
if r.testMode {
return
}

ticker := time.NewTicker(1 * time.Second)
go func() {
for range ticker.C {
// Only attempt to send spans if we're announced and if the buffer is not empty
if sensor.agent.canSend() && len(r.spans) > 0 {
log.debug("Sending spans to agent", len(r.spans))
r.send()
}
}
}()

}

func (r *SpanRecorder) reset() {
Expand Down Expand Up @@ -217,23 +220,25 @@ func (r *SpanRecorder) RecordSpan(rawSpan basictracer.RawSpan) {
From: sensor.agent.from,
Data: &data})

if !r.testMode && (len(r.spans) == sensor.options.ForceTransmissionStartingAt) {
if r.testMode || !sensor.agent.canSend() {
return
}

if len(r.spans) >= sensor.options.ForceTransmissionStartingAt {
log.debug("Forcing spans to agent", len(r.spans))

r.send()
}
}

func (r *SpanRecorder) send() {
if sensor.agent.canSend() && !r.testMode {
go func() {
_, err := sensor.agent.request(sensor.agent.makeURL(AgentTracesURL), "POST", r.spans)
go func() {
_, err := sensor.agent.request(sensor.agent.makeURL(AgentTracesURL), "POST", r.spans)

r.reset()
r.reset()

if err != nil {
sensor.agent.reset()
}
}()
}
if err != nil {
sensor.agent.reset()
}
}()
}

0 comments on commit 6ba0655

Please sign in to comment.