-
Notifications
You must be signed in to change notification settings - Fork 43
[processor/elasticapm] Add host.ip enrichment using the client info address
#924
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
Open
isaacaflores2
wants to merge
13
commits into
elastic:main
Choose a base branch
from
isaacaflores2:processor-host-ip
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+1,767
−249
Open
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
7ee2c00
[processor/elaticapm] Add `host.ip` enrichment using the client addre…
isaacaflores2 d2f5449
Updated `TestProcessorECS` to include test cases for logs and metrics
isaacaflores2 54bd1ef
Add config to enable the host.ip enrichment
isaacaflores2 bdbe515
Update README.md
isaacaflores2 7cab4c7
Merge branch 'main' into processor-host-ip
isaacaflores2 83b6d91
Run make license-update
isaacaflores2 03bb7e3
Merge branch 'main' into processor-host-ip
isaacaflores2 9b91848
Update README.md with last config
isaacaflores2 e3a8af6
Ran `make goporto`
isaacaflores2 7c79df7
Changed `host_ip_enabled` to be disabled by default
isaacaflores2 79b7dfc
Merge branch 'main' into processor-host-ip
isaacaflores2 2570b11
Add `TestProcessorECS` which was moved to handle merge conflict
isaacaflores2 32d26fc
Merged test cases from `TestECSLogs` and `TestECSMetrics` into `TestP…
isaacaflores2 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
45 changes: 45 additions & 0 deletions
45
processor/elasticapmprocessor/internal/ecs/client_address.go
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 |
|---|---|---|
| @@ -0,0 +1,45 @@ | ||
| // Licensed to Elasticsearch B.V. under one or more contributor | ||
| // license agreements. See the NOTICE file distributed with | ||
| // this work for additional information regarding copyright | ||
| // ownership. Elasticsearch B.V. licenses this file to you under | ||
| // the Apache License, Version 2.0 (the "License"); you may | ||
| // not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, | ||
| // software distributed under the License is distributed on an | ||
| // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| // KIND, either express or implied. See the License for the | ||
| // specific language governing permissions and limitations | ||
| // under the License. | ||
|
|
||
| package ecs // import "github.com/elastic/opentelemetry-collector-components/processor/elasticapmprocessor/internal/ecs" | ||
|
|
||
| import ( | ||
| "context" | ||
|
|
||
| "go.opentelemetry.io/collector/client" | ||
| "go.opentelemetry.io/collector/pdata/pcommon" | ||
| ) | ||
|
|
||
| // SetHostIP sets the `host.ip` attribute using the client address. | ||
| // Does not override any existing `host.ip` values. | ||
| // Safely handles empty client addresses and empty attribute maps. | ||
| func SetHostIP(ctx context.Context, attributesMap pcommon.Map) { | ||
| cl := client.FromContext(ctx) | ||
| if cl.Addr == nil { | ||
| return | ||
| } | ||
|
|
||
| ip := cl.Addr.String() | ||
| if ip == "" { | ||
| return | ||
| } | ||
|
|
||
| // Only set host.ip when it does not exist or is empty | ||
| if value, ok := attributesMap.Get("host.ip"); !ok || value.Str() == "" { | ||
| attributesMap.PutStr("host.ip", ip) | ||
| } | ||
| } |
101 changes: 101 additions & 0 deletions
101
processor/elasticapmprocessor/internal/ecs/client_address_test.go
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 |
|---|---|---|
| @@ -0,0 +1,101 @@ | ||
| // Licensed to Elasticsearch B.V. under one or more contributor | ||
| // license agreements. See the NOTICE file distributed with | ||
| // this work for additional information regarding copyright | ||
| // ownership. Elasticsearch B.V. licenses this file to you under | ||
| // the Apache License, Version 2.0 (the "License"); you may | ||
| // not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, | ||
| // software distributed under the License is distributed on an | ||
| // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| // KIND, either express or implied. See the License for the | ||
| // specific language governing permissions and limitations | ||
| // under the License. | ||
|
|
||
| package ecs | ||
|
|
||
| import ( | ||
| "context" | ||
| "net" | ||
| "testing" | ||
|
|
||
| "go.opentelemetry.io/collector/client" | ||
| "go.opentelemetry.io/collector/pdata/pcommon" | ||
| ) | ||
|
|
||
| func TestSetHostIP(t *testing.T) { | ||
| var ( | ||
| ctxWithAddr = client.NewContext(context.Background(), client.Info{ | ||
| Addr: &net.IPAddr{ | ||
| IP: net.IPv4(1, 2, 3, 4), | ||
| }, | ||
| Metadata: client.NewMetadata(map[string][]string{"x-elastic-mapping-mode": {"ecs"}}), | ||
| }) | ||
| ctxWithEmptyAddr = client.NewContext(context.Background(), client.Info{ | ||
| Addr: &net.IPAddr{IP: nil}, | ||
| Metadata: client.NewMetadata(map[string][]string{"x-elastic-mapping-mode": {"ecs"}}), | ||
| }) | ||
| ) | ||
|
|
||
| tests := []struct { | ||
| name string | ||
| ctx context.Context | ||
| attributesMap pcommon.Map | ||
| wantIP string | ||
| }{ | ||
| { | ||
| name: "empty attributes map with valid client address", | ||
| ctx: ctxWithAddr, | ||
| attributesMap: pcommon.NewMap(), | ||
| wantIP: "1.2.3.4", | ||
| }, | ||
| { | ||
| name: "empty client context", | ||
| ctx: context.Background(), | ||
| attributesMap: pcommon.NewMap(), | ||
| wantIP: "", | ||
| }, | ||
| { | ||
| name: "client with nil address", | ||
| ctx: ctxWithEmptyAddr, | ||
| attributesMap: pcommon.NewMap(), | ||
| wantIP: "", | ||
| }, | ||
| { | ||
| name: "attributes map with existing host.ip", | ||
| ctx: ctxWithAddr, | ||
| attributesMap: func() pcommon.Map { | ||
| m := pcommon.NewMap() | ||
| m.PutStr("host.ip", "10.0.0.1") | ||
| m.PutStr("service.name", "test-service") | ||
| return m | ||
| }(), | ||
| wantIP: "10.0.0.1", | ||
| }, | ||
| { | ||
| name: "attributes map with empty host.ip string", | ||
| ctx: ctxWithAddr, | ||
| attributesMap: func() pcommon.Map { | ||
| m := pcommon.NewMap() | ||
| m.PutStr("host.ip", "") | ||
| m.PutStr("service.name", "test-service") | ||
| return m | ||
| }(), | ||
| wantIP: "1.2.3.4", | ||
| }, | ||
| } | ||
|
|
||
| for _, tt := range tests { | ||
| t.Run(tt.name, func(t *testing.T) { | ||
| SetHostIP(tt.ctx, tt.attributesMap) | ||
|
|
||
| value, _ := tt.attributesMap.Get("host.ip") | ||
| if value.Str() != tt.wantIP { | ||
| t.Errorf("SetHostIP() host.ip = %q, want %q", value.Str(), tt.wantIP) | ||
| } | ||
| }) | ||
| } | ||
| } |
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
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.
Whats the reasoning behind doing this via a flag vs adding it by default if the Client address exists?
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 added the config since I thought users may not/need this new field and to align with the existing approach in the opentelemtry-lib config where each field enrichment is behind a config.