Skip to content

util.GetHostname should not panic, instead it should return an error#294

Merged
gmmeyer merged 3 commits intomasterfrom
greg/hostname
Jun 14, 2017
Merged

util.GetHostname should not panic, instead it should return an error#294
gmmeyer merged 3 commits intomasterfrom
greg/hostname

Conversation

@gmmeyer
Copy link
Copy Markdown
Contributor

@gmmeyer gmmeyer commented Jun 12, 2017

What does this PR do?

util.GetHostname panics right now if it cannot get the hostname. Instead it should return an error that is handled as is appropriate.

Motivation

We need a hostname for the Flare command. The Flare command should work even if it is not possible to get a hostname. This will ensure that the flare command does not break when it attempts to get the hostname.

Additional Notes

I am not certain if I handled it properly everywhere. For example, in the aggregator I ignored the hostname failure, as I thought that was the appropriate course of action. If there had been a failure to grab it, it would have been handled much earlier.

Copy link
Copy Markdown
Member

@olivielpeau olivielpeau left a comment

Choose a reason for hiding this comment

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

panicking only in StartAgent makes sense to me

Comment thread cmd/dogstatsd/main.go Outdated
@@ -99,7 +99,8 @@ func start(cmd *cobra.Command, args []string) error {
f.Start()

// FIXME: the aggregator should probably be initialized with the resolved hostname instead
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

unrelated: can you remove this FIXME (it's been fixed for a long time)

Copy link
Copy Markdown
Contributor

@masci masci left a comment

Choose a reason for hiding this comment

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

Much better like this, 👍 overall but I think we should be explicit in handling the error now that we have one.

Comment thread cmd/agent/api/agent/agent.go Outdated
func getHostname(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
hname := util.GetHostname()
hname, _ := util.GetHostname()
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

We should not rely on the value returned by GetHostname at this point, better catching the error and set a default value for hname, like:

hname, err := util.GetHostname()
if err != nil {
  log.warning(err) // or something like this
  hname = ""
}

Comment thread cmd/agent/app/runloop.go
hostname := util.GetHostname()
hostname, err := util.GetHostname()
if err != nil {
panic(err)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

👍

Comment thread cmd/dogstatsd/main.go
// FIXME: the aggregator should probably be initialized with the resolved hostname instead
aggregatorInstance := aggregator.InitAggregator(f, util.GetHostname())
hname, _ := util.GetHostname()
aggregatorInstance := aggregator.InitAggregator(f, hname)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

same as above, in case of error it's better to explicitly set hname before passing it to InitAggregator

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

it could make sense to panic here if no hostname is found (i.e. have the same behavior for dogstatsd as the full agent)

Comment thread pkg/collector/py/datadog_agent.go Outdated
//export GetHostname
func GetHostname(self *C.PyObject, args *C.PyObject) *C.PyObject {
hostname := util.GetHostname()
hostname, _ := util.GetHostname()
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

same as above, let's explicitly set an empty string

@gmmeyer
Copy link
Copy Markdown
Contributor Author

gmmeyer commented Jun 13, 2017

@masci I explicitly set the empty string in all of those, that's better than what I was doing (assuming that it would exist if it made it past the initial panic gate

@masci
Copy link
Copy Markdown
Contributor

masci commented Jun 14, 2017

let's 🚢 this!

@gmmeyer gmmeyer merged commit 4d59f69 into master Jun 14, 2017
@gmmeyer gmmeyer deleted the greg/hostname branch June 14, 2017 19:15
s-alad pushed a commit that referenced this pull request Jan 6, 2026
Bumps [github.com/aws/aws-sdk-go-v2/config](https://github.com/aws/aws-sdk-go-v2) from 1.32.5 to 1.32.6.
- [Release notes](https://github.com/aws/aws-sdk-go-v2/releases)
- [Changelog](https://github.com/aws/aws-sdk-go-v2/blob/main/changelog-template.json)
- [Commits](aws/aws-sdk-go-v2@v1.32.5...v1.32.6)

---
updated-dependencies:
- dependency-name: github.com/aws/aws-sdk-go-v2/config
  dependency-version: 1.32.6
  dependency-type: direct:production
  update-type: version-update:semver-patch
...

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
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.

3 participants