From ae1890d3dfc3a66ef8e8bc5aba9611472cb6758f Mon Sep 17 00:00:00 2001 From: Izabella Raulin Date: Wed, 22 Jun 2016 11:00:53 +0200 Subject: [PATCH] Added publishing tags by file publisher --- examples/tasks/mock_tagged-file.json | 43 +++++++++++++++++++ .../snap-publisher-file/file/file.go | 15 ++++++- 2 files changed, 57 insertions(+), 1 deletion(-) create mode 100644 examples/tasks/mock_tagged-file.json diff --git a/examples/tasks/mock_tagged-file.json b/examples/tasks/mock_tagged-file.json new file mode 100644 index 000000000..9a43baff0 --- /dev/null +++ b/examples/tasks/mock_tagged-file.json @@ -0,0 +1,43 @@ +{ + "version": 1, + "schedule": { + "type": "simple", + "interval": "1s" + }, + "workflow": { + "collect": { + "metrics": { + "/intel/mock/foo": {}, + "/intel/mock/bar": {}, + "/intel/mock/*/baz": {} + }, + "config": { + "/intel/mock": { + "name": "root", + "password": "secret" + } + }, + "tags": { + "/intel/mock": { + "experiment": "1", + "os": "linux" + } + }, + + "process": [ + { + "plugin_name": "passthru", + "process": null, + "publish": [ + { + "plugin_name": "file", + "config": { + "file": "/tmp/snap_published_mock_file.log" + } + } + ] + } + ] + } + } +} diff --git a/plugin/publisher/snap-publisher-file/file/file.go b/plugin/publisher/snap-publisher-file/file/file.go index 60af43ee3..8fb3b5a3e 100644 --- a/plugin/publisher/snap-publisher-file/file/file.go +++ b/plugin/publisher/snap-publisher-file/file/file.go @@ -32,6 +32,7 @@ import ( "github.com/intelsdi-x/snap/control/plugin" "github.com/intelsdi-x/snap/control/plugin/cpolicy" "github.com/intelsdi-x/snap/core/ctypes" + "strings" ) const ( @@ -73,12 +74,24 @@ func (f *filePublisher) Publish(contentType string, content []byte, config map[s } w := bufio.NewWriter(file) for _, m := range metrics { - w.WriteString(fmt.Sprintf("%v|%v|%v\n", m.Timestamp(), m.Namespace(), m.Data())) + formattedTags := formatMetricTagsAsString(m.Tags()) + w.WriteString(fmt.Sprintf("%v|%v|%v|%v\n", m.Timestamp(), m.Namespace(), m.Data(), formattedTags)) } w.Flush() return nil } +// formatMetricTagsAsString returns metric's tags as a string in the following format tagKey:tagValue where the next tags are separated by semicolon +func formatMetricTagsAsString(metricTags map[string]string) string { + var tags string + for tag, value := range metricTags { + tags += fmt.Sprintf("%s:%s; ", tag, value) + } + // trim the last semicolon + tags = strings.TrimSuffix(tags, "; ") + + return "tags["+tags+"]" +} func Meta() *plugin.PluginMeta { return plugin.NewPluginMeta(name, version, pluginType, []string{plugin.SnapGOBContentType}, []string{plugin.SnapGOBContentType})