Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 6 commits into from
Dec 3, 2020
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.
MrAlias marked this conversation as resolved.
Show resolved Hide resolved
_, 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")
MrAlias marked this conversation as resolved.
Show resolved Hide resolved
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