提交 1b601ada 编写于 作者: H heyanlong

New agent

上级 927135da
package main
import (
"agent/agent/logger"
"agent/agent/pb/agent"
"agent/agent/pb/common"
"agent/agent/pb/register2"
......@@ -9,6 +10,7 @@ import (
"encoding/json"
"fmt"
"github.com/google/uuid"
"github.com/urfave/cli"
"google.golang.org/grpc"
"net"
"os"
......@@ -18,6 +20,8 @@ import (
"time"
)
var log = logger.Log
type PHPSkyBind struct {
Version int
AppId int32
......@@ -320,6 +324,24 @@ func heartbeat() {
}
func main() {
a := service.NewAgent()
a.Run()
app := cli.NewApp()
app.Name = "sky_php_agent"
app.Usage = "the skywalking trace sending agent"
app.Flags = []cli.Flag{
cli.StringFlag{Name: "grpc", Usage: "--grpc", Value: "127.0.0.1:10800"},
cli.StringFlag{Name: "socket", Usage: "--socket", Value: "/tmp/sky_agent.sock"},
cli.IntFlag{Name: "queue", Usage: "--queue", Value: 100},
}
app.Action = func(c *cli.Context) error {
a := service.NewAgent(c)
a.Run()
return nil
}
err := app.Run(os.Args)
if err != nil {
log.Errorln(err)
}
}
package logger
import (
"github.com/sirupsen/logrus"
"os"
)
var Log *logrus.Logger
func init() {
if Log == nil {
Log = logrus.New()
}
Log.SetOutput(os.Stdout)
Log.SetFormatter(&logrus.TextFormatter{
FullTimestamp: true,
TimestampFormat: "2006-01-02 15:04:05",
})
}
package service
import (
"agent/agent/logger"
"agent/agent/pb/agent"
"agent/agent/pb/agent2"
"agent/agent/pb/register2"
"container/list"
"context"
"fmt"
"github.com/sirupsen/logrus"
"github.com/urfave/cli"
"google.golang.org/grpc"
"io"
"net"
......@@ -15,15 +17,7 @@ import (
"time"
)
var log = logrus.New()
func init() {
log.SetOutput(os.Stdout)
log.SetFormatter(&logrus.TextFormatter{
FullTimestamp: true,
TimestampFormat: "2006-01-02 15:04:05",
})
}
var log = logger.Log
type register struct {
c net.Conn
......@@ -33,13 +27,15 @@ type register struct {
type grpcClient struct {
segmentClientV5 agent.TraceSegmentServiceClient
segmentClientV6 agent2.TraceSegmentReportServiceClient
pingClient5 agent.InstanceDiscoveryServiceClient
pintClient6 register2.ServiceInstancePingClient
streamV5 agent.TraceSegmentService_CollectClient
streamV6 agent2.TraceSegmentReportService_CollectClient
}
type Agent struct {
grpc string
conn *grpc.ClientConn
flag *cli.Context
grpcConn *grpc.ClientConn
grpcClient grpcClient
socket string
socketListener net.Listener
......@@ -49,9 +45,10 @@ type Agent struct {
queue *list.List
}
func NewAgent() *Agent {
func NewAgent(cli *cli.Context) *Agent {
var agent = &Agent{
socket: "/tmp/sky_agent.sock",
flag: cli,
socket: cli.String("socket"),
register: make(chan *register),
trace: make(chan string),
queue: list.New(),
......@@ -99,7 +96,7 @@ func (t *Agent) Run() {
log.Errorln(err)
}
}
err = t.conn.Close()
err = t.grpcConn.Close()
if err != nil {
log.Errorln(err)
}
......@@ -108,12 +105,14 @@ func (t *Agent) Run() {
func (t *Agent) connGRPC() {
var err error
t.conn, err = grpc.Dial(t.grpc, grpc.WithInsecure())
t.grpcConn, err = grpc.Dial(t.flag.String("grpc"), grpc.WithInsecure())
if err != nil {
log.Panic(err)
}
t.grpcClient.segmentClientV5 = agent.NewTraceSegmentServiceClient(t.conn)
t.grpcClient.segmentClientV6 = agent2.NewTraceSegmentReportServiceClient(t.conn)
t.grpcClient.segmentClientV5 = agent.NewTraceSegmentServiceClient(t.grpcConn)
t.grpcClient.segmentClientV6 = agent2.NewTraceSegmentReportServiceClient(t.grpcConn)
t.grpcClient.pingClient5 = agent.NewInstanceDiscoveryServiceClient(t.grpcConn)
t.grpcClient.pintClient6 = register2.NewServiceInstancePingClient(t.grpcConn)
ctx, cancel := context.WithTimeout(context.Background(), time.Second*3)
defer cancel()
......
......@@ -9,22 +9,16 @@ import (
)
func (t *Agent) heartbeat() {
var client5 agent.InstanceDiscoveryServiceClient
var client6 register2.ServiceInstancePingClient
t.registerCache.Range(func(key, value interface{}) bool {
fmt.Println("heartbeat => ...")
bind := value.(registerCache)
if bind.Version == 5 {
if client5 == nil {
client5 = agent.NewInstanceDiscoveryServiceClient(t.conn)
}
ctx, cancel := context.WithTimeout(context.Background(), time.Second*3)
defer cancel()
_, err := client5.Heartbeat(ctx, &agent.ApplicationInstanceHeartbeat{
_, err := t.grpcClient.pingClient5.Heartbeat(ctx, &agent.ApplicationInstanceHeartbeat{
ApplicationInstanceId: bind.InstanceId,
HeartbeatTime: time.Now().UnixNano() / 1000000,
})
......@@ -36,14 +30,10 @@ func (t *Agent) heartbeat() {
}
} else if bind.Version == 6 {
if client6 == nil {
client6 = register2.NewServiceInstancePingClient(t.conn)
}
ctx, cancel := context.WithTimeout(context.Background(), time.Second*3)
defer cancel()
_, err := client6.DoPing(ctx, &register2.ServiceInstancePingPkg{
_, err := t.grpcClient.pintClient6.DoPing(ctx, &register2.ServiceInstancePingPkg{
ServiceInstanceId: bind.InstanceId,
Time: time.Now().UnixNano() / 1000000,
ServiceInstanceUUID: bind.Uuid,
......
......@@ -4,5 +4,6 @@ require (
github.com/golang/protobuf v1.3.1
github.com/google/uuid v1.1.1
github.com/sirupsen/logrus v1.4.2
github.com/urfave/cli v1.22.1
google.golang.org/grpc v1.21.1
)
cloud.google.com/go v0.26.0/go.mod h1:aQUYkXzVsufM+DwF1aE+0xfcU+56JwCaLick0ClmMTw=
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
github.com/client9/misspell v0.3.4/go.mod h1:qj6jICC3Q7zFZvVWo7KLAzC3yx5G7kyvSDkc90ppPyw=
github.com/cpuguy83/go-md2man/v2 v2.0.0-20190314233015-f79a8a8ca69d h1:U+s90UTSYgptZMwQh2aRr3LuazLJIa+Pg3Kc1ylSYVY=
github.com/cpuguy83/go-md2man/v2 v2.0.0-20190314233015-f79a8a8ca69d/go.mod h1:maD7wRr/U5Z6m/iR4s+kqSMx2CaBsrgA7czyZG/E6dU=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b/go.mod h1:SBH7ygxi8pfUlaOkMMuAQtPIUF8ecWP5IEl/CR7VP2Q=
github.com/golang/mock v1.1.1/go.mod h1:oTYuIxOrZwtPieC+H1uAHpcLFnEyAGVDL/k47Jfbm0A=
......@@ -13,10 +15,16 @@ github.com/google/uuid v1.1.1 h1:Gkbcsh/GbpXz7lPftLA3P6TYMwjCLYm83jiFQZF/3gY=
github.com/google/uuid v1.1.1/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/konsorten/go-windows-terminal-sequences v1.0.1/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/russross/blackfriday/v2 v2.0.1 h1:lPqVAte+HuHNfhJ/0LC98ESWRz8afy9tM/0RK8m9o+Q=
github.com/russross/blackfriday/v2 v2.0.1/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
github.com/shurcooL/sanitized_anchor_name v1.0.0 h1:PdmoCO6wvbs+7yrJyMORt4/BmY5IYyJwS/kOiWx8mHo=
github.com/shurcooL/sanitized_anchor_name v1.0.0/go.mod h1:1NzhyTcUVG4SuEtjjoZeVRXNmyL/1OwPU0+IJeTBvfc=
github.com/sirupsen/logrus v1.4.2 h1:SPIRibHv4MatM3XXNO2BJeFLZwZ2LvZgfQ5+UNI2im4=
github.com/sirupsen/logrus v1.4.2/go.mod h1:tLMulIdttU9McNUspp0xgXVQah82FyeX6MwdIuYE2rE=
github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
github.com/urfave/cli v1.22.1 h1:+mkCCcOFKPnCmVYVcURKps1Xe+3zP90gSYGNfRkjoIY=
github.com/urfave/cli v1.22.1/go.mod h1:Gos4lmkARVdJ6EkW0WaNv/tZAAMe9V7XWyB60NtXRu0=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/lint v0.0.0-20190313153728-d0100b6bd8b3/go.mod h1:6SW0HCj/g11FgYtHlgUYUwCkIfeOF89ocIRzGO/8vkc=
golang.org/x/net v0.0.0-20190311183353-d8887717615a h1:oWX7TPOiFAMXLq8o0ikBYfCJVlRHBcsciT5bXOrH628=
......@@ -35,4 +43,6 @@ google.golang.org/genproto v0.0.0-20180817151627-c66870c02cf8 h1:Nw54tB0rB7hY/N0
google.golang.org/genproto v0.0.0-20180817151627-c66870c02cf8/go.mod h1:JiN7NxoALGmiZfu7CAH4rXhgtRTLTxftemlI0sWmxmc=
google.golang.org/grpc v1.21.1 h1:j6XxA85m/6txkUCHvzlV5f+HBNl/1r5cZ2A/3IEFOO8=
google.golang.org/grpc v1.21.1/go.mod h1:oYelfM1adQP15Ek0mdvEgi9Df8B9CZIaU1084ijfRaM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
honnef.co/go/tools v0.0.0-20190102054323-c2f93a96b099/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册