-
Notifications
You must be signed in to change notification settings - Fork 16
[enrichments] Add support for inferred spans #116
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
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 |
|---|---|---|
|
|
@@ -26,6 +26,7 @@ import ( | |
|
|
||
| "github.com/elastic/opentelemetry-lib/enrichments/trace/config" | ||
| "github.com/google/go-cmp/cmp" | ||
| "github.com/open-telemetry/opentelemetry-collector-contrib/pkg/pdatatest/ptracetest" | ||
| "github.com/stretchr/testify/assert" | ||
| "go.opentelemetry.io/collector/pdata/pcommon" | ||
| "go.opentelemetry.io/collector/pdata/ptrace" | ||
|
|
@@ -48,10 +49,11 @@ func TestElasticTransactionEnrich(t *testing.T) { | |
| return span | ||
| } | ||
| for _, tc := range []struct { | ||
| name string | ||
| input ptrace.Span | ||
| config config.ElasticTransactionConfig | ||
| enrichedAttrs map[string]any | ||
| name string | ||
| input ptrace.Span | ||
| config config.ElasticTransactionConfig | ||
| enrichedAttrs map[string]any | ||
| expectedSpanLinks *ptrace.SpanLinkSlice | ||
| }{ | ||
| { | ||
| // test case gives a summary of what is emitted by default | ||
|
|
@@ -319,20 +321,67 @@ func TestElasticTransactionEnrich(t *testing.T) { | |
| AttributeTransactionType: "messaging", | ||
| }, | ||
| }, | ||
| { | ||
| name: "inferred_spans", | ||
| input: func() ptrace.Span { | ||
| span := getElasticTxn() | ||
| span.SetName("testtxn") | ||
| span.SetSpanID([8]byte{1}) | ||
| normalLink := span.Links().AppendEmpty() | ||
| normalLink.SetSpanID([8]byte{2}) | ||
|
|
||
| childLink := span.Links().AppendEmpty() | ||
| childLink.SetSpanID([8]byte{3}) | ||
| childLink.Attributes().PutBool("is_child", true) | ||
|
|
||
| childLink2 := span.Links().AppendEmpty() | ||
| childLink2.SetSpanID([8]byte{4}) | ||
| childLink2.Attributes().PutBool("elastic.is_child", true) | ||
| return span | ||
| }(), | ||
| config: config.Enabled().Transaction, | ||
| enrichedAttrs: map[string]any{ | ||
| AttributeTimestampUs: startTs.AsTime().UnixMicro(), | ||
| AttributeTransactionSampled: true, | ||
| AttributeTransactionRoot: true, | ||
| AttributeTransactionID: "0100000000000000", | ||
| AttributeTransactionName: "testtxn", | ||
| AttributeProcessorEvent: "transaction", | ||
| AttributeTransactionRepresentativeCount: float64(1), | ||
| AttributeTransactionDurationUs: expectedDuration.Microseconds(), | ||
| AttributeEventOutcome: "success", | ||
| AttributeSuccessCount: int64(1), | ||
| AttributeTransactionResult: "Success", | ||
| AttributeTransactionType: "unknown", | ||
| AttributeChildIDs: []any{"0300000000000000", "0400000000000000"}, | ||
| }, | ||
| expectedSpanLinks: func() *ptrace.SpanLinkSlice { | ||
| spanLinks := ptrace.NewSpanLinkSlice() | ||
| // Only the span link without `is_child` or `elastic.is_child` is expected | ||
| spanLinks.AppendEmpty().SetSpanID([8]byte{2}) | ||
| return &spanLinks | ||
| }(), | ||
| }, | ||
| } { | ||
| t.Run(tc.name, func(t *testing.T) { | ||
| // Merge existing input attrs with the attrs added | ||
| // by enrichment to get the expected attributes. | ||
| expectedAttrs := tc.input.Attributes().AsRaw() | ||
| expectedSpan := ptrace.NewSpan() | ||
| tc.input.CopyTo(expectedSpan) | ||
|
|
||
| // Merge with the expected attributes and override the span links. | ||
| for k, v := range tc.enrichedAttrs { | ||
| expectedAttrs[k] = v | ||
| expectedSpan.Attributes().PutEmpty(k).FromRaw(v) | ||
| } | ||
| // Override span links | ||
| if tc.expectedSpanLinks != nil { | ||
| tc.expectedSpanLinks.CopyTo(expectedSpan.Links()) | ||
| } else { | ||
| expectedSpan.Links().RemoveIf(func(_ ptrace.SpanLink) bool { return true }) | ||
|
Comment on lines
+375
to
+378
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [For reviewers] This piece is a bit weird because CopyTo doesn't handle non initialized empty span links. |
||
| } | ||
|
|
||
| EnrichSpan(tc.input, config.Config{ | ||
| Transaction: tc.config, | ||
| }) | ||
|
|
||
| assert.Empty(t, cmp.Diff(expectedAttrs, tc.input.Attributes().AsRaw())) | ||
| assert.NoError(t, ptracetest.CompareSpan(expectedSpan, tc.input)) | ||
| }) | ||
| } | ||
| } | ||
|
|
@@ -351,10 +400,11 @@ func TestElasticSpanEnrich(t *testing.T) { | |
| return span | ||
| } | ||
| for _, tc := range []struct { | ||
| name string | ||
| input ptrace.Span | ||
| config config.ElasticSpanConfig | ||
| enrichedAttrs map[string]any | ||
| name string | ||
| input ptrace.Span | ||
| config config.ElasticSpanConfig | ||
| enrichedAttrs map[string]any | ||
| expectedSpanLinks *ptrace.SpanLinkSlice | ||
| }{ | ||
| { | ||
| // test case gives a summary of what is emitted by default | ||
|
|
@@ -827,20 +877,63 @@ func TestElasticSpanEnrich(t *testing.T) { | |
| AttributeSpanDestinationServiceResource: "testsvc", | ||
| }, | ||
| }, | ||
| { | ||
| name: "inferred_spans", | ||
| input: func() ptrace.Span { | ||
| span := getElasticSpan() | ||
| span.SetName("testspan") | ||
| span.SetSpanID([8]byte{1}) | ||
| normalLink := span.Links().AppendEmpty() | ||
| normalLink.SetSpanID([8]byte{2}) | ||
|
|
||
| childLink := span.Links().AppendEmpty() | ||
| childLink.SetSpanID([8]byte{3}) | ||
| childLink.Attributes().PutBool("is_child", true) | ||
|
|
||
| childLink2 := span.Links().AppendEmpty() | ||
| childLink2.SetSpanID([8]byte{4}) | ||
| childLink2.Attributes().PutBool("elastic.is_child", true) | ||
| return span | ||
| }(), | ||
| config: config.Enabled().Span, | ||
| enrichedAttrs: map[string]any{ | ||
| AttributeTimestampUs: startTs.AsTime().UnixMicro(), | ||
| AttributeSpanName: "testspan", | ||
| AttributeProcessorEvent: "span", | ||
| AttributeSpanRepresentativeCount: float64(1), | ||
| AttributeSpanType: "unknown", | ||
| AttributeSpanDurationUs: expectedDuration.Microseconds(), | ||
| AttributeEventOutcome: "success", | ||
| AttributeSuccessCount: int64(1), | ||
| AttributeChildIDs: []any{"0300000000000000", "0400000000000000"}, | ||
| }, | ||
| expectedSpanLinks: func() *ptrace.SpanLinkSlice { | ||
| spanLinks := ptrace.NewSpanLinkSlice() | ||
| // Only the span link without `is_child` or `elastic.is_child` is expected | ||
| spanLinks.AppendEmpty().SetSpanID([8]byte{2}) | ||
| return &spanLinks | ||
| }(), | ||
| }, | ||
| } { | ||
| t.Run(tc.name, func(t *testing.T) { | ||
| // Merge existing input attrs with the attrs added | ||
| // by enrichment to get the expected attributes. | ||
| expectedAttrs := tc.input.Attributes().AsRaw() | ||
| expectedSpan := ptrace.NewSpan() | ||
| tc.input.CopyTo(expectedSpan) | ||
|
|
||
| // Merge with the expected attributes and override the span links. | ||
| for k, v := range tc.enrichedAttrs { | ||
| expectedAttrs[k] = v | ||
| expectedSpan.Attributes().PutEmpty(k).FromRaw(v) | ||
| } | ||
| // Override span links | ||
| if tc.expectedSpanLinks != nil { | ||
| tc.expectedSpanLinks.CopyTo(expectedSpan.Links()) | ||
| } else { | ||
| expectedSpan.Links().RemoveIf(func(_ ptrace.SpanLink) bool { return true }) | ||
| } | ||
|
|
||
| EnrichSpan(tc.input, config.Config{ | ||
| Span: tc.config, | ||
| }) | ||
|
|
||
| assert.Empty(t, cmp.Diff(expectedAttrs, tc.input.Attributes().AsRaw())) | ||
| assert.NoError(t, ptracetest.CompareSpan(expectedSpan, tc.input)) | ||
| }) | ||
| } | ||
| } | ||
|
|
||
This file contains hidden or 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 hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[For reviewers] In APM data we add an empty string in the slice if span ID is empty (ref), however, I didn't think it was important so didn't handle it here. Let me know if it needs to be added.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, that was not intentionally done in apm-data for that code path. I don't think span links are allowed to have an empty span ID in OTLP?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I am not sure about this, I kept the span ID check for now, let me know if you think we should remove this.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just wanted to agree with you here. I'd keep the span ID check as is just as a safety measure.