From 8518ba7029a70abb06efc4a065f5f3aaca61f665 Mon Sep 17 00:00:00 2001 From: weixipeng Date: Wed, 6 Sep 2017 13:06:13 +0800 Subject: [PATCH] new feature: 1. web template add pprof and prometheus Signed-off-by: weixipeng --- cmd/templates/web/conf/config.json | 9 ++++++++- cmd/templates/web/conf/configPrd.json | 3 ++- cmd/templates/web/main.go | 20 ++++++++++++++++++++ 3 files changed, 30 insertions(+), 2 deletions(-) diff --git a/cmd/templates/web/conf/config.json b/cmd/templates/web/conf/config.json index 5745271..bb95535 100644 --- a/cmd/templates/web/conf/config.json +++ b/cmd/templates/web/conf/config.json @@ -1,6 +1,13 @@ { "server": { "ip":"0.0.0.0", - "port": 8080 + "port": 8080, + "pprofPort": 8089 + }, + "metric": { + "pprofAddr": "127.0.0.1", + "pprofPort": 8089, + "prometheusNetType": "tcp", + "prometheusAddr": "127.0.0.1:8090" } } \ No newline at end of file diff --git a/cmd/templates/web/conf/configPrd.json b/cmd/templates/web/conf/configPrd.json index 5745271..71119b6 100644 --- a/cmd/templates/web/conf/configPrd.json +++ b/cmd/templates/web/conf/configPrd.json @@ -1,6 +1,7 @@ { "server": { "ip":"0.0.0.0", - "port": 8080 + "port": 8080, + "pprofPort": 8089 } } \ No newline at end of file diff --git a/cmd/templates/web/main.go b/cmd/templates/web/main.go index ced812f..0ba8852 100644 --- a/cmd/templates/web/main.go +++ b/cmd/templates/web/main.go @@ -2,9 +2,13 @@ package main import ( "flag" + "fmt" + "net" "xframe/cmd" + "xframe/cmd/templates/web/handler" "xframe/config" "xframe/log" + "xframe/metric" "xframe/server" ) @@ -25,6 +29,22 @@ func main() { panic(err) } config.DumpConfigContent() + pprofAddr, _ := config.GetConfigByKey("server.pprofAddr") + pprofPort, _ := config.GetConfigByKey("server.pprofPort") + + go func() { + pprof := fmt.Sprintf("%s:%d", pprofAddr, pprofPort) + log.INFOF("start to initialize metric at %s", pprof) + metric.InitPprof(pprof) + }() + + prometheusNet, _ := config.GetConfigByKey("metric.prometheusNetType") + prometheusAddr, _ := config.GetConfigByKey("metric.prometheusAddr") + if l, err := net.Listen(prometheusNet.(string), prometheusAddr.(string)); err != nil { + panic(err) + } else { + metric.ServePrometheus(l) + } addr, _ := config.GetConfigByKey("server.ip") port, _ := config.GetConfigByKey("server.port")