-
Notifications
You must be signed in to change notification settings - Fork 525
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
processor: Avoid leaking labels between batches (#8081)
Fixes a bug introduced in #6682 by performing a copy of the `baseEvent` before each of the events is decoded. The event was being shallow copied and it was reusing the same maps between batches. The linked PR changed the behavior (although not in a very obviously) and was instead merging the labels onto the passed `APMEvent`. This patch reintroduces the previous behavior in a more obvious manner. Signed-off-by: Marc Lopez Rubio <[email protected]> Co-authored-by: stuart nelson <[email protected]>
- Loading branch information
1 parent
f23d051
commit b782e9e
Showing
13 changed files
with
86 additions
and
131 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -24,6 +24,7 @@ import ( | |
"io/ioutil" | ||
"net" | ||
"path/filepath" | ||
"strings" | ||
"testing" | ||
"testing/iotest" | ||
"time" | ||
|
@@ -268,6 +269,44 @@ func TestRUMV3(t *testing.T) { | |
} | ||
} | ||
|
||
func TestLabelLeak(t *testing.T) { | ||
payload := `{"metadata": {"service": {"name": "testsvc", "environment": "staging", "version": null, "agent": {"name": "python", "version": "6.9.1"}, "language": {"name": "python", "version": "3.10.4"}, "runtime": {"name": "CPython", "version": "3.10.4"}, "framework": {"name": "flask", "version": "2.1.1"}}, "process": {"pid": 2112739, "ppid": 2112738, "argv": ["/home/stuart/workspace/sdh/581/venv/lib/python3.10/site-packages/flask/__main__.py", "run"], "title": null}, "system": {"hostname": "slaptop", "architecture": "x86_64", "platform": "linux"}, "labels": {"ci_commit": "unknown", "numeric": 1}}} | ||
{"transaction": {"id": "88dee29a6571b948", "trace_id": "ba7f5d18ac4c7f39d1ff070c79b2bea5", "name": "GET /withlabels", "type": "request", "duration": 1.6199999999999999, "result": "HTTP 2xx", "timestamp": 1652185276804681, "outcome": "success", "sampled": true, "span_count": {"started": 0, "dropped": 0}, "sample_rate": 1.0, "context": {"request": {"env": {"REMOTE_ADDR": "127.0.0.1", "SERVER_NAME": "127.0.0.1", "SERVER_PORT": "5000"}, "method": "GET", "socket": {"remote_address": "127.0.0.1"}, "cookies": {}, "headers": {"host": "localhost:5000", "user-agent": "curl/7.81.0", "accept": "*/*", "app-os": "Android", "content-type": "application/json; charset=utf-8", "content-length": "29"}, "url": {"full": "http://localhost:5000/withlabels?second_with_labels", "protocol": "http:", "hostname": "localhost", "pathname": "/withlabels", "port": "5000", "search": "?second_with_labels"}}, "response": {"status_code": 200, "headers": {"Content-Type": "application/json", "Content-Length": "14"}}, "tags": {"appOs": "Android", "email_set": "[email protected]", "time_set": 1652185276}}}} | ||
{"transaction": {"id": "ba5c6d6c1ab44bd1", "trace_id": "88c0a00431531a80c5ca9a41fe115f41", "name": "GET /nolabels", "type": "request", "duration": 0.652, "result": "HTTP 2xx", "timestamp": 1652185278813952, "outcome": "success", "sampled": true, "span_count": {"started": 0, "dropped": 0}, "sample_rate": 1.0, "context": {"request": {"env": {"REMOTE_ADDR": "127.0.0.1", "SERVER_NAME": "127.0.0.1", "SERVER_PORT": "5000"}, "method": "GET", "socket": {"remote_address": "127.0.0.1"}, "cookies": {}, "headers": {"host": "localhost:5000", "user-agent": "curl/7.81.0", "accept": "*/*"}, "url": {"full": "http://localhost:5000/nolabels?third_no_label", "protocol": "http:", "hostname": "localhost", "pathname": "/nolabels", "port": "5000", "search": "?third_no_label"}}, "response": {"status_code": 200, "headers": {"Content-Type": "text/html; charset=utf-8", "Content-Length": "14"}}, "tags": {}}}}` | ||
|
||
baseEvent := model.APMEvent{ | ||
Host: model.Host{IP: []net.IP{net.ParseIP("192.0.0.1")}}, | ||
} | ||
|
||
var processed *model.Batch | ||
batchProcessor := model.ProcessBatchFunc(func(_ context.Context, b *model.Batch) error { | ||
processed = b | ||
return nil | ||
}) | ||
|
||
p := BackendProcessor(&config.Config{MaxEventSize: 100 * 1024}, make(chan struct{}, 1)) | ||
var actualResult Result | ||
err := p.HandleStream(context.Background(), baseEvent, strings.NewReader(payload), 10, batchProcessor, &actualResult) | ||
require.NoError(t, err) | ||
|
||
txs := *processed | ||
assert.Len(t, txs, 2) | ||
// Assert first tx | ||
assert.Equal(t, model.NumericLabels{ | ||
"time_set": {Value: 1652185276}, | ||
"numeric": {Value: 1}, | ||
}, txs[0].NumericLabels) | ||
assert.Equal(t, model.Labels{ | ||
"appOs": {Value: "Android"}, | ||
"email_set": {Value: "[email protected]"}, | ||
"ci_commit": {Value: "unknown"}, | ||
}, txs[0].Labels) | ||
|
||
// Assert second tx | ||
assert.Equal(t, model.NumericLabels{"numeric": {Value: 1}}, txs[1].NumericLabels) | ||
assert.Equal(t, model.Labels{"ci_commit": {Value: "unknown"}}, txs[1].Labels) | ||
} | ||
|
||
func makeApproveEventsBatchProcessor(t *testing.T, name string, count *int) model.BatchProcessor { | ||
return model.ProcessBatchFunc(func(ctx context.Context, b *model.Batch) error { | ||
events := b.Transform(ctx) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.