Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
SkyAPM
SkyAPM-php-sdk
提交
5859a7f9
S
SkyAPM-php-sdk
项目概览
SkyAPM
/
SkyAPM-php-sdk
通知
14
Star
1
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
S
SkyAPM-php-sdk
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
5859a7f9
编写于
6月 26, 2019
作者:
H
heyanlong
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
agent
上级
2a65b024
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
251 addition
and
6 deletion
+251
-6
src/agent/agent.go
src/agent/agent.go
+36
-6
src/agent/service/trace.go
src/agent/service/trace.go
+215
-0
未找到文件。
src/agent/agent.go
浏览文件 @
5859a7f9
...
...
@@ -2,6 +2,7 @@ package main
import
(
"agent/agent/pb5"
"agent/agent/service"
"context"
"encoding/json"
"fmt"
...
...
@@ -151,14 +152,9 @@ func register(c net.Conn, j string) {
log
.
Println
(
"register => "
,
err
)
log
.
Println
(
"register => Start register error..."
)
}
}
}
func
sendTrace
(
json
string
)
{
fmt
.
Println
(
json
)
}
func
handleConn
(
c
net
.
Conn
)
{
defer
func
()
{
err
:=
recover
()
...
...
@@ -191,7 +187,7 @@ func handleConn(c net.Conn) {
go
register
(
c
,
body
[
1
:
])
}
else
if
body
[
:
1
]
==
"1"
{
log
.
Println
(
"Service send trace protocol"
)
go
se
ndTrace
(
body
[
1
:
])
go
se
rvice
.
SendTrace
(
grpcConn
,
body
[
1
:
])
}
json
=
json
[
endIndex
+
1
:
]
}
else
{
...
...
@@ -201,9 +197,41 @@ func handleConn(c net.Conn) {
}
}
func
heartbeat
()
{
defer
func
()
{
err
:=
recover
()
if
err
!=
nil
{
log
.
Println
(
"System error[register]:"
,
err
)
go
heartbeat
()
}
}()
for
{
registerMap
.
Range
(
func
(
key
,
value
interface
{})
bool
{
log
.
Println
(
"heartbeat => ..."
)
bind
:=
value
.
(
PHPSkyBind
)
c
:=
pb5
.
NewInstanceDiscoveryServiceClient
(
grpcConn
)
ctx
,
cancel
:=
context
.
WithTimeout
(
context
.
Background
(),
time
.
Second
*
3
)
defer
cancel
()
_
,
err
:=
c
.
Heartbeat
(
ctx
,
&
pb5
.
ApplicationInstanceHeartbeat
{
ApplicationInstanceId
:
bind
.
InstanceId
,
HeartbeatTime
:
time
.
Now
()
.
UnixNano
(),
})
if
err
!=
nil
{
log
.
Println
(
"heartbeat =>"
,
err
)
}
return
true
})
time
.
Sleep
(
time
.
Second
*
40
)
}
}
func
main
()
{
// connection to sky server
log
.
Println
(
"hello"
)
var
err
error
grpcConn
,
err
=
grpc
.
Dial
(
"172.16.68.37:11800"
,
grpc
.
WithInsecure
())
...
...
@@ -223,6 +251,8 @@ func main() {
}
defer
l
.
Close
()
go
heartbeat
()
for
{
c
,
err
:=
l
.
Accept
()
if
err
!=
nil
{
...
...
src/agent/service/trace.go
0 → 100644
浏览文件 @
5859a7f9
package
service
import
(
"agent/agent/pb5"
"context"
"encoding/json"
"github.com/golang/protobuf/proto"
"google.golang.org/grpc"
"log"
"strconv"
"strings"
"time"
)
type
trace
struct
{
ApplicationInstance
int32
`json:"application_instance"`
Pid
int
`json:"pid"`
ApplicationId
int32
`json:"application_id"`
Version
int
`json:"version"`
Segment
segment
`json:"segment"`
GlobalTraceIds
[]
string
`json:"globalTraceIds"`
}
type
segment
struct
{
TraceSegmentId
string
`json:"traceSegmentId"`
IsSizeLimited
int
`json:"isSizeLimited"`
Spans
[]
span
`json:"spans"`
}
type
span
struct
{
Tags
map
[
string
]
string
`json:"tags"`
SpanId
int32
`json:"spanId"`
ParentSpanId
int32
`json:"parentSpanId"`
StartTime
int64
`json:"startTime"`
OperationName
string
`json:"operationName"`
Peer
string
`json:"peer"`
SpanType
int32
`json:"spanType"`
SpanLayer
int32
`json:"spanLayer"`
ComponentId
int32
`json:"componentId"`
ComponentName
string
`json:"component"`
Refs
[]
ref
`json:"refs"`
EndTime
int64
`json:"endTime"`
IsError
int
`json:"isError"`
}
type
ref
struct
{
Type
int32
`json:"type"`
ParentTraceSegmentId
string
`json:"parentTraceSegmentId"`
ParentSpanId
int32
`json:"parentSpanId"`
ParentApplicationInstanceId
int32
`json:"parentApplicationInstanceId"`
NetworkAddress
string
`json:"networkAddress"`
EntryApplicationInstanceId
int32
`json:"entryApplicationInstanceId"`
EntryServiceName
string
`json:"entryServiceName"`
ParentServiceName
string
`json:"parentServiceName"`
}
func
SendTrace
(
conn
*
grpc
.
ClientConn
,
j
string
)
{
info
:=
trace
{}
err
:=
json
.
Unmarshal
([]
byte
(
j
),
&
info
)
if
err
!=
nil
{
log
.
Println
(
"trace => "
,
err
)
return
}
log
.
Println
(
"trace => Start trace..."
)
c
:=
pb5
.
NewTraceSegmentServiceClient
(
conn
)
ctx
,
cancel
:=
context
.
WithTimeout
(
context
.
Background
(),
time
.
Second
*
3
)
defer
cancel
()
client
,
err
:=
c
.
Collect
(
ctx
)
if
err
!=
nil
{
log
.
Println
(
"trace => "
,
err
)
return
}
var
globalTrace
[]
*
pb5
.
UniqueId
for
_
,
v
:=
range
info
.
GlobalTraceIds
{
globalTrace
=
append
(
globalTrace
,
buildUniqueId
(
v
))
}
var
spans
[]
*
pb5
.
SpanObject
for
_
,
v
:=
range
info
.
Segment
.
Spans
{
span
:=
&
pb5
.
SpanObject
{
SpanId
:
v
.
SpanId
,
ParentSpanId
:
v
.
ParentSpanId
,
StartTime
:
v
.
StartTime
,
EndTime
:
v
.
EndTime
,
OperationName
:
v
.
OperationName
,
Peer
:
v
.
Peer
,
Component
:
v
.
ComponentName
,
IsError
:
v
.
IsError
!=
0
,
}
if
v
.
ComponentId
!=
0
{
span
.
ComponentId
=
v
.
ComponentId
}
if
v
.
SpanType
==
0
{
span
.
SpanType
=
pb5
.
SpanType_Entry
}
else
if
v
.
SpanType
==
1
{
span
.
SpanType
=
pb5
.
SpanType_Exit
}
else
if
v
.
SpanType
==
2
{
span
.
SpanType
=
pb5
.
SpanType_Local
}
if
v
.
SpanLayer
==
3
{
span
.
SpanLayer
=
pb5
.
SpanLayer_Http
}
else
if
v
.
SpanLayer
==
1
{
span
.
SpanLayer
=
pb5
.
SpanLayer_Database
}
buildTags
(
span
,
v
.
Tags
)
buildRefs
(
span
,
v
.
Refs
)
spans
=
append
(
spans
,
span
)
}
segmentObject
:=
&
pb5
.
TraceSegmentObject
{
TraceSegmentId
:
buildUniqueId
(
info
.
Segment
.
TraceSegmentId
),
Spans
:
spans
,
ApplicationId
:
info
.
ApplicationId
,
ApplicationInstanceId
:
info
.
ApplicationInstance
,
IsSizeLimited
:
info
.
Segment
.
IsSizeLimited
!=
0
,
}
//m := jsonpb.Marshaler{
// EnumsAsInts: true,
//}
seg
,
err
:=
proto
.
Marshal
(
segmentObject
)
//fmt.Println(seg)
if
err
!=
nil
{
log
.
Println
(
"trace => "
,
err
)
return
}
segment
:=
&
pb5
.
UpstreamSegment
{
GlobalTraceIds
:
globalTrace
,
Segment
:
seg
,
}
err
=
client
.
Send
(
segment
)
if
err
!=
nil
{
log
.
Println
(
"trace => "
,
err
)
return
}
reply
,
err
:=
client
.
CloseAndRecv
()
if
err
!=
nil
{
log
.
Println
(
"trace =>"
,
err
)
}
log
.
Println
(
"trace => send ok"
)
log
.
Printf
(
"Route summary: %v"
,
reply
)
}
func
buildRefs
(
span
*
pb5
.
SpanObject
,
refs
[]
ref
)
{
// refs
var
spanRefs
[]
*
pb5
.
TraceSegmentReference
for
_
,
rev
:=
range
refs
{
var
refType
pb5
.
RefType
if
rev
.
Type
==
0
{
refType
=
pb5
.
RefType_CrossProcess
}
spanRefs
=
append
(
spanRefs
,
&
pb5
.
TraceSegmentReference
{
RefType
:
refType
,
ParentTraceSegmentId
:
buildUniqueId
(
rev
.
ParentTraceSegmentId
),
ParentSpanId
:
rev
.
ParentSpanId
,
ParentApplicationInstanceId
:
rev
.
ParentApplicationInstanceId
,
NetworkAddress
:
rev
.
NetworkAddress
,
EntryApplicationInstanceId
:
rev
.
EntryApplicationInstanceId
,
EntryServiceName
:
rev
.
EntryServiceName
,
ParentServiceName
:
rev
.
ParentServiceName
,
})
}
if
len
(
spanRefs
)
>
0
{
span
.
Refs
=
spanRefs
}
}
func
buildUniqueId
(
str
string
)
*
pb5
.
UniqueId
{
uniqueId
:=
&
pb5
.
UniqueId
{}
var
ids
[]
int64
for
_
,
idStr
:=
range
strings
.
Split
(
str
,
"."
)
{
id
,
err
:=
strconv
.
ParseInt
(
idStr
,
10
,
64
)
if
err
!=
nil
{
log
.
Println
(
"trace => "
,
err
)
panic
(
err
)
}
ids
=
append
(
ids
,
id
)
}
uniqueId
.
IdParts
=
ids
return
uniqueId
}
func
buildTags
(
span
*
pb5
.
SpanObject
,
t
map
[
string
]
string
)
{
// tags
var
tags
[]
*
pb5
.
KeyWithStringValue
for
k
,
v
:=
range
t
{
kv
:=
&
pb5
.
KeyWithStringValue
{
Key
:
k
,
Value
:
v
,
}
tags
=
append
(
tags
,
kv
)
}
if
len
(
tags
)
>
0
{
span
.
Tags
=
tags
}
}
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录