| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973 |
- package otlptrace
- import (
- "crypto/md5"
- "encoding/json"
- "fmt"
- "os"
- "sort"
- "strconv"
- "strings"
- "sync"
- "time"
- "go.opentelemetry.io/otel/exporters/otlp/otlptrace/internal/tracetransform"
- tracesdk "go.opentelemetry.io/otel/sdk/trace"
- tracepb "go.opentelemetry.io/proto/otlp/trace/v1"
- )
- const (
- ProtocolTrace int = 200
- ProtocolHTTP int = 1
- ProtocolPostgres int = 2
- ProtocolRedis int = 3
- ProtocolMemcached int = 4
- ProtocolMysql int = 5
- ProtocolMongo int = 6
- ProtocolKafka int = 7
- ProtocolCassandra int = 8
- ProtocolRabbitmq int = 9
- ProtocolNats int = 10
- ProtocolHTTP2 int = 11
- ProtocolDubbo2 int = 12
- ProtocolDNS int = 13
- ProtocolDM int = 14
- )
- const (
- APP_SERVICE_TYPE = "APPLICATION"
- SQL_SERVICE_TYPE = "SQL"
- NOSQL_SERVICE_TYPE = "NOSQL"
- HTTP_SERVICE_TYPE = "HTTP"
- )
- const (
- GO_SERVICE_NAME = "GO"
- MYSQL_SERVICE_NAME = "MYSQL"
- DM_SERVICE_NAME = "DM"
- REDIS_SERVICE_NAME = "REDIS"
- HTTP_SERVICE_NAME = "HTTPCLIENT"
- )
- type apmTraceSpan tracesdk.ReadOnlySpan
- // GO:0:10154813500555812:5450531005555981:5610250100539899:ee022542c3940f1b:1001025098564810:888ceb3df1bdbe2c:110
- type RootDataT struct {
- AccountId int `json:"account_id"`
- AgentId int64 `json:"agent_id"`
- AgentVersion string `json:"agent_version"`
- AppId int64 `json:"app_id"`
- AppIdFrom int64 `json:"app_id_from"` // from header app_id
- AppName string `json:"app_name"`
- CalledId int64 `json:"called_id"` // from header assumed_app_id
- ClientIp string `json:"client_ip"`
- CollTime uint64 `json:"coll_time"`
- Cpu int `json:"cpu"`
- Custom string `json:"custom"`
- HostId int64 `json:"host_id"`
- HostName string `json:"host_name"`
- HttpCode int64 `json:"http_code"`
- HttpMethod string `json:"http_method"`
- InstanceId int64 `json:"instance_id"`
- InstanceIdFrom int64 `json:"instance_id_from"` // from header instance_id
- LocalPort int64 `json:"local_port"`
- Maps []MapInfoT `json:"maps"`
- MemU int `json:"mem_u"`
- MemUP int `json:"mem_u_p"`
- OperType string `json:"oper_type"`
- Parameters []interface{} `json:"parameters"`
- ParentTaskName int `json:"parent_task_name"`
- Period int `json:"period"`
- RespTime uint64 `json:"resp_time"`
- Sampling int `json:"sampling"`
- ServiceName string `json:"service_name"`
- ServiceType string `json:"service_type"`
- Sip string `json:"sip"`
- Sn string `json:"sn"`
- SpanIdFrom string `json:"span_id_from"` // from header span_id
- Sport int64 `json:"sport"`
- TId int `json:"t_id"`
- TName string `json:"t_name"`
- TraceId string `json:"trace_id"` // from header trace_id
- TransIds []interface{} `json:"trans_ids"`
- TypeFrom string `json:"type_from"`
- Uri string `json:"uri"`
- UserDir int `json:"user_dir"`
- VipIds []interface{} `json:"vip_ids"`
- }
- type MapInfoT struct {
- Dbn string `json:"dbn,omitempty"`
- Exception int `json:"exception,omitempty"`
- ExceptionMsg string `json:"exception_msg,omitempty"`
- ExceptionStack string `json:"exception_stack,omitempty"`
- Ip string `json:"ip,omitempty"`
- Level int `json:"level"`
- MethodDesc string `json:"method_desc,omitempty"`
- MethodName string `json:"method_name"`
- Nid int `json:"nid"`
- OperType string `json:"oper_type,omitempty"`
- Pid int `json:"pid"`
- Port int64 `json:"port,omitempty"`
- Ps []string `json:"ps,omitempty"`
- PureTime uint64 `json:"pure_time"`
- ServiceName string `json:"service_name"`
- ServiceType string `json:"service_type"`
- StartTime uint64 `json:"start_time"`
- EndTime uint64 `json:"end_time"`
- WallTime uint64 `json:"wall_time"`
- Schema string `json:"schema,omitempty"`
- AssumedAppId int64 `json:"assumed_app_id,omitempty"`
- Uri string `json:"uri,omitempty"`
- SpanId string `json:"span_id,omitempty"`
- }
- type TraceMapT struct {
- RootData RootDataT
- Index int
- lock *sync.RWMutex
- TheEnd bool
- }
- var TraceRootMap map[string]*TraceMapT
- func init() {
- TraceRootMap = make(map[string]*TraceMapT)
- go func() {
- for {
- //fmt.Println(G_sdl)
- time.Sleep(5 * time.Second)
- }
- }()
- }
- var G_sdl int
- func tracetransformData(sdl []tracesdk.ReadOnlySpan) map[int][]RootDataT {
- G_sdl += len(sdl)
- if len(sdl) == 0 {
- return nil
- }
- // 多次请求 sdl
- sendDataMap := make(map[int][]RootDataT)
- //sendData := []RootDataT{}
- for _, sd := range sdl {
- if sd == nil {
- continue
- }
- //traceId := sd.SpanContext().TraceID().String()
- fmt.Println("------event_num---- "+sd.Name(), "--->", len(sd.Events())) // 一次请求完整数据
- // 构建map *RootDataT
- var rootData RootDataT
- // todo 应用注册逻辑
- if os.Getenv("JAVA") == "1" {
- rootData = initRootDataJava()
- } else {
- rootData = initRootDataFromEvent()
- }
- // build http入口 MapInfoT
- code_type := buildAppMapFromEvent(&rootData, sd)
- // 构建maps
- for _, event := range sd.Events() {
- aaa, _ := json.Marshal(event)
- fmt.Println("event.info", string(aaa))
- mNode := buildMapNodeFromEvent(event)
- switch event.EventType {
- // stack
- case 11:
- // l7 event
- case 10:
- switch event.ProtocolType {
- // http
- case ProtocolHTTP:
- buildHttpMapFromEvent(&mNode, event)
- // mysql
- case ProtocolMysql:
- buildMysqlMapEvent(&mNode, event)
- // redis
- case ProtocolRedis:
- buildRedisMapEvent(&mNode, event)
- // dm
- case ProtocolDM:
- buildDMMapEvent(&mNode, event)
- }
- }
- rootData.Maps = append(rootData.Maps, mNode)
- fmt.Println(event.Name)
- //buildAndAssemblyMapFromEvent(event, rootData)
- }
- buildLevelFromEvent(&rootData)
- sendDataMap[code_type] = append(sendDataMap[code_type], rootData)
- //a, _ := json.Marshal(rootData)
- //fmt.Println(string(a))
- //sendData = append(sendData, rootData)
- //if _, ok := TraceRootMap[traceId]; !ok {
- //TraceRootMap[traceId] = &TraceMapT{RootData: initRootData(traceId), Index: 1}
- //}
- //TraceRootMap[traceId].Index++
- //buildAndAssemblyMap(sd, TraceRootMap[traceId])
- }
- // 发送完整数据 | 大量长耗时请求会增加内存占用
- //sendData := []RootDataT{}
- //for traceId, v := range TraceRootMap {
- // if v.TheEnd {
- // buildLevel(v)
- // sendData = append(sendData, v.RootData)
- // delete(TraceRootMap, traceId)
- // //fmt.Println("the end!")
- // } else {
- // //fmt.Println("not end!")
- // }
- //}
- //Transform the categorized map into a slice
- aa, _ := json.Marshal(sendDataMap)
- fmt.Println(string(aa))
- //fmt.Println(len(sendData))
- //fmt.Println("sdl len:", len(sdl))
- return sendDataMap
- }
- type TimeMap struct {
- Time uint64
- Type int
- Map *MapInfoT
- }
- //type TraceMapT struct {
- // RootData RootDataT
- // Index int
- // lock *sync.RWMutex
- // TheEnd bool
- //}
- func buildLevel(sdl *TraceMapT) {
- nidMap := make(map[int]*MapInfoT)
- mapSlice := []TimeMap{}
- for i, v := range sdl.RootData.Maps {
- if v.ServiceType == "APPLICATION" {
- continue
- }
- nidMap[v.Nid] = &sdl.RootData.Maps[i]
- timeStartMap := TimeMap{
- Time: v.StartTime,
- Type: 0,
- Map: &sdl.RootData.Maps[i],
- }
- mapSlice = append(mapSlice, timeStartMap)
- timeEndMap := TimeMap{
- Time: v.EndTime,
- Type: 1,
- Map: &sdl.RootData.Maps[i],
- }
- mapSlice = append(mapSlice, timeEndMap)
- }
- sort.Slice(mapSlice, func(i, j int) bool {
- return mapSlice[i].Time < mapSlice[j].Time
- })
- funStack := []TimeMap{}
- currentNid := 1
- Nid := 2
- level := 2
- for _, v := range mapSlice {
- // fmt.Println("SliceSliceindex", k, "value", v.Time, v.Type, v.Map.MethodName, v.Map.Nid)
- if v.Type == 0 {
- // 函数入口
- funStack = append(funStack, v)
- v.Map.Pid = currentNid
- v.Map.Level = level
- v.Map.Nid = Nid
- currentNid = Nid
- level += 1
- Nid += 1
- } else if v.Type == 1 {
- // 函数出口
- len := len(funStack)
- funStack = funStack[:len-1]
- if (len - 2) < 0 {
- currentNid = 1
- } else {
- currentNid = funStack[len-2].Map.Nid
- }
- level -= 1
- }
- }
- }
- func buildLevelFromEvent(sdl *RootDataT) {
- nidMap := make(map[int]*MapInfoT)
- mapSlice := []TimeMap{}
- for i, v := range sdl.Maps {
- if v.ServiceType == "APPLICATION" {
- continue
- }
- nidMap[v.Nid] = &sdl.Maps[i]
- timeStartMap := TimeMap{
- Time: v.StartTime,
- Type: 0,
- Map: &sdl.Maps[i],
- }
- mapSlice = append(mapSlice, timeStartMap)
- timeEndMap := TimeMap{
- Time: v.EndTime,
- Type: 1,
- Map: &sdl.Maps[i],
- }
- mapSlice = append(mapSlice, timeEndMap)
- }
- sort.Slice(mapSlice, func(i, j int) bool {
- return mapSlice[i].Time < mapSlice[j].Time
- })
- funStack := []TimeMap{}
- currentNid := 1
- Nid := 2
- level := 2
- for k, v := range mapSlice {
- fmt.Println("SliceSliceindex", k, "value", v.Time, v.Type, v.Map.MethodName, v.Map.Nid)
- if v.Type == 0 {
- // 函数入口
- funStack = append(funStack, v)
- v.Map.Pid = currentNid
- v.Map.Level = level
- v.Map.Nid = Nid
- currentNid = Nid
- level += 1
- Nid += 1
- } else if v.Type == 1 {
- // 函数出口
- len := len(funStack)
- funStack = funStack[:len-1]
- if (len - 2) < 0 {
- currentNid = 1
- } else {
- currentNid = funStack[len-2].Map.Nid
- }
- level -= 1
- }
- }
- }
- func initRootData(traceId string) RootDataT {
- data := RootDataT{
- AccountId: 110,
- AgentId: 1011005252979954, // TODO 更新 基于 ip:port + process_name + exe路径生成
- AgentVersion: "2.1.0",
- AppId: 5410049101545798, // TODO 更新 基于appname生成
- AppIdFrom: -1,
- AppName: "eBPF-agent", // TODO 更新 ip:port || process_name
- CalledId: -1,
- ClientIp: "",
- CollTime: 0,
- Cpu: 0,
- Custom: "",
- HostId: 10154813500555812,
- HostName: "localhost",
- HttpCode: 0,
- HttpMethod: "",
- InstanceId: 1005051101515357, // TODO 更新 基于ip:port
- InstanceIdFrom: -1,
- Maps: []MapInfoT{},
- MemU: 0,
- MemUP: 0,
- OperType: "",
- Parameters: []interface{}{},
- ParentTaskName: 0,
- Period: -1,
- RespTime: 0,
- Sampling: 0,
- ServiceName: "GO",
- ServiceType: APP_SERVICE_TYPE,
- Sip: "",
- Sn: "",
- SpanIdFrom: "",
- Sport: 0,
- TId: -1,
- TName: "",
- TraceId: traceId,
- TransIds: []interface{}{},
- TypeFrom: "",
- Uri: "",
- UserDir: 0,
- VipIds: []interface{}{},
- }
- return data
- }
- func initRootDataFromEvent() RootDataT {
- data := RootDataT{
- AccountId: 110,
- AgentId: 1011005252979954, // TODO 更新 基于 ip:port + process_name + exe路径生成
- AgentVersion: "2.1.0",
- AppId: 5410049101545798, // TODO 更新 基于appname生成
- AppIdFrom: -1,
- AppName: "eBPF-agent", // TODO 更新 ip:port || process_name
- CalledId: -1,
- ClientIp: "",
- CollTime: 0,
- Cpu: 0,
- Custom: "",
- HostId: 10154813500555812,
- HostName: "localhost",
- HttpCode: 0,
- HttpMethod: "",
- InstanceId: 1005051101515357, // TODO 更新 基于ip:port
- InstanceIdFrom: -1,
- Maps: []MapInfoT{},
- MemU: 0,
- MemUP: 0,
- OperType: "",
- Parameters: []interface{}{},
- ParentTaskName: 0,
- Period: -1,
- RespTime: 0,
- Sampling: 0,
- ServiceName: "GO",
- ServiceType: APP_SERVICE_TYPE,
- Sip: "",
- Sn: "",
- SpanIdFrom: "",
- Sport: 0,
- TId: -1,
- TName: "",
- TraceId: "",
- TransIds: []interface{}{},
- TypeFrom: "",
- Uri: "",
- UserDir: 0,
- VipIds: []interface{}{},
- }
- return data
- }
- func initRootDataJava() RootDataT {
- data := RootDataT{
- AccountId: 110,
- AgentId: 3934815089541000, // TODO 更新 基于 ip:port + process_name + exe路径生成
- AgentVersion: "2.21.0",
- AppId: 3365853273187618, // TODO 更新 基于appname生成
- AppIdFrom: -1,
- AppName: "eBPF-javaApplication", // TODO 更新 ip:port || process_name
- CalledId: -1,
- ClientIp: "",
- CollTime: 0,
- Cpu: 0,
- Custom: "",
- HostId: 2315065183171055,
- HostName: "localhost",
- HttpCode: 0,
- HttpMethod: "",
- InstanceId: 1128864082033413, // TODO 更新 基于ip:port
- InstanceIdFrom: -1,
- Maps: []MapInfoT{},
- MemU: 0,
- MemUP: 0,
- OperType: "",
- Parameters: []interface{}{},
- ParentTaskName: 0,
- Period: -1,
- RespTime: 0,
- Sampling: 0,
- ServiceName: "TOMCAT",
- ServiceType: APP_SERVICE_TYPE,
- Sip: "",
- Sn: "",
- SpanIdFrom: "",
- Sport: 0,
- TId: -1,
- TName: "",
- TraceId: "",
- TransIds: []interface{}{},
- TypeFrom: "",
- Uri: "",
- UserDir: 0,
- VipIds: []interface{}{},
- }
- return data
- }
- func initMapNode(spanSd *tracepb.Span) (MapInfoT, string) {
- mNode := MapInfoT{
- Exception: 0,
- ExceptionMsg: "",
- ExceptionStack: "",
- Ip: "",
- Level: 2,
- Pid: 1,
- Port: 0,
- Ps: []string{},
- ServiceName: "",
- ServiceType: "",
- WallTime: 0,
- }
- mNode.MethodName = spanSd.Name
- mNode.PureTime = (spanSd.EndTimeUnixNano - spanSd.StartTimeUnixNano) / 1e3
- mNode.WallTime = mNode.PureTime
- mNode.StartTime = spanSd.StartTimeUnixNano
- mNode.EndTime = spanSd.EndTimeUnixNano
- for _, attr := range spanSd.GetAttributes() {
- fmt.Println(attr.Key, ":", attr.Value.GetValue())
- switch attr.Key {
- case "nid":
- mNode.Nid = int(attr.Value.GetIntValue())
- case "pid":
- mNode.Pid = int(attr.Value.GetIntValue())
- case "level":
- mNode.Level = int(attr.Value.GetIntValue())
- }
- }
- return mNode, spanSd.Name
- }
- func buildMapNodeFromEvent(event tracesdk.Event) MapInfoT {
- mNode := MapInfoT{
- Exception: 0,
- ExceptionMsg: "",
- ExceptionStack: "",
- Ip: "",
- Level: 2,
- Pid: 1,
- Port: 0,
- Ps: []string{},
- ServiceName: "",
- ServiceType: "",
- WallTime: 0,
- }
- mNode.MethodName = event.Name
- //mNode.PureTime = (event.EndTimeUnixNano - event.StartTimeUnixNano) / 1e3
- //mNode.WallTime = mNode.PureTime
- //mNode.StartTime = spanSd.StartTimeUnixNano
- //mNode.EndTime = spanSd.EndTimeUnixNano
- for _, attr := range event.Attributes {
- fmt.Println(event.Name, "--->buildMapNodeFromEvent--->", attr.Key, ":", attr.Value.AsInterface())
- switch attr.Key {
- case "nid":
- mNode.Nid = int(attr.Value.AsInt64())
- case "pid":
- mNode.Pid = int(attr.Value.AsInt64())
- case "level":
- mNode.Level = int(attr.Value.AsInt64())
- case "time.start_at":
- mNode.StartTime = uint64(attr.Value.AsInt64())
- case "time.end_at":
- mNode.EndTime = uint64(attr.Value.AsInt64())
- case "time.duration":
- //mNode.PureTime = uint64(attr.Value.AsInt64()) / 1e3
- mNode.WallTime = uint64(attr.Value.AsInt64()) / 1e3
- }
- }
- return mNode
- }
- // 构建拼装
- func buildAndAssemblyMap(sd apmTraceSpan, traceRoot *TraceMapT) MapInfoT {
- mNode, mapType := initMapNode(span(sd))
- switch mapType {
- case "APPLICATION":
- buildAppMap(&mNode, traceRoot, sd)
- traceRoot.TheEnd = true
- case "HTTP":
- buildHttpMap(&mNode, sd)
- case "Mysql":
- buildMysqlMap(&mNode, sd)
- case "Redis":
- buildRedisMap(&mNode, sd)
- }
- if mapType != "" {
- mNode.Nid = traceRoot.Index
- traceRoot.RootData.Maps = append(traceRoot.RootData.Maps, mNode)
- }
- return mNode
- }
- //func buildAndAssemblyMapFromEvent(event tracesdk.Event, traceRoot *RootDataT) MapInfoT {
- // mNode := buildMapNodeFromEvent(event)
- // switch mapType {
- // case "HTTP":
- // buildHttpMapFromEvent(mNode, event)
- // //case "Mysql":
- // // buildMysqlMap(mNode, sd)
- // //case "Redis":
- // // buildRedisMap(mNode, sd)
- // }
- // if mapType != "" {
- // //mNode.Nid = traceRoot.Index
- // traceRoot.Maps = append(traceRoot.Maps, mNode)
- // }
- // return mNode
- //}
- func buildAppMap(mNode *MapInfoT, traceRoot *TraceMapT, sd apmTraceSpan) {
- mNode.ServiceName = GO_SERVICE_NAME
- mNode.ServiceType = APP_SERVICE_TYPE
- mNode.MethodName = "net/http.(*Transport).roundTrip()"
- mNode.Level = 1
- mNode.Pid = 0
- mNode.Nid = 1
- // 构建root节点
- traceRoot.RootData.RespTime = mNode.PureTime
- traceRoot.RootData.CollTime = mNode.StartTime
- traceRoot.Index = 1
- for _, attr := range sd.Attributes() {
- fmt.Println(attr.Key, ":", attr.Value.AsInterface())
- switch attr.Key {
- case "http.uri":
- traceRoot.RootData.Uri = attr.Value.AsString()
- case "http.method":
- traceRoot.RootData.HttpMethod = attr.Value.AsString()
- case "http.status_code":
- traceRoot.RootData.HttpCode = attr.Value.AsInt64()
- case "net.peer.name":
- traceRoot.RootData.ClientIp = attr.Value.AsString()
- traceRoot.RootData.Sip = attr.Value.AsString()
- traceRoot.RootData.Sn = attr.Value.AsString()
- case "net.peer.port":
- traceRoot.RootData.Sport = attr.Value.AsInt64()
- traceRoot.RootData.LocalPort = attr.Value.AsInt64()
- case "server.trace_id_from":
- traceRoot.RootData.TraceId = attr.Value.AsString()
- case "server.called_id":
- traceRoot.RootData.CalledId = attr.Value.AsInt64()
- case "server.instance_id_from":
- traceRoot.RootData.InstanceIdFrom = attr.Value.AsInt64()
- case "server.app_id_from":
- traceRoot.RootData.AppIdFrom = attr.Value.AsInt64()
- case "server.span_id_from":
- traceRoot.RootData.SpanIdFrom = attr.Value.AsString()
- case "server.type_from":
- traceRoot.RootData.TypeFrom = attr.Value.AsString()
- }
- }
- }
- func buildAppMapFromEvent(traceRoot *RootDataT, sd apmTraceSpan) int {
- mNode := MapInfoT{
- Exception: 0,
- ExceptionMsg: "",
- ExceptionStack: "",
- Ip: "",
- Level: 1,
- Pid: 1,
- Port: 0,
- Ps: []string{},
- ServiceName: "",
- ServiceType: "",
- WallTime: 0,
- }
- mNode.ServiceName = GO_SERVICE_NAME
- mNode.ServiceType = APP_SERVICE_TYPE
- mNode.MethodName = "Kernel Endpoint()"
- mNode.Level = 1
- mNode.Pid = 0
- mNode.Nid = 1
- var code_type int64
- // 构建root节点
- //traceRoot.RespTime = mNode.PureTimex
- //traceRoot.CollTime = mNode.StartTime
- for _, attr := range sd.Attributes() {
- fmt.Println("Appmap:", attr.Key, ":", attr.Value.AsInterface())
- switch attr.Key {
- case "http.uri":
- traceRoot.Uri = attr.Value.AsString()
- case "http.method":
- traceRoot.HttpMethod = attr.Value.AsString()
- case "http.status_code":
- traceRoot.HttpCode = attr.Value.AsInt64()
- case "net.peer.name":
- traceRoot.ClientIp = attr.Value.AsString()
- traceRoot.Sip = attr.Value.AsString()
- traceRoot.Sn = attr.Value.AsString()
- case "net.peer.port":
- traceRoot.Sport = attr.Value.AsInt64()
- traceRoot.LocalPort = attr.Value.AsInt64()
- case "server.trace_id_from":
- traceRoot.TraceId = attr.Value.AsString()
- case "server.called_id":
- traceRoot.CalledId = attr.Value.AsInt64()
- case "server.instance_id_from":
- traceRoot.InstanceIdFrom = attr.Value.AsInt64()
- case "server.app_id_from":
- traceRoot.AppIdFrom = attr.Value.AsInt64()
- case "server.span_id_from":
- traceRoot.SpanIdFrom = attr.Value.AsString()
- case "server.type_from":
- traceRoot.TypeFrom = attr.Value.AsString()
- case "time.start_at":
- traceRoot.CollTime = uint64(attr.Value.AsInt64())
- mNode.StartTime = traceRoot.CollTime
- case "time.end_at":
- mNode.EndTime = uint64(attr.Value.AsInt64())
- case "time.duration":
- traceRoot.RespTime = uint64(attr.Value.AsInt64()) / 1e3
- mNode.PureTime = traceRoot.RespTime
- mNode.WallTime = uint64(attr.Value.AsInt64()) / 1e3
- case "server.code_type":
- code_type = attr.Value.AsInt64()
- }
- }
- traceRoot.Maps = append(traceRoot.Maps, mNode)
- return int(code_type)
- }
- func buildHttpMap(mNode *MapInfoT, sd apmTraceSpan) {
- mNode.ServiceName = HTTP_SERVICE_NAME
- mNode.ServiceType = HTTP_SERVICE_TYPE
- mNode.Schema = "http"
- mNode.MethodName = "net/http.serverHandler.ServeHTTP()"
- var descAddr string
- for _, attr := range sd.Attributes() {
- //fmt.Println(attr.Key, ":", attr.Value.AsInterface())
- switch attr.Key {
- case "http.ip":
- mNode.Ip = attr.Value.AsString()
- descAddr += mNode.Ip
- case "http.port":
- mNode.Port = attr.Value.AsInt64()
- descAddr += ":" + attr.Value.AsString()
- case "http.uri":
- mNode.Uri = attr.Value.AsString()
- case "http.assumed_app_id":
- mNode.AssumedAppId = attr.Value.AsInt64()
- case "http.span_id":
- mNode.SpanId = attr.Value.AsString()
- }
- }
- //mNode.AssumedAppId = Md5ToInt64(descAddr, 16)
- }
- func buildHttpMapFromEvent(mNode *MapInfoT, event tracesdk.Event) {
- mNode.ServiceName = HTTP_SERVICE_NAME
- mNode.ServiceType = HTTP_SERVICE_TYPE
- mNode.Schema = "http"
- mNode.MethodName = "HTTP"
- //var descAddr string
- for _, attr := range event.Attributes {
- fmt.Println("HTTP--->", attr.Key, ":", attr.Value.AsInterface())
- switch attr.Key {
- case "http.ip":
- mNode.Ip = attr.Value.AsString()
- //descAddr += mNode.Ip
- case "http.port":
- mNode.Port = attr.Value.AsInt64()
- case "http.method":
- mNode.MethodName += " " + attr.Value.AsString()
- //descAddr += ":" + attr.Value.AsString()
- case "http.uri":
- mNode.Uri = attr.Value.AsString()
- mNode.MethodName += " " + attr.Value.AsString()
- case "http.assumed_app_id":
- mNode.AssumedAppId = attr.Value.AsInt64()
- case "http.span_id":
- mNode.SpanId = attr.Value.AsString()
- case "time.start_at":
- mNode.StartTime = uint64(attr.Value.AsInt64())
- case "time.end_at":
- mNode.EndTime = uint64(attr.Value.AsInt64())
- case "time.duration":
- //mNode.PureTime = uint64(attr.Value.AsInt64()) / 1e3
- mNode.WallTime = uint64(attr.Value.AsInt64()) / 1e3
- }
- }
- //mNode.AssumedAppId = Md5ToInt64(descAddr, 16)
- }
- func buildMysqlMap(mNode *MapInfoT, sd apmTraceSpan) {
- mNode.Dbn = "unknown"
- mNode.ServiceName = MYSQL_SERVICE_NAME
- mNode.ServiceType = SQL_SERVICE_TYPE
- mNode.MethodName = "database/sql.Query()"
- for _, attr := range sd.Attributes() {
- //fmt.Println(attr.Key, ":", attr.Value.AsInterface())
- switch attr.Key {
- case "net.peer.name":
- mNode.Ip = attr.Value.AsString()
- case "net.peer.port":
- mNode.Port = attr.Value.AsInt64()
- case "db.statement":
- query := attr.Value.AsString()
- mNode.Ps = []string{query}
- words := strings.Fields(query)
- if len(words) > 0 {
- mNode.OperType = strings.ToUpper(words[0])
- }
- }
- }
- }
- func buildMysqlMapEvent(mNode *MapInfoT, event tracesdk.Event) {
- mNode.Dbn = "unknown"
- mNode.ServiceName = MYSQL_SERVICE_NAME
- mNode.ServiceType = SQL_SERVICE_TYPE
- mNode.MethodName = "database/sql.Query()"
- for _, attr := range event.Attributes {
- //fmt.Println(attr.Key, ":", attr.Value.AsInterface())
- switch attr.Key {
- case "net.peer.name":
- mNode.Ip = attr.Value.AsString()
- case "net.peer.port":
- mNode.Port = attr.Value.AsInt64()
- case "db.statement":
- query := attr.Value.AsString()
- mNode.Ps = []string{query}
- words := strings.Fields(query)
- if len(words) > 0 {
- mNode.OperType = strings.ToUpper(words[0])
- }
- }
- }
- }
- func buildDMMapEvent(mNode *MapInfoT, event tracesdk.Event) {
- mNode.Dbn = "TEST"
- mNode.ServiceName = DM_SERVICE_NAME
- mNode.ServiceType = SQL_SERVICE_TYPE
- mNode.MethodName = "database/sql.Query()"
- for _, attr := range event.Attributes {
- //fmt.Println(attr.Key, ":", attr.Value.AsInterface())
- switch attr.Key {
- case "net.peer.name":
- mNode.Ip = attr.Value.AsString()
- case "net.peer.port":
- mNode.Port = attr.Value.AsInt64()
- case "db.statement":
- query := attr.Value.AsString()
- mNode.Ps = []string{query}
- words := strings.Fields(query)
- if len(words) > 0 {
- mNode.OperType = strings.ToUpper(words[0])
- }
- case "sql.exception":
- if attr.Value.AsBool() {
- mNode.Exception = 1
- } else {
- mNode.Exception = 0
- }
- }
- }
- }
- func buildRedisMap(mNode *MapInfoT, sd apmTraceSpan) {
- mNode.ServiceName = REDIS_SERVICE_NAME
- mNode.ServiceType = NOSQL_SERVICE_TYPE
- //mNode.MethodName = span(sd).Name + " query"
- mNode.MethodName = "redis.Do()"
- for _, attr := range sd.Attributes() {
- //fmt.Println(attr.Key, ":", attr.Value.AsInterface())
- switch attr.Key {
- case "net.peer.name":
- mNode.Ip = attr.Value.AsString()
- case "net.peer.port":
- mNode.Port = attr.Value.AsInt64()
- case "db.statement":
- query := attr.Value.AsString()
- mNode.Ps = []string{query}
- words := strings.Fields(query)
- if len(words) > 0 {
- mNode.OperType = strings.ToUpper(words[0])
- }
- }
- }
- }
- func buildRedisMapEvent(mNode *MapInfoT, event tracesdk.Event) {
- mNode.ServiceName = REDIS_SERVICE_NAME
- mNode.ServiceType = NOSQL_SERVICE_TYPE
- //mNode.MethodName = span(sd).Name + " query"
- mNode.MethodName = "redis.Do()"
- for _, attr := range event.Attributes {
- //fmt.Println(attr.Key, ":", attr.Value.AsInterface())
- switch attr.Key {
- case "net.peer.name":
- mNode.Ip = attr.Value.AsString()
- case "net.peer.port":
- mNode.Port = attr.Value.AsInt64()
- case "db.statement":
- query := attr.Value.AsString()
- mNode.Ps = []string{query}
- words := strings.Fields(query)
- if len(words) > 0 {
- mNode.OperType = strings.ToUpper(words[0])
- }
- }
- }
- }
- func isEnter(_type string) bool {
- if _type == "APPLICATION" {
- return true
- }
- return false
- }
- func span(sd apmTraceSpan) *tracepb.Span {
- if sd == nil {
- return nil
- }
- tid := sd.SpanContext().TraceID()
- sid := sd.SpanContext().SpanID()
- s := &tracepb.Span{
- TraceId: tid[:],
- SpanId: sid[:],
- TraceState: sd.SpanContext().TraceState().String(),
- //Status: status(sd.Status().Code, sd.Status().Description),
- StartTimeUnixNano: uint64(sd.StartTime().UnixNano()),
- EndTimeUnixNano: uint64(sd.EndTime().UnixNano()),
- //Links: links(sd.Links()),
- //Kind: spanKind(sd.SpanKind()),
- Name: sd.Name(),
- Attributes: tracetransform.KeyValues(sd.Attributes()),
- //Events: spanEvents(sd.Events()),
- DroppedAttributesCount: uint32(sd.DroppedAttributes()),
- DroppedEventsCount: uint32(sd.DroppedEvents()),
- DroppedLinksCount: uint32(sd.DroppedLinks()),
- }
- if psid := sd.Parent().SpanID(); psid.IsValid() {
- s.ParentSpanId = psid[:]
- }
- return s
- }
- func Md5ToInt64(strParam string, Len int) int64 {
- sign := md5.Sum([]byte(strParam))
- signStr := fmt.Sprintf("%x", sign)
- charArr := []rune(signStr)
- var intStr string
- for _, value := range charArr {
- intStr += strconv.Itoa(int(value))
- }
- intStr = intStr[:Len]
- int64Data, err := strconv.ParseInt(intStr, 10, 64)
- if err != nil {
- return 0
- }
- return int64Data
- }
|