Skip to content

Commit 560cc4b

Browse files
authored
Add field for span http request body to IntakeV2 (#366)
* Added intakeV2 context.http.request.body field * Added body to existing modeljson test * Switch body from string to any value (string or dictionary) * Switch field order for lint
1 parent e8b8d0a commit 560cc4b

File tree

6 files changed

+31
-4
lines changed

6 files changed

+31
-4
lines changed

input/elasticapm/docs/spec/v2/span.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,9 @@
188188
"object"
189189
],
190190
"properties": {
191+
"body": {
192+
"description": "The http request body usually as a string, but may be a dictionary for multipart/form-data content"
193+
},
191194
"id": {
192195
"description": "ID holds the unique identifier for the http request.",
193196
"type": [

input/elasticapm/internal/modeldecoder/v2/decoder.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1071,14 +1071,19 @@ func mapToSpanModel(from *span, event *modelpb.APMEvent) {
10711071
}
10721072
event.Http.Request.Method = from.Context.HTTP.Method.Val
10731073
}
1074-
if from.Context.HTTP.Request.ID.IsSet() {
1074+
if from.Context.HTTP.Request.IsSet() {
10751075
if event.Http == nil {
10761076
event.Http = &modelpb.HTTP{}
10771077
}
10781078
if event.Http.Request == nil {
10791079
event.Http.Request = &modelpb.HTTPRequest{}
10801080
}
1081-
event.Http.Request.Id = from.Context.HTTP.Request.ID.Val
1081+
if from.Context.HTTP.Request.ID.IsSet() {
1082+
event.Http.Request.Id = from.Context.HTTP.Request.ID.Val
1083+
}
1084+
if from.Context.HTTP.Request.Body.IsSet() {
1085+
event.Http.Request.Body = modeldecoderutil.ToValue(from.Context.HTTP.Request.Body.Val)
1086+
}
10821087
}
10831088
if from.Context.HTTP.Response.IsSet() {
10841089
if event.Http == nil {

input/elasticapm/internal/modeldecoder/v2/model.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -843,6 +843,8 @@ type spanContextHTTP struct {
843843
}
844844

845845
type spanContextHTTPRequest struct {
846+
// The http request body usually as a string, but may be a dictionary for multipart/form-data content
847+
Body nullable.Interface `json:"body"`
846848
// ID holds the unique identifier for the http request.
847849
ID nullable.String `json:"id"`
848850
}

input/elasticapm/internal/modeldecoder/v2/model_generated.go

Lines changed: 2 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

input/elasticapm/internal/modeldecoder/v2/span_test.go

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ import (
2525
"testing"
2626
"time"
2727

28+
"google.golang.org/protobuf/types/known/structpb"
29+
2830
"github.com/google/go-cmp/cmp"
2931
"github.com/stretchr/testify/assert"
3032
"github.com/stretchr/testify/require"
@@ -258,14 +260,22 @@ func TestDecodeMapToSpanModel(t *testing.T) {
258260
}
259261
})
260262

261-
t.Run("http-request", func(t *testing.T) {
263+
t.Run("http-request-id", func(t *testing.T) {
262264
var input span
263265
input.Context.HTTP.Request.ID.Set("some-request-id")
264266
var out modelpb.APMEvent
265267
mapToSpanModel(&input, &out)
266268
assert.Equal(t, "some-request-id", out.Http.Request.Id)
267269
})
268270

271+
t.Run("http-request-body", func(t *testing.T) {
272+
var input span
273+
input.Context.HTTP.Request.Body.Set("some-request-body")
274+
var out modelpb.APMEvent
275+
mapToSpanModel(&input, &out)
276+
assert.Equal(t, structpb.NewStringValue("some-request-body"), out.Http.Request.Body)
277+
})
278+
269279
t.Run("http-headers", func(t *testing.T) {
270280
var input span
271281
input.Context.HTTP.Response.Headers.Set(http.Header{"a": []string{"b", "c"}})

model/modeljson/http.pb.json_test.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ package modeljson
2020
import (
2121
"testing"
2222

23+
"google.golang.org/protobuf/types/known/structpb"
24+
2325
modeljson "github.com/elastic/apm-data/model/modeljson/internal"
2426
"github.com/elastic/apm-data/model/modelpb"
2527
"github.com/google/go-cmp/cmp"
@@ -51,6 +53,7 @@ func TestHTTPToModelJSON(t *testing.T) {
5153
Id: "id",
5254
Method: "method",
5355
Referrer: "referrer",
56+
Body: structpb.NewStringValue("request-body"),
5457
},
5558
},
5659
expected: &modeljson.HTTP{
@@ -61,6 +64,9 @@ func TestHTTPToModelJSON(t *testing.T) {
6164
ID: "id",
6265
Method: "method",
6366
Referrer: "referrer",
67+
Body: &modeljson.HTTPRequestBody{
68+
Original: "request-body",
69+
},
6470
},
6571
},
6672
},

0 commit comments

Comments
 (0)