diff --git a/.chloggen/observer_expose_host_port.yaml b/.chloggen/observer_expose_host_port.yaml new file mode 100644 index 000000000000..bbe4a341d4b6 --- /dev/null +++ b/.chloggen/observer_expose_host_port.yaml @@ -0,0 +1,27 @@ +# 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: observerextension + +# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`). +note: Expose host and port in endpoint's environment + +# Mandatory: One or more tracking issues related to the change. You can use the PR number here if no issue exists. +issues: [33571] + +# (Optional) One or more lines of additional information to render under the primary note. +# These lines will be padded with 2 spaces and then inserted directly into the document. +# Use pipe (|) for multiline entries. +subtext: + +# 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: [] diff --git a/extension/observer/endpoints.go b/extension/observer/endpoints.go index 89a57b255a89..86cfc5e8e72a 100644 --- a/extension/observer/endpoints.go +++ b/extension/observer/endpoints.go @@ -6,6 +6,7 @@ package observer // import "github.com/open-telemetry/opentelemetry-collector-co import ( "errors" "fmt" + "net" "reflect" ) @@ -69,6 +70,27 @@ func (e *Endpoint) Env() (EndpointEnv, error) { env["type"] = string(e.Details.Type()) env["id"] = string(e.ID) + // Exposing the target as a split "host" and "port" enables the receiver creator + // to be able to discover receivers that require these options to be configured + // separately. + const hostKey = "host" + const portKey = "port" + host, port, err := net.SplitHostPort(e.Target) + // An error most likely means there was no port when splitting, so the host + // can simply be the target. + if err != nil { + host = e.Target + } else { + // Only try to set the port if a valid port was found when splitting the target + if _, keyExists := env[portKey]; !keyExists { + env[portKey] = port + } + } + + if _, keyExists := env[hostKey]; !keyExists { + env[hostKey] = host + } + return env, nil } diff --git a/extension/observer/endpoints_test.go b/extension/observer/endpoints_test.go index c90be0900d34..619ebe83fcc5 100644 --- a/extension/observer/endpoints_test.go +++ b/extension/observer/endpoints_test.go @@ -46,6 +46,7 @@ func TestEndpointEnv(t *testing.T) { }, "uid": "pod-uid", "namespace": "pod-namespace", + "host": "192.68.73.2", }, }, { @@ -88,6 +89,7 @@ func TestEndpointEnv(t *testing.T) { "namespace": "pod-namespace", }, "transport": ProtocolTCP, + "host": "192.68.73.2", }, }, { @@ -124,6 +126,7 @@ func TestEndpointEnv(t *testing.T) { "namespace": "service-namespace", "cluster_ip": "192.68.73.2", "service_type": "LoadBalancer", + "host": "service.namespace", }, }, { @@ -148,6 +151,7 @@ func TestEndpointEnv(t *testing.T) { "is_ipv6": true, "port": uint16(2379), "transport": ProtocolUDP, + "host": "127.0.0.1", }, }, { @@ -228,6 +232,54 @@ func TestEndpointEnv(t *testing.T) { "labels": map[string]string{ "label_key": "label_val", }, + "host": "127.0.0.1", + "port": "1234", + }, + }, + { + // This is an invalid test case, to ensure "port" keeps the original value and + // isn't overwritten by a port parsed from the "Target". The two ports shouldn't mismatch + // if they're exposed in both places. + name: "K8s pod port - conflicting ports", + endpoint: Endpoint{ + ID: EndpointID("port_id"), + Target: "192.68.73.2:4321", + Details: &Port{ + Name: "port_name", + Pod: Pod{ + Name: "pod_name", + Labels: map[string]string{ + "label_key": "label_val", + }, + Annotations: map[string]string{ + "annotation_1": "value_1", + }, + Namespace: "pod-namespace", + UID: "pod-uid", + }, + Port: 2379, + Transport: ProtocolTCP, + }, + }, + want: EndpointEnv{ + "type": "port", + "endpoint": "192.68.73.2:4321", + "id": "port_id", + "name": "port_name", + "port": uint16(2379), + "pod": EndpointEnv{ + "name": "pod_name", + "labels": map[string]string{ + "label_key": "label_val", + }, + "annotations": map[string]string{ + "annotation_1": "value_1", + }, + "uid": "pod-uid", + "namespace": "pod-namespace", + }, + "transport": ProtocolTCP, + "host": "192.68.73.2", }, }, } diff --git a/receiver/receivercreator/README.md b/receiver/receivercreator/README.md index 6356e252983c..5008cc342c3b 100644 --- a/receiver/receivercreator/README.md +++ b/receiver/receivercreator/README.md @@ -278,6 +278,14 @@ receivers: # Set a resource attribute based on endpoint value. rule: type == "port" && port == 6379 + sqlserver: + rule: type == "port" && pod.name matches "(?i)mssql" + config: + server: '`host`' + port: '`port`' + username: sa + password: password + resource_attributes: # Dynamic configuration values, overwriting default attributes` pod: