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

module/apmot: fix nil-pointer deref in Inject #763

Merged
merged 2 commits into from
May 6, 2020
Merged
Show file tree
Hide file tree
Changes from all 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.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ https://github.com/elastic/apm-agent-go/compare/v1.7.2...master[View commits]
- Add "recording" config option, to dynamically disable event recording {pull}737[(#737)]
- Enable central configuration of "stack_frames_min_duration" and "stack_trace_limit" {pull}742[(#742)]
- Implement "CloseIdleConnections" on the Elasticsearch RoundTripper {pull}750[(#750)]
- Fix apmot nil pointer dereference in Tracer.Inject {pull}763[(#763)]

[[release-notes-1.x]]
=== Go Agent version 1.x
Expand Down
3 changes: 1 addition & 2 deletions module/apmot/tracer.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,10 +107,9 @@ func (t *otTracer) Inject(sc opentracing.SpanContext, format interface{}, carrie
if !ok {
return opentracing.ErrInvalidCarrier
}
tx := spanContext.Transaction()
headerValue := apmhttp.FormatTraceparentHeader(spanContext.traceContext)
writer.Set(apmhttp.W3CTraceparentHeader, headerValue)
if tx.ShouldPropagateLegacyHeader() {
if t.tracer.ShouldPropagateLegacyHeader() {
writer.Set(apmhttp.ElasticTraceparentHeader, headerValue)
}
if tracestate := spanContext.traceContext.State.String(); tracestate != "" {
Expand Down
38 changes: 38 additions & 0 deletions module/apmot/tracer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,10 @@ package apmot_test
import (
"context"
"errors"
"net/http"
"net/url"
"os"
"strconv"
"testing"
"time"

Expand Down Expand Up @@ -363,6 +366,41 @@ func TestSpanFinishWithOptionsLogs(t *testing.T) {
assert.Equal(t, model.Time(spanFinish), errors[1].Timestamp)
}

func TestTraceInjectExtract(t *testing.T) {
testTraceInjectExtract(t, true)
testTraceInjectExtract(t, false)
}

func testTraceInjectExtract(t *testing.T, shouldPropagateLegacyHeader bool) {
os.Setenv("ELASTIC_APM_USE_ELASTIC_TRACEPARENT_HEADER", strconv.FormatBool(shouldPropagateLegacyHeader))
defer os.Unsetenv("ELASTIC_APM_USE_ELASTIC_TRACEPARENT_HEADER")

tracer, apmtracer, _ := newTestTracer()
defer apmtracer.Close()

span := tracer.StartSpan("span")
headers1 := make(http.Header)
carrier1 := opentracing.HTTPHeadersCarrier(headers1)

err := tracer.Inject(span.Context(), opentracing.HTTPHeaders, carrier1)
require.NoError(t, err)

spanContext, err := tracer.Extract(opentracing.HTTPHeaders, carrier1)
require.NoError(t, err)

headers2 := make(http.Header)
carrier2 := opentracing.HTTPHeadersCarrier(headers2)
err = tracer.Inject(spanContext, opentracing.HTTPHeaders, carrier2)
require.NoError(t, err)

assert.Equal(t, headers1, headers2)
if shouldPropagateLegacyHeader {
assert.Contains(t, headers1, "Elastic-Apm-Traceparent")
} else {
assert.NotContains(t, headers1, "Elastic-Apm-Traceparent")
}
}

func BenchmarkSpanSetSpanContext(b *testing.B) {
tags := opentracing.Tags{
"component": "myComponent",
Expand Down
10 changes: 10 additions & 0 deletions tracer.go
Original file line number Diff line number Diff line change
Expand Up @@ -503,6 +503,16 @@ func (t *Tracer) Active() bool {
return atomic.LoadInt32(&t.active) == 1
}

// ShouldPropagateLegacyHeader reports whether instrumentation should
// propagate the legacy "Elastic-Apm-Traceparent" header in addition to
// the standard W3C "traceparent" header.
//
// This method will be removed in a future major version when we remove
// support for propagating the legacy header.
func (t *Tracer) ShouldPropagateLegacyHeader() bool {
return t.instrumentationConfig().propagateLegacyHeader
}

// SetRequestDuration sets the maximum amount of time to keep a request open
// to the APM server for streaming data before closing the stream and starting
// a new request.
Expand Down