本文整理汇总了Golang中github.com/intelsdi-x/snap-plugin-lib-go/v1/plugin.Config.GetString方法的典型用法代码示例。如果您正苦于以下问题:Golang Config.GetString方法的具体用法?Golang Config.GetString怎么用?Golang Config.GetString使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类github.com/intelsdi-x/snap-plugin-lib-go/v1/plugin.Config
的用法示例。
在下文中一共展示了Config.GetString方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: Publish
func (f *filePublisher) Publish(metrics []plugin.Metric, config plugin.Config) error {
log.SetFormatter(&log.TextFormatter{DisableTimestamp: true})
if _, err := config.GetBool(debug); err == nil {
log.SetLevel(log.DebugLevel)
} else {
log.SetLevel(log.InfoLevel)
}
log.Debug("publishing started")
filename, err := config.GetString("file")
if err != nil {
log.Error(err)
return err
}
file, err := os.OpenFile(filename, os.O_APPEND|os.O_WRONLY|os.O_CREATE, 0666)
defer file.Close()
if err != nil {
log.Error(err)
return err
}
log.WithFields(log.Fields{
"file": filename,
"metrics-published-count": len(metrics),
}).Debug("metrics published")
w := bufio.NewWriter(file)
for _, m := range metrics {
formattedTags := formatMetricTagsAsString(m.Tags)
w.WriteString(fmt.Sprintf("%v|%v|%v|%v\n", m.Timestamp, m.Namespace, m.Data, formattedTags))
}
w.Flush()
return nil
}
示例2: TestFilePublish
func TestFilePublish(t *testing.T) {
metrics := []plugin.Metric{plugin.Metric{Namespace: plugin.NewNamespace("foo"), Timestamp: time.Now(), Data: 99, Tags: nil}}
config := plugin.Config{"file": "/tmp/pub.out"}
Convey("TestFilePublish", t, func() {
fp := NewFilePublisher()
So(fp, ShouldNotBeNil)
err := fp.Publish(metrics, config)
So(err, ShouldBeNil)
filename, _ := config.GetString("file")
_, err = os.Stat(filename)
So(err, ShouldBeNil)
})
}
示例3: getLogger
func getLogger(cfg plugin.Config) *log.Entry {
logger := log.WithFields(log.Fields{
"plugin-name": Name,
"plugin-version": Version,
"plugin-type": "publisher",
})
log.SetLevel(log.WarnLevel)
levelValue, err := cfg.GetString("log-level")
if err == nil {
if level, err := log.ParseLevel(strings.ToLower(levelValue)); err == nil {
log.SetLevel(level)
} else {
log.WithFields(log.Fields{
"value": strings.ToLower(levelValue),
"acceptable values": "warn, error, debug, info",
}).Warn("Invalid config value")
}
}
return logger
}
示例4: Publish
func (f *GraphitePublisher) Publish(metrics []plugin.Metric, cfg plugin.Config) error {
logger := getLogger(cfg)
logger.Debug("Publishing started")
var tagsForPrefix []string
logger.Debug("publishing %v metrics to %v", len(metrics), cfg)
server, err := cfg.GetString("server")
if err != nil {
return err
}
port, err := cfg.GetInt("port")
if err != nil {
return err
}
tagConfigs, err := cfg.GetString("prefix_tags")
if err == nil {
tagsForPrefix = strings.Split(tagConfigs, ",")
}
pre, err := cfg.GetString("prefix")
if err != nil {
pre = ""
}
logger.Debug("Attempting to connect to %s:%d", server, port)
gite, err := graphite.NewGraphiteWithMetricPrefix(server, int(port), pre)
if err != nil {
logger.Errorf("Error Connecting to graphite at %s:%d. Error: %v", server, port, err)
return fmt.Errorf("Error Connecting to graphite at %s:%d. Error: %v", server, port, err)
}
defer gite.Disconnect()
logger.Debug("Connected to %s:%s successfully", server, port)
giteMetrics := make([]graphite.Metric, len(metrics))
for i, m := range metrics {
key := strings.Join(m.Namespace.Strings(), ".")
for _, tag := range tagsForPrefix {
nextTag, ok := m.Tags[tag]
if ok {
key = nextTag + "." + key
}
}
data := fmt.Sprintf("%v", m.Data)
logger.Debug("Metric ready to send %s:%s", key, data)
giteMetrics[i] = graphite.NewMetric(key, data, m.Timestamp.Unix())
}
err = gite.SendMetrics(giteMetrics)
if err != nil {
logger.Errorf("Unable to send metrics. Error: %s", err)
return fmt.Errorf("Unable to send metrics. Error: %s", err)
}
logger.Debug("Metrics sent to Graphite.")
return nil
}