Skip to content

Commit

Permalink
Copy span.id to documents with transaction.id (#112)
Browse files Browse the repository at this point in the history
* add span.id to documents that have transaction.id

* fix linting

* Update model/modelprocessor/transaction_span_id.go

Co-authored-by: kruskall <[email protected]>

* remove model processor

* set span.id wherever we can

* fix a few tests

* fix last failing test

---------

Co-authored-by: kruskall <[email protected]>
  • Loading branch information
SylvainJuge and kruskall authored Jul 19, 2023
1 parent 9c38d23 commit 88a3977
Show file tree
Hide file tree
Showing 13 changed files with 110 additions and 1 deletion.
9 changes: 9 additions & 0 deletions input/elasticapm/internal/modeldecoder/v2/decoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -482,6 +482,9 @@ func mapToErrorModel(from *errorEvent, event *modelpb.APMEvent) {
}
if from.TransactionID.IsSet() {
event.Transaction.Id = from.TransactionID.Val
event.Span = &modelpb.Span{
Id: from.TransactionID.Val,
}
}
if from.Transaction.IsSet() {
if from.Transaction.Sampled.IsSet() {
Expand Down Expand Up @@ -1341,6 +1344,9 @@ func mapToTransactionModel(from *transaction, event *modelpb.APMEvent) {
}
if from.ID.IsSet() {
out.Id = from.ID.Val
event.Span = &modelpb.Span{
Id: from.ID.Val,
}
}
if from.Marks.IsSet() {
out.Marks = make(map[string]*modelpb.TransactionMark, len(from.Marks.Events))
Expand Down Expand Up @@ -1472,6 +1478,9 @@ func mapToLogModel(from *log, event *modelpb.APMEvent) {
event.Transaction = &modelpb.Transaction{
Id: from.TransactionID.Val,
}
event.Span = &modelpb.Span{
Id: from.TransactionID.Val,
}
}
if from.SpanID.IsSet() {
event.Span = &modelpb.Span{
Expand Down
9 changes: 9 additions & 0 deletions input/elasticapm/internal/modeldecoder/v2/error_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -241,4 +241,13 @@ func TestDecodeMapToErrorModel(t *testing.T) {
mapToErrorModel(&input, &out)
assert.Equal(t, "12341231", out.Transaction.Id)
})

t.Run("transaction-span.id", func(t *testing.T) {
var input errorEvent
var out modelpb.APMEvent
input.TransactionID.Set("1234")
mapToErrorModel(&input, &out)
assert.Equal(t, "1234", out.Transaction.Id)
assert.Equal(t, "1234", out.Span.Id)
})
}
18 changes: 18 additions & 0 deletions input/elasticapm/internal/modeldecoder/v2/log_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -231,4 +231,22 @@ func TestDecodeMapToLogModel(t *testing.T) {
"float64": {Value: 1.1},
}, modelpb.NumericLabels(out.NumericLabels))
})

t.Run("transaction-id", func(t *testing.T) {
var input log
var out modelpb.APMEvent
input.TransactionID.Set("1234")
mapToLogModel(&input, &out)
assert.Equal(t, "1234", out.Transaction.Id)
assert.Equal(t, "1234", out.Span.Id)
})

t.Run("span-id", func(t *testing.T) {
var input log
var out modelpb.APMEvent
input.SpanID.Set("1234")
mapToLogModel(&input, &out)
assert.Equal(t, "1234", out.Span.Id)
})

}
2 changes: 1 addition & 1 deletion input/elasticapm/internal/modeldecoder/v2/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -979,7 +979,7 @@ type log struct {
TraceID nullable.String `json:"trace.id" validate:"maxLength=1024"`
// TransactionID holds the ID of the correlated transaction.
TransactionID nullable.String `json:"transaction.id" validate:"maxLength=1024"`
// SpanID holds the ID ID of the correlated span.
// SpanID holds the ID of the correlated span.
SpanID nullable.String `json:"span.id" validate:"maxLength=1024"`
// Message logged as part of the log. In case a parameterized message is
// captured, Message should contain the same information, but with any placeholders
Expand Down
9 changes: 9 additions & 0 deletions input/elasticapm/internal/modeldecoder/v2/transaction_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -672,4 +672,13 @@ func TestDecodeMapToTransactionModel(t *testing.T) {
RepresentativeCount: 1,
}, out.Span)
})

