-
Notifications
You must be signed in to change notification settings - Fork 2.5k
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
[v2][adjuster] Implement ip attribute adjuster to operate on otlp data model #6355
Merged
+165
−7
Merged
Changes from 2 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
cb986d4
Use Consistent Parameter Naming
mahadzaryab1 2781b35
Implement IP Attribute Adjuster
mahadzaryab1 1a46df9
Fix Linting
mahadzaryab1 8309531
Adjust Attributes of Resource
mahadzaryab1 ccaf396
Add More Tests
mahadzaryab1 a4b3ae6
Make Adjuster Struct To Simplify Body
mahadzaryab1 00d1835
Clean Up Test File
mahadzaryab1 602d9c7
Fix Test
mahadzaryab1 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 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 |
---|---|---|
@@ -0,0 +1,72 @@ | ||
package adjuster | ||
|
||
import ( | ||
"bytes" | ||
"encoding/binary" | ||
"strconv" | ||
|
||
"go.opentelemetry.io/collector/pdata/pcommon" | ||
"go.opentelemetry.io/collector/pdata/ptrace" | ||
) | ||
|
||
var ipAttributesToCorrect = map[string]struct{}{ | ||
"ip": {}, | ||
"peer.ipv4": {}, | ||
} | ||
|
||
// IPAttribute returns an adjuster that replaces numeric "ip" attributes, | ||
// which usually contain IPv4 packed into uint32, with their string | ||
// representation (e.g. "8.8.8.8""). | ||
func IPAttribute() Adjuster { | ||
adjustAttributes := func(attributes pcommon.Map) { | ||
adjusted := make(map[string]string) | ||
|
||
attributes.Range(func(k string, v pcommon.Value) bool { | ||
if _, ok := ipAttributesToCorrect[k]; !ok { | ||
return true | ||
} | ||
var value uint32 | ||
switch v.Type() { | ||
case pcommon.ValueTypeInt: | ||
//nolint: gosec // G115 | ||
value = uint32(v.Int()) | ||
case pcommon.ValueTypeDouble: | ||
value = uint32(v.Double()) | ||
default: | ||
return true | ||
} | ||
var buf [4]byte | ||
binary.BigEndian.PutUint32(buf[:], value) | ||
var sBuf bytes.Buffer | ||
for i, b := range buf { | ||
if i > 0 { | ||
sBuf.WriteRune('.') | ||
} | ||
sBuf.WriteString(strconv.FormatUint(uint64(b), 10)) | ||
} | ||
adjusted[k] = sBuf.String() | ||
return true | ||
}) | ||
|
||
for k, v := range adjusted { | ||
attributes.PutStr(k, v) | ||
} | ||
} | ||
|
||
return Func(func(traces ptrace.Traces) (ptrace.Traces, error) { | ||
resourceSpans := traces.ResourceSpans() | ||
for i := 0; i < resourceSpans.Len(); i++ { | ||
rs := resourceSpans.At(i) | ||
scopeSpans := rs.ScopeSpans() | ||
for j := 0; j < scopeSpans.Len(); j++ { | ||
ss := scopeSpans.At(j) | ||
spans := ss.Spans() | ||
for k := 0; k < spans.Len(); k++ { | ||
span := spans.At(k) | ||
adjustAttributes(span.Attributes()) | ||
} | ||
} | ||
} | ||
return traces, nil | ||
}) | ||
} |
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 |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package adjuster | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
"go.opentelemetry.io/collector/pdata/ptrace" | ||
) | ||
|
||
func TestIPAttributeAdjuster(t *testing.T) { | ||
traces := ptrace.NewTraces() | ||
spans := traces.ResourceSpans().AppendEmpty().ScopeSpans().AppendEmpty().Spans() | ||
|
||
spanA := spans.AppendEmpty() | ||
spanA.Attributes().PutInt("a", 42) | ||
spanA.Attributes().PutStr("ip", "not integer") | ||
spanA.Attributes().PutInt("ip", 1<<24|2<<16|3<<8|4) | ||
spanA.Attributes().PutStr("peer.ipv4", "something") | ||
|
||
spanB := spans.AppendEmpty() | ||
spanB.Attributes().PutDouble("ip", 1<<25|2<<16|3<<8|4) | ||
|
||
trace, err := IPAttribute().Adjust(traces) | ||
require.NoError(t, err) | ||
|
||
span := trace.ResourceSpans().At(0).ScopeSpans().At(0).Spans() | ||
attributesA := span.At(0).Attributes() | ||
|
||
val, ok := attributesA.Get("a") | ||
require.True(t, ok) | ||
require.EqualValues(t, 42, val.Int()) | ||
|
||
val, ok = attributesA.Get("ip") | ||
require.True(t, ok) | ||
require.EqualValues(t, "1.2.3.4", val.Str()) | ||
|
||
val, ok = attributesA.Get("peer.ipv4") | ||
require.True(t, ok) | ||
require.EqualValues(t, "something", val.Str()) | ||
|
||
attributesB := span.At(1).Attributes() | ||
|
||
val, ok = attributesB.Get("ip") | ||
require.True(t, ok) | ||
require.EqualValues(t, "2.2.3.4", val.Str()) | ||
|
||
} |
Oops, something went wrong.
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.
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.
@yurishkuro The function in v1 looks like this
Couple of questions:
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.