Skip to content

Commit

Permalink
调整链路实现
Browse files Browse the repository at this point in the history
  • Loading branch information
steden committed Nov 30, 2024
1 parent c900ef3 commit 7155e31
Show file tree
Hide file tree
Showing 5 changed files with 108 additions and 283 deletions.
34 changes: 0 additions & 34 deletions eumTraceType/enum.go

This file was deleted.

7 changes: 4 additions & 3 deletions module.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
package linkTrace

import (
"strings"
"time"

"github.com/farseer-go/fs/configure"
"github.com/farseer-go/fs/container"
"github.com/farseer-go/fs/modules"
"github.com/farseer-go/fs/trace"
"github.com/farseer-go/queue"
"strings"
"time"
)

// Enable 是否启用
Expand Down Expand Up @@ -38,6 +39,6 @@ func (module Module) PreInitialize() {
if !strings.HasSuffix(FopsServer, "/") {
FopsServer += "/"
}
queue.Subscribe("TraceContext", "SaveTraceContext", 1000,5*time.Second, SaveTraceContextConsumer)
queue.Subscribe("TraceContext", "SaveTraceContext", 1000, 5*time.Second, SaveTraceContextConsumer)
}
}
17 changes: 8 additions & 9 deletions saveTraceContextConsumer.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,12 @@ import (

"github.com/farseer-go/collections"
"github.com/farseer-go/fs/configure"
"github.com/farseer-go/fs/container"
"github.com/farseer-go/fs/core"
"github.com/farseer-go/fs/exception"
"github.com/farseer-go/fs/flog"
"github.com/farseer-go/fs/snc"
"github.com/farseer-go/fs/trace"
"github.com/farseer-go/linkTrace/eumTraceType"
"github.com/farseer-go/fs/trace/eumTraceType"
)

// FopsServer fops地址
Expand All @@ -26,14 +25,13 @@ func SaveTraceContextConsumer(subscribeName string, lstMessage collections.ListA
if traceContext := trace.CurTraceContext.Get(); traceContext != nil {
traceContext.Ignore()
}
lstTraceContext := collections.NewList[TraceContext]()
lstTraceContext := collections.NewList[*trace.TraceContext]()
lstMessage.Foreach(func(item *any) {
// 上下文
dto := (*item).(TraceContext)
dto := (*item).(*trace.TraceContext)
if len(dto.List) == 0 && dto.TraceType != eumTraceType.WebApi {
return
}
dto.printLog()

// 链路超过200条,则丢弃
if len(dto.List) > 200 {
Expand Down Expand Up @@ -61,15 +59,16 @@ type UploadTraceRequest struct {
}

// UploadTrace 上传链路记录
func uploadTrace(lstTraceContext any) error {
func uploadTrace(lstTraceContext collections.List[*trace.TraceContext]) error {
bodyByte, _ := snc.Marshal(UploadTraceRequest{List: lstTraceContext})
url := configure.GetFopsServer() + "linkTrace/upload"
newRequest, _ := http.NewRequest("POST", url, bytes.NewReader(bodyByte))
newRequest.Header.Set("Content-Type", "application/json")

// 链路追踪
if traceContext := container.Resolve[trace.IManager]().GetCurTrace(); traceContext != nil {
newRequest.Header.Set("Trace-Id", traceContext.GetTraceId())
newRequest.Header.Set("Trace-Level", strconv.Itoa(traceContext.GetTraceLevel()))
if traceContext := trace.CurTraceContext.Get(); traceContext != nil {
newRequest.Header.Set("Trace-Id", traceContext.TraceId)
newRequest.Header.Set("Trace-Level", strconv.Itoa(traceContext.TraceLevel))
newRequest.Header.Set("Trace-App-Name", core.AppName)
}
client := &http.Client{
Expand Down
Loading

0 comments on commit 7155e31

Please sign in to comment.