t.Run("transaction-span.id", func(t *testing.T) {
var input transaction
var out modelpb.APMEvent
input.ID.Set("1234")
mapToTransactionModel(&input, &out)
assert.Equal(t, "1234", out.Transaction.Id)
assert.Equal(t, "1234", out.Span.Id)
})
}
9 changes: 9 additions & 0 deletions input/otlp/exceptions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,9 @@ Caused by: LowLevelException
Type: transactionEvent.Transaction.Type,
Sampled: true,
},
Span: &modelpb.Span{
Id: transactionEvent.Transaction.Id,
},
Event: &modelpb.Event{Received: transactionEvent.Event.Received},
Error: &modelpb.Error{
Exception: &modelpb.Exception{
Expand Down Expand Up @@ -176,6 +179,9 @@ Caused by: LowLevelException
Type: transactionEvent.Transaction.Type,
Sampled: true,
},
Span: &modelpb.Span{
Id: transactionEvent.Transaction.Id,
},
Event: &modelpb.Event{Received: transactionEvent.Event.Received},
Error: &modelpb.Error{
Exception: &modelpb.Exception{
Expand Down Expand Up @@ -337,6 +343,9 @@ func TestEncodeSpanEventsNonJavaExceptions(t *testing.T) {
Type: transactionEvent.Transaction.Type,
Sampled: true,
},
Span: &modelpb.Span{
Id: transactionEvent.Transaction.Id,
},
Event: &modelpb.Event{Received: transactionEvent.Event.Received},
Error: &modelpb.Error{
Exception: &modelpb.Exception{
Expand Down
1 change: 1 addition & 0 deletions input/otlp/metadata_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,7 @@ func transformResourceMetadata(t *testing.T, resourceAttrs map[string]interface{
otelSpan.SetSpanID(pcommon.SpanID{2})
events := transformTraces(t, traces)
(*events)[0].Transaction = nil
(*events)[0].Span = nil
(*events)[0].Trace = nil
(*events)[0].Event = nil
(*events)[0].Timestamp = nil
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@
},
"name": "unknown"
},
"span": {
"id": "0000000041414646"
},
"timestamp": {
"us": 1576500418000768
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@
},
"name": "unknown"
},
"span": {
"id": "0000000041414646"
},
"timestamp": {
"us": 1576500418000768
},
Expand Down
3 changes: 3 additions & 0 deletions input/otlp/test_approved/metadata_jaeger.approved.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@
},
"name": "foo"
},
"span": {
"id": "0000000041414646"
},
"timestamp": {
"us": 1576500418000768
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@
},
"name": "unknown"
},
"span": {
"id": "41414646"
},
"timestamp": {
"us": 1576500418000768
},
Expand Down
30 changes: 30 additions & 0 deletions input/otlp/test_approved/transaction_jaeger_full.approved.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@
"name": "unknown",
"version": "1.0"
},
"span": {
"id": "0000000041414646"
},
"timestamp": {
"us": 1576500418000768
},
Expand Down Expand Up @@ -98,6 +101,9 @@
"name": "unknown",
"version": "1.0"
},
"span": {
"id": "0000000041414646"
},
"timestamp": {
"us": 1576500418000768
},
Expand Down Expand Up @@ -150,6 +156,9 @@
"name": "unknown",
"version": "1.0"
},
"span": {
"id": "0000000041414646"
},
"timestamp": {
"us": 1576500418000768
},
Expand Down Expand Up @@ -204,6 +213,9 @@
"name": "unknown",
"version": "1.0"
},
"span": {
"id": "0000000041414646"
},
"timestamp": {
"us": 1576500418000768
},
Expand Down Expand Up @@ -258,6 +270,9 @@
"name": "unknown",
"version": "1.0"
},
"span": {
"id": "0000000041414646"
},
"timestamp": {
"us": 1576500418000768
},
Expand Down Expand Up @@ -312,6 +327,9 @@
"name": "unknown",
"version": "1.0"
},
"span": {
"id": "0000000041414646"
},
"timestamp": {
"us": 1576500418000768
},
Expand Down Expand Up @@ -364,6 +382,9 @@
"name": "unknown",
"version": "1.0"
},
"span": {
"id": "0000000041414646"
},
"timestamp": {
"us": 1576500418000768
},
Expand Down Expand Up @@ -415,6 +436,9 @@
"name": "unknown",
"version": "1.0"
},
"span": {
"id": "0000000041414646"
},
"trace": {
"id": "00000000000000000000000046467830"
},
Expand Down Expand Up @@ -461,6 +485,9 @@
"name": "unknown",
"version": "1.0"
},
"span": {
"id": "0000000041414646"
},
"trace": {
"id": "00000000000000000000000046467830"
},
Expand Down Expand Up @@ -506,6 +533,9 @@
"name": "unknown",
"version": "1.0"
},
"span": {
"id": "0000000041414646"
},
"trace": {
"id": "00000000000000000000000046467830"
},
Expand Down
12 changes: 12 additions & 0 deletions input/otlp/traces.go
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,12 @@ func (c *Consumer) convertSpan(
Sampled: true,
RepresentativeCount: representativeCount,
}
if spanID != "" {
event.Span = &modelpb.Span{
Id: spanID,
}
}

TranslateTransaction(otelSpan.Attributes(), otelSpan.Status(), otelLibrary, event)
} else {
event.Span = &modelpb.Span{
Expand Down Expand Up @@ -1012,6 +1018,9 @@ func setErrorContext(out *modelpb.APMEvent, parent *modelpb.APMEvent) {
Sampled: parent.Transaction.Sampled,
Type: parent.Transaction.Type,
}
out.Span = &modelpb.Span{
Id: parent.Transaction.Id,
}
out.Error.Custom = parent.Transaction.Custom
out.ParentId = parent.Transaction.Id
}
Expand All @@ -1025,6 +1034,9 @@ func setLogContext(out *modelpb.APMEvent, parent *modelpb.APMEvent) {
out.Transaction = &modelpb.Transaction{
Id: parent.Transaction.Id,
}
out.Span = &modelpb.Span{
Id: parent.Transaction.Id,
}
}
if parent.Span != nil {
out.Span = &modelpb.Span{
Expand Down

0 comments on commit 88a3977

Please sign in to comment.