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

Commit

Permalink
Fixes #1214: makes hostname a stored var with a 1 hour ttl when apply…
Browse files Browse the repository at this point in the history
…ing tags, unknown hosts will be labeled not_found
  • Loading branch information
jtlisi committed Sep 22, 2016
1 parent 3cf5025 commit 5e83d04
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 18 deletions.
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)
}

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

0 comments on commit 5e83d04

Please sign in to comment.