diff --git a/pkg/stanza/operator/helper/host_identifier.go b/pkg/stanza/operator/helper/host_identifier.go deleted file mode 100644 index 6b395d506a93..000000000000 --- a/pkg/stanza/operator/helper/host_identifier.go +++ /dev/null @@ -1,140 +0,0 @@ -// Copyright The OpenTelemetry Authors -// -// Licensed 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 helper // import "github.com/open-telemetry/opentelemetry-collector-contrib/pkg/stanza/operator/helper" - -import ( - "fmt" - "net" - "os" - "strings" - - "github.com/open-telemetry/opentelemetry-collector-contrib/pkg/stanza/entry" - "github.com/open-telemetry/opentelemetry-collector-contrib/pkg/stanza/errors" -) - -// NewHostIdentifierConfig returns a HostIdentifierConfig with default values -func NewHostIdentifierConfig() HostIdentifierConfig { - return HostIdentifierConfig{ - IncludeHostname: true, - IncludeIP: true, - getHostname: getHostname, - getIP: getIP, - } -} - -// HostIdentifierConfig is the configuration of a host identifier -type HostIdentifierConfig struct { - IncludeHostname bool `json:"include_hostname,omitempty" yaml:"include_hostname,omitempty"` - IncludeIP bool `json:"include_ip,omitempty" yaml:"include_ip,omitempty"` - getHostname func() (string, error) - getIP func() (string, error) -} - -// Build will build a host attributer from the supplied configuration -func (c HostIdentifierConfig) Build() (HostIdentifier, error) { - identifier := HostIdentifier{ - includeHostname: c.IncludeHostname, - includeIP: c.IncludeIP, - } - - if c.getHostname == nil { - return identifier, fmt.Errorf("getHostname func is not set") - } - - if c.getIP == nil { - return identifier, fmt.Errorf("getIP func is not set") - } - - if c.IncludeHostname { - hostname, err := c.getHostname() - if err != nil { - return identifier, errors.Wrap(err, "get hostname") - } - identifier.hostname = hostname - } - - if c.IncludeIP { - ip, err := c.getIP() - if err != nil { - return identifier, errors.Wrap(err, "get ip address") - } - identifier.ip = ip - } - - return identifier, nil -} - -// getHostname will return the hostname of the current host -func getHostname() (string, error) { - return os.Hostname() -} - -// getIP will return the IP address of the current host -func getIP() (string, error) { - var ip string - - interfaces, err := net.Interfaces() - if err != nil { - return "", errors.Wrap(err, "list interfaces") - } - - for _, i := range interfaces { - // Skip loopback interfaces - if i.Flags&net.FlagLoopback != 0 { - continue - } - - // Skip down interfaces - if i.Flags&net.FlagUp == 0 { - continue - } - - addrs, err := i.Addrs() - if err != nil { - continue - } - if len(addrs) > 0 { - ip = strings.Split(addrs[0].String(), "/")[0] - } - } - - if len(ip) == 0 { - return "", errors.NewError( - "failed to find ip address", - "check that a non-loopback interface with an assigned IP address exists and is running", - ) - } - - return ip, nil -} - -// HostIdentifier is a helper that adds host related metadata to an entry's resource -type HostIdentifier struct { - hostname string - ip string - includeHostname bool - includeIP bool -} - -// Identify will add host related metadata to an entry's resource -func (h *HostIdentifier) Identify(entry *entry.Entry) { - if h.includeHostname { - entry.AddResourceKey("host.name", h.hostname) - } - - if h.includeIP { - entry.AddResourceKey("host.ip", h.ip) - } -} diff --git a/pkg/stanza/operator/helper/host_identifier_test.go b/pkg/stanza/operator/helper/host_identifier_test.go deleted file mode 100644 index ea987d7ddd1f..000000000000 --- a/pkg/stanza/operator/helper/host_identifier_test.go +++ /dev/null @@ -1,79 +0,0 @@ -// Copyright The OpenTelemetry Authors -// -// Licensed 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 helper - -import ( - "testing" - - "github.com/stretchr/testify/require" - - "github.com/open-telemetry/opentelemetry-collector-contrib/pkg/stanza/entry" -) - -func MockHostIdentifierConfig(includeIP, includeHostname bool, ip, hostname string) HostIdentifierConfig { - return HostIdentifierConfig{ - IncludeIP: includeIP, - IncludeHostname: includeHostname, - getIP: func() (string, error) { return ip, nil }, - getHostname: func() (string, error) { return hostname, nil }, - } -} - -func TestHostAttributer(t *testing.T) { - cases := []struct { - name string - config HostIdentifierConfig - expectedResource map[string]interface{} - }{ - { - "HostnameAndIP", - MockHostIdentifierConfig(true, true, "ip", "hostname"), - map[string]interface{}{ - "host.name": "hostname", - "host.ip": "ip", - }, - }, - { - "HostnameNoIP", - MockHostIdentifierConfig(false, true, "ip", "hostname"), - map[string]interface{}{ - "host.name": "hostname", - }, - }, - { - "IPNoHostname", - MockHostIdentifierConfig(true, false, "ip", "hostname"), - map[string]interface{}{ - "host.ip": "ip", - }, - }, - { - "NoHostnameNoIP", - MockHostIdentifierConfig(false, false, "", "test"), - nil, - }, - } - - for _, tc := range cases { - t.Run(tc.name, func(t *testing.T) { - identifier, err := tc.config.Build() - require.NoError(t, err) - - e := entry.New() - identifier.Identify(e) - require.Equal(t, tc.expectedResource, e.Resource) - }) - } -} diff --git a/unreleased/pkg-stanza-rmhostid.yaml b/unreleased/pkg-stanza-rmhostid.yaml new file mode 100755 index 000000000000..f815982a1a40 --- /dev/null +++ b/unreleased/pkg-stanza-rmhostid.yaml @@ -0,0 +1,16 @@ +# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix' +change_type: breaking + +# The name of the component, or a single word describing the area of concern, (e.g. filelogreceiver) +component: pkg/stanza + +# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`). +note: Remove HostIdentifier, HostIdentifierConfig and associated functions + +# One or more tracking issues related to the change +issues: [13396] + +# (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: