Skip to content
This repository has been archived by the owner on Nov 8, 2022. It is now read-only.

Make hostname a stored variable so it is not retrieved for each metri… #1215

Merged
merged 1 commit into from
Sep 22, 2016
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
50 changes: 36 additions & 14 deletions control/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,17 +46,46 @@ var (

// hostnameReader, hostnamer created for mocking
func init() {
hostnameReader = &hostnameReaderType{}
host, err := os.Hostname()
if err != nil {
log.WithFields(log.Fields{
"_module": "control",
"_file": "metrics.go,",
"_block": "addStandardAndWorkflowTags",
"error": err.Error(),
}).Error("Unable to determine hostname")
host = "not_found"
}
hostnameReader = &hostnameReaderType{hostname: host, hostnameRefreshTTL: time.Hour, lastRefresh: time.Now()}
}

type hostnamer interface {
Hostname() (name string, err error)
Hostname() (name string)
Copy link
Contributor

Choose a reason for hiding this comment

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

Do we still need the hostnamer interface?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I believe not, I'll take that out as well.

}

type hostnameReaderType struct{}
type hostnameReaderType struct {
hostname string
hostnameRefreshTTL time.Duration
lastRefresh time.Time
}

func (h *hostnameReaderType) Hostname() (name string, err error) {
return os.Hostname()
func (h *hostnameReaderType) Hostname() (name string) {
if time.Now().After(h.lastRefresh.Add(h.hostnameRefreshTTL)) {
host, err := os.Hostname()
if err != nil {
log.WithFields(log.Fields{
"_module": "control",
"_file": "metrics.go,",
"_block": "addStandardAndWorkflowTags",
"error": err.Error(),
}).Error("Unable to determine hostname")
host = "not_found"
}

h.hostname = host
h.lastRefresh = time.Now()
}
return h.hostname
}

func errorMetricNotFound(ns string, ver ...int) error {
Expand Down Expand Up @@ -513,15 +542,8 @@ func appendIfMissing(keys []string, ns string) []string {
}

func addStandardAndWorkflowTags(m core.Metric, allTags map[string]map[string]string) core.Metric {
hostname, err := hostnameReader.Hostname()
if err != nil {
log.WithFields(log.Fields{
"_module": "control",
"_file": "metrics.go,",
"_block": "addStandardAndWorkflowTags",
"error": err.Error(),
}).Error("Unable to determine hostname")
}
hostname := hostnameReader.Hostname()

tags := m.Tags()
if tags == nil {
tags = map[string]string{}
Expand Down
6 changes: 3 additions & 3 deletions control/metrics_small_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,8 +130,8 @@ func TestContainsTupleNegative(t *testing.T) {

type mockHostnameReader struct{}

func (m *mockHostnameReader) Hostname() (string, error) {
return "hostname", nil
func (m *mockHostnameReader) Hostname() string {
return "hostname"
}

type testCase struct {
Expand All @@ -141,7 +141,7 @@ type testCase struct {
}

func prepareTestCases() []testCase {
hostname, _ := hostnameReader.Hostname()
hostname := hostnameReader.Hostname()
fooTags := map[string]string{
"foo_tag": "foo_val",
}
Expand Down
2 changes: 1 addition & 1 deletion docs/METRICS.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ A metric in snap has the following fields.
* Are key value pairs that provide additional meta data about the metric
* May be added by the framework or other plugins (processors)
* The framework currently adds the following standard tag to all metrics
* `plugin_running_on` describing on which host the plugin is running
* `plugin_running_on` describing on which host the plugin is running. This value is updated every hour due to a TTL set internally.
* May be added by a task manifests as described [here](https://github.com/intelsdi-x/snap/pull/941)
* May be added by the snapd config as described [here](https://github.com/intelsdi-x/snap/issues/827)
* Unit `string`
Expand Down