Hyperledger结构指标

时间:2018-06-04 08:37:58

标签: hyperledger-fabric blockchain hyperledger

只是想确认面料指标是否适用于任何人?我正在使用1.1.0版本。

metrics:
    # enable or disable metrics server
    enabled: true

    # when enable metrics server, must specific metrics reporter type
    # currently supported type: "statsd","prom"
    reporter: statsd

    # determines frequency of report metrics(unit: second)
    interval: 1s

    statsdReporter:

          # statsd server address to connect
          address: 172.18.19.29:8125

          # determines frequency of push metrics to statsd server(unit: second)
          flushInterval: 2s

          # max size bytes for each push metrics request
          # intranet recommend 1432 and internet recommend 512
          flushBytes: 1432

    promReporter:

          # prometheus http server listen address for pull metrics
          listenAddress: 0.0.0.0:8080

这是我的core.yaml配置,我尝试使用Prometheus和Statsd,但无法使用其中任何一种方法查看任何指标。我可以看到对等体中存在的代码来导出指标。

有人可以帮忙配置吗?

1 个答案:

答案 0 :(得分:0)

在我的环境中,我自己添加了一个出口商。 您不仅需要更改core.yaml中的配置,还需要使用指标包来实现自己的导出器。

成为出口商

  1. 初始化viper并加载core.yaml以在对等节点上启用指标功能。
  2. 通过viper使用core.yaml配置指标软件包。
  3. 设置指标标签。 (在下面的示例代码中,创建了标签hyperledger_fabric_peer_blocknum)
  4. 调用Start()函数。该功能将被阻止,直到完成导出为止。因此,我们需要将其作为go例程执行。
  5. 此外,您还需要在另一个执行例程中定期更新导出的指标。 (在下面的示例代码中,将块数用作指标)
  6. 在主线程中,开始侦听频道以等待中断。

package main

import (
	"encoding/json"
	"fmt"
	"os"
	"os/exec"
	"time"

	"github.com/hyperledger/fabric/common/flogging"
	"github.com/hyperledger/fabric/common/metrics"
	"github.com/hyperledger/fabric/peer/common"
)

var logger = flogging.MustGetLogger("fabexporter")

var block_num metrics.Gauge

func main() {

	err := common.InitConfig("core")
	if err != nil {
		logger.Error(err)
	}

	opts := metrics.NewOpts()
	if err := metrics.Init(opts); err != nil {
		logger.Error(err)
		return
	}

	s := metrics.RootScope.SubScope("peer")
	block_num = s.Gauge("blocknum")

	go startMonitor()
	go func() {
		metrics.RootScope.Start()
	}()

	server := make(chan int)
	<-server
}

type Retdata struct {
	Height            int    `json:"height"`
	CurrentBlockHash  string `json:"currentBlockHash"`
	PreviousBlockHash string `json:"previousBlockHash"`
}

func startMonitor() {
	var ret Retdata

	for {
		time.Sleep(5 * time.Second)

		cmd := exec.Command("peer", "channel", "getinfo", "-c", "mychannel")
		cmd.Env = os.Environ()
		cmd.Env = append(cmd.Env, "CORE_LOGGING_LEVEL=CRITICAL")
		out, err := cmd.Output()
		if err != nil {
			logger.Error(err)
			continue
		} else if len(out) == 0 {
			continue
		}

		jsond := ([]byte)(out[17:]) // To trim "Blockchain info: " 
		if err := json.Unmarshal(jsond, &ret); err != nil {
			logger.Error(err)
			break
		}

		logger.Info(fmt.Sprintf("num of block : %d\n", ret.Height))
		block_num.Update(float64(ret.Height))
	}
}

我也写了一篇关于它的文章。 希望能对您有所帮助。

https://medium.com/@nekiaiken/hyperledger-fabric-meets-prometheus-649c452ba26a