@@ -24,12 +24,14 @@ import (
2424 "errors"
2525 "fmt"
2626 "math/big"
27+ "net"
2728 "net/http"
2829 "os"
2930 "path/filepath"
3031 godebug "runtime/debug"
3132 "strconv"
3233 "strings"
34+ "time"
3335
3436 "github.com/ethereum/go-ethereum/accounts"
3537 "github.com/ethereum/go-ethereum/accounts/keystore"
@@ -58,6 +60,8 @@ import (
5860 "github.com/ethereum/go-ethereum/internal/flags"
5961 "github.com/ethereum/go-ethereum/log"
6062 "github.com/ethereum/go-ethereum/metrics"
63+ "github.com/ethereum/go-ethereum/metrics/exp"
64+ "github.com/ethereum/go-ethereum/metrics/influxdb"
6165 "github.com/ethereum/go-ethereum/miner"
6266 "github.com/ethereum/go-ethereum/node"
6367 "github.com/ethereum/go-ethereum/p2p"
@@ -2139,6 +2143,54 @@ func RegisterSyncOverrideService(stack *node.Node, eth *eth.Ethereum, target com
21392143 syncer .Register (stack , eth , target , exitWhenSynced )
21402144}
21412145
2146+ // SetupMetrics configures the metrics system.
2147+ func SetupMetrics (cfg * metrics.Config ) {
2148+ if ! cfg .Enabled {
2149+ return
2150+ }
2151+ log .Info ("Enabling metrics collection" )
2152+ metrics .Enable ()
2153+
2154+ // InfluxDB exporter.
2155+ var (
2156+ enableExport = cfg .EnableInfluxDB
2157+ enableExportV2 = cfg .EnableInfluxDBV2
2158+ )
2159+ if cfg .EnableInfluxDB && cfg .EnableInfluxDBV2 {
2160+ Fatalf ("Flags %v can't be used at the same time" , strings .Join ([]string {MetricsEnableInfluxDBFlag .Name , MetricsEnableInfluxDBV2Flag .Name }, ", " ))
2161+ }
2162+ var (
2163+ endpoint = cfg .InfluxDBEndpoint
2164+ database = cfg .InfluxDBDatabase
2165+ username = cfg .InfluxDBUsername
2166+ password = cfg .InfluxDBPassword
2167+
2168+ token = cfg .InfluxDBToken
2169+ bucket = cfg .InfluxDBBucket
2170+ organization = cfg .InfluxDBOrganization
2171+ tagsMap = SplitTagsFlag (cfg .InfluxDBTags )
2172+ )
2173+ if enableExport {
2174+ log .Info ("Enabling metrics export to InfluxDB" )
2175+ go influxdb .InfluxDBWithTags (metrics .DefaultRegistry , 10 * time .Second , endpoint , database , username , password , "geth." , tagsMap )
2176+ } else if enableExportV2 {
2177+ log .Info ("Enabling metrics export to InfluxDB (v2)" )
2178+ go influxdb .InfluxDBV2WithTags (metrics .DefaultRegistry , 10 * time .Second , endpoint , token , bucket , organization , "geth." , tagsMap )
2179+ }
2180+
2181+ // Expvar exporter.
2182+ if cfg .HTTP != "" {
2183+ address := net .JoinHostPort (cfg .HTTP , fmt .Sprintf ("%d" , cfg .Port ))
2184+ log .Info ("Enabling stand-alone metrics HTTP endpoint" , "address" , address )
2185+ exp .Setup (address )
2186+ } else if cfg .HTTP == "" && cfg .Port != 0 {
2187+ log .Warn (fmt .Sprintf ("--%s specified without --%s, metrics server will not start." , MetricsPortFlag .Name , MetricsHTTPFlag .Name ))
2188+ }
2189+
2190+ // Enable system metrics collection.
2191+ go metrics .CollectProcessMetrics (3 * time .Second )
2192+ }
2193+
21422194// SplitTagsFlag parses a comma-separated list of k=v metrics tags.
21432195func SplitTagsFlag (tagsFlag string ) map [string ]string {
21442196 tags := strings .Split (tagsFlag , "," )
0 commit comments