當前位置: 首頁>>代碼示例>>Golang>>正文


Golang internal.JSONFlattener類代碼示例

本文整理匯總了Golang中github.com/influxdata/telegraf/internal.JSONFlattener的典型用法代碼示例。如果您正苦於以下問題:Golang JSONFlattener類的具體用法?Golang JSONFlattener怎麽用?Golang JSONFlattener使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。


在下文中一共展示了JSONFlattener類的5個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。

示例1: Gather

func (e *Exec) Gather(acc telegraf.Accumulator) error {
	out, err := e.runner.Run(e)
	if err != nil {
		return err
	}

	switch e.DataFormat {
	case "", "json":
		var jsonOut interface{}
		err = json.Unmarshal(out, &jsonOut)
		if err != nil {
			return fmt.Errorf("exec: unable to parse output of '%s' as JSON, %s",
				e.Command, err)
		}

		f := internal.JSONFlattener{}
		err = f.FlattenJSON("", jsonOut)
		if err != nil {
			return err
		}
		acc.AddFields("exec", f.Fields, nil)
	case "influx":
		now := time.Now()
		metrics, err := telegraf.ParseMetrics(out)
		for _, metric := range metrics {
			acc.AddFields(metric.Name(), metric.Fields(), metric.Tags(), now)
		}
		return err
	default:
		return fmt.Errorf("Unsupported data format: %s. Must be either json "+
			"or influx.", e.DataFormat)
	}
	return nil
}
開發者ID:zooplus,項目名稱:telegraf,代碼行數:34,代碼來源:exec.go

示例2: gatherServer

// Gathers data from a particular server
// Parameters:
//     acc      : The telegraf Accumulator to use
//     serverURL: endpoint to send request to
//     service  : the service being queried
//
// Returns:
//     error: Any error that may have occurred
func (h *HttpJson) gatherServer(
	acc telegraf.Accumulator,
	serverURL string,
) error {
	resp, responseTime, err := h.sendRequest(serverURL)

	if err != nil {
		return err
	}

	var jsonOut map[string]interface{}
	if err = json.Unmarshal([]byte(resp), &jsonOut); err != nil {
		return errors.New("Error decoding JSON response")
	}

	tags := map[string]string{
		"server": serverURL,
	}

	for _, tag := range h.TagKeys {
		switch v := jsonOut[tag].(type) {
		case string:
			tags[tag] = v
		}
		delete(jsonOut, tag)
	}

	if responseTime >= 0 {
		jsonOut["response_time"] = responseTime
	}
	f := internal.JSONFlattener{}
	err = f.FlattenJSON("", jsonOut)
	if err != nil {
		return err
	}

	var msrmnt_name string
	if h.Name == "" {
		msrmnt_name = "httpjson"
	} else {
		msrmnt_name = "httpjson_" + h.Name
	}
	acc.AddFields(msrmnt_name, f.Fields, tags)
	return nil
}
開發者ID:zooplus,項目名稱:telegraf,代碼行數:53,代碼來源:httpjson.go

示例3: gatherNodeStats

func (e *Elasticsearch) gatherNodeStats(url string, acc inputs.Accumulator) error {
	nodeStats := &struct {
		ClusterName string           `json:"cluster_name"`
		Nodes       map[string]*node `json:"nodes"`
	}{}
	if err := e.gatherData(url, nodeStats); err != nil {
		return err
	}
	for id, n := range nodeStats.Nodes {
		tags := map[string]string{
			"node_id":      id,
			"node_host":    n.Host,
			"node_name":    n.Name,
			"cluster_name": nodeStats.ClusterName,
		}

		for k, v := range n.Attributes {
			tags["node_attribute_"+k] = v
		}

		stats := map[string]interface{}{
			"indices":     n.Indices,
			"os":          n.OS,
			"process":     n.Process,
			"jvm":         n.JVM,
			"thread_pool": n.ThreadPool,
			"fs":          n.FS,
			"transport":   n.Transport,
			"http":        n.HTTP,
			"breakers":    n.Breakers,
		}

		now := time.Now()
		for p, s := range stats {
			f := internal.JSONFlattener{}
			err := f.FlattenJSON("", s)
			if err != nil {
				return err
			}
			acc.AddFields("elasticsearch_"+p, f.Fields, tags, now)
		}
	}
	return nil
}
開發者ID:CarterTsai,項目名稱:telegraf,代碼行數:44,代碼來源:elasticsearch.go

示例4: Gather

func (e *Exec) Gather(acc telegraf.Accumulator) error {
	out, err := e.runner.Run(e)
	if err != nil {
		return err
	}

	var jsonOut interface{}
	err = json.Unmarshal(out, &jsonOut)
	if err != nil {
		return fmt.Errorf("exec: unable to parse output of '%s' as JSON, %s",
			e.Command, err)
	}

	f := internal.JSONFlattener{}
	err = f.FlattenJSON("", jsonOut)
	if err != nil {
		return err
	}

	acc.AddFields("exec", f.Fields, nil)
	return nil
}
開發者ID:marcinbunsch,項目名稱:telegraf,代碼行數:22,代碼來源:exec.go

示例5: Parse

func (p *JsonParser) Parse(buf []byte) ([]telegraf.Metric, error) {

	metrics := make([]telegraf.Metric, 0)

	var jsonOut interface{}
	err := json.Unmarshal(buf, &jsonOut)
	if err != nil {
		err = fmt.Errorf("unable to parse out as JSON, %s", err)
		return nil, err
	}

	f := internal.JSONFlattener{}
	err = f.FlattenJSON("", jsonOut)
	if err != nil {
		return nil, err
	}

	metric, err := telegraf.NewMetric("exec", nil, f.Fields, time.Now().UTC())

	if err != nil {
		return nil, err
	}
	return append(metrics, metric), nil
}
開發者ID:henrypfhu,項目名稱:telegraf,代碼行數:24,代碼來源:parser.go


注:本文中的github.com/influxdata/telegraf/internal.JSONFlattener類示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。