github.com/turingchain2020/turingchain@v1.1.21/metrics/metrics.go (about)

     1  package metrics
     2  
     3  import (
     4  	"time"
     5  
     6  	turingchainlog "github.com/turingchain2020/turingchain/common/log/log15"
     7  	"github.com/turingchain2020/turingchain/metrics/influxdb"
     8  	"github.com/turingchain2020/turingchain/types"
     9  	go_metrics "github.com/rcrowley/go-metrics"
    10  )
    11  
    12  type influxDBPara struct {
    13  	// 以纳秒为单位
    14  	Duration  int64  `json:"duration,omitempty"`
    15  	URL       string `json:"url,omitempty"`
    16  	Database  string `json:"database,omitempty"`
    17  	Username  string `json:"username,omitempty"`
    18  	Password  string `json:"password,omitempty"`
    19  	Namespace string `json:"namespace,omitempty"`
    20  }
    21  
    22  var (
    23  	log = turingchainlog.New("module", "turingchain metrics")
    24  )
    25  
    26  //StartMetrics 根据配置文件相关参数启动m
    27  func StartMetrics(cfg *types.TuringchainConfig) {
    28  	metrics := cfg.GetModuleConfig().Metrics
    29  	if !metrics.EnableMetrics {
    30  		log.Info("Metrics data is not enabled to emit")
    31  		return
    32  	}
    33  
    34  	switch metrics.DataEmitMode {
    35  	case "influxdb":
    36  		sub := cfg.GetSubConfig().Metrics
    37  		subcfg, ok := sub[metrics.DataEmitMode]
    38  		if !ok {
    39  			log.Error("nil parameter for influxdb")
    40  		}
    41  		var influxdbcfg influxDBPara
    42  		types.MustDecode(subcfg, &influxdbcfg)
    43  		log.Info("StartMetrics with influxdb", "influxdbcfg.Duration", influxdbcfg.Duration,
    44  			"influxdbcfg.URL", influxdbcfg.URL,
    45  			"influxdbcfg.DatabaseName,", influxdbcfg.Database,
    46  			"influxdbcfg.Username", influxdbcfg.Username,
    47  			"influxdbcfg.Password", influxdbcfg.Password,
    48  			"influxdbcfg.Namespace", influxdbcfg.Namespace)
    49  		go influxdb.InfluxDB(go_metrics.DefaultRegistry,
    50  			time.Duration(influxdbcfg.Duration),
    51  			influxdbcfg.URL,
    52  			influxdbcfg.Database,
    53  			influxdbcfg.Username,
    54  			influxdbcfg.Password,
    55  			"")
    56  	default:
    57  		log.Error("startMetrics", "The dataEmitMode set is not supported now ", metrics.DataEmitMode)
    58  		return
    59  	}
    60  }