forked from open-telemetry/opentelemetry-collector-contrib
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[processor/resource][processor/attributes] Option to read client addr…
…ess (open-telemetry#34048) We have options to extract headers and auth info from the client. This change adds an option to extract client address by specifying `client.address` value in the `from_context` field. The code to extract address from the client info is taken from the k8sattributes receiver. Fixes open-telemetry#34051 --------- Co-authored-by: Curtis Robert <crobert@splunk.com>
- Loading branch information
Showing
8 changed files
with
172 additions
and
36 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
# Use this changelog template to create an entry for release notes. | ||
|
||
# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix' | ||
change_type: 'enhancement' | ||
|
||
# The name of the component, or a single word describing the area of concern, (e.g. filelogreceiver) | ||
component: processor/resource, processor/attributes | ||
|
||
# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`). | ||
note: Add an option to extract value from a client address by specifying `client.address` value in the `from_context` field. | ||
|
||
# Mandatory: One or more tracking issues related to the change. You can use the PR number here if no issue exists. | ||
issues: [34051] | ||
|
||
# If your change doesn't affect end users or the exported elements of any package, | ||
# you should instead start your pull request title with [chore] or use the "Skip Changelog" label. | ||
# Optional: The change log or logs in which this entry should be included. | ||
# e.g. '[user]' or '[user, api]' | ||
# Include 'user' if the change is relevant to end users. | ||
# Include 'api' if there is a change to a library API. | ||
# Default: '[user]' | ||
change_logs: [user] |
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
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,40 @@ | ||
// Copyright The OpenTelemetry Authors | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package clientutil // import "github.com/open-telemetry/opentelemetry-collector-contrib/internal/coreinternal/clientutil" | ||
|
||
import ( | ||
"net" | ||
"strings" | ||
|
||
"go.opentelemetry.io/collector/client" | ||
) | ||
|
||
// Address returns the address of the client connecting to the collector. | ||
func Address(client client.Info) string { | ||
if client.Addr == nil { | ||
return "" | ||
} | ||
switch addr := client.Addr.(type) { | ||
case *net.UDPAddr: | ||
return addr.IP.String() | ||
case *net.TCPAddr: | ||
return addr.IP.String() | ||
case *net.IPAddr: | ||
return addr.IP.String() | ||
} | ||
|
||
// If this is not a known address type, check for known "untyped" formats. | ||
// 1.1.1.1:<port> | ||
|
||
lastColonIndex := strings.LastIndex(client.Addr.String(), ":") | ||
if lastColonIndex != -1 { | ||
ipString := client.Addr.String()[:lastColonIndex] | ||
ip := net.ParseIP(ipString) | ||
if ip != nil { | ||
return ip.String() | ||
} | ||
} | ||
|
||
return client.Addr.String() | ||
} |
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,79 @@ | ||
// Copyright The OpenTelemetry Authors | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package clientutil | ||
|
||
import ( | ||
"net" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"go.opentelemetry.io/collector/client" | ||
) | ||
|
||
type fakeAddr string | ||
|
||
func (s fakeAddr) String() string { | ||
return string(s) | ||
} | ||
|
||
func (fakeAddr) Network() string { | ||
return "tcp" | ||
} | ||
|
||
func TestAddress(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
client client.Info | ||
want string | ||
}{ | ||
{ | ||
name: "UDPAddr", | ||
client: client.Info{ | ||
Addr: &net.UDPAddr{ | ||
IP: net.IPv4(192, 0, 2, 1), | ||
Port: 1234, | ||
}, | ||
}, | ||
want: "192.0.2.1", | ||
}, | ||
{ | ||
name: "TCPAddr", | ||
client: client.Info{ | ||
Addr: &net.TCPAddr{ | ||
IP: net.IPv4(192, 0, 2, 2), | ||
Port: 1234, | ||
}, | ||
}, | ||
want: "192.0.2.2", | ||
}, | ||
{ | ||
name: "IPAddr", | ||
client: client.Info{ | ||
Addr: &net.IPAddr{ | ||
IP: net.IPv4(192, 0, 2, 3), | ||
}, | ||
}, | ||
want: "192.0.2.3", | ||
}, | ||
{ | ||
name: "fake_addr_with_port", | ||
client: client.Info{ | ||
Addr: fakeAddr("1.1.1.1:3200"), | ||
}, | ||
want: "1.1.1.1", | ||
}, | ||
{ | ||
name: "fake_addr_without_port", | ||
client: client.Info{ | ||
Addr: fakeAddr("1.1.1.1"), | ||
}, | ||
want: "1.1.1.1", | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
assert.Equal(t, tt.want, Address(tt.client)) | ||
}) | ||
} | ||
} |
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
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