Skip to content

Commit

Permalink
stash
Browse files Browse the repository at this point in the history
  • Loading branch information
jingxu85 committed Nov 15, 2019
1 parent 6cdf0b3 commit 7b9b34a
Show file tree
Hide file tree
Showing 4 changed files with 100 additions and 1 deletion.
10 changes: 10 additions & 0 deletions Dockerfile
@@ -0,0 +1,10 @@
FROM golang:latest
ENV GOPATH=/app
ENV PATH=$GOPATH/bin:$PATH
WORKDIR /app/src/github.com/thetatoken/theta
COPY . .
RUN make install
RUN cp -r ./integration/testnet_amber ../
EXPOSE 28888
CMD theta start --config=../testnet_amber/node --password="qwertyuiop"

10 changes: 10 additions & 0 deletions cmd/theta/cmd/start.go
Expand Up @@ -14,6 +14,7 @@ import (
"github.com/spf13/viper"
"github.com/thetatoken/theta/cmd/thetacli/cmd/utils"
"github.com/thetatoken/theta/common"
"github.com/thetatoken/theta/common/metrics"
"github.com/thetatoken/theta/core"
"github.com/thetatoken/theta/crypto"
"github.com/thetatoken/theta/node"
Expand Down Expand Up @@ -257,6 +258,12 @@ func printWelcomeBanner() {
fmt.Println("")
fmt.Printf("Version %v, GitHash %s\nBuilt at %s\n", version.Version, version.GitHash, version.Timestamp)
fmt.Println("")
// var gs *metrics.GraphiteServer
// gs = metrics.GetGraphiteServer()
// fmt.Println("xj3 gs is %v \n", gs)
if gs := metrics.GetGraphiteServer(); gs != nil {
gs.OneTimeJob("online", "1")
}
}

func printExitBanner() {
Expand All @@ -276,4 +283,7 @@ func printExitBanner() {
fmt.Println(" #################################################### ")
fmt.Println("")
fmt.Println("")
if gs := metrics.GetGraphiteServer(); gs != nil {
gs.OneTimeJob("online", "0")
}
}
7 changes: 6 additions & 1 deletion common/config.go
Expand Up @@ -66,6 +66,9 @@ const (

// CfgGuardianRoundLength defines the length of a guardian voting round.
CfgGuardianRoundLength = "guardian.roundLength"

// Graphite Server to collet metrics
CfgMetricsServer = "metrics.server"
)

// Starting block heights of features.
Expand Down Expand Up @@ -106,8 +109,10 @@ func init() {
viper.SetDefault(CfgLogPrintSelfID, false)

viper.SetDefault(CfgGuardianRoundLength, 30)

viper.SetDefault(CfgLibP2PDiscoverable, true)
viper.SetDefault(CfgMetricsServer, "localhost:2003")
// viper.SetDefault(CfgMetricsServer, "35.160.150.13:2003")
}

// WriteInitialConfig writes initial config file to file system.
Expand Down
74 changes: 74 additions & 0 deletions common/metrics/graphite.go
Expand Up @@ -2,12 +2,18 @@ package metrics

import (
"bufio"
"encoding/hex"
"fmt"
"log"
"math/rand"
"net"
"os"
"strconv"
"strings"
"time"

"github.com/spf13/viper"
"github.com/thetatoken/theta/common"
)

// GraphiteConfig provides a container with configuration parameters for
Expand Down Expand Up @@ -111,3 +117,71 @@ func graphite(c *GraphiteConfig) error {
})
return nil
}

//--------------------new functions---------------------
var gs *GraphiteServer

type GraphiteServer struct {
Addr string // Network address to connect to
TCPaddr *net.TCPAddr
Hostname string
Prefix string
repeatable []*MetricJob
}

type MetricJob struct {
FlushInterval time.Duration // Flush interval
Prefix string // Prefix to be prepended to metric names
}

func GetGraphiteServer() *GraphiteServer {
if gs == nil {
if mserver := viper.GetString(common.CfgMetricsServer); mserver != "" {
addr, err := net.ResolveTCPAddr("tcp", mserver)
if err != nil {
log.Printf("can not connect to server")
return nil
}
gs = &GraphiteServer{
Addr: mserver,
TCPaddr: addr,
repeatable: make([]*MetricJob, 0),
}
// fmt.Printf("xj1 addr is %v, msserver is %s \n", addr, mserver)
}
hostname, err := os.Hostname()
if err != nil {
// Use random string if hostname is not available.
b := make([]byte, 10)
rand.Read(b)
hostname = hex.EncodeToString(b)
}
hostname = strings.Replace(hostname, ".", "_", -1)
gs.Hostname = hostname
prefix := fmt.Sprintf("%s.Theta.%s", viper.GetString(common.CfgLibP2PRendezvous), hostname)
gs.Prefix = prefix
}
return gs
}

func (gs *GraphiteServer) addJob() error {
return nil
}

func (gs *GraphiteServer) OneTimeJob(key string, value string) error {
conn, err := net.DialTCP("tcp", nil, gs.TCPaddr)
if err != nil {
return err
}
defer conn.Close()
message := fmt.Sprintf("%s.%s %s %d\n", gs.Prefix, key, value, time.Now().Unix())
if _, err := conn.Write([]byte(message)); err != nil {
return err
}
fmt.Printf("xj2 message is %s \n", message)
return nil
}

func (gs *GraphiteServer) String() string {
return fmt.Sprintf("GraphServer: address is %s, prefix is %s. \n", gs.Addr, gs.Prefix)
}

0 comments on commit 7b9b34a

Please sign in to comment.