本文整理汇总了Golang中github.com/intelsdi-x/snap-plugin-lib-go/v1/plugin.Config类的典型用法代码示例。如果您正苦于以下问题:Golang Config类的具体用法?Golang Config怎么用?Golang Config使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了Config类的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的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
}
开发者ID:IRCody,项目名称:snap,代码行数:34,代码来源:file.go
示例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)
})
}
开发者ID:IRCody,项目名称:snap,代码行数:13,代码来源:file_small_test.go
示例3: 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
}
开发者ID:intelsdi-x,项目名称:snap-plugin-publisher-graphite,代码行数:55,代码来源:graphite.go
示例4: GetMetricTypes
// GetMetricTypes returns metric types for testing
func (f *Mock) GetMetricTypes(cfg plugin.Config) ([]plugin.Metric, error) {
mts := []plugin.Metric{}
if _, err := cfg.GetBool("test-fail"); err == nil {
return mts, fmt.Errorf("testing")
}
if _, err := cfg.GetBool("test"); err == nil {
mts = append(mts, plugin.Metric{
Namespace: plugin.NewNamespace("intel", "mock", "test"),
Description: "mock description",
Unit: "mock unit",
})
}
if _, err := cfg.GetBool("test-less"); err != nil {
mts = append(mts, plugin.Metric{
Namespace: plugin.NewNamespace("intel", "mock", "foo"),
Description: "mock description",
Unit: "mock unit",
})
}
mts = append(mts, plugin.Metric{
Namespace: plugin.NewNamespace("intel", "mock", "bar"),
Description: "mock description",
Unit: "mock unit",
})
mts = append(mts, plugin.Metric{
Namespace: plugin.NewNamespace("intel", "mock").
AddDynamicElement("host", "name of the host").
AddStaticElement("baz"),
Description: "mock description",
Unit: "mock unit",
})
return mts, nil
}
开发者ID:IRCody,项目名称:snap,代码行数:34,代码来源:mock.go
示例5: 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
}
开发者ID:intelsdi-x,项目名称:snap-plugin-publisher-graphite,代码行数:22,代码来源:graphite.go
示例6: Process
func (p *passthruProcessor) Process(metrics []plugin.Metric, config plugin.Config) ([]plugin.Metric, 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("processing started")
if _, err := config.GetBool("test"); err == nil {
log.Debug("test configuration found")
for idx, m := range metrics {
if m.Namespace.Strings()[0] == "foo" {
log.Print("found foo metric")
metrics[idx].Data = 2
}
}
}
return metrics, nil
}
开发者ID:IRCody,项目名称:snap,代码行数:22,代码来源:passthru.go
注:本文中的github.com/intelsdi-x/snap-plugin-lib-go/v1/plugin.Config类示例整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。 |
请发表评论