Skip to content

Commit dd3e8c5

Browse files
authored
Tidy up modelwriter (#888)
Some light refactoring to move "context" model building logic out of modelwriter and into the Context.build method. There remains some context model building in modelwriter (related to setting destination service type) because it requires additional information outside of "context", i.e. the span type.
1 parent 697d794 commit dd3e8c5

5 files changed

Lines changed: 54 additions & 41 deletions

File tree

context.go

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -22,23 +22,25 @@ import (
2222
"net/http"
2323

2424
"go.elastic.co/apm/internal/apmhttputil"
25+
"go.elastic.co/apm/internal/wildcard"
2526
"go.elastic.co/apm/model"
2627
)
2728

2829
// Context provides methods for setting transaction and error context.
2930
//
3031
// NOTE this is entirely unrelated to the standard library's context.Context.
3132
type Context struct {
32-
model model.Context
33-
request model.Request
34-
requestBody model.RequestBody
35-
requestSocket model.RequestSocket
36-
response model.Response
37-
user model.User
38-
service model.Service
39-
serviceFramework model.Framework
40-
captureHeaders bool
41-
captureBodyMask CaptureBodyMode
33+
model model.Context
34+
request model.Request
35+
requestBody model.RequestBody
36+
requestSocket model.RequestSocket
37+
response model.Response
38+
user model.User
39+
service model.Service
40+
serviceFramework model.Framework
41+
captureHeaders bool
42+
captureBodyMask CaptureBodyMode
43+
sanitizedFieldNames wildcard.Matchers
4244
}
4345

4446
func (c *Context) build() *model.Context {
@@ -52,6 +54,15 @@ func (c *Context) build() *model.Context {
5254
default:
5355
return nil
5456
}
57+
if len(c.sanitizedFieldNames) != 0 {
58+
if c.model.Request != nil {
59+
sanitizeRequest(c.model.Request, c.sanitizedFieldNames)
60+
}
61+
if c.model.Response != nil {
62+
sanitizeResponse(c.model.Response, c.sanitizedFieldNames)
63+
}
64+
65+
}
5566
return &c.model
5667
}
5768

error.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,7 @@ func (t *Tracer) newError() *Error {
148148
if e.recording {
149149
e.Timestamp = time.Now()
150150
e.Context.captureHeaders = instrumentationConfig.captureHeaders
151+
e.Context.sanitizedFieldNames = instrumentationConfig.sanitizedFieldNames
151152
e.stackTraceLimit = instrumentationConfig.stackTraceLimit
152153
}
153154

modelwriter.go

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -125,15 +125,6 @@ func (w *modelWriter) buildModelTransaction(out *model.Transaction, tx *Transact
125125
if sampled {
126126
out.Context = td.Context.build()
127127
}
128-
129-
if len(td.sanitizedFieldNames) != 0 && out.Context != nil {
130-
if out.Context.Request != nil {
131-
sanitizeRequest(out.Context.Request, td.sanitizedFieldNames)
132-
}
133-
if out.Context.Response != nil {
134-
sanitizeResponse(out.Context.Response, td.sanitizedFieldNames)
135-
}
136-
}
137128
}
138129

139130
func (w *modelWriter) buildModelSpan(out *model.Span, span *Span, sd *SpanData) {

sanitizer_test.go

Lines changed: 31 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import (
2222
"net/http"
2323
"testing"
2424

25+
"github.com/pkg/errors"
2526
"github.com/stretchr/testify/assert"
2627
"github.com/stretchr/testify/require"
2728

@@ -43,34 +44,46 @@ func TestSanitizeRequestResponse(t *testing.T) {
4344
req.AddCookie(c)
4445
}
4546

46-
tx, _, _ := apmtest.WithTransaction(func(ctx context.Context) {
47+
tx, _, errors := apmtest.WithTransaction(func(ctx context.Context) {
48+
e := apm.CaptureError(ctx, errors.New("boom!"))
49+
defer e.Send()
50+
4751
tx := apm.TransactionFromContext(ctx)
4852
tx.Context.SetHTTPRequest(req)
53+
e.Context.SetHTTPRequest(req)
4954

5055
h := make(http.Header)
5156
h.Add("Set-Cookie", (&http.Cookie{Name: "foo", Value: "bar"}).String())
5257
h.Add("Set-Cookie", (&http.Cookie{Name: "baz", Value: "qux"}).String())
5358
tx.Context.SetHTTPResponseHeaders(h)
5459
tx.Context.SetHTTPStatusCode(http.StatusTeapot)
60+
e.Context.SetHTTPResponseHeaders(h)
61+
e.Context.SetHTTPStatusCode(http.StatusTeapot)
5562
})
5663

57-
assert.Equal(t, tx.Context.Request.Cookies, model.Cookies{
58-
{Name: "Custom-Credit-Card-Number", Value: "[REDACTED]"},
59-
{Name: "secret", Value: "[REDACTED]"},
60-
{Name: "sessionid", Value: "[REDACTED]"},
61-
{Name: "user_id", Value: "456"},
62-
})
63-
assert.Equal(t, model.Headers{{
64-
Key: "Authorization",
65-
Values: []string{"[REDACTED]"},
66-
}}, tx.Context.Request.Headers)
67-
68-
// NOTE: the response includes multiple Set-Cookie headers,
69-
// but we only report a single "[REDACTED]" value.
70-
assert.Equal(t, model.Headers{{
71-
Key: "Set-Cookie",
72-
Values: []string{"[REDACTED]"},
73-
}}, tx.Context.Response.Headers)
64+
checkContext := func(context *model.Context) {
65+
assert.Equal(t, context.Request.Cookies, model.Cookies{
66+
{Name: "Custom-Credit-Card-Number", Value: "[REDACTED]"},
67+
{Name: "secret", Value: "[REDACTED]"},
68+
{Name: "sessionid", Value: "[REDACTED]"},
69+
{Name: "user_id", Value: "456"},
70+
})
71+
assert.Equal(t, model.Headers{{
72+
Key: "Authorization",
73+
Values: []string{"[REDACTED]"},
74+
}}, context.Request.Headers)
75+
76+
// NOTE: the response includes multiple Set-Cookie headers,
77+
// but we only report a single "[REDACTED]" value.
78+
assert.Equal(t, model.Headers{{
79+
Key: "Set-Cookie",
80+
Values: []string{"[REDACTED]"},
81+
}}, context.Response.Headers)
82+
}
83+
checkContext(tx.Context)
84+
for _, e := range errors {
85+
checkContext(e.Context)
86+
}
7487
}
7588

7689
func TestSetSanitizedFieldNamesNone(t *testing.T) {

transaction.go

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,6 @@ import (
2323
"math/rand"
2424
"sync"
2525
"time"
26-
27-
"go.elastic.co/apm/internal/wildcard"
2826
)
2927

3028
// StartTransaction returns a new Transaction with the specified
@@ -68,7 +66,7 @@ func (t *Tracer) StartTransactionOptions(name, transactionType string, opts Tran
6866
tx.stackTraceLimit = instrumentationConfig.stackTraceLimit
6967
tx.Context.captureHeaders = instrumentationConfig.captureHeaders
7068
tx.propagateLegacyHeader = instrumentationConfig.propagateLegacyHeader
71-
tx.sanitizedFieldNames = instrumentationConfig.sanitizedFieldNames
69+
tx.Context.sanitizedFieldNames = instrumentationConfig.sanitizedFieldNames
7270
tx.breakdownMetricsEnabled = t.breakdownMetrics.enabled
7371

7472
var root bool
@@ -346,7 +344,6 @@ type TransactionData struct {
346344
stackTraceLimit int
347345
breakdownMetricsEnabled bool
348346
propagateLegacyHeader bool
349-
sanitizedFieldNames wildcard.Matchers
350347
timestamp time.Time
351348

352349
mu sync.Mutex

0 commit comments

Comments
 (0)