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

Do not assume hostname binary is available #179

Merged
merged 3 commits into from
Sep 4, 2019

Conversation

pschipitsch
Copy link
Contributor

We run this code in AWS Lambda. AWS recently removed the hostname binary from the execution environment.

Copy link
Contributor

@gzussa gzussa left a comment

Choose a reason for hiding this comment

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

Thanks for this contribution @pschipitsch. This is awesome 😄
See my comment regarding your changes.

TLDR; This change doesn't seem to be backward compatible.

@@ -90,7 +90,7 @@ def record_hostname(hostname)
end

def report()
hostname = %x[hostname -f].strip
hostname = Dogapi.find_localhost
Copy link
Contributor

Choose a reason for hiding this comment

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

👍

@@ -164,10 +164,9 @@ def Dogapi.find_datadog_host

def Dogapi.find_localhost
begin
# prefer hostname -f over Socket.gethostname
@@hostname ||= %x[hostname -f].strip
@@hostname ||= Addrinfo.getaddrinfo(Socket.gethostname, nil, nil, nil, nil, Socket::AI_CANONNAME).first.canonname
Copy link
Contributor

Choose a reason for hiding this comment

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

From https://github.com/DataDog/dogapi-rb it seems that Socket.gethostname is not recommended (apparently it only returns the short name). However, when calling %x[hostname -f].strip and Socket.gethostname on my box, I get the same result. Addrinfo.getaddrinfo(Socket.gethostname, nil, nil, nil, nil, Socket::AI_CANONNAME).first.canonname returns the same value on my box but with upper cases here and there instead. This will introduce a regression since hostnames in Datadog are case sensitive [1]. Datadog is less permissive than the spec [2].

So It seems that using Socket.gethostname is not possible and it seems that your solution would not be backward compatible.
Another option I see is to let the user choose between %x[hostname -f].strip and your solution.
Finally, I am not Ruby expert and I am assuming your solution is the proper way to get the canonical hostname. Still, I have to ask you, is there a shorter/cleaner syntax?

[1] https://docs.datadoghq.com/tagging/assigning_tags/?tab=agentv6#hostname.
[2] https://tools.ietf.org/html/rfc952

Copy link

@cabrinha cabrinha Aug 20, 2019

Choose a reason for hiding this comment

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

The hostname binary is available in my case. My hostnames are always unique, as well.

It does return the short name, however. Is DogAPI RB expecting the FQDN, including the TLD?

If so, we should allow users to set that themselves.

Copy link
Contributor

Choose a reason for hiding this comment

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

Hey, I think the main idea here being that the hostname should be a unique value and avoid clashing with other hosts. For some context, there is a doc about how the Datadog Agent detects the hostname - https://docs.datadoghq.com/agent/faq/how-datadog-agent-determines-the-hostname/?tab=agentv6

To the other point, about staying backwards compatible, one idea could be to leave the hostname -f as the default, and fallback to this kind of approach in the rescue should that error for whatever reason.

Copy link
Contributor Author

@pschipitsch pschipitsch Aug 21, 2019

Choose a reason for hiding this comment

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

@gzussa Thanks for the feedback! I've updated to try hostname -f and use Addrinfo.getaddrinfo if hostname is not found.

rescue
raise 'Cannot determine local hostname via hostname -f'
raise 'Cannot determine local hostname via Socket.gethostname'
Copy link
Contributor

Choose a reason for hiding this comment

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

While we're here, I think it may be helpful to also provide the message of the original error that caused this to occur. What do you think?

@dabcoder
Copy link
Contributor

dabcoder commented Sep 3, 2019

@pschipitsch Thanks for making the suggested changes.
Re: #179 (comment), did you keep the message of the original error? Can't seem to see it in your last update, unless I am mistaken.

@pschipitsch
Copy link
Contributor Author

@dabcoder The latest error message is kept in the global variable $!. The English module in the standard library provides "less cryptic names" for the global variables: https://ruby-doc.com/stdlib-2.5.2/libdoc/English/rdoc/English.html.

Copy link
Contributor

@zippolyte zippolyte left a comment

Choose a reason for hiding this comment

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

LGTM, thanks fro addressing all the comments

@zippolyte zippolyte merged commit 2c2606b into DataDog:master Sep 4, 2019
@pschipitsch
Copy link
Contributor Author

Thanks @zippolyte. How do we go about building a new gem with this change?

@therve therve mentioned this pull request Mar 21, 2022
6 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants