Skip to content

Commit

Permalink
chore(zipkin-exporter): relay on the status code for the request but …
Browse files Browse the repository at this point in the history
…still read the response body. (open-telemetry#1328)

* chore(zipkin-exporter): relay on the status code for the request but still read the response body.

* fix(zipkin-exporter): fix tests.

* chore(zipkin-exporter): adds changelog.

* chore: 202 -> http.StatusAccepted

Co-authored-by: Chris Bandy <bandy.chris@gmail.com>

* chore: 202 -> http.StatusAccepted

Co-authored-by: Anthony Mirabella <a9@aneurysm9.com>

Co-authored-by: Chris Bandy <bandy.chris@gmail.com>
Co-authored-by: Anthony Mirabella <a9@aneurysm9.com>
Co-authored-by: Tyler Yahn <MrAlias@users.noreply.github.com>
  • Loading branch information
4 people committed Dec 3, 2020
1 parent 66db2d8 commit 787e3f4
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 14 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm

- Move the OpenCensus example into `example` directory. (#1359)
- `NewExporter` and `Start` functions in `go.opentelemetry.io/otel/exporters/otlp` now receive `context.Context` as a first parameter. (#1357)
- Zipkin exporter relies on the status code for success rather than body read but still read the response body. (#1328)

## [0.14.0] - 2020-11-19

Expand Down
25 changes: 14 additions & 11 deletions exporters/trace/zipkin/zipkin.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"encoding/json"
"errors"
"fmt"
"io"
"io/ioutil"
"log"
"net/http"
Expand Down Expand Up @@ -112,14 +113,14 @@ func NewRawExporter(collectorURL, serviceName string, opts ...Option) (*Exporter
// NewExportPipeline sets up a complete export pipeline
// with the recommended setup for trace provider
func NewExportPipeline(collectorURL, serviceName string, opts ...Option) (*sdktrace.TracerProvider, error) {
exp, err := NewRawExporter(collectorURL, serviceName, opts...)
exporter, err := NewRawExporter(collectorURL, serviceName, opts...)
if err != nil {
return nil, err
}

tp := sdktrace.NewTracerProvider(sdktrace.WithBatcher(exp))
if exp.o.config != nil {
tp.ApplyConfig(*exp.o.config)
tp := sdktrace.NewTracerProvider(sdktrace.WithBatcher(exporter))
if exporter.o.config != nil {
tp.ApplyConfig(*exporter.o.config)
}

return tp, err
Expand Down Expand Up @@ -166,19 +167,21 @@ func (e *Exporter) ExportSpans(ctx context.Context, batch []*export.SpanData) er
if err != nil {
return e.errf("request to %s failed: %v", e.url, err)
}
e.logf("zipkin responded with status %d", resp.StatusCode)
defer resp.Body.Close()

_, err = ioutil.ReadAll(resp.Body)
// Zipkin API returns a 202 on success and the content of the body isn't interesting
// but it is still being read because according to https://golang.org/pkg/net/http/#Response
// > The default HTTP client's Transport may not reuse HTTP/1.x "keep-alive" TCP connections
// > if the Body is not read to completion and closed.
_, err = io.Copy(ioutil.Discard, resp.Body)
if err != nil {
// Best effort to clean up here.
resp.Body.Close()
return e.errf("failed to read response body: %v", err)
}

err = resp.Body.Close()
if err != nil {
return e.errf("failed to close response body: %v", err)
if resp.StatusCode != http.StatusAccepted {
return e.errf("failed to send spans to zipkin server with status %d", resp.StatusCode)
}

return nil
}

Expand Down
5 changes: 2 additions & 3 deletions exporters/trace/zipkin/zipkin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,7 @@ func (c *mockZipkinCollector) handler(w http.ResponseWriter, r *http.Request) {
c.lock.Lock()
defer c.lock.Unlock()
c.models = append(c.models, models...)
w.WriteHeader(http.StatusAccepted)
}

func (c *mockZipkinCollector) Close() {
Expand Down Expand Up @@ -340,17 +341,15 @@ func TestExportSpans(t *testing.T) {
ctx := context.Background()
require.Len(t, ls.Messages, 0)
require.NoError(t, exporter.ExportSpans(ctx, spans[0:1]))
require.Len(t, ls.Messages, 2)
require.Len(t, ls.Messages, 1)
require.Contains(t, ls.Messages[0], "send a POST request")
require.Contains(t, ls.Messages[1], "zipkin responded")
ls.Messages = nil
require.NoError(t, exporter.ExportSpans(ctx, nil))
require.Len(t, ls.Messages, 1)
require.Contains(t, ls.Messages[0], "no spans to export")
ls.Messages = nil
require.NoError(t, exporter.ExportSpans(ctx, spans[1:2]))
require.Contains(t, ls.Messages[0], "send a POST request")
require.Contains(t, ls.Messages[1], "zipkin responded")
checkFunc := func() bool {
return collector.ModelsLen() == len(models)
}
Expand Down

0 comments on commit 787e3f4

Please sign in to comment.