Skip to content
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

[extension/observer] Expose host and port independently in endpoint env #33572

Merged
merged 2 commits into from
Jun 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions .chloggen/observer_expose_host_port.yaml
Original file line number Diff line number Diff line change
@@ -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: []
22 changes: 22 additions & 0 deletions extension/observer/endpoints.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package observer // import "github.com/open-telemetry/opentelemetry-collector-co
import (
"errors"
"fmt"
"net"
"reflect"
)

Expand Down Expand Up @@ -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
}

Expand Down
52 changes: 52 additions & 0 deletions extension/observer/endpoints_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ func TestEndpointEnv(t *testing.T) {
},
"uid": "pod-uid",
"namespace": "pod-namespace",
"host": "192.68.73.2",
},
},
{
Expand Down Expand Up @@ -88,6 +89,7 @@ func TestEndpointEnv(t *testing.T) {
"namespace": "pod-namespace",
},
"transport": ProtocolTCP,
"host": "192.68.73.2",
},
},
{
Expand Down Expand Up @@ -124,6 +126,7 @@ func TestEndpointEnv(t *testing.T) {
"namespace": "service-namespace",
"cluster_ip": "192.68.73.2",
"service_type": "LoadBalancer",
"host": "service.namespace",
},
},
{
Expand All @@ -148,6 +151,7 @@ func TestEndpointEnv(t *testing.T) {
"is_ipv6": true,
"port": uint16(2379),
"transport": ProtocolUDP,
"host": "127.0.0.1",
},
},
{
Expand Down Expand Up @@ -228,6 +232,54 @@ func TestEndpointEnv(t *testing.T) {
"labels": map[string]string{
"label_key": "label_val",
},
"host": "127.0.0.1",
"port": "1234",
Comment on lines +235 to +236
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the test case that ensures the Target gets properly parsed as host and port.

},
},
{
// 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",
},
},
}
Expand Down
8 changes: 8 additions & 0 deletions receiver/receivercreator/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